glibc/sysdeps/unix/sysv/linux/x86_64/allocate-shadow-stack.c
H.J. Lu edb5e0c8f9 x86/cet: Sync with Linux kernel 6.6 shadow stack interface
Sync with Linux kernel 6.6 shadow stack interface.  Since only x86-64 is
supported, i386 shadow stack codes are unchanged and CET shouldn't be
enabled for i386.

1. When the shadow stack base in TCB is unset, the default shadow stack
is in use.  Use the current shadow stack pointer as the marker for the
default shadow stack. It is used to identify if the current shadow stack
is the same as the target shadow stack when switching ucontexts.  If yes,
INCSSP will be used to unwind shadow stack.  Otherwise, shadow stack
restore token will be used.
2. Allocate shadow stack with the map_shadow_stack syscall.  Since there
is no function to explicitly release ucontext, there is no place to
release shadow stack allocated by map_shadow_stack in ucontext functions.
Such shadow stacks will be leaked.
3. Rename arch_prctl CET commands to ARCH_SHSTK_XXX.
4. Rewrite the CET control functions with the current kernel shadow stack
interface.

Since CET is no longer enabled by kernel, a separate patch will enable
shadow stack during startup.
2024-01-01 05:22:48 -08:00

55 lines
1.9 KiB
C

/* Helper function to allocate shadow stack.
Copyright (C) 2023 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/>. */
#include <sysdep.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include <libc-pointer-arith.h>
#include <allocate-shadow-stack.h>
/* NB: This can be treated as a syscall by caller. */
long int
__allocate_shadow_stack (size_t stack_size,
shadow_stack_size_t *child_stack)
{
#ifdef __NR_map_shadow_stack
size_t shadow_stack_size
= stack_size >> STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT;
/* Align shadow stack to 8 bytes. */
shadow_stack_size = ALIGN_UP (shadow_stack_size, 8);
/* Since sigaltstack shares shadow stack with the current context in
the thread, add extra 20 stack frames in shadow stack for signal
handlers. */
shadow_stack_size += 20 * 8;
void *shadow_stack = (void *)INLINE_SYSCALL_CALL
(map_shadow_stack, NULL, shadow_stack_size, SHADOW_STACK_SET_TOKEN);
/* Report the map_shadow_stack error. */
if (shadow_stack == MAP_FAILED)
return -errno;
/* Save the shadow stack base and size on child stack. */
child_stack[0] = (uintptr_t) shadow_stack;
child_stack[1] = shadow_stack_size;
return 0;
#else
return -ENOSYS;
#endif
}