Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Enable BTI in bionic linker
Browse files Browse the repository at this point in the history
This patch adds support to load BTI-enabled objects.

According to the ABI, BTI is recorded in the .note.gnu.property section.
The new parser evaluates the property section, if exists.
It searches for .note section with NT_GNU_PROPERTY_TYPE_0.
Once found it tries to find GNU_PROPERTY_AARCH64_FEATURE_1_AND.
The results are cached.

The main change in linker is when protection of loaded ranges gets
applied. When BTI is requested and the platform also supports it
the prot flags have to be amended with PROT_BTI for executable ranges.
Failing to add PROT_BTI flag would disable BTI protection.
Moreover, adding the new PROT flag for shared objects without BTI
compatibility would break applications.

Kernel does not add PROT_BTI to a loaded ELF which has interpreter.
Linker handles this case too.

Test: 1. Flame boots
      2. Tested on FVP with BTI enabled

Change-Id: Iafdf223b74c6e75d9f17ca90500e6fe42c4c1218
  • Loading branch information
tamaspetz authored and rprichard committed Sep 24, 2020
1 parent 0a12075 commit 8d55d18
Show file tree
Hide file tree
Showing 12 changed files with 816 additions and 13 deletions.
5 changes: 5 additions & 0 deletions libc/include/bits/elf_arm64.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@
#define R_AARCH64_TLSDESC 1031 /* 16-byte descriptor: resolver func + arg. */
#define R_AARCH64_IRELATIVE 1032

/* Dynamic array tags */
#define DT_AARCH64_BTI_PLT 0x70000001
#define DT_AARCH64_PAC_PLT 0x70000003
#define DT_AARCH64_VARIANT_PCS 0x70000005

