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

Unified Diff: base/memory/shared_memory_unittest.cc

Issue 17779002: Posix: fix named SHM mappings permissions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Switch to proper low level functions. 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
« base/memory/shared_memory_posix.cc ('K') | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/shared_memory_unittest.cc
diff --git a/base/memory/shared_memory_unittest.cc b/base/memory/shared_memory_unittest.cc
index 5c0cc8b8a8efcc25c03c4892b648043d66ba840f..ea93900fdd83adec097bd58e6570146d4a665fd1 100644
--- a/base/memory/shared_memory_unittest.cc
+++ b/base/memory/shared_memory_unittest.cc
@@ -8,6 +8,8 @@
#endif
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
+#include "base/rand_util.h"
+#include "base/strings/string_number_conversions.h"
#include "base/sys_info.h"
#include "base/test/multiprocess_test.h"
#include "base/threading/platform_thread.h"
@@ -21,6 +23,9 @@
#if defined(OS_POSIX)
#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
#endif
static const int kNumThreads = 5;
@@ -401,7 +406,60 @@ TEST(SharedMemoryTest, AnonymousExecutable) {
EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(),
PROT_READ | PROT_EXEC));
}
-#endif
+
+// Create a shared memory object, check its permissions.
+TEST(SharedMemoryTest, FilePermissionsAnonymous) {
+ const uint32 kTestSize = 1 << 8;
+
+ SharedMemory shared_memory;
+ SharedMemoryCreateOptions options;
+ options.size = kTestSize;
+ // Set a permissive umask.
+ mode_t old_umask = umask(S_IWGRP | S_IWOTH);
+
+ EXPECT_TRUE(shared_memory.Create(options));
+
+ int shm_fd = shared_memory.handle().fd;
+ struct stat shm_stat;
+ EXPECT_EQ(0, fstat(shm_fd, &shm_stat));
+ // Neither the group, nor others should be able to read the shared memory
+ // file.
+ EXPECT_FALSE(shm_stat.st_mode & S_IRWXO);
+ EXPECT_FALSE(shm_stat.st_mode & S_IRWXG);
+
+ // Restore umask.
+ umask(old_umask);
+}
+
+// Create a shared memory object, check its permissions.
+TEST(SharedMemoryTest, FilePermissionsNamed) {
+ const uint32 kTestSize = 1 << 8;
+
+ SharedMemory shared_memory;
+ SharedMemoryCreateOptions options;
+ options.size = kTestSize;
+ std::string shared_mem_name =
+ "shared_perm_test-" + Uint64ToString(RandUint64());
+ options.name = &shared_mem_name;
+ // Set a permissive umask.
+ mode_t old_umask = umask(S_IWGRP | S_IWOTH);
+
+ EXPECT_TRUE(shared_memory.Create(options));
+ // Clean-up the backing file immediately, we don't need it.
+ EXPECT_TRUE(shared_memory.Delete(shared_mem_name));
+
+ int shm_fd = shared_memory.handle().fd;
+ struct stat shm_stat;
+ EXPECT_EQ(0, fstat(shm_fd, &shm_stat));
+ // Neither the group, nor others should be able to read the shared memory
+ // file.
+ EXPECT_FALSE(shm_stat.st_mode & S_IRWXO);
+ EXPECT_FALSE(shm_stat.st_mode & S_IRWXG);
+ // Restore umask.
+ umask(old_umask);
+}
+
+#endif // defined(OS_POSIX)
// Map() will return addresses which are aligned to the platform page size, this
// varies from platform to platform though. Since we'd like to advertise a
« base/memory/shared_memory_posix.cc ('K') | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698