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

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

Issue 1501003002: Added protection against mapping image sections between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes from review Created 5 years 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
« no previous file with comments | « base/memory/shared_memory.h ('k') | base/memory/shared_memory_win.cc » ('j') | 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/atomicops.h" 5 #include "base/atomicops.h"
6 #include "base/basictypes.h" 6 #include "base/basictypes.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/shared_memory.h" 8 #include "base/memory/shared_memory.h"
9 #include "base/memory/shared_memory_handle.h"
9 #include "base/process/kill.h" 10 #include "base/process/kill.h"
10 #include "base/rand_util.h" 11 #include "base/rand_util.h"
11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
12 #include "base/sys_info.h" 13 #include "base/sys_info.h"
13 #include "base/test/multiprocess_test.h" 14 #include "base/test/multiprocess_test.h"
14 #include "base/threading/platform_thread.h" 15 #include "base/threading/platform_thread.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/multiprocess_func_list.h" 18 #include "testing/multiprocess_func_list.h"
18 19
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 TEST(SharedMemoryTest, MapMinimumAlignment) { 565 TEST(SharedMemoryTest, MapMinimumAlignment) {
565 static const int kDataSize = 8192; 566 static const int kDataSize = 8192;
566 567
567 SharedMemory shared_memory; 568 SharedMemory shared_memory;
568 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize)); 569 ASSERT_TRUE(shared_memory.CreateAndMapAnonymous(kDataSize));
569 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>( 570 EXPECT_EQ(0U, reinterpret_cast<uintptr_t>(
570 shared_memory.memory()) & (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); 571 shared_memory.memory()) & (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
571 shared_memory.Close(); 572 shared_memory.Close();
572 } 573 }
573 574
575 #if defined(OS_WIN)
576 TEST(SharedMemoryTest, UnsafeImageSection) {
577 const char kTestSectionName[] = "UnsafeImageSection";
578 wchar_t path[MAX_PATH];
579 EXPECT_GT(::GetModuleFileName(nullptr, path, arraysize(path)), 0U);
580
581 // Map the current executable image to save us creating a new PE file on disk.
582 base::win::ScopedHandle file_handle(
583 ::CreateFile(path, GENERIC_READ, 0, nullptr, OPEN_EXISTING, 0, nullptr));
584 EXPECT_TRUE(file_handle.IsValid());
585 base::win::ScopedHandle section_handle(
586 ::CreateFileMappingA(file_handle.Get(), nullptr,
587 PAGE_READONLY | SEC_IMAGE, 0, 0, kTestSectionName));
588 EXPECT_TRUE(section_handle.IsValid());
589
590 // Check direct opening by name, from handle and duplicated from handle.
591 SharedMemory shared_memory_open;
592 EXPECT_TRUE(shared_memory_open.Open(kTestSectionName, true));
593 EXPECT_FALSE(shared_memory_open.Map(1));
594 EXPECT_EQ(nullptr, shared_memory_open.memory());
595
596 SharedMemory shared_memory_handle_dup(
597 SharedMemoryHandle(section_handle.Get(), ::GetCurrentProcessId()), true,
598 GetCurrentProcess());
599 EXPECT_FALSE(shared_memory_handle_dup.Map(1));
600 EXPECT_EQ(nullptr, shared_memory_handle_dup.memory());
601
602 SharedMemory shared_memory_handle_local(
603 SharedMemoryHandle(section_handle.Take(), ::GetCurrentProcessId()), true);
604 EXPECT_FALSE(shared_memory_handle_local.Map(1));
605 EXPECT_EQ(nullptr, shared_memory_handle_local.memory());
606
607 // Check that a handle without SECTION_QUERY also can't be mapped as it can't
608 // be checked.
609 SharedMemory shared_memory_handle_dummy;
610 SharedMemoryCreateOptions options;
611 options.size = 0x1000;
612 EXPECT_TRUE(shared_memory_handle_dummy.Create(options));
613 HANDLE handle_no_query;
614 EXPECT_TRUE(::DuplicateHandle(
615 ::GetCurrentProcess(), shared_memory_handle_dummy.handle().GetHandle(),
616 ::GetCurrentProcess(), &handle_no_query, FILE_MAP_READ, FALSE, 0));
617 SharedMemory shared_memory_handle_no_query(
618 SharedMemoryHandle(handle_no_query, ::GetCurrentProcessId()), true);
619 EXPECT_FALSE(shared_memory_handle_no_query.Map(1));
620 EXPECT_EQ(nullptr, shared_memory_handle_no_query.memory());
621 }
622 #endif // defined(OS_WIN)
623
574 // iOS does not allow multiple processes. 624 // iOS does not allow multiple processes.
575 // Android ashmem does not support named shared memory. 625 // Android ashmem does not support named shared memory.
576 // Mac SharedMemory does not support named shared memory. crbug.com/345734 626 // Mac SharedMemory does not support named shared memory. crbug.com/345734
577 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) 627 #if !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
578 // On POSIX it is especially important we test shmem across processes, 628 // On POSIX it is especially important we test shmem across processes,
579 // not just across threads. But the test is enabled on all platforms. 629 // not just across threads. But the test is enabled on all platforms.
580 class SharedMemoryProcessTest : public MultiProcessTest { 630 class SharedMemoryProcessTest : public MultiProcessTest {
581 public: 631 public:
582 static void CleanUp() { 632 static void CleanUp() {
583 SharedMemory memory; 633 SharedMemory memory;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 memory.Close(); 695 memory.Close();
646 SharedMemoryProcessTest::CleanUp(); 696 SharedMemoryProcessTest::CleanUp();
647 } 697 }
648 698
649 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { 699 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) {
650 return SharedMemoryProcessTest::TaskTestMain(); 700 return SharedMemoryProcessTest::TaskTestMain();
651 } 701 }
652 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) 702 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
653 703
654 } // namespace base 704 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/shared_memory.h ('k') | base/memory/shared_memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698