Compare commits

...
Author SHA1 Message Date
Szabolcs Nagy 20e7c9d2cc aarch64: add NEWS entry about branch protection support
This is a new security feature that relies on architecture
extensions and needs glibc to be built with a gcc configured
with branch protection.
2020-06-12 14:34:39 +01:00
Szabolcs Nagy a8aad403a6 aarch64: redefine RETURN_ADDRESS to strip PAC
RETURN_ADDRESS is used at several places in glibc to mean a valid
code address of the call site, but with pac-ret it may contain a
pointer authentication code (PAC), so its definition is adjusted.

This is gcc PR target/95891: __builtin_return_address should not
expose signed pointers to user code where it can cause ABI issues.
In glibc RETURN_ADDRESS is only changed if it is built with pac-ret.
There is no detection for the specific gcc issue because it is
hard to test and the additional xpac does not cause problems.
2020-06-12 14:34:39 +01:00
Szabolcs Nagy f70dbaf7a1 aarch64: fix pac-ret support in _mcount
Currently gcc -pg -mbranch-protection=pac-ret passes signed return
address to _mcount, so _mcount now has to always strip pac from the
frompc since that's from user code that may be built with pac-ret.

This is gcc PR target/94791: signed pointers should not escape and get
passed across extern call boundaries, since that's an ABI break, but
because existing gcc has this issue we work it around in glibc until
that is resolved. This is compatible with a fixed gcc and it is a nop
on systems without PAuth support. The bug was introduced in gcc-7 with
-msign-return-address=non-leaf|all support which in gcc-9 got renamed
to -mbranch-protection=pac-ret|pac-ret+leaf|standard.

strip_pac uses inline asm instead of __builtin_aarch64_xpaclri since
that is not a documented api and not available in all supported gccs.
2020-06-12 14:34:39 +01:00
Szabolcs Nagy a7833d6a29 aarch64: Add pac-ret support to assembly files
Use return address signing in assembly files for functions that save
LR when pac-ret is enabled in the compiler.

The GNU property note for PAC-RET is not meaningful to the dynamic
linker so it is not strictly required, but it may be used to track
the security property of binaries. (The PAC-RET property is only set
if BTI is set too because BTI implies working GNU property support.)

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Szabolcs Nagy f69c4d0439 aarch64: configure check for pac-ret code generation
Return address signing requires unwinder support, which is
present in libgcc since >=gcc-7, however due to bugs the
support may be broken in <gcc-10 (and similarly there may
be issues in custom unwinders), so pac-ret is not always
safe to use. So in assembly code glibc should only use
pac-ret if the compiler uses it too. Unfortunately there
is no predefined feature macro for it set by the compiler
so pac-ret is inferred from the code generation.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Sudakshina DasandSzabolcs Nagy 3d0cab6a17 aarch64: enable BTI at runtime
Binaries can opt-in to using BTI via an ELF object file marking.
The dynamic linker has to then mprotect the executable segments
with PROT_BTI. In case of static linked executables or in case
of the dynamic linker itself, PROT_BTI protection is done by the
operating system.

On AArch64 glibc uses PT_GNU_PROPERTY instead of PT_NOTE to check
the properties of a binary because PT_NOTE can be unreliable with
old linkers (old linkers just append the notes of input objects
together and add them to the output without checking them for
consistency which means multiple incompatible GNU property notes
can be present in PT_NOTE). A new _dl_process_pt_gnu_property
hook is introduced in dl-prop.h and to keep it maintainable the
rtld and dlopen code paths use the same function (if the main
map needs special treatment, that should be inferred by the hook
from the link map). Unlike the _dt_process_pt_note hook this one
is called after segments are mapped to avoid unbounded allocation
and additional read syscall. Otherwise the AArch64 logic follows
the x86 logic for handling GNU properties (but the code is not
shared because x86 needs to manage internal CET state and look
out for multiple property notes).

BTI property is handled in the loader even if glibc is not built
with BTI support, so in theory user code can be BTI protected
independently of glibc. In practice though user binaries are not
marked with the BTI property if glibc has no support because the
static linked libc objects (crt files, libc_nonshared.a) are
unmarked.

This patch relies on Linux userspace API that is scheduled to be
merged in Linux 5.8 and now it is in the for-next/bti-user branch
of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git.

