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