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.
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
9 // thus use base/ and the C++ STL.
11 #include "content/shell/android/linker_test_apk/content_linker_test_linker_tests.h"
18 #include "base/basictypes.h"
19 #include "base/debug/proc_maps_linux.h"
20 #include "base/logging.h"
21 #include "base/strings/stringprintf.h"
23 #include "jni/LinkerTests_jni.h"
29 using base::debug::MappedMemoryRegion;
31 jboolean RunChecks(bool in_browser_process, bool need_relros) {
33 // IMPORTANT NOTE: The Python test control script reads the logcat for
35 // BROWSER_LINKER_TEST: <status>
36 // RENDERER_LINKER_TEST: <status>
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.
41 in_browser_process ? "BROWSER_LINKER_TEST: " : "RENDERER_LINKER_TEST: ";
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.
47 // Note that the full name will be something like:
48 // "/dev/ashmem/RELRO:<libname> (deleted)"
50 // Where <libname> is the library name and '(deleted)' is actually
51 // added by the kernel to indicate there is no corresponding file
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:";
59 // Parse /proc/self/maps and builds a list of region mappings in this
62 base::debug::ReadProcMaps(&maps);
64 LOG(ERROR) << prefix << "FAIL Cannot parse /proc/self/maps";
68 std::vector<MappedMemoryRegion> regions;
69 base::debug::ParseProcMaps(maps, ®ions);
70 if (regions.empty()) {
71 LOG(ERROR) << prefix << "FAIL Cannot read memory mappings in this process";
75 size_t num_shared_relros = 0;
76 size_t num_bad_shared_relros = 0;
78 for (size_t n = 0; n < regions.size(); ++n) {
79 MappedMemoryRegion& region = regions[n];
81 if (region.path.find(kRelroSectionPrefix) != 0) {
82 // Ignore any mapping that isn't a shared RELRO.
88 void* region_start = reinterpret_cast<void*>(region.start);
89 void* region_end = reinterpret_cast<void*>(region.end);
91 // Check that it is mapped read-only.
92 const uint8 expected_flags = MappedMemoryRegion::READ;
93 const uint8 expected_mask = MappedMemoryRegion::READ |
94 MappedMemoryRegion::WRITE |
95 MappedMemoryRegion::EXECUTE;
97 uint8 region_flags = region.permissions & expected_mask;
98 if (region_flags != expected_flags) {
101 << base::StringPrintf(
102 "Shared RELRO section at %p-%p is not mapped read-only. "
103 "Protection flags are %d (%d expected)!",
108 num_bad_shared_relros++;
112 // Check that trying to remap it read-write fails with EACCES
113 size_t region_size = region.end - region.start;
114 int ret = ::mprotect(region_start, region_size, PROT_READ | PROT_WRITE);
118 << base::StringPrintf(
119 "Shared RELRO section at %p-%p could be remapped read-write!?",
122 num_bad_shared_relros++;
124 ::mprotect(region_start, region_size, PROT_READ);
125 } else if (errno != EACCES) {
126 LOG(ERROR) << prefix << base::StringPrintf(
127 "Shared RELRO section at %p-%p failed "
128 "read-write mprotect with "
129 "unexpected error %d (EACCES:%d wanted): %s",
135 num_bad_shared_relros++;
141 << base::StringPrintf(
142 "There are %d shared RELRO sections in this process, %d are bad",
144 num_bad_shared_relros);
146 if (num_bad_shared_relros > 0) {
147 LOG(ERROR) << prefix << "FAIL Bad Relros sections in this process";
152 if (num_shared_relros == 0) {
154 << "FAIL Missing shared RELRO sections in this process!";
158 if (num_shared_relros > 0) {
159 LOG(ERROR) << prefix << "FAIL Unexpected " << num_shared_relros
160 << " shared RELRO sections in this process!";
165 LOG(INFO) << prefix << "SUCCESS";
171 jboolean CheckForSharedRelros(JNIEnv* env,
173 jboolean in_browser_process) {
174 return RunChecks(in_browser_process, true);
177 jboolean CheckForNoSharedRelros(JNIEnv* env,
179 jboolean in_browser_process) {
180 return RunChecks(in_browser_process, false);
183 bool RegisterLinkerTestsJni(JNIEnv* env) { return RegisterNativesImpl(env); }
185 } // namespace content