| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 | 46 |
| 47 // Makes a temporary file, fdopens it, and then unlinks it. |fp| is populated | 47 // Makes a temporary file, fdopens it, and then unlinks it. |fp| is populated |
| 48 // with the fdopened FILE. |readonly_fd| is populated with the opened fd if | 48 // with the fdopened FILE. |readonly_fd| is populated with the opened fd if |
| 49 // options.share_read_only is true. |path| is populated with the location of | 49 // options.share_read_only is true. |path| is populated with the location of |
| 50 // the file before it was unlinked. | 50 // the file before it was unlinked. |
| 51 // Returns false if there's an unhandled failure. | 51 // Returns false if there's an unhandled failure. |
| 52 bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options, | 52 bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options, |
| 53 ScopedFILE* fp, | 53 ScopedFILE* fp, |
| 54 ScopedFD* readonly_fd, | 54 ScopedFD* readonly_fd, |
| 55 FilePath* path) { | 55 FilePath* path) { |
| 56 // It doesn't make sense to have a open-existing private piece of shmem | |
| 57 DCHECK(!options.open_existing_deprecated); | |
| 58 // Q: Why not use the shm_open() etc. APIs? | 56 // Q: Why not use the shm_open() etc. APIs? |
| 59 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU | 57 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU |
| 60 FilePath directory; | 58 FilePath directory; |
| 61 ScopedPathUnlinker path_unlinker; | 59 ScopedPathUnlinker path_unlinker; |
| 62 if (GetShmemTempDir(options.executable, &directory)) { | 60 if (GetShmemTempDir(options.executable, &directory)) { |
| 63 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | 61 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
| 64 // is fixed. | 62 // is fixed. |
| 65 tracked_objects::ScopedTracker tracking_profile( | 63 tracked_objects::ScopedTracker tracking_profile( |
| 66 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 64 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 67 "466437 SharedMemory::Create::OpenTemporaryFile")); | 65 "466437 SharedMemory::Create::OpenTemporaryFile")); |
| 68 fp->reset(base::CreateAndOpenTemporaryFileInDir(directory, path)); | 66 fp->reset(CreateAndOpenTemporaryFileInDir(directory, path)); |
| 69 | 67 |
| 70 // Deleting the file prevents anyone else from mapping it in (making it | 68 // Deleting the file prevents anyone else from mapping it in (making it |
| 71 // private), and prevents the need for cleanup (once the last fd is | 69 // private), and prevents the need for cleanup (once the last fd is |
| 72 // closed, it is truly freed). | 70 // closed, it is truly freed). |
| 73 if (*fp) | 71 if (*fp) |
| 74 path_unlinker.reset(path); | 72 path_unlinker.reset(path); |
| 75 } | 73 } |
| 76 | 74 |
| 77 if (*fp) { | 75 if (*fp) { |
| 78 if (options.share_read_only) { | 76 if (options.share_read_only) { |
| 79 // TODO(erikchen): Remove ScopedTracker below once | 77 // TODO(erikchen): Remove ScopedTracker below once |
| 80 // http://crbug.com/466437 is fixed. | 78 // http://crbug.com/466437 is fixed. |
| 81 tracked_objects::ScopedTracker tracking_profile( | 79 tracked_objects::ScopedTracker tracking_profile( |
| 82 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 80 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 83 "466437 SharedMemory::Create::OpenReadonly")); | 81 "466437 SharedMemory::Create::OpenReadonly")); |
| 84 // Also open as readonly so that we can ShareReadOnlyToProcess. | 82 // Also open as readonly so that we can ShareReadOnlyToProcess. |
| 85 readonly_fd->reset(HANDLE_EINTR(open(path->value().c_str(), O_RDONLY))); | 83 readonly_fd->reset(HANDLE_EINTR(open(path->value().c_str(), O_RDONLY))); |
| 86 if (!readonly_fd->is_valid()) { | 84 if (!readonly_fd->is_valid()) { |
| 87 DPLOG(ERROR) << "open(\"" << path->value() << "\", O_RDONLY) failed"; | 85 DPLOG(ERROR) << "open(\"" << path->value() << "\", O_RDONLY) failed"; |
| 88 fp->reset(); | 86 fp->reset(); |
| 89 return false; | 87 return false; |
| 90 } | 88 } |
| 91 } | 89 } |
| 92 } | 90 } |
| 93 return true; | 91 return true; |
| 94 } | 92 } |
| 95 } | 93 |
| 94 } // namespace |
| 96 | 95 |
| 97 SharedMemory::SharedMemory() | 96 SharedMemory::SharedMemory() |
| 98 : mapped_file_(-1), | 97 : mapped_file_(-1), |
| 99 readonly_mapped_file_(-1), | 98 readonly_mapped_file_(-1), |
| 100 mapped_size_(0), | 99 mapped_size_(0), |
| 101 memory_(NULL), | 100 memory_(NULL), |
| 102 read_only_(false), | 101 read_only_(false), |
| 103 requested_size_(0) { | 102 requested_size_(0) { |
| 104 } | 103 } |
| 105 | 104 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 142 |
| 144 // static | 143 // static |
| 145 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 144 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 146 DCHECK_GE(GetFdFromSharedMemoryHandle(handle), 0); | 145 DCHECK_GE(GetFdFromSharedMemoryHandle(handle), 0); |
| 147 if (close(GetFdFromSharedMemoryHandle(handle)) < 0) | 146 if (close(GetFdFromSharedMemoryHandle(handle)) < 0) |
| 148 DPLOG(ERROR) << "close"; | 147 DPLOG(ERROR) << "close"; |
| 149 } | 148 } |
| 150 | 149 |
| 151 // static | 150 // static |
| 152 size_t SharedMemory::GetHandleLimit() { | 151 size_t SharedMemory::GetHandleLimit() { |
| 153 return base::GetMaxFds(); | 152 return GetMaxFds(); |
| 154 } | 153 } |
| 155 | 154 |
| 156 // static | 155 // static |
| 157 SharedMemoryHandle SharedMemory::DuplicateHandle( | 156 SharedMemoryHandle SharedMemory::DuplicateHandle( |
| 158 const SharedMemoryHandle& handle) { | 157 const SharedMemoryHandle& handle) { |
| 159 return handle.Duplicate(); | 158 return handle.Duplicate(); |
| 160 } | 159 } |
| 161 | 160 |
| 162 // static | 161 // static |
| 163 int SharedMemory::GetFdFromSharedMemoryHandle( | 162 int SharedMemory::GetFdFromSharedMemoryHandle( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 194 |
| 196 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 195 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 197 return false; | 196 return false; |
| 198 | 197 |
| 199 // This function theoretically can block on the disk, but realistically | 198 // This function theoretically can block on the disk, but realistically |
| 200 // the temporary files we create will just go into the buffer cache | 199 // the temporary files we create will just go into the buffer cache |
| 201 // and be deleted before they ever make it out to disk. | 200 // and be deleted before they ever make it out to disk. |
| 202 base::ThreadRestrictions::ScopedAllowIO allow_io; | 201 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 203 | 202 |
| 204 ScopedFILE fp; | 203 ScopedFILE fp; |
| 205 bool fix_size = true; | |
| 206 ScopedFD readonly_fd; | 204 ScopedFD readonly_fd; |
| 207 | 205 |
| 208 FilePath path; | 206 FilePath path; |
| 209 if (options.name_deprecated == NULL || options.name_deprecated->empty()) { | 207 bool result = CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); |
| 210 bool result = | 208 if (!result) |
| 211 CreateAnonymousSharedMemory(options, &fp, &readonly_fd, &path); | 209 return false; |
| 212 if (!result) | |
| 213 return false; | |
| 214 } else { | |
| 215 if (!FilePathForMemoryName(*options.name_deprecated, &path)) | |
| 216 return false; | |
| 217 | 210 |
| 218 // Make sure that the file is opened without any permission | 211 if (!fp) { |
| 219 // to other users on the system. | |
| 220 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; | |
| 221 | |
| 222 // First, try to create the file. | |
| 223 int fd = HANDLE_EINTR( | |
| 224 open(path.value().c_str(), O_RDWR | O_CREAT | O_EXCL, kOwnerOnly)); | |
| 225 if (fd == -1 && options.open_existing_deprecated) { | |
| 226 // If this doesn't work, try and open an existing file in append mode. | |
| 227 // Opening an existing file in a world writable directory has two main | |
| 228 // security implications: | |
| 229 // - Attackers could plant a file under their control, so ownership of | |
| 230 // the file is checked below. | |
| 231 // - Attackers could plant a symbolic link so that an unexpected file | |
| 232 // is opened, so O_NOFOLLOW is passed to open(). | |
| 233 fd = HANDLE_EINTR( | |
| 234 open(path.value().c_str(), O_RDWR | O_APPEND | O_NOFOLLOW)); | |
| 235 | |
| 236 // Check that the current user owns the file. | |
| 237 // If uid != euid, then a more complex permission model is used and this | |
| 238 // API is not appropriate. | |
| 239 const uid_t real_uid = getuid(); | |
| 240 const uid_t effective_uid = geteuid(); | |
| 241 struct stat sb; | |
| 242 if (fd >= 0 && | |
| 243 (fstat(fd, &sb) != 0 || sb.st_uid != real_uid || | |
| 244 sb.st_uid != effective_uid)) { | |
| 245 LOG(ERROR) << | |
| 246 "Invalid owner when opening existing shared memory file."; | |
| 247 close(fd); | |
| 248 return false; | |
| 249 } | |
| 250 | |
| 251 // An existing file was opened, so its size should not be fixed. | |
| 252 fix_size = false; | |
| 253 } | |
| 254 | |
| 255 if (options.share_read_only) { | |
| 256 // Also open as readonly so that we can ShareReadOnlyToProcess. | |
| 257 readonly_fd.reset(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | |
| 258 if (!readonly_fd.is_valid()) { | |
| 259 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | |
| 260 close(fd); | |
| 261 fd = -1; | |
| 262 return false; | |
| 263 } | |
| 264 } | |
| 265 if (fd >= 0) { | |
| 266 // "a+" is always appropriate: if it's a new file, a+ is similar to w+. | |
| 267 fp.reset(fdopen(fd, "a+")); | |
| 268 } | |
| 269 } | |
| 270 if (fp && fix_size) { | |
| 271 // Get current size. | |
| 272 struct stat stat; | |
| 273 if (fstat(fileno(fp.get()), &stat) != 0) | |
| 274 return false; | |
| 275 const size_t current_size = stat.st_size; | |
| 276 if (current_size != options.size) { | |
| 277 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) | |
| 278 return false; | |
| 279 } | |
| 280 requested_size_ = options.size; | |
| 281 } | |
| 282 if (fp == NULL) { | |
| 283 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; | 212 PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
| 284 return false; | 213 return false; |
| 285 } | 214 } |
| 286 | 215 |
| 216 // Get current size. |
| 217 struct stat stat; |
| 218 if (fstat(fileno(fp.get()), &stat) != 0) |
| 219 return false; |
| 220 const size_t current_size = stat.st_size; |
| 221 if (current_size != options.size) { |
| 222 if (HANDLE_EINTR(ftruncate(fileno(fp.get()), options.size)) != 0) |
| 223 return false; |
| 224 } |
| 225 requested_size_ = options.size; |
| 226 |
| 287 return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); | 227 return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); |
| 288 } | 228 } |
| 289 | 229 |
| 290 // Our current implementation of shmem is with mmap()ing of files. | |
| 291 // These files need to be deleted explicitly. | |
| 292 // In practice this call is only needed for unit tests. | |
| 293 bool SharedMemory::Delete(const std::string& name) { | |
| 294 FilePath path; | |
| 295 if (!FilePathForMemoryName(name, &path)) | |
| 296 return false; | |
| 297 | |
| 298 if (PathExists(path)) | |
| 299 return base::DeleteFile(path, false); | |
| 300 | |
| 301 // Doesn't exist, so success. | |
| 302 return true; | |
| 303 } | |
| 304 | |
| 305 bool SharedMemory::Open(const std::string& name, bool read_only) { | |
| 306 FilePath path; | |
| 307 if (!FilePathForMemoryName(name, &path)) | |
| 308 return false; | |
| 309 | |
| 310 read_only_ = read_only; | |
| 311 | |
| 312 const char *mode = read_only ? "r" : "r+"; | |
| 313 ScopedFILE fp(base::OpenFile(path, mode)); | |
| 314 ScopedFD readonly_fd(HANDLE_EINTR(open(path.value().c_str(), O_RDONLY))); | |
| 315 if (!readonly_fd.is_valid()) { | |
| 316 DPLOG(ERROR) << "open(\"" << path.value() << "\", O_RDONLY) failed"; | |
| 317 return false; | |
| 318 } | |
| 319 return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); | |
| 320 } | |
| 321 | |
| 322 bool SharedMemory::MapAt(off_t offset, size_t bytes) { | 230 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| 323 if (mapped_file_ == -1) | 231 if (mapped_file_ == -1) |
| 324 return false; | 232 return false; |
| 325 | 233 |
| 326 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 234 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 327 return false; | 235 return false; |
| 328 | 236 |
| 329 if (memory_) | 237 if (memory_) |
| 330 return false; | 238 return false; |
| 331 | 239 |
| 332 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 240 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 333 MAP_SHARED, mapped_file_, offset); | 241 MAP_SHARED, mapped_file_, offset); |
| 334 | 242 |
| 335 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; | 243 bool mmap_succeeded = memory_ && memory_ != reinterpret_cast<void*>(-1); |
| 336 if (mmap_succeeded) { | 244 if (mmap_succeeded) { |
| 337 mapped_size_ = bytes; | 245 mapped_size_ = bytes; |
| 338 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 246 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 339 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 247 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 340 } else { | 248 } else { |
| 341 memory_ = NULL; | 249 memory_ = NULL; |
| 342 } | 250 } |
| 343 | 251 |
| 344 return mmap_succeeded; | 252 return mmap_succeeded; |
| 345 } | 253 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 FilePath* path) { | 324 FilePath* path) { |
| 417 // mem_name will be used for a filename; make sure it doesn't | 325 // mem_name will be used for a filename; make sure it doesn't |
| 418 // contain anything which will confuse us. | 326 // contain anything which will confuse us. |
| 419 DCHECK_EQ(std::string::npos, mem_name.find('/')); | 327 DCHECK_EQ(std::string::npos, mem_name.find('/')); |
| 420 DCHECK_EQ(std::string::npos, mem_name.find('\0')); | 328 DCHECK_EQ(std::string::npos, mem_name.find('\0')); |
| 421 | 329 |
| 422 FilePath temp_dir; | 330 FilePath temp_dir; |
| 423 if (!GetShmemTempDir(false, &temp_dir)) | 331 if (!GetShmemTempDir(false, &temp_dir)) |
| 424 return false; | 332 return false; |
| 425 | 333 |
| 426 std::string name_base = std::string(base::mac::BaseBundleID()); | 334 std::string name_base = std::string(mac::BaseBundleID()); |
| 427 *path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); | 335 *path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); |
| 428 return true; | 336 return true; |
| 429 } | 337 } |
| 430 | 338 |
| 431 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 339 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 432 SharedMemoryHandle* new_handle, | 340 SharedMemoryHandle* new_handle, |
| 433 bool close_self, | 341 bool close_self, |
| 434 ShareMode share_mode) { | 342 ShareMode share_mode) { |
| 435 int handle_to_dup = -1; | 343 int handle_to_dup = -1; |
| 436 switch(share_mode) { | 344 switch (share_mode) { |
| 437 case SHARE_CURRENT_MODE: | 345 case SHARE_CURRENT_MODE: |
| 438 handle_to_dup = mapped_file_; | 346 handle_to_dup = mapped_file_; |
| 439 break; | 347 break; |
| 440 case SHARE_READONLY: | 348 case SHARE_READONLY: |
| 441 // We could imagine re-opening the file from /dev/fd, but that can't make | 349 // We could imagine re-opening the file from /dev/fd, but that can't make |
| 442 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 | 350 // it readonly on Mac: https://codereview.chromium.org/27265002/#msg10 |
| 443 CHECK_GE(readonly_mapped_file_, 0); | 351 CHECK_GE(readonly_mapped_file_, 0); |
| 444 handle_to_dup = readonly_mapped_file_; | 352 handle_to_dup = readonly_mapped_file_; |
| 445 break; | 353 break; |
| 446 } | 354 } |
| 447 | 355 |
| 448 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); | 356 const int new_fd = HANDLE_EINTR(dup(handle_to_dup)); |
| 449 if (new_fd < 0) { | 357 if (new_fd < 0) { |
| 450 DPLOG(ERROR) << "dup() failed."; | 358 DPLOG(ERROR) << "dup() failed."; |
| 451 return false; | 359 return false; |
| 452 } | 360 } |
| 453 | 361 |
| 454 new_handle->SetFileHandle(new_fd, true); | 362 new_handle->SetFileHandle(new_fd, true); |
| 455 | 363 |
| 456 if (close_self) { | 364 if (close_self) { |
| 457 Unmap(); | 365 Unmap(); |
| 458 Close(); | 366 Close(); |
| 459 } | 367 } |
| 460 | 368 |
| 461 return true; | 369 return true; |
| 462 } | 370 } |
| 463 | 371 |
| 464 } // namespace base | 372 } // namespace base |
| OLD | NEW |