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

Side by Side Diff: base/memory/shared_memory_unittest.cc

Issue 19106006: Merge 209814 "Posix: fix named SHM mappings permissions." (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1547/src/
Patch Set: Created 7 years, 5 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/basictypes.h" 5 #include "base/basictypes.h"
6 #if defined(OS_MACOSX) 6 #if defined(OS_MACOSX)
7 #include "base/mac/scoped_nsautorelease_pool.h" 7 #include "base/mac/scoped_nsautorelease_pool.h"
8 #endif 8 #endif
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
11 #include "base/rand_util.h"
12 #include "base/strings/string_number_conversions.h"
11 #include "base/sys_info.h" 13 #include "base/sys_info.h"
12 #include "base/test/multiprocess_test.h" 14 #include "base/test/multiprocess_test.h"
13 #include "base/threading/platform_thread.h" 15 #include "base/threading/platform_thread.h"
14 #include "base/time.h" 16 #include "base/time.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/multiprocess_func_list.h" 18 #include "testing/multiprocess_func_list.h"
17 19
18 #if defined(OS_MACOSX) 20 #if defined(OS_MACOSX)
19 #include "base/mac/scoped_nsautorelease_pool.h" 21 #include "base/mac/scoped_nsautorelease_pool.h"
20 #endif 22 #endif
21 23
22 #if defined(OS_POSIX) 24 #if defined(OS_POSIX)
23 #include <sys/mman.h> 25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
24 #endif 29 #endif
25 30
26 static const int kNumThreads = 5; 31 static const int kNumThreads = 5;
27 static const int kNumTasks = 5; 32 static const int kNumTasks = 5;
28 33
29 namespace base { 34 namespace base {
30 35
31 namespace { 36 namespace {
32 37
33 // Each thread will open the shared memory. Each thread will take a different 4 38 // Each thread will open the shared memory. Each thread will take a different 4
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 SharedMemoryCreateOptions options; 399 SharedMemoryCreateOptions options;
395 options.size = kTestSize; 400 options.size = kTestSize;
396 options.executable = true; 401 options.executable = true;
397 402
398 EXPECT_TRUE(shared_memory.Create(options)); 403 EXPECT_TRUE(shared_memory.Create(options));
399 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size())); 404 EXPECT_TRUE(shared_memory.Map(shared_memory.requested_size()));
400 405
401 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(), 406 EXPECT_EQ(0, mprotect(shared_memory.memory(), shared_memory.requested_size(),
402 PROT_READ | PROT_EXEC)); 407 PROT_READ | PROT_EXEC));
403 } 408 }
404 #endif 409
410 // Android supports a different permission model than POSIX for its "ashmem"
411 // shared memory implementation. So the tests about file permissions are not
412 // included on Android.
413 #if !defined(OS_ANDROID)
414
415 // Set a umask and restore the old mask on destruction.
416 class ScopedUmaskSetter {
417 public:
418 explicit ScopedUmaskSetter(mode_t target_mask) {
419 old_umask_ = umask(target_mask);
420 }
421 ~ScopedUmaskSetter() { umask(old_umask_); }
422 private:
423 mode_t old_umask_;
424 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedUmaskSetter);
425 };
426
427 // Create a shared memory object, check its permissions.
428 TEST(SharedMemoryTest, FilePermissionsAnonymous) {
429 const uint32 kTestSize = 1 << 8;
430
431 SharedMemory shared_memory;
432 SharedMemoryCreateOptions options;
433 options.size = kTestSize;
434 // Set a file mode creation mask that gives all permissions.
435 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH);
436
437 EXPECT_TRUE(shared_memory.Create(options));
438
439 int shm_fd = shared_memory.handle().fd;
440 struct stat shm_stat;
441 EXPECT_EQ(0, fstat(shm_fd, &shm_stat));
442 // Neither the group, nor others should be able to read the shared memory
443 // file.
444 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO);
445 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG);
446 }
447
448 // Create a shared memory object, check its permissions.
449 TEST(SharedMemoryTest, FilePermissionsNamed) {
450 const uint32 kTestSize = 1 << 8;
451
452 SharedMemory shared_memory;
453 SharedMemoryCreateOptions options;
454 options.size = kTestSize;
455 std::string shared_mem_name = "shared_perm_test-" + IntToString(getpid()) +
456 "-" + Uint64ToString(RandUint64());
457 options.name = &shared_mem_name;
458 // Set a file mode creation mask that gives all permissions.
459 ScopedUmaskSetter permissive_mask(S_IWGRP | S_IWOTH);
460
461 EXPECT_TRUE(shared_memory.Create(options));
462 // Clean-up the backing file name immediately, we don't need it.
463 EXPECT_TRUE(shared_memory.Delete(shared_mem_name));
464
465 int shm_fd = shared_memory.handle().fd;
466 struct stat shm_stat;
467 EXPECT_EQ(0, fstat(shm_fd, &shm_stat));
468 // Neither the group, nor others should have been able to open the shared
469 // memory file while its name existed.
470 EXPECT_FALSE(shm_stat.st_mode & S_IRWXO);
471 EXPECT_FALSE(shm_stat.st_mode & S_IRWXG);
472 }
473 #endif // !defined(OS_ANDROID)
474
475 #endif // defined(OS_POSIX)
405 476
406 // Map() will return addresses which are aligned to the platform page size, this 477 // Map() will return addresses which are aligned to the platform page size, this
407 // varies from platform to platform though. Since we'd like to advertise a 478 // varies from platform to platform though. Since we'd like to advertise a
408 // minimum alignment that callers can count on, test for it here. 479 // minimum alignment that callers can count on, test for it here.
409 TEST(SharedMemoryTest, MapMinimumAlignment) { 480 TEST(SharedMemoryTest, MapMinimumAlignment) {
410 static const int kDataSize = 8192; 481 static const int kDataSize = 8192;
411 482
412 SharedMemory shared_memory; 483 SharedMemory shared_memory;
413 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); 484 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize));
414 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( 485 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>(
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 SharedMemoryProcessTest::CleanUp(); 554 SharedMemoryProcessTest::CleanUp();
484 } 555 }
485 556
486 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { 557 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) {
487 return SharedMemoryProcessTest::TaskTestMain(); 558 return SharedMemoryProcessTest::TaskTestMain();
488 } 559 }
489 560
490 #endif // !OS_IOS 561 #endif // !OS_IOS
491 562
492 } // namespace base 563 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/shared_memory_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698