OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_TEST_CONTENT_LINKER_UNITTEST_H |
| 6 #define CONTENT_TEST_CONTENT_LINKER_UNITTEST_H |
| 7 |
| 8 #include <errno.h> |
| 9 #include <sys/mman.h> |
| 10 #include <string> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/debug/proc_maps_linux.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // The RELRO section(s), after being copied into an ashmem region, will |
| 22 // appear in /proc/self/maps as a mapped memory region for a file name |
| 23 // that begins with the following prefix. |
| 24 // |
| 25 // Note that the full name will be something like: |
| 26 // "/dev/ashmem/RELRO:<libname> (deleted)" |
| 27 // |
| 28 // Where <libname> is the library name and '(deleted)' is actually |
| 29 // added by the kernel to indicate there is no corresponding file |
| 30 // on the filesystem. |
| 31 // |
| 32 // For regular builds, there is only one library, and thus one RELRO |
| 33 // section, but for the component build, there are several libraries, |
| 34 // each one with its own RELRO. |
| 35 static const char kRelroSectionPrefix[] = |
| 36 "/dev/ashmem/RELRO:"; |
| 37 |
| 38 using base::debug::MappedMemoryRegion; |
| 39 |
| 40 class ContentLinkerTest : public ::testing::Test { |
| 41 public: |
| 42 ContentLinkerTest() |
| 43 : regions_(), |
| 44 relro_section_prefix_(kRelroSectionPrefix) {} |
| 45 |
| 46 protected: |
| 47 virtual void SetUp() { |
| 48 regions_.clear(); |
| 49 std::string maps; |
| 50 base::debug::ReadProcMaps(&maps); |
| 51 base::debug::ParseProcMaps(maps, ®ions_); |
| 52 } |
| 53 |
| 54 virtual void TearDown() { |
| 55 } |
| 56 |
| 57 std::vector<MappedMemoryRegion> regions_; |
| 58 std::string relro_section_prefix_; |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
| 63 // This test checks that our content-based library has been loaded |
| 64 // and that its RELRO section is in an ashmem region, with a name |
| 65 // that begins with /dev/ashmem/RELRO: |
| 66 // |
| 67 TEST_F(ContentLinkerTest, RelroInAshmem) { |
| 68 size_t count = 0; |
| 69 |
| 70 for (size_t n = 0; n < regions_.size(); ++n) { |
| 71 MappedMemoryRegion& region = regions_[n]; |
| 72 |
| 73 if (region.path.find(relro_section_prefix_) == 0) |
| 74 count++; |
| 75 } |
| 76 |
| 77 // Can't use EXPECT_EQ here due to the component build having |
| 78 // many libraries loaded through our linker, each one with its own |
| 79 // shared RELRO section. There are also plenty of system libraries |
| 80 // without a shared RELRO section as well. |
| 81 EXPECT_GE(1U, count) |
| 82 << "The number of RELRO sections in ashmem regions in this process\n"; |
| 83 } |
| 84 |
| 85 // This test checks that every shared RELRO section is always read-only |
| 86 // and non-executable. |
| 87 TEST_F(ContentLinkerTest, RelroIsReadOnly) { |
| 88 uint8 expected_flags = MappedMemoryRegion::READ; |
| 89 uint8 expected_mask = |
| 90 MappedMemoryRegion::READ | |
| 91 MappedMemoryRegion::WRITE | |
| 92 MappedMemoryRegion::EXECUTE; |
| 93 |
| 94 for (size_t n = 0; n < regions_.size(); ++n) { |
| 95 MappedMemoryRegion& region = regions_[n]; |
| 96 |
| 97 if (region.path.find(relro_section_prefix_) != 0) |
| 98 continue; |
| 99 |
| 100 EXPECT_EQ(expected_flags, region.permissions & expected_mask) |
| 101 << base::StringPrintf( |
| 102 "Checking permissions for RELRO region at %p-%p\n", |
| 103 reinterpret_cast<void*>(region.start), |
| 104 reinterpret_cast<void*>(region.end)); |
| 105 } |
| 106 } |
| 107 |
| 108 // This test checks that each RELRO section cannot be remapped read-write |
| 109 // with mprotect(), for security reasons. |
| 110 TEST_F(ContentLinkerTest, RelroCantBeRemappedWritable) { |
| 111 for (size_t n = 0; n < regions_.size(); ++n) { |
| 112 MappedMemoryRegion& region = regions_[n]; |
| 113 |
| 114 if (region.path.find(relro_section_prefix_) != 0) |
| 115 continue; |
| 116 |
| 117 std::string text = base::StringPrintf( |
| 118 "Checking non-writability for RELRO region at %p-%p\n", |
| 119 reinterpret_cast<void*>(region.start), |
| 120 reinterpret_cast<void*>(region.end)); |
| 121 |
| 122 EXPECT_EQ(-1, ::mprotect(reinterpret_cast<void*>(region.start), |
| 123 region.end - region.start, |
| 124 PROT_READ | PROT_WRITE)) << text; |
| 125 EXPECT_EQ(EACCES, errno) << text; |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace content |
| 130 |
| 131 #endif // CONTENT_TEST_CONTENT_LINKER_UNITTEST_H |
OLD | NEW |