Chromium Code Reviews| 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| 11 #include <sys/types.h> | |
| 11 #include <unistd.h> | 12 #include <unistd.h> |
| 12 | 13 |
| 13 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 14 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/process_util.h" | 17 #include "base/process_util.h" |
| 17 #include "base/safe_strerror_posix.h" | 18 #include "base/safe_strerror_posix.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 // private), and prevents the need for cleanup (once the last fd is closed, | 143 // private), and prevents the need for cleanup (once the last fd is closed, |
| 143 // it is truly freed). | 144 // it is truly freed). |
| 144 if (fp) { | 145 if (fp) { |
| 145 if (unlink(path.value().c_str())) | 146 if (unlink(path.value().c_str())) |
| 146 PLOG(WARNING) << "unlink"; | 147 PLOG(WARNING) << "unlink"; |
| 147 } | 148 } |
| 148 } else { | 149 } else { |
| 149 if (!FilePathForMemoryName(*options.name, &path)) | 150 if (!FilePathForMemoryName(*options.name, &path)) |
| 150 return false; | 151 return false; |
| 151 | 152 |
| 152 fp = file_util::OpenFile(path, "w+x"); | 153 // Make sure that the file is opened without any permission |
| 153 if (fp == NULL && options.open_existing) { | 154 // to other users on the system. |
| 154 // "w+" will truncate if it already exists. | 155 const mode_t kOwnerOnly = S_IRUSR | S_IWUSR; |
| 155 fp = file_util::OpenFile(path, "a+"); | 156 // First, try to create the file. |
|
Mark Mentovai
2013/07/02 19:52:56
Blank line before this.
jln (very slow on Chromium)
2013/07/02 21:20:02
Done.
| |
| 157 int fd = HANDLE_EINTR( | |
| 158 open(path.value().c_str(), O_RDWR | O_CREAT | O_EXCL, kOwnerOnly)); | |
| 159 if (fd == -1 && options.open_existing) { | |
| 160 // If this doesn't work, try and open an existing file in append mode. | |
| 161 // Opening an existing file in a world writable directory has two main | |
| 162 // security implications: | |
| 163 // - Attackers could plant a file under their control, so ownership of | |
| 164 // the file is checked below. | |
| 165 // - Attackers could plant a symbolic link so that an unexpected file | |
| 166 // is opened, so O_NOFOLLOW is passed to open(). | |
| 167 // O_NOFOLLOW makes sure that the latter doesn't happen. | |
| 168 // Checking the former happens below. | |
| 169 fd = HANDLE_EINTR( | |
| 170 open(path.value().c_str(), O_RDWR | O_APPEND | O_NOFOLLOW)); | |
| 171 struct stat sb; | |
| 172 // Check that the current user owns the file. | |
|
Mark Mentovai
2013/07/02 19:52:56
And this too. When you have comments interspersed
jln (very slow on Chromium)
2013/07/02 21:20:02
Done.
| |
| 173 if (fd >= 0 && | |
| 174 (fstat(fd, &sb) != 0 || sb.st_uid != getuid())) { | |
| 175 HANDLE_EINTR(close(fd)); | |
| 176 return false; | |
| 177 } | |
| 178 // An existing file was opened, so its size should not be fixed. | |
| 156 fix_size = false; | 179 fix_size = false; |
| 157 } | 180 } |
| 181 fp = NULL; | |
| 182 if (fd >= 0) { | |
| 183 // "a+" is always appropriate: if it's a new file, a+ is similar to w+. | |
| 184 fp = fdopen(fd, "a+"); | |
| 185 } | |
| 158 } | 186 } |
| 159 if (fp && fix_size) { | 187 if (fp && fix_size) { |
| 160 // Get current size. | 188 // Get current size. |
| 161 struct stat stat; | 189 struct stat stat; |
| 162 if (fstat(fileno(fp), &stat) != 0) { | 190 if (fstat(fileno(fp), &stat) != 0) { |
| 163 file_util::CloseFile(fp); | 191 file_util::CloseFile(fp); |
| 164 return false; | 192 return false; |
| 165 } | 193 } |
| 166 const size_t current_size = stat.st_size; | 194 const size_t current_size = stat.st_size; |
| 167 if (current_size != options.size) { | 195 if (current_size != options.size) { |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 new_handle->fd = new_fd; | 407 new_handle->fd = new_fd; |
| 380 new_handle->auto_close = true; | 408 new_handle->auto_close = true; |
| 381 | 409 |
| 382 if (close_self) | 410 if (close_self) |
| 383 Close(); | 411 Close(); |
| 384 | 412 |
| 385 return true; | 413 return true; |
| 386 } | 414 } |
| 387 | 415 |
| 388 } // namespace base | 416 } // namespace base |
| OLD | NEW |