#endif /* _AARCH64_ELF_MACHDEP_H_ */
3 changes: 3 additions & 0 deletions linker/Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ filegroup {
"linker_namespaces.cpp",
"linker_logger.cpp",
"linker_mapped_file_fragment.cpp",
"linker_note_gnu_property.cpp",
"linker_phdr.cpp",
"linker_relocate.cpp",
"linker_sdk_versions.cpp",
Expand Down Expand Up @@ -447,6 +448,7 @@ cc_test {
"linker_block_allocator_test.cpp",
"linker_config_test.cpp",
"linked_list_test.cpp",
"linker_note_gnu_property_test.cpp",
"linker_sleb128_test.cpp",
"linker_utils_test.cpp",
"linker_gnu_hash_test.cpp",
Expand All @@ -455,6 +457,7 @@ cc_test {
"linker_block_allocator.cpp",
"linker_config.cpp",
"linker_debug.cpp",
"linker_note_gnu_property.cpp",
"linker_test_globals.cpp",
"linker_utils.cpp",
],
Expand Down
8 changes: 8 additions & 0 deletions linker/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3141,6 +3141,14 @@ bool soinfo::prelink_image() {
// resolves everything eagerly, so these can be ignored.
break;

#if defined(__aarch64__)
case DT_AARCH64_BTI_PLT:
case DT_AARCH64_PAC_PLT:
case DT_AARCH64_VARIANT_PCS:
// Ignored: AArch64 processor-specific dynamic array tags.
break;
#endif

default:
if (!relocating_linker) {
const char* tag_name;
Expand Down
6 changes: 6 additions & 0 deletions linker/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,9 @@ struct address_space_params {
int get_application_target_sdk_version();
ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi);
bool validate_verdef_section(const soinfo* si);

struct platform_properties {
#if defined(__aarch64__)
bool bti_supported = false;
#endif
};
2 changes: 2 additions & 0 deletions linker/linker_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ android_namespace_t g_default_namespace;

std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;

platform_properties g_platform_properties;

static char __linker_dl_err_buf[768];

char* linker_get_error_buffer() {
Expand Down
3 changes: 3 additions & 0 deletions linker/linker_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ extern char** g_envp;

struct soinfo;
struct android_namespace_t;
struct platform_properties;

extern android_namespace_t g_default_namespace;

extern std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;

extern platform_properties g_platform_properties;

// Error buffer "variable"
char* linker_get_error_buffer();
size_t linker_get_error_buffer_size();
Expand Down
24 changes: 24 additions & 0 deletions linker/linker_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,13 @@ static ExecutableInfo load_executable(const char* orig_path) {
return result;
}

static void platform_properties_init() {
#if defined(__aarch64__)
const unsigned long hwcap2 = getauxval(AT_HWCAP2);
g_platform_properties.bti_supported = (hwcap2 & HWCAP2_BTI) != 0;
#endif
}

static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load) {
ProtectedDataGuard guard;

Expand All @@ -311,6 +318,9 @@ static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load
// Initialize system properties
__system_properties_init(); // may use 'environ'

// Initialize platform properties.
platform_properties_init();

// Register the debuggerd signal handler.
linker_debuggerd_init();

Expand Down Expand Up @@ -381,6 +391,20 @@ static ElfW(Addr) linker_main(KernelArgumentBlock& args, const char* exe_to_load
solinker->set_realpath(interp);
init_link_map_head(*solinker);

#if defined(__aarch64__)
if (exe_to_load == nullptr) {
// Kernel does not add PROT_BTI to executable pages of the loaded ELF.
// Apply appropriate protections here if it is needed.
auto note_gnu_property = GnuPropertySection(somain);
if (note_gnu_property.IsBTICompatible() &&
(phdr_table_protect_segments(somain->phdr, somain->phnum, somain->load_bias,
&note_gnu_property) < 0)) {
__linker_error("error: can't protect segments for \"%s\": %s", exe_info.path.c_str(),
strerror(errno));
}
}
#endif

// Register the main executable and the linker upfront to have
// gdb aware of them before loading the rest of the dependency
// tree.
Expand Down
186 changes: 186 additions & 0 deletions linker/linker_note_gnu_property.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2020 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include "linker_note_gnu_property.h"

#include <elf.h>
#include <link.h>

#include "linker.h"
#include "linker_debug.h"
#include "linker_globals.h"
#include "linker_soinfo.h"

GnuPropertySection::GnuPropertySection(const soinfo* si)
: GnuPropertySection(si->phdr, si->phnum, si->load_bias, si->get_realpath()) {}

GnuPropertySection::GnuPropertySection(const ElfW(Phdr)* phdr, size_t phdr_count,
const ElfW(Addr) load_bias, const char* name) {
// Try to find PT_GNU_PROPERTY segment.
auto note_gnu_property = FindSegment(phdr, phdr_count, load_bias, name);
// Perform some validity checks.
if (note_gnu_property && SanityCheck(note_gnu_property, name)) {
// Parse section.
Parse(note_gnu_property, name);
}
}

const ElfW(NhdrGNUProperty)* GnuPropertySection::FindSegment(const ElfW(Phdr)* phdr,
size_t phdr_count,
const ElfW(Addr) load_bias,
const char* name) const {
// According to Linux gABI extension this segment should contain
// .note.gnu.property section only.
if (phdr != nullptr) {
for (size_t i = 0; i < phdr_count; ++i) {
if (phdr[i].p_type != PT_GNU_PROPERTY) {
continue;
}

TRACE("\"%s\" PT_GNU_PROPERTY: found at segment index %zu", name, i);

// Check segment size.
if (phdr[i].p_memsz < sizeof(ElfW(NhdrGNUProperty))) {
DL_ERR_AND_LOG(
"\"%s\" PT_GNU_PROPERTY segment is too small. Segment "
"size is %zu, minimum is %zu.",
name, static_cast<size_t>(phdr[i].p_memsz), sizeof(ElfW(NhdrGNUProperty)));
return nullptr;
}

// PT_GNU_PROPERTY contains .note.gnu.property which has SHF_ALLOC
// attribute, therefore it is loaded.
auto note_nhdr = reinterpret_cast<ElfW(NhdrGNUProperty)*>(load_bias + phdr[i].p_vaddr);

// Check that the n_descsz <= p_memsz
if ((phdr[i].p_memsz - sizeof(ElfW(NhdrGNUProperty))) < note_nhdr->nhdr.n_descsz) {
DL_ERR_AND_LOG(
"\"%s\" PT_GNU_PROPERTY segment p_memsz (%zu) is too small for note n_descsz (%zu).",
name, static_cast<size_t>(phdr[i].p_memsz),
static_cast<size_t>(note_nhdr->nhdr.n_descsz));
return nullptr;
}

return note_nhdr;
}
}

TRACE("\"%s\" PT_GNU_PROPERTY: not found", name);
return nullptr;
}

bool GnuPropertySection::SanityCheck(const ElfW(NhdrGNUProperty)* note_nhdr,
const char* name) const {
// Check .note section type
if (note_nhdr->nhdr.n_type != NT_GNU_PROPERTY_TYPE_0) {
DL_ERR_AND_LOG("\"%s\" .note.gnu.property: unexpected note type. Expected %u, got %u.", name,
NT_GNU_PROPERTY_TYPE_0, note_nhdr->nhdr.n_type);
return false;
}

if (note_nhdr->nhdr.n_namesz != 4) {
DL_ERR_AND_LOG("\"%s\" .note.gnu.property: unexpected name size. Expected 4, got %u.", name,
note_nhdr->nhdr.n_namesz);
return false;
}

if (strncmp(note_nhdr->n_name, "GNU", 4) != 0) {
DL_ERR_AND_LOG("\"%s\" .note.gnu.property: unexpected name. Expected 'GNU', got '%s'.", name,
note_nhdr->n_name);
return false;
}

return true;
}

bool GnuPropertySection::Parse(const ElfW(NhdrGNUProperty)* note_nhdr, const char* name) {
// The total length of the program property array is in _bytes_.
ElfW(Word) offset = 0;
while (offset < note_nhdr->nhdr.n_descsz) {
DEBUG("\"%s\" .note.gnu.property: processing at offset 0x%x", name, offset);

// At least the "header" part must fit.
// The ABI doesn't say that pr_datasz can't be 0.
if ((note_nhdr->nhdr.n_descsz - offset) < sizeof(ElfW(Prop))) {
DL_ERR_AND_LOG(
"\"%s\" .note.gnu.property: no more space left for a "
"Program Property Note header.",
name);
return false;
}

// Loop on program property array.
const ElfW(Prop)* property = reinterpret_cast<const ElfW(Prop)*>(&note_nhdr->n_desc[offset]);
const ElfW(Word) property_size =
align_up(sizeof(ElfW(Prop)) + property->pr_datasz, sizeof(ElfW(Addr)));
if ((note_nhdr->nhdr.n_descsz - offset) < property_size) {
DL_ERR_AND_LOG(
"\"%s\" .note.gnu.property: property descriptor size is "
"invalid. Expected at least %u bytes, got %u.",
name, property_size, note_nhdr->nhdr.n_descsz - offset);
return false;
}

// Cache found properties.
switch (property->pr_type) {
#if defined(__aarch64__)
case GNU_PROPERTY_AARCH64_FEATURE_1_AND: {
if (property->pr_datasz != 4) {
DL_ERR_AND_LOG(
"\"%s\" .note.gnu.property: property descriptor size is "
"invalid. Expected %u bytes for GNU_PROPERTY_AARCH64_FEATURE_1_AND, got %u.",
name, 4, property->pr_datasz);
return false;
}

const ElfW(Word) flags = *reinterpret_cast<const ElfW(Word)*>(&property->pr_data[0]);
properties_.bti_compatible = (flags & GNU_PROPERTY_AARCH64_FEATURE_1_BTI) != 0;
if (properties_.bti_compatible) {
INFO("[ BTI compatible: \"%s\" ]", name);
}
break;
}
#endif
default:
DEBUG("\"%s\" .note.gnu.property: found property pr_type %u pr_datasz 0x%x", name,
property->pr_type, property->pr_datasz);
break;
}

// Move offset, this should be safe to add because of previous checks.
offset += property_size;
}

return true;
}

#if defined(__aarch64__)
bool GnuPropertySection::IsBTICompatible() const {
return (g_platform_properties.bti_supported && properties_.bti_compatible);
}
#endif
Loading

0 comments on commit 8d55d18

Please sign in to comment.