Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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 // This file implements the native methods of | |
| 6 // org.content.chromium.app.LinkerTests | |
| 7 // Unlike the content of linker_jni.cc, it is part of the content library and | |
| 8 // can | |
| 9 // thus use base/ and the C++ STL. | |
| 10 | |
| 11 #include "content/shell/android/linker_test_apk/content_linker_test_linker_tests .h" | |
| 12 | |
| 13 #include <errno.h> | |
| 14 #include <sys/mman.h> | |
| 15 #include <stdio.h> | |
| 16 #include <string> | |
| 17 | |
| 18 #include "base/basictypes.h" | |
| 19 #include "base/debug/proc_maps_linux.h" | |
| 20 #include "base/logging.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 | |
| 23 #include "jni/LinkerTests_jni.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 using base::debug::MappedMemoryRegion; | |
| 30 | |
| 31 jboolean RunChecks(bool inBrowserProcess, bool needRelros) { | |
|
palmer
2013/10/04 00:05:35
Is it possible to write a test to ensure that the
digit1
2013/10/07 13:32:29
Yes, I've added two four new tests that check the
| |
| 32 | |
| 33 // IMPORTANT NOTE: The Python test control script reads the logcat for | |
| 34 // lines like: | |
| 35 // BROWSER_LINKER_TEST: <status> | |
| 36 // RENDERER_LINKER_TEST: <status> | |
| 37 // | |
| 38 // Where <status> can be either SUCCESS or FAIL. Other lines starting | |
| 39 // with the same prefixes, but not using SUCCESS or FAIL are ignored. | |
| 40 const char* prefix = | |
| 41 inBrowserProcess ? "BROWSER_LINKER_TEST: " : "RENDERER_LINKER_TEST: "; | |
| 42 | |
| 43 // The RELRO section(s), after being copied into an ashmem region, will | |
| 44 // appear in /proc/self/maps as a mapped memory region for a file name | |
| 45 // that begins with the following prefix. | |
| 46 // | |
| 47 // Note that the full name will be something like: | |
| 48 // "/dev/ashmem/RELRO:<libname> (deleted)" | |
| 49 // | |
| 50 // Where <libname> is the library name and '(deleted)' is actually | |
| 51 // added by the kernel to indicate there is no corresponding file | |
| 52 // on the filesystem. | |
| 53 // | |
| 54 // For regular builds, there is only one library, and thus one RELRO | |
| 55 // section, but for the component build, there are several libraries, | |
| 56 // each one with its own RELRO. | |
| 57 static const char kRelroSectionPrefix[] = "/dev/ashmem/RELRO:"; | |
| 58 std::string relro_section_prefix(kRelroSectionPrefix); | |
|
palmer
2013/10/04 00:05:35
Nit: I think you can just do
static const std
digit1
2013/10/07 13:32:29
I tend to avoid static std::string variables, sinc
| |
| 59 | |
| 60 // Parse /proc/self/maps and builds a list of region mappings in this | |
| 61 // process. | |
| 62 std::string maps; | |
| 63 base::debug::ReadProcMaps(&maps); | |
| 64 if (maps.empty()) { | |
| 65 LOG(ERROR) << prefix << "FAIL Cannot parse /proc/self/maps"; | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 std::vector<MappedMemoryRegion> regions; | |
| 70 base::debug::ParseProcMaps(maps, ®ions); | |
| 71 if (regions.empty()) { | |
| 72 LOG(ERROR) << prefix << "FAIL Cannot read memory mappings in this process"; | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 size_t num_shared_relros = 0; | |
| 77 size_t num_bad_shared_relros = 0; | |
| 78 | |
| 79 for (size_t n = 0; n < regions.size(); ++n) { | |
| 80 MappedMemoryRegion& region = regions[n]; | |
| 81 | |
| 82 if (region.path.find(relro_section_prefix) != 0) { | |
| 83 // Ignore any mapping that isn't a shared RELRO. | |
| 84 continue; | |
| 85 } | |
| 86 | |
| 87 num_shared_relros++; | |
| 88 | |
| 89 void* region_start = reinterpret_cast<void*>(region.start); | |
| 90 void* region_end = reinterpret_cast<void*>(region.end); | |
| 91 | |
| 92 // Check that it is mapped read-only. | |
| 93 const uint8 expected_flags = MappedMemoryRegion::READ; | |
| 94 const uint8 expected_mask = MappedMemoryRegion::READ | | |
| 95 MappedMemoryRegion::WRITE | | |
| 96 MappedMemoryRegion::EXECUTE; | |
| 97 | |
| 98 uint8 region_flags = region.permissions & expected_mask; | |
| 99 if (region_flags != expected_flags) { | |
| 100 LOG(ERROR) | |
| 101 << prefix | |
| 102 << base::StringPrintf( | |
| 103 "Shared RELRO section at %p-%p is not mapped read-only. " | |
| 104 "Protection flags are %d (%d expected)!", | |
| 105 region_start, | |
| 106 region_end, | |
| 107 region_flags, | |
| 108 expected_flags); | |
| 109 num_bad_shared_relros++; | |
| 110 continue; | |
| 111 } | |
| 112 | |
| 113 // Check that trying to remap it read-write fails with EACCES | |
| 114 size_t region_size = region.end - region.start; | |
| 115 int ret = ::mprotect(region_start, region_size, PROT_READ | PROT_WRITE); | |
| 116 if (ret != -1) { | |
| 117 LOG(ERROR) | |
| 118 << prefix | |
| 119 << base::StringPrintf( | |
| 120 "Shared RELRO section at %p-%p could be remapped read-write!?", | |
| 121 region_start, | |
| 122 region_end); | |
| 123 num_bad_shared_relros++; | |
| 124 // Just in case. | |
| 125 ::mprotect(region_start, region_size, PROT_READ); | |
| 126 } else if (errno != EACCES) { | |
| 127 LOG(ERROR) << prefix << base::StringPrintf( | |
| 128 "Shared RELRO section at %p-%p failed " | |
| 129 "read-write mprotect with " | |
| 130 "unexpected error %d (EACCES:%d wanted): %s", | |
| 131 region_start, | |
| 132 region_end, | |
| 133 errno, | |
| 134 EACCES, | |
| 135 strerror(errno)); | |
| 136 num_bad_shared_relros++; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 LOG(INFO) | |
| 141 << prefix | |
| 142 << base::StringPrintf( | |
| 143 "There are %d shared RELRO sections in this process, %d are bad", | |
| 144 num_shared_relros, | |
| 145 num_bad_shared_relros); | |
| 146 | |
| 147 if (num_bad_shared_relros > 0) { | |
| 148 LOG(INFO) << prefix << "FAIL Bad Relros sections in this process"; | |
| 149 return false; | |
| 150 } | |
| 151 | |
| 152 if (needRelros) { | |
| 153 if (num_shared_relros == 0) { | |
| 154 LOG(ERROR) << prefix | |
| 155 << "FAIL Missing shared RELRO sections in this process!"; | |
| 156 return false; | |
| 157 } | |
| 158 } else { | |
| 159 if (num_shared_relros > 0) { | |
| 160 LOG(ERROR) << prefix << "FAIL Unexpected " << num_shared_relros | |
| 161 << " shared RELRO sections in this process!"; | |
| 162 return false; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 LOG(INFO) << prefix << "SUCCESS"; | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 } // namespace | |
| 171 | |
| 172 jboolean CheckForSharedRelros(JNIEnv* env, | |
| 173 jclass clazz, | |
| 174 jboolean inBrowserProcess) { | |
| 175 return RunChecks(inBrowserProcess, true); | |
| 176 } | |
| 177 | |
| 178 jboolean CheckForNoSharedRelros(JNIEnv* env, | |
| 179 jclass clazz, | |
| 180 jboolean inBrowserProcess) { | |
| 181 return RunChecks(inBrowserProcess, false); | |
| 182 } | |
| 183 | |
| 184 bool RegisterLinkerTestsJni(JNIEnv* env) { return RegisterNativesImpl(env); } | |
| 185 | |
| 186 } // namespace content | |
| OLD | NEW |