glibc/sysdeps
H.J. Lu 25123a1c5c x86-64/CET: Extend ucontext_t to save shadow stack
This patch adds a field to ucontext_t to save shadow stack:

1. getcontext and swapcontext are updated to save the caller's shadow
stack pointer and return addresses.
2. setcontext and swapcontext are updated to restore shadow stack and
jump to new context directly.
3. makecontext is updated to allocate a new shadow stack and set the
caller's return address to __start_context.

Since makecontext allocates a new shadow stack when making a new
context and kernel allocates a new shadow stack for clone/fork/vfork
syscalls, we track the current shadow stack base.  In setcontext and
swapcontext, if the target shadow stack base is the same as the current
shadow stack base, we unwind the shadow stack.  Otherwise it is a stack
switch and we look for a restore token.

We enable shadow stack at run-time only if program and all used shared
objects, including dlopened ones, are shadow stack enabled, which means
that they must be compiled with GCC 8 or above and glibc 2.28 or above.
We need to save and restore shadow stack only if shadow stack is enabled.
When caller of getcontext, setcontext, swapcontext and makecontext is
compiled with smaller ucontext_t, shadow stack won't be enabled at
run-time.  We check if shadow stack is enabled before accessing the
extended field in ucontext_t.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

	* sysdeps/unix/sysv/linux/x86/sys/ucontext.h (ucontext_t): Add
	__ssp.
	* sysdeps/unix/sysv/linux/x86_64/__start_context.S: Include
	<asm/prctl.h> and "ucontext_i.h" when shadow stack is enabled.
	(__push___start_context): New.
	* sysdeps/unix/sysv/linux/x86_64/getcontext.S: Include
	<asm/prctl.h>.
	(__getcontext): Record the current shadow stack base.  Save the
	caller's shadow stack pointer and base.
	* sysdeps/unix/sysv/linux/x86_64/makecontext.c: Include
	<pthread.h>, <libc-pointer-arith.h> and <sys/prctl.h>.
	(__push___start_context): New prototype.
	(__makecontext): Call __push___start_context to allocate a new
	shadow stack, push __start_context onto the new stack as well
	as the new shadow stack.
	* sysdeps/unix/sysv/linux/x86_64/setcontext.S: Include
	<asm/prctl.h>.
	(__setcontext): Restore the target shadow stack.
	* sysdeps/unix/sysv/linux/x86_64/swapcontext.S: Include
	<asm/prctl.h>.
	(__swapcontext): Record the current shadow stack base.  Save
	the caller's shadow stack pointer and base.  Restore the target
	shadow stack.
	* sysdeps/unix/sysv/linux/x86_64/sysdep.h
	(STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT): New.
	* sysdeps/unix/sysv/linux/x86_64/ucontext_i.sym (oSSP): New.
2018-07-25 06:37:31 -07:00
..
aarch64
alpha
arm
generic x86: Support IBT and SHSTK in Intel CET [BZ #21598] 2018-07-16 14:08:27 -07:00
gnu
hppa
htl
hurd hurd: Fix installed-headers tests 2018-07-20 03:28:08 +02:00
i386 x86: Rename __glibc_reserved2 to ssp_base in tcbhead_t 2018-07-25 04:39:39 -07:00
ia64
ieee754
init_array
m68k
mach check-execstack: Permit sysdeps to xfail some libs 2018-07-20 03:28:14 +02:00
microblaze
mips
nios2
nptl
posix
powerpc powerpc64: Always restore TOC on longjmp [BZ #21895] 2018-07-16 16:08:41 -03:00
pthread
riscv
s390
sh
sparc
unix x86-64/CET: Extend ucontext_t to save shadow stack 2018-07-25 06:37:31 -07:00
wordsize-32
wordsize-64
x86 x86/CET: Add tests with legacy non-CET shared objects 2018-07-25 04:47:05 -07:00
x86_64 x86: Rename __glibc_reserved2 to ssp_base in tcbhead_t 2018-07-25 04:39:39 -07:00