This source file includes following definitions.
- RunChecks
- CheckForSharedRelros
- CheckForNoSharedRelros
- RegisterLinkerTestsJni
#include "content/shell/android/linker_test_apk/chromium_linker_test_linker_tests.h"
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>
#include <string>
#include "base/basictypes.h"
#include "base/debug/proc_maps_linux.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "jni/LinkerTests_jni.h"
namespace content {
namespace {
using base::debug::MappedMemoryRegion;
jboolean RunChecks(bool in_browser_process, bool need_relros) {
const char* prefix =
in_browser_process ? "BROWSER_LINKER_TEST: " : "RENDERER_LINKER_TEST: ";
static const char kRelroSectionPrefix[] = "/dev/ashmem/RELRO:";
std::string maps;
base::debug::ReadProcMaps(&maps);
if (maps.empty()) {
LOG(ERROR) << prefix << "FAIL Cannot parse /proc/self/maps";
return false;
}
std::vector<MappedMemoryRegion> regions;
base::debug::ParseProcMaps(maps, ®ions);
if (regions.empty()) {
LOG(ERROR) << prefix << "FAIL Cannot read memory mappings in this process";
return false;
}
size_t num_shared_relros = 0;
size_t num_bad_shared_relros = 0;
for (size_t n = 0; n < regions.size(); ++n) {
MappedMemoryRegion& region = regions[n];
if (region.path.find(kRelroSectionPrefix) != 0) {
continue;
}
num_shared_relros++;
void* region_start = reinterpret_cast<void*>(region.start);
void* region_end = reinterpret_cast<void*>(region.end);
const uint8 expected_flags = MappedMemoryRegion::READ;
const uint8 expected_mask = MappedMemoryRegion::READ |
MappedMemoryRegion::WRITE |
MappedMemoryRegion::EXECUTE;
uint8 region_flags = region.permissions & expected_mask;
if (region_flags != expected_flags) {
LOG(ERROR)
<< prefix
<< base::StringPrintf(
"Shared RELRO section at %p-%p is not mapped read-only. "
"Protection flags are %d (%d expected)!",
region_start,
region_end,
region_flags,
expected_flags);
num_bad_shared_relros++;
continue;
}
size_t region_size = region.end - region.start;
int ret = ::mprotect(region_start, region_size, PROT_READ | PROT_WRITE);
if (ret != -1) {
LOG(ERROR)
<< prefix
<< base::StringPrintf(
"Shared RELRO section at %p-%p could be remapped read-write!?",
region_start,
region_end);
num_bad_shared_relros++;
::mprotect(region_start, region_size, PROT_READ);
} else if (errno != EACCES) {
LOG(ERROR) << prefix << base::StringPrintf(
"Shared RELRO section at %p-%p failed "
"read-write mprotect with "
"unexpected error %d (EACCES:%d wanted): %s",
region_start,
region_end,
errno,
EACCES,
strerror(errno));
num_bad_shared_relros++;
}
}
VLOG(0)
<< prefix
<< base::StringPrintf(
"There are %d shared RELRO sections in this process, %d are bad",
num_shared_relros,
num_bad_shared_relros);
if (num_bad_shared_relros > 0) {
LOG(ERROR) << prefix << "FAIL Bad Relros sections in this process";
return false;
}
if (need_relros) {
if (num_shared_relros == 0) {
LOG(ERROR) << prefix
<< "FAIL Missing shared RELRO sections in this process!";
return false;
}
} else {
if (num_shared_relros > 0) {
LOG(ERROR) << prefix << "FAIL Unexpected " << num_shared_relros
<< " shared RELRO sections in this process!";
return false;
}
}
VLOG(0) << prefix << "SUCCESS";
return true;
}
}
jboolean CheckForSharedRelros(JNIEnv* env,
jclass clazz,
jboolean in_browser_process) {
return RunChecks(in_browser_process, true);
}
jboolean CheckForNoSharedRelros(JNIEnv* env,
jclass clazz,
jboolean in_browser_process) {
return RunChecks(in_browser_process, false);
}
bool RegisterLinkerTestsJni(JNIEnv* env) { return RegisterNativesImpl(env); }
}