Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Unified Diff: base/memory/shared_memory_posix.cc

Issue 17779002: Posix: fix named SHM mappings permissions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Check uid if opening existing file. (And rebase). Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/memory/shared_memory_unittest.cc » ('j') | base/memory/shared_memory_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/shared_memory_posix.cc
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_posix.cc
index 5d580d08c38c0294485e69a28082094fdcbd211d..c000c45a2f1b947c836515abae54688d7e19bc52 100644
--- a/base/memory/shared_memory_posix.cc
+++ b/base/memory/shared_memory_posix.cc
@@ -6,8 +6,11 @@
#include <errno.h>
#include <fcntl.h>
+#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
#include <sys/mman.h>
#include <sys/stat.h>
+#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.
+#include <sys/types.h>
#include <unistd.h>
#include "base/file_util.h"
@@ -149,12 +152,37 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
if (!FilePathForMemoryName(*options.name, &path))
return false;
- fp = file_util::OpenFile(path, "w+x");
- if (fp == NULL && options.open_existing) {
- // "w+" will truncate if it already exists.
- fp = file_util::OpenFile(path, "a+");
+ // 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.
+ // to other users on the system.
+ const mode_t owner_only = S_IRUSR | S_IWUSR;
+ int fd;
+ // First, try to create the file.
+ 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.
+ if (fd == -1 && options.open_existing) {
+ // If this doesn't work, try and open an existing file in append mode.
+ // Opening an existing file in a world writable directory has two main
+ // security implications:
+ // - Attackers could plant a file under their control.
+ // - Attackers could plant a symbolic link so that an unexpected file
+ // is opened.
+ // O_NOFOLLOW makes sure that the latter doesn't happen.
+ // Checking the former happens below.
Mark Mentovai 2013/07/02 14:31:54 Former? Below? Ick. Why have you separated the pr
+ fd = open(path.value().c_str(), O_RDWR | O_APPEND | O_NOFOLLOW);
+ 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.
+ // Check that the current user owns the file.
+ if (fd >= 0 &&
+ (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
+ HANDLE_EINTR(close(fd));
+ return false;
+ }
+ // An existing file was opened so its size should not be fixed.
fix_size = false;
}
+ fp = NULL;
+ if (fd >= 0) {
+ // "a+" is always appropriate: if it's a new file, a+ is similar to w+.
+ fp = fdopen(fd, "a+");
+ }
}
if (fp && fix_size) {
// Get current size.
« no previous file with comments | « no previous file | base/memory/shared_memory_unittest.cc » ('j') | base/memory/shared_memory_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698