Co-authored-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Szabolcs Nagy 479a62fd3c aarch64: fix RTLD_START for BTI
Tailcalls must use x16 or x17 for the indirect branch instruction
to be compatible with code that uses BTI c at function entries.
(Other forms of indirect branches can only land on BTI j.)

Also added a BTI c at the ELF entry point of rtld, this is not
strictly necessary since the kernel does not use indirect branch
to get there, but it seems safest once building glibc itself with
BTI is supported.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Szabolcs Nagy 72d607dcad aarch64: fix swapcontext for BTI
setcontext returns to the specified context via an indirect jump,
so there should be a BTI j.

In case of getcontext (and all other returns_twice functions) the
compiler adds BTI j at the call site, but swapcontext is a normal
c call that is currently not handled specially by the compiler.

So we change swapcontext such that the saved context returns to a
local address that has BTI j and then swapcontext returns to the
caller via a normal RET. For this we save the original return
address in the slot for x1 of the context because x1 need not be
preserved by swapcontext but it is restored when the context saved
by swapcontext is resumed.

The alternative fix (which is done on x86) would make swapcontext
special in the compiler so BTI j is emitted at call sites, on
x86 there is an indirect_return attribute for this, on AArch64
we would have to use returns_twice. It was decided against because
such fix may need user code updates: the attribute has to be added
when swapcontext is called via a function pointer and it breaks
always_inline functions with swapcontext.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Szabolcs Nagy 26245e2725 aarch64: Rename place holder .S files to .c
The compiler can add required elf markings based on CFLAGS
but the assembler cannot, so using C code for empty files
creates less of a maintenance problem.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Sudakshina DasandSzabolcs Nagy f440d0a804 aarch64: Add BTI support to assembly files
To enable building glibc with branch protection, assembly code
needs BTI landing pads and ELF object file markings in the form
of a GNU property note.

The landing pads are unconditionally added to all functions that
may be indirectly called. When the code segment is not mapped
with PROT_BTI these instructions are nops. They are kept in the
code when BTI is not supported so that the layout of performance
critical code is unchanged across configurations.

The GNU property notes are only added when there is support for
BTI in the toolchain, because old binutils does not handle the
notes right. (Does not know how to merge them nor to put them in
PT_GNU_PROPERTY segment instead of PT_NOTE, and some versions
of binutils emit warnings about the unknown GNU property. In
such cases the produced libc binaries would not have valid
ELF marking so BTI would not be enabled.)

Note: functions using ENTRY or ENTRY_ALIGN now start with an
additional BTI c, so alignment of the following code changes,
but ENTRY_ALIGN_AND_PAD was fixed so there is no change to the
existing code layout. Some string functions may need to be
tuned for optimal performance after this commit.

Co-authored-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2020-06-12 14:34:39 +01:00
Szabolcs Nagy 088a606441 aarch64: configure test for BTI support
Check BTI support in the compiler and linker.  The check also
requires READELF that understands the BTI GNU property note.
It is expected to succeed with gcc >=gcc-9 configured with
--enable-standard-branch-protection and binutils >=binutils-2.33.

Note: passing -mbranch-protection=bti in CFLAGS when building glibc
may not be enough to get a glibc that supports BTI because crtbegin*
and crtend* provided by the compiler needs to be BTI compatible too.
2020-06-12 14:34:39 +01:00
Szabolcs Nagy d9c9571877 Rewrite abi-note.S in C.
Using C code allows the compiler to add target specific object file
markings based on CFLAGS.

