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