|
Packit |
6c4009 |
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Andreas Jaeger <aj@suse.de>, 2002.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if SHSTK_ENABLED
|
|
Packit |
6c4009 |
# include <asm/prctl.h>
|
|
Packit |
6c4009 |
# include "ucontext_i.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Use CALL to push __start_context onto the new stack as well as the new
|
|
Packit |
6c4009 |
shadow stack. RDI points to ucontext:
|
|
Packit |
6c4009 |
Incoming:
|
|
Packit |
6c4009 |
__ssp[0]: The original caller's shadow stack pointer.
|
|
Packit |
6c4009 |
__ssp[1]: The size of the new shadow stack.
|
|
Packit |
6c4009 |
__ssp[2]: The size of the new shadow stack.
|
|
Packit |
6c4009 |
Outgoing:
|
|
Packit |
6c4009 |
__ssp[0]: The new shadow stack pointer.
|
|
Packit |
6c4009 |
__ssp[1]: The base address of the new shadow stack.
|
|
Packit |
6c4009 |
__ssp[2]: The size of the new shadow stack.
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY(__push___start_context)
|
|
Packit |
6c4009 |
/* Save the pointer to ucontext. */
|
|
Packit |
6c4009 |
movq %rdi, %r9
|
|
Packit |
6c4009 |
/* Get the original shadow stack pointer. */
|
|
Packit |
6c4009 |
rdsspq %r8
|
|
Packit |
6c4009 |
/* Save the original stack pointer. */
|
|
Packit |
6c4009 |
movq %rsp, %rdx
|
|
Packit |
6c4009 |
/* Load the top of the new stack into RSI. */
|
|
Packit |
6c4009 |
movq oRSP(%rdi), %rsi
|
|
Packit |
6c4009 |
/* Add 8 bytes to RSI since CALL will push the 8-byte return
|
|
Packit |
6c4009 |
address onto stack. */
|
|
Packit |
6c4009 |
leaq 8(%rsi), %rsp
|
|
Packit |
6c4009 |
/* Allocate the new shadow stack. The size of the new shadow
|
|
Packit |
6c4009 |
stack is passed in __ssp[1]. */
|
|
Packit |
6c4009 |
lea (oSSP + 8)(%rdi), %RSI_LP
|
|
Packit |
6c4009 |
movl $ARCH_CET_ALLOC_SHSTK, %edi
|
|
Packit |
6c4009 |
movl $__NR_arch_prctl, %eax
|
|
Packit |
6c4009 |
/* The new shadow stack base is returned in __ssp[1]. */
|
|
Packit |
6c4009 |
syscall
|
|
Packit |
6c4009 |
testq %rax, %rax
|
|
Packit |
6c4009 |
jne L(hlt) /* This should never happen. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the size of the new shadow stack. */
|
|
Packit |
6c4009 |
movq 8(%rsi), %rdi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the base address of the new shadow stack. */
|
|
Packit |
6c4009 |
movq (%rsi), %rsi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Use the restore stoken to restore the new shadow stack. */
|
|
Packit |
6c4009 |
rstorssp -8(%rsi, %rdi)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Save the restore token on the original shadow stack. */
|
|
Packit |
6c4009 |
saveprevssp
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Push the address of "jmp __start_context" onto the new stack
|
|
Packit |
6c4009 |
as well as the new shadow stack. */
|
|
Packit |
6c4009 |
call 1f
|
|
Packit |
6c4009 |
jmp __start_context
|
|
Packit |
6c4009 |
1:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the new shadow stack pointer. */
|
|
Packit |
6c4009 |
rdsspq %rdi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Use the restore stoken to restore the original shadow stack. */
|
|
Packit |
6c4009 |
rstorssp -8(%r8)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Save the restore token on the new shadow stack. */
|
|
Packit |
6c4009 |
saveprevssp
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Store the new shadow stack pointer in __ssp[0]. */
|
|
Packit |
6c4009 |
movq %rdi, oSSP(%r9)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Restore the original stack. */
|
|
Packit |
6c4009 |
mov %rdx, %rsp
|
|
Packit |
6c4009 |
ret
|
|
Packit |
6c4009 |
END(__push___start_context)
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This is the helper code which gets called if a function which is
|
|
Packit |
6c4009 |
registered with 'makecontext' returns. In this case we have to
|
|
Packit |
6c4009 |
install the context listed in the uc_link element of the context
|
|
Packit |
6c4009 |
'makecontext' manipulated at the time of the 'makecontext' call.
|
|
Packit |
6c4009 |
If the pointer is NULL the process must terminate. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTRY(__start_context)
|
|
Packit |
6c4009 |
/* This removes the parameters passed to the function given to
|
|
Packit |
6c4009 |
'makecontext' from the stack. RBX contains the address
|
|
Packit |
6c4009 |
on the stack pointer for the next context. */
|
|
Packit |
6c4009 |
movq %rbx, %rsp
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Don't use pop here so that stack is aligned to 16 bytes. */
|
|
Packit |
6c4009 |
movq (%rsp), %rdi /* This is the next context. */
|
|
Packit |
6c4009 |
testq %rdi, %rdi
|
|
Packit |
6c4009 |
je 2f /* If it is zero exit. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
call __setcontext
|
|
Packit |
6c4009 |
/* If this returns (which can happen if the syscall fails) we'll
|
|
Packit |
6c4009 |
exit the program with the return error value (-1). */
|
|
Packit |
6c4009 |
movq %rax,%rdi
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
2:
|
|
Packit |
6c4009 |
call HIDDEN_JUMPTARGET(exit)
|
|
Packit |
6c4009 |
/* The 'exit' call should never return. In case it does cause
|
|
Packit |
6c4009 |
the process to terminate. */
|
|
Packit |
6c4009 |
L(hlt):
|
|
Packit |
6c4009 |
hlt
|
|
Packit |
6c4009 |
END(__start_context)
|