The arm specific abi-note.S is removed and similar object file fix
up will be avoided on AArch64 with standard branch-prtection.
2020-06-12 14:34:39 +01:00
30 changed files with 557 additions and 28 deletions
+12
View File
@@ -31,6 +31,18 @@ Major new features:
pthread_attr_getsigmask_np have been added. They allow applications
to specify the signal mask of a thread created with pthread_create.
* AArch64 now supports standard branch protection security hardening
in glibc when it is built with a GCC that is configured with
--enable-standard-branch-protection. This includes branch target
identification (BTI) and pointer authentication for return addresses
(PAC-RET). They require armv8.5-a and armv8.3-a architecture
extensions respectively for the protection to be effective,
otherwise the used instructions are nops. User code can use PAC-RET
without libc support, but BTI requires a libc that is built with BTI
support, otherwise runtime objects linked into user code will not be
BTI compatible. It is recommended to use GCC 10 or newer when
building glibc with branch protection.
Deprecated and removed features, and other changes affecting compatibility:
* The deprecated <sys/sysctl.h> header and the sysctl function have been
+6
View File
@@ -109,6 +109,12 @@
/* AArch64 big endian ABI */
#undef HAVE_AARCH64_BE
/* AArch64 BTI support enabled. */
#define HAVE_AARCH64_BTI 0
/* AArch64 PAC-RET code generation is enabled. */
#define HAVE_AARCH64_PAC_RET 0
/* C-SKY ABI version. */
#undef CSKYABI
+13 -10
View File
@@ -53,6 +53,8 @@ offset length contents
identify the earliest release of that OS that supports this ABI.
See abi-tags (top level) for details. */
#include <link.h>
#include <stdint.h>
#include <config.h>
#include <abi-tag.h> /* OS-specific ABI tag value */
@@ -60,13 +62,14 @@ offset length contents
name begins with `.note' and creates a PT_NOTE program header entry
pointing at it. */
.section ".note.ABI-tag", "a"
.p2align 2
.long 1f - 0f /* name length */
.long 3f - 2f /* data length */
.long 1 /* note type */
0: .asciz "GNU" /* vendor name */
1: .p2align 2
2: .long __ABI_TAG_OS /* note data: the ABI tag */
.long __ABI_TAG_VERSION
3: .p2align 2 /* pad out section */
__attribute__ ((used, aligned (4), section (".note.ABI-tag")))
static const struct
{
ElfW(Nhdr) nhdr;
char name[4];
int32_t desc[4];
} __abi_tag = {
{ .n_namesz = 4, .n_descsz = 16, .n_type = 1 },
"GNU",
{ __ABI_TAG_OS, __ABI_TAG_VERSION }
};
+14
View File
@@ -1188,6 +1188,20 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
maplength, has_holes, loader);
if (__glibc_unlikely (errstring != NULL))
goto call_lose;
/* Process program headers again after load segments are mapped in
case processing requires accessing those segments. */
for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
switch (ph->p_type)
{
case PT_GNU_PROPERTY:
if (_dl_process_pt_gnu_property (l, ph))
{
errstring = N_("cannot process GNU property segment");
goto call_lose;
}
break;
}
}
if (l->l_ld == 0)
+6
View File
@@ -1508,6 +1508,12 @@ of this helper program; chances are you did not intend to run this program.\n\
main_map->l_relro_size = ph->p_memsz;
break;
case PT_GNU_PROPERTY:
if (_dl_process_pt_gnu_property (main_map, ph))
_dl_error_printf (
"ERROR: '%s': cannot process GNU property segment.\n", _dl_argv[0]);
break;
case PT_NOTE:
if (_rtld_process_pt_note (main_map, ph))
_dl_error_printf ("\
+4
View File
@@ -1,5 +1,9 @@
long-double-fcts = yes
ifeq ($(subdir),elf)
sysdep-dl-routines += dl-bti
endif
ifeq ($(subdir),elf)
sysdep-dl-routines += tlsdesc dl-tlsdesc
gen-as-const-headers += dl-link.sym
+81
View File
@@ -172,3 +172,84 @@ else
config_vars="$config_vars
default-abi = lp64"
fi
# Only consider BTI supported if -mbranch-protection=bti is
# on by default in the compiler and the linker produces
# binaries with GNU property notes in PT_GNU_PROPERTY segment.
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for BTI support" >&5
$as_echo_n "checking for BTI support... " >&6; }
if ${libc_cv_aarch64_bti+:} false; then :
$as_echo_n "(cached) " >&6
else
cat > conftest.c <<EOF
void foo (void) { }
EOF
libc_cv_aarch64_bti=no
if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -nostdlib -nostartfiles $no_ssp -shared -fPIC -o conftest.so conftest.c'
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
(eval $ac_try) 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; } \
&& { ac_try='$READELF -lW conftest.so | grep -q GNU_PROPERTY'
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
(eval $ac_try) 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; } \
&& { ac_try='$READELF -nW conftest.so | grep -q "NT_GNU_PROPERTY_TYPE_0.*AArch64 feature:.* BTI"'
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
(eval $ac_try) 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; }
then
libc_cv_aarch64_bti=yes
fi
rm -rf conftest.*
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_aarch64_bti" >&5
$as_echo "$libc_cv_aarch64_bti" >&6; }
if test $libc_cv_aarch64_bti = yes; then
$as_echo "#define HAVE_AARCH64_BTI 1" >>confdefs.h
fi
# Check if glibc is built with return address signing, i.e.
# if -mbranch-protection=pac-ret is on. We need this because
# pac-ret relies on unwinder support so it's not safe to use
# it in assembly code unconditionally, but there is no
# feature test macro for it in gcc.
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if pac-ret is enabled" >&5
$as_echo_n "checking if pac-ret is enabled... " >&6; }
if ${libc_cv_aarch64_pac_ret+:} false; then :
$as_echo_n "(cached) " >&6
else
cat > conftest.c <<EOF
int bar (void);
int foo (void) { return bar () + 1; }
EOF
libc_cv_aarch64_pac_ret=no
if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -S -o conftest.s conftest.c'
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
(eval $ac_try) 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; } \
&& { ac_try='grep -q -E '\''(hint( | )+25|paciasp)'\'' conftest.s'
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
(eval $ac_try) 2>&5
ac_status=$?
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
test $ac_status = 0; }; }
then
libc_cv_aarch64_pac_ret=yes
fi
rm -rf conftest.*
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_aarch64_pac_ret" >&5
$as_echo "$libc_cv_aarch64_pac_ret" >&6; }
if test $libc_cv_aarch64_pac_ret = yes; then
$as_echo "#define HAVE_AARCH64_PAC_RET 1" >>confdefs.h
fi
+40
View File
@@ -20,3 +20,43 @@ if test $libc_cv_aarch64_be = yes; then
else
LIBC_CONFIG_VAR([default-abi], [lp64])
fi
# Only consider BTI supported if -mbranch-protection=bti is
# on by default in the compiler and the linker produces
# binaries with GNU property notes in PT_GNU_PROPERTY segment.
AC_CACHE_CHECK([for BTI support], [libc_cv_aarch64_bti], [dnl
cat > conftest.c <<EOF
void foo (void) { }
EOF
libc_cv_aarch64_bti=no
if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -nostdlib -nostartfiles $no_ssp -shared -fPIC -o conftest.so conftest.c]) \
&& AC_TRY_COMMAND([$READELF -lW conftest.so | grep -q GNU_PROPERTY]) \
&& AC_TRY_COMMAND([$READELF -nW conftest.so | grep -q "NT_GNU_PROPERTY_TYPE_0.*AArch64 feature:.* BTI"])
then
libc_cv_aarch64_bti=yes
fi
rm -rf conftest.*])
if test $libc_cv_aarch64_bti = yes; then
AC_DEFINE(HAVE_AARCH64_BTI)
fi
# Check if glibc is built with return address signing, i.e.
# if -mbranch-protection=pac-ret is on. We need this because
# pac-ret relies on unwinder support so it's not safe to use
# it in assembly code unconditionally, but there is no
# feature test macro for it in gcc.
AC_CACHE_CHECK([if pac-ret is enabled], [libc_cv_aarch64_pac_ret], [dnl
cat > conftest.c <<EOF
int bar (void);
int foo (void) { return bar () + 1; }
EOF
libc_cv_aarch64_pac_ret=no
if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -S -o conftest.s conftest.c]) \
&& AC_TRY_COMMAND([grep -q -E '\''(hint( | )+25|paciasp)'\'' conftest.s])
then
libc_cv_aarch64_pac_ret=yes
fi
rm -rf conftest.*])
if test $libc_cv_aarch64_pac_ret = yes; then
AC_DEFINE(HAVE_AARCH64_PAC_RET)
fi
+10
View File
@@ -75,6 +75,11 @@ call_weak_fn:
.hidden _init
.type _init, %function
_init:
#if HAVE_AARCH64_PAC_RET
PACIASP
#else
BTI_C
#endif
stp x29, x30, [sp, -16]!
mov x29, sp
#if PREINIT_FUNCTION_WEAK
@@ -89,5 +94,10 @@ _init:
.hidden _fini
.type _fini, %function
_fini:
#if HAVE_AARCH64_PAC_RET
PACIASP
#else
BTI_C
#endif
stp x29, x30, [sp, -16]!
mov x29, sp
+8
View File
@@ -37,10 +37,18 @@
/* crtn.S puts function epilogues in the .init and .fini sections
corresponding to the prologues in crti.S. */
#include <sysdep.h>
.section .init,"ax",%progbits
ldp x29, x30, [sp], 16
#if HAVE_AARCH64_PAC_RET
AUTIASP
#endif
RET
.section .fini,"ax",%progbits
ldp x29, x30, [sp], 16
#if HAVE_AARCH64_PAC_RET
AUTIASP
#endif
RET
+54
View File
@@ -0,0 +1,54 @@
/* AArch64 BTI functions.
Copyright (C) 2020 Free Software Foundation, Inc.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <unistd.h>
#include <errno.h>
#include <libintl.h>
#include <ldsodefs.h>
static int
enable_bti (struct link_map *map, const char *program)
{
const ElfW(Phdr) *phdr;
unsigned prot = PROT_READ | PROT_EXEC | PROT_BTI;
for (phdr = map->l_phdr; phdr < &map->l_phdr[map->l_phnum]; ++phdr)
if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
{
ElfW(Addr) start = phdr->p_vaddr + map->l_addr;
ElfW(Addr) len = phdr->p_memsz;
if (__mprotect ((void *) start, len, prot) < 0)
{
if (program)
_dl_fatal_printf ("%s: mprotect failed to turn on BTI\n",
map->l_name);
else
_dl_signal_error (errno, map->l_name, "dlopen",
N_("mprotect failed to turn on BTI"));
}
}
return 0;
}
/* Enable BTI for L if required. */
void
_dl_bti_check (struct link_map *l, const char *program)
{
if (GLRO(dl_aarch64_cpu_features).bti && l->l_mach.bti)
enable_bti (l, program);
}
+4 -1
View File
@@ -125,6 +125,8 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
.globl _dl_start_user \n\
.type _dl_start_user, %function \n\
_start: \n\
// bti c \n\
hint 34 \n\
mov " PTR "0, " PTR_SP " \n\
bl _dl_start \n\
// returns user entry point in x0 \n\
@@ -178,7 +180,8 @@ _dl_start_user: \n\
adrp x0, _dl_fini \n\
add " PTR "0, " PTR "0, #:lo12:_dl_fini \n\
// jump to the user_s entry point \n\
br x21 \n\
mov x16, x21 \n\
br x16 \n\
");
#define elf_machine_type_class(type) \
+145
View File
@@ -0,0 +1,145 @@
/* Support for GNU properties. AArch64 version.
Copyright (C) 2018-2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifndef _DL_PROP_H
#define _DL_PROP_H
#include <not-cancel.h>
extern void _dl_bti_check (struct link_map *, const char *)
attribute_hidden;
static inline void __attribute__ ((always_inline))
_rtld_main_check (struct link_map *m, const char *program)
{
_dl_bti_check (m, program);
}
static inline void __attribute__ ((always_inline))
_dl_open_check (struct link_map *m)
{
_dl_bti_check (m, NULL);
}
static inline void __attribute__ ((unused))
_dl_process_aarch64_property (struct link_map *l,
const ElfW(Nhdr) *note,
const ElfW(Addr) size,
const ElfW(Addr) align)
{
/* The NT_GNU_PROPERTY_TYPE_0 note must be aligned to 4 bytes in
32-bit objects and to 8 bytes in 64-bit objects. Skip notes
with incorrect alignment. */
if (align != (__ELF_NATIVE_CLASS / 8))
return;
const ElfW(Addr) start = (ElfW(Addr)) note;
unsigned int feature_1 = 0;
unsigned int last_type = 0;
while ((ElfW(Addr)) (note + 1) - start < size)
{
/* Find the NT_GNU_PROPERTY_TYPE_0 note. */
if (note->n_namesz == 4
&& note->n_type == NT_GNU_PROPERTY_TYPE_0
&& memcmp (note + 1, "GNU", 4) == 0)
{
/* Check for invalid property. */
if (note->n_descsz < 8
|| (note->n_descsz % sizeof (ElfW(Addr))) != 0)
return;
/* Start and end of property array. */
unsigned char *ptr = (unsigned char *) (note + 1) + 4;
unsigned char *ptr_end = ptr + note->n_descsz;
do
{
unsigned int type = *(unsigned int *) ptr;
unsigned int datasz = *(unsigned int *) (ptr + 4);
/* Property type must be in ascending order. */
if (type < last_type)
return;
ptr += 8;
if ((ptr + datasz) > ptr_end)
return;
last_type = type;
if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
{
/* The size of GNU_PROPERTY_AARCH64_FEATURE_1_AND is 4
bytes. When seeing GNU_PROPERTY_AARCH64_FEATURE_1_AND,
we stop the search regardless if its size is correct
or not. There is no point to continue if this note
is ill-formed. */
if (datasz != 4)
return;
feature_1 = *(unsigned int *) ptr;
if ((feature_1 & GNU_PROPERTY_AARCH64_FEATURE_1_BTI))
l->l_mach.bti = true;
/* Stop if we found the property note. */
return;
}
else if (type > GNU_PROPERTY_AARCH64_FEATURE_1_AND)
{
/* Stop since property type is in ascending order. */
return;
}
/* Check the next property item. */
ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr)));
}
while ((ptr_end - ptr) >= 8);
}
note = ((const void *) note
+ ELF_NOTE_NEXT_OFFSET (note->n_namesz, note->n_descsz,
align));
}
}
#ifdef FILEBUF_SIZE
static inline int __attribute__ ((always_inline))
_dl_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph,
int fd, struct filebuf *fbp)
{
return 0;
}
#endif
static inline int __attribute__ ((always_inline))
_rtld_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph)
{
return 0;
}
static inline int
_dl_process_pt_gnu_property (struct link_map *l, const ElfW(Phdr) *ph)
{
const ElfW(Nhdr) *note = (const void *) (ph->p_vaddr + l->l_addr);
_dl_process_aarch64_property (l, note, ph->p_memsz, ph->p_align);
return 0;
}
#endif /* _DL_PROP_H */
+11
View File
@@ -74,6 +74,7 @@
cfi_startproc
.align 2
_dl_tlsdesc_return:
BTI_C
DELOUSE (0)
ldr PTR_REG (0), [x0, #PTR_SIZE]
RET
@@ -95,6 +96,7 @@ _dl_tlsdesc_return:
cfi_startproc
.align 2
_dl_tlsdesc_undefweak:
BTI_C
str x1, [sp, #-16]!
cfi_adjust_cfa_offset (16)
DELOUSE (0)
@@ -142,6 +144,7 @@ _dl_tlsdesc_undefweak:
cfi_startproc
.align 2
_dl_tlsdesc_dynamic:
BTI_C
DELOUSE (0)
/* Save just enough registers to support fast path, if we fall
@@ -180,6 +183,10 @@ _dl_tlsdesc_dynamic:
callee will trash. */
/* Save the remaining registers that we must treat as caller save. */
# if HAVE_AARCH64_PAC_RET
PACIASP
cfi_window_save
# endif
# define NSAVEXREGPAIRS 8
stp x29, x30, [sp,#-16*NSAVEXREGPAIRS]!
cfi_adjust_cfa_offset (16*NSAVEXREGPAIRS)
@@ -230,6 +237,10 @@ _dl_tlsdesc_dynamic:
cfi_adjust_cfa_offset (-16*NSAVEXREGPAIRS)
cfi_restore (x29)
cfi_restore (x30)
# if HAVE_AARCH64_PAC_RET
AUTIASP
cfi_window_save
# endif
b 1b
cfi_endproc
.size _dl_tlsdesc_dynamic, .-_dl_tlsdesc_dynamic
+20
View File
@@ -35,6 +35,7 @@
cfi_startproc
.align 2
_dl_runtime_resolve:
BTI_C
/* AArch64 we get called with:
ip0 &PLTGOT[2]
ip1 temp(dl resolver entry point)
@@ -126,6 +127,12 @@ _dl_runtime_resolve:
cfi_startproc
.align 2
_dl_runtime_profile:
# if HAVE_AARCH64_PAC_RET
PACIASP
cfi_window_save
# else
BTI_C
# endif
/* AArch64 we get called with:
ip0 &PLTGOT[2]
ip1 temp(dl resolver entry point)
@@ -237,8 +244,17 @@ _dl_runtime_profile:
cfi_restore(x29)
cfi_restore(x30)
# if HAVE_AARCH64_PAC_RET
add sp, sp, SF_SIZE
cfi_adjust_cfa_offset (-SF_SIZE)
AUTIASP
cfi_window_save
add sp, sp, 16
cfi_adjust_cfa_offset (-16)
# else
add sp, sp, SF_SIZE + 16
cfi_adjust_cfa_offset (- SF_SIZE - 16)
# endif
/* Jump to the newly found address. */
br ip0
@@ -285,6 +301,10 @@ _dl_runtime_profile:
/* LR from within La_aarch64_reg */
ldr lr, [x29, #OFFSET_RG + DL_OFFSET_RG_LR]
cfi_restore(lr)
# if HAVE_AARCH64_PAC_RET
/* Note: LR restored from La_aarch64_reg has no PAC. */
cfi_window_save
# endif
mov sp, x29
cfi_def_cfa_register (sp)
ldr x29, [x29, #0]
+3
View File
@@ -16,8 +16,11 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdbool.h>
struct link_map_machine
{
ElfW(Addr) plt; /* Address of .plt */
void *tlsdesc_table; /* Address of TLS descriptor hash table. */
bool bti; /* Branch Target Identification is enabled. */
};
+2 -1
View File
@@ -27,8 +27,9 @@ static void mcount_internal (u_long frompc, u_long selfpc);
#define _MCOUNT_DECL(frompc, selfpc) \
static inline void mcount_internal (u_long frompc, u_long selfpc)
/* Note: strip_pac is needed for frompc because of gcc PR target/94791. */
#define MCOUNT \
void __mcount (void *frompc) \
{ \
mcount_internal ((u_long) frompc, (u_long) RETURN_ADDRESS (0)); \
mcount_internal ((u_long) strip_pac (frompc), (u_long) RETURN_ADDRESS (0)); \
}
+1
View File
@@ -46,6 +46,7 @@
.globl _start
.type _start,#function
_start:
BTI_C
/* Create an initial frame with 0 LR and FP */
mov x29, #0
mov x30, #0
+57 -1
View File
@@ -35,18 +35,72 @@
#define PTR_SIZE (1<<PTR_LOG_SIZE)
#ifndef __ASSEMBLER__
/* Strip pointer authentication code from pointer p. */
static inline void *
strip_pac (void *p)
{
register void *ra asm ("x30") = (p);
asm ("hint 7 // xpaclri" : "+r"(ra));
return ra;
}
/* This is needed when glibc is built with -mbranch-protection=pac-ret
with a gcc that is affected by PR target/95891. */
# if HAVE_AARCH64_PAC_RET
# undef RETURN_ADDRESS
# define RETURN_ADDRESS(n) strip_pac (__builtin_return_address (n))
# endif
#endif
#ifdef __ASSEMBLER__
/* Syntactic details of assembler. */
#define ASM_SIZE_DIRECTIVE(name) .size name,.-name
/* Branch Target Identitication support. */
#define BTI_C hint 34
#define BTI_J hint 36
/* Return address signing support (pac-ret). */
#define PACIASP hint 25
#define AUTIASP hint 29
/* GNU_PROPERTY_AARCH64_* macros from elf.h for use in asm code. */
#define FEATURE_1_AND 0xc0000000
#define FEATURE_1_BTI 1
#define FEATURE_1_PAC 2
/* Add a NT_GNU_PROPERTY_TYPE_0 note. */
#define GNU_PROPERTY(type, value) \
.section .note.gnu.property, "a"; \
.p2align 3; \
.word 4; \
.word 16; \
.word 5; \
.asciz "GNU"; \
.word type; \
.word 4; \
.word value; \
.word 0; \
.text
/* Add GNU property note with the supported features to all asm code
where sysdep.h is included. */
#if HAVE_AARCH64_BTI && HAVE_AARCH64_PAC_RET
GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI|FEATURE_1_PAC)
#elif HAVE_AARCH64_BTI
GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI)
#endif
/* Define an entry point visible from C. */
#define ENTRY(name) \
.globl C_SYMBOL_NAME(name); \
.type C_SYMBOL_NAME(name),%function; \
.align 4; \
C_LABEL(name) \
BTI_C; \
cfi_startproc; \
CALL_MCOUNT
@@ -56,6 +110,7 @@
.type C_SYMBOL_NAME(name),%function; \
.p2align align; \
C_LABEL(name) \
BTI_C; \
cfi_startproc; \
CALL_MCOUNT
@@ -68,10 +123,11 @@
.globl C_SYMBOL_NAME(name); \
.type C_SYMBOL_NAME(name),%function; \
.p2align align; \
.rep padding; \
.rep padding - 1; /* -1 for bti c. */ \
nop; \
.endr; \
C_LABEL(name) \
BTI_C; \
cfi_startproc; \
CALL_MCOUNT
-8
View File
@@ -1,8 +0,0 @@
/* Tag_ABI_align8_preserved: This code preserves 8-byte
alignment in any callee. */
.eabi_attribute 25, 1
/* Tag_ABI_align8_needed: This code may require 8-byte alignment from
the caller. */
.eabi_attribute 24, 1
#include <csu/abi-note.S>
+11 -5
View File
@@ -20,11 +20,11 @@
#define _DL_PROP_H
/* The following functions are used by the dynamic loader and the
dlopen machinery to process PT_NOTE entries in the binary or
shared object. The notes can be used to change the behaviour of
the loader, and as such offer a flexible mechanism for hooking in
various checks related to ABI tags or implementing "flag day" ABI
transitions. */
dlopen machinery to process PT_NOTE and PT_GNU_PROPERTY entries in
the binary or shared object. The notes can be used to change the
behaviour of the loader, and as such offer a flexible mechanism
for hooking in various checks related to ABI tags or implementing
"flag day" ABI transitions. */
static inline void __attribute__ ((always_inline))
_rtld_main_check (struct link_map *m, const char *program)
@@ -51,4 +51,10 @@ _rtld_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph)
return 0;
}
static inline int __attribute__ ((always_inline))
_dl_process_pt_gnu_property (struct link_map *l, const ElfW(Phdr) *ph)
{
return 0;
}
#endif /* _DL_PROP_H */
@@ -72,3 +72,4 @@
#define HWCAP2_BF16 (1 << 14)
#define HWCAP2_DGH (1 << 15)
#define HWCAP2_RNG (1 << 16)
#define HWCAP2_BTI (1 << 17)
@@ -0,0 +1,31 @@
/* Definitions for POSIX memory map interface. Linux/AArch64 version.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#ifndef _SYS_MMAN_H
# error "Never use <bits/mman.h> directly; include <sys/mman.h> instead."
#endif
/* AArch64 specific definitions, should be in sync with
arch/arm64/include/uapi/asm/mman.h. */
#define PROT_BTI 0x10
#include <bits/mman-map-flags-generic.h>
/* Include generic Linux declarations. */
#include <bits/mman-linux.h>
@@ -83,4 +83,7 @@ init_cpu_features (struct cpu_features *cpu_features)
if ((dczid & DCZID_DZP_MASK) == 0)
cpu_features->zva_size = 4 << (dczid & DCZID_BS_MASK);
/* Check if BTI is supported. */
cpu_features->bti = GLRO (dl_hwcap2) & HWCAP2_BTI;
}
@@ -20,6 +20,7 @@
#define _CPU_FEATURES_AARCH64_H
#include <stdint.h>
#include <stdbool.h>
#define MIDR_PARTNUM_SHIFT 4
#define MIDR_PARTNUM_MASK (0xfff << MIDR_PARTNUM_SHIFT)
@@ -64,6 +65,7 @@ struct cpu_features
{
uint64_t midr_el1;
unsigned zva_size;
bool bti;
};
#endif /* _CPU_FEATURES_AARCH64_H */
+12 -2
View File
@@ -28,8 +28,12 @@
.text
ENTRY(__swapcontext)
DELOUSE (0)
/* Set the value returned when swapcontext() returns in this context. */
str xzr, [x0, oX0 + 0 * SZREG]
/* Set the value returned when swapcontext() returns in this context.
And set up x1 to become the return address of the caller, so we
can return there with a normal RET instead of an indirect jump. */
stp xzr, x30, [x0, oX0 + 0 * SZREG]
/* Arrange the oucp context to return to 2f. */
adr x30, 2f
stp x18, x19, [x0, oX0 + 18 * SZREG]
stp x20, x21, [x0, oX0 + 20 * SZREG]
@@ -97,5 +101,11 @@ ENTRY(__swapcontext)
1:
b C_SYMBOL_NAME(__syscall_error)
2:
/* The oucp context is restored here via an indirect branch,
x1 must be restored too which has the real return address. */
BTI_J
mov x30, x1
RET
PSEUDO_END (__swapcontext)
weak_alias (__swapcontext, swapcontext)
+6
View File
@@ -191,4 +191,10 @@ _rtld_process_pt_note (struct link_map *l, const ElfW(Phdr) *ph)
return 0;
}
static inline int __attribute__ ((always_inline))
_dl_process_pt_gnu_property (struct link_map *l, const ElfW(Phdr) *ph)
{
return 0;
}
#endif /* _DL_PROP_H */