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 <fcntl.h> | |
Mark Mentovai
2013/07/02 14:31:54
Really? Again?
jln (very slow on Chromium)
2013/07/02 18:49:08
Oops! I'll update the presubmit script later to de
| |
9 #include <sys/mman.h> | 10 #include <sys/mman.h> |
10 #include <sys/stat.h> | 11 #include <sys/stat.h> |
12 #include <sys/stat.h> | |
Mark Mentovai
2013/07/02 14:31:54
REALLY?
jln (very slow on Chromium)
2013/07/02 18:49:08
Done.
| |
13 #include <sys/types.h> | |
11 #include <unistd.h> | 14 #include <unistd.h> |
12 | 15 |
13 #include "base/file_util.h" | 16 #include "base/file_util.h" |
14 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 18 #include "base/logging.h" |
16 #include "base/process_util.h" | 19 #include "base/process_util.h" |
17 #include "base/safe_strerror_posix.h" | 20 #include "base/safe_strerror_posix.h" |
18 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
19 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
20 #include "base/threading/platform_thread.h" | 23 #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, | 145 // private), and prevents the need for cleanup (once the last fd is closed, |
143 // it is truly freed). | 146 // it is truly freed). |
144 if (fp) { | 147 if (fp) { |
145 if (unlink(path.value().c_str())) | 148 if (unlink(path.value().c_str())) |
146 PLOG(WARNING) << "unlink"; | 149 PLOG(WARNING) << "unlink"; |
147 } | 150 } |
148 } else { | 151 } else { |
149 if (!FilePathForMemoryName(*options.name, &path)) | 152 if (!FilePathForMemoryName(*options.name, &path)) |
150 return false; | 153 return false; |
151 | 154 |
152 fp = file_util::OpenFile(path, "w+x"); | 155 // Make sure that we don't give permissions to access this file |
Mark Mentovai
2013/07/02 14:31:54
Nit: permission.
Nit: avoid “we” (only picking on
jln (very slow on Chromium)
2013/07/02 18:49:08
Done.
| |
153 if (fp == NULL && options.open_existing) { | 156 // to other users on the system. |
154 // "w+" will truncate if it already exists. | 157 const mode_t owner_only = S_IRUSR | S_IWUSR; |
155 fp = file_util::OpenFile(path, "a+"); | 158 int fd; |
159 // First, try to create the file. | |
160 fd = open(path.value().c_str(), O_RDWR | O_CREAT | O_EXCL, owner_only); | |
Mark Mentovai
2013/07/02 14:31:54
You can declare fd on this line.
Mark Mentovai
2013/07/02 14:31:54
HANDLE_EINTR. Same around the open on line 170.
jln (very slow on Chromium)
2013/07/02 18:49:08
Done.
jln (very slow on Chromium)
2013/07/02 18:49:08
Done.
| |
161 if (fd == -1 && options.open_existing) { | |
162 // If this doesn't work, try and open an existing file in append mode. | |
163 // Opening an existing file in a world writable directory has two main | |
164 // security implications: | |
165 // - Attackers could plant a file under their control. | |
166 // - Attackers could plant a symbolic link so that an unexpected file | |
167 // is opened. | |
168 // O_NOFOLLOW makes sure that the latter doesn't happen. | |
169 // Checking the former happens below. | |
Mark Mentovai
2013/07/02 14:31:54
Former? Below? Ick.
Why have you separated the pr
| |
170 fd = open(path.value().c_str(), O_RDWR | O_APPEND | O_NOFOLLOW); | |
171 struct stat stat; | |
Markus (顧孟勤)
2013/07/02 02:49:38
Ideally, avoid using "stat" as a local symbol, as
jln (very slow on Chromium)
2013/07/02 02:51:38
Yeah, I only used it for consistency with the code
Mark Mentovai
2013/07/02 14:31:54
Pick a different name.
jln (very slow on Chromium)
2013/07/02 18:49:08
Done.
| |
172 // Check that the current user owns the file. | |
173 if (fd >= 0 && | |
174 (fstat(fd, &stat) != 0 || stat.st_uid != getuid())) { | |
Mark Mentovai
2013/07/02 14:31:54
getuid or geteuid?
jln (very slow on Chromium)
2013/07/02 18:49:08
I had thought about it and I believe it's slightly
Mark Mentovai
2013/07/02 19:52:56
Julien Tinnes wrote:
jln (very slow on Chromium)
2013/07/02 21:20:02
The O_CREAT is O_CREAT | O_EXCL: i.e. we guarantee
Mark Mentovai
2013/07/02 21:27:59
Julien Tinnes wrote:
jln (very slow on Chromium)
2013/07/02 21:44:52
Yes, I think my example still works. If there is a
Mark Mentovai
2013/07/02 22:05:27
Julien Tinnes wrote:
jln (very slow on Chromium)
2013/07/02 22:57:05
Because if creating a new resource mark is not put
| |
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 |