* V2 [PATCH 0/5] i386: Finish CET support
@ 2020-01-08 16:15 H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] H.J. Lu
` (4 more replies)
0 siblings, 5 replies; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
This patch set finishes CET support on i386:
1. getcontext, setcontext and swapcontext are updated not to preserve
EAX, ECX and EDX. Since they are caller-saved, caller will reload them
after getcontext, setcontext and swapcontext calls if needed. The extra
scratch registers are used to enable CET.
2. Add missing _CET_ENDBR to i386 assembly files.
3. Enable CET support in i386 ucontext functions.
Tested on i386 CET/non-CET machines.
H.J. Lu (5):
i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ#
25262]
i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target
i386: Add _CET_ENDBR to assembly files without ENTRY
i386: Remove _exit.S
i386: Enable CET support in ucontext functions
sysdeps/i386/i386-mcount.S | 2 +
sysdeps/i386/nptl/pthread_spin_lock.S | 2 +
sysdeps/i386/nptl/pthread_spin_unlock.S | 3 +
sysdeps/i386/pthread_spin_trylock.S | 2 +
sysdeps/i386/sub_n.S | 1 +
sysdeps/unix/sysv/linux/i386/_exit.S | 44 ------
sysdeps/unix/sysv/linux/i386/getcontext.S | 64 +++++++-
sysdeps/unix/sysv/linux/i386/makecontext.S | 123 +++++++++++++++
sysdeps/unix/sysv/linux/i386/setcontext.S | 112 ++++++++++++--
sysdeps/unix/sysv/linux/i386/swapcontext.S | 156 ++++++++++++++++++--
sysdeps/unix/sysv/linux/i386/sysdep.h | 5 +
sysdeps/unix/sysv/linux/i386/ucontext_i.sym | 1 +
12 files changed, 442 insertions(+), 73 deletions(-)
delete mode 100644 sysdeps/unix/sysv/linux/i386/_exit.S
--
2.24.1
^ permalink raw reply [flat|nested] 13+ messages in thread
* V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262]
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
@ 2020-01-08 16:15 ` H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target H.J. Lu
` (3 subsequent siblings)
4 siblings, 1 reply; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
On i386, since EAX, ECX and EDX are caller-saved, there are no need
to save and restore EAX, ECX and EDX in getcontext, setcontext and
swapcontext. They just need to clear EAX on success. The extra
scratch registers are needed to enable CET.
Tested on i386.
---
sysdeps/unix/sysv/linux/i386/getcontext.S | 8 +-------
sysdeps/unix/sysv/linux/i386/setcontext.S | 11 ++++-------
sysdeps/unix/sysv/linux/i386/swapcontext.S | 17 +++++------------
3 files changed, 10 insertions(+), 26 deletions(-)
diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
index f86df4d555..9c1df9a2aa 100644
--- a/sysdeps/unix/sysv/linux/i386/getcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
@@ -26,13 +26,7 @@ ENTRY(__getcontext)
/* Load address of the context data structure. */
movl 4(%esp), %eax
- /* Return value of getcontext. EAX is the only register whose
- value is not preserved. */
- movl $0, oEAX(%eax)
-
- /* Save the 32-bit register values and the return address. */
- movl %ecx, oECX(%eax)
- movl %edx, oEDX(%eax)
+ /* Save the preserved register values and the return address. */
movl %edi, oEDI(%eax)
movl %esi, oESI(%eax)
movl %ebp, oEBP(%eax)
diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
index b4b5c0298c..f042d80bf4 100644
--- a/sysdeps/unix/sysv/linux/i386/setcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
@@ -65,22 +65,19 @@ ENTRY(__setcontext)
cfi_offset (esi, oESI)
cfi_offset (ebp, oEBP)
cfi_offset (ebx, oEBX)
- cfi_offset (edx, oEDX)
- cfi_offset (ecx, oECX)
movl oESP(%eax), %esp
/* Push the return address on the new stack so we can return there. */
pushl %ecx
- /* Load the values of all the 32-bit registers (except ESP).
- Since we are loading from EAX, it must be last. */
+ /* Load the values of all the preserved registers (except ESP). */
movl oEDI(%eax), %edi
movl oESI(%eax), %esi
movl oEBP(%eax), %ebp
movl oEBX(%eax), %ebx
- movl oEDX(%eax), %edx
- movl oECX(%eax), %ecx
- movl oEAX(%eax), %eax
+
+ /* All done, return 0 for success. */
+ xorl %eax, %eax
/* End FDE here, we fall into another context. */
cfi_endproc
diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
index 792bfdf7e6..090c2d8c3e 100644
--- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
@@ -26,13 +26,7 @@ ENTRY(__swapcontext)
/* Load address of the context data structure we save in. */
movl 4(%esp), %eax
- /* Return value of swapcontext. EAX is the only register whose
- value is not preserved. */
- movl $0, oEAX(%eax)
-
- /* Save the 32-bit register values and the return address. */
- movl %ecx, oECX(%eax)
- movl %edx, oEDX(%eax)
+ /* Save the preserved register values and the return address. */
movl %edi, oEDI(%eax)
movl %esi, oESI(%eax)
movl %ebp, oEBP(%eax)
@@ -91,15 +85,14 @@ ENTRY(__swapcontext)
/* Push the return address on the new stack so we can return there. */
pushl %ecx
- /* Load the values of all the 32-bit registers (except ESP).
- Since we are loading from EAX, it must be last. */
+ /* Load the values of all the preserved registers (except ESP). */
movl oEDI(%eax), %edi
movl oESI(%eax), %esi
movl oEBP(%eax), %ebp
movl oEBX(%eax), %ebx
- movl oEDX(%eax), %edx
- movl oECX(%eax), %ecx
- movl oEAX(%eax), %eax
+
+ /* All done, return 0 for success. */
+ xorl %eax, %eax
/* The following 'ret' will pop the address of the code and jump
to it. */
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] H.J. Lu
@ 2020-01-08 16:15 ` H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY H.J. Lu
` (2 subsequent siblings)
4 siblings, 1 reply; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
Add a missing _CET_ENDBR to indirect jump targe in sysdeps/i386/sub_n.S.
---
sysdeps/i386/sub_n.S | 1 +
1 file changed, 1 insertion(+)
diff --git a/sysdeps/i386/sub_n.S b/sysdeps/i386/sub_n.S
index caac89177b..16ebd9169b 100644
--- a/sysdeps/i386/sub_n.S
+++ b/sysdeps/i386/sub_n.S
@@ -91,6 +91,7 @@ L(oop): movl (%esi),%eax
movl 8(%esi),%eax
sbbl 8(%edx),%eax
movl %eax,8(%edi)
+ _CET_ENDBR
movl 12(%esi),%eax
sbbl 12(%edx),%eax
movl %eax,12(%edi)
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target H.J. Lu
@ 2020-01-08 16:15 ` H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 4/5] i386: Remove _exit.S H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 5/5] i386: Enable CET support in ucontext functions H.J. Lu
4 siblings, 1 reply; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
Add _CET_ENDBR to i386 assembly files which don't use ENTRY to add
ENDBR32 at function entries when CET is enabled.
---
sysdeps/i386/i386-mcount.S | 2 ++
sysdeps/i386/nptl/pthread_spin_lock.S | 2 ++
sysdeps/i386/nptl/pthread_spin_unlock.S | 3 +++
sysdeps/i386/pthread_spin_trylock.S | 2 ++
4 files changed, 9 insertions(+)
diff --git a/sysdeps/i386/i386-mcount.S b/sysdeps/i386/i386-mcount.S
index 9516265bcb..8b60bd20fe 100644
--- a/sysdeps/i386/i386-mcount.S
+++ b/sysdeps/i386/i386-mcount.S
@@ -30,6 +30,7 @@
.type C_SYMBOL_NAME(_mcount), @function
.align ALIGNARG(4)
C_LABEL(_mcount)
+ _CET_ENDBR
/* Save the caller-clobbered registers. */
pushl %eax
pushl %ecx
@@ -58,6 +59,7 @@ weak_alias (_mcount, mcount)
.type C_SYMBOL_NAME(__fentry__), @function
.align ALIGNARG(4)
C_LABEL(__fentry__)
+ _CET_ENDBR
/* Save the caller-clobbered registers. */
pushl %eax
pushl %ecx
diff --git a/sysdeps/i386/nptl/pthread_spin_lock.S b/sysdeps/i386/nptl/pthread_spin_lock.S
index 5736c82078..7eb8f0e069 100644
--- a/sysdeps/i386/nptl/pthread_spin_lock.S
+++ b/sysdeps/i386/nptl/pthread_spin_lock.S
@@ -15,12 +15,14 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <sysdep.h>
#include <lowlevellock.h>
.globl pthread_spin_lock
.type pthread_spin_lock,@function
.align 16
pthread_spin_lock:
+ _CET_ENDBR
mov 4(%esp), %eax
1: LOCK
decl 0(%eax)
diff --git a/sysdeps/i386/nptl/pthread_spin_unlock.S b/sysdeps/i386/nptl/pthread_spin_unlock.S
index e7757d0a03..dac730af1f 100644
--- a/sysdeps/i386/nptl/pthread_spin_unlock.S
+++ b/sysdeps/i386/nptl/pthread_spin_unlock.S
@@ -16,10 +16,13 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <sysdep.h>
+
.globl pthread_spin_unlock
.type pthread_spin_unlock,@function
.align 16
pthread_spin_unlock:
+ _CET_ENDBR
movl 4(%esp), %eax
movl $1, (%eax)
xorl %eax, %eax
diff --git a/sysdeps/i386/pthread_spin_trylock.S b/sysdeps/i386/pthread_spin_trylock.S
index dd08d38f8d..b3965a150f 100644
--- a/sysdeps/i386/pthread_spin_trylock.S
+++ b/sysdeps/i386/pthread_spin_trylock.S
@@ -16,6 +16,7 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <sysdep.h>
#include <pthread-errnos.h>
@@ -29,6 +30,7 @@
.type pthread_spin_trylock,@function
.align 16
pthread_spin_trylock:
+ _CET_ENDBR
movl 4(%esp), %edx
movl $1, %eax
xorl %ecx, %ecx
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* V2 [PATCH 4/5] i386: Remove _exit.S
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
` (2 preceding siblings ...)
2020-01-08 16:15 ` V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY H.J. Lu
@ 2020-01-08 16:15 ` H.J. Lu
2020-01-09 21:14 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 5/5] i386: Enable CET support in ucontext functions H.J. Lu
4 siblings, 1 reply; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
The generic implementation is suffice since __NR_exit_group is always
support and i386 does define ABORT_INSTRUCTION.
---
sysdeps/unix/sysv/linux/i386/_exit.S | 44 ----------------------------
1 file changed, 44 deletions(-)
delete mode 100644 sysdeps/unix/sysv/linux/i386/_exit.S
diff --git a/sysdeps/unix/sysv/linux/i386/_exit.S b/sysdeps/unix/sysv/linux/i386/_exit.S
deleted file mode 100644
index 1f7bfeb7e7..0000000000
--- a/sysdeps/unix/sysv/linux/i386/_exit.S
+++ /dev/null
@@ -1,44 +0,0 @@
-/* Copyright (C) 2002-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/>. */
-
-#include <sysdep.h>
-
- .text
- .type _exit,@function
- .global _exit
-_exit:
- movl 4(%esp), %ebx
-
- /* Try the new syscall first. */
-#ifdef __NR_exit_group
- movl $__NR_exit_group, %eax
- ENTER_KERNEL
-#endif
-
- /* Not available. Now the old one. */
- movl $__NR_exit, %eax
- /* Don't bother using ENTER_KERNEL here. If the exit_group
- syscall is not available AT_SYSINFO isn't either. */
- int $0x80
-
- /* This must not fail. Be sure we don't return. */
- hlt
- .size _exit,.-_exit
-
-libc_hidden_def (_exit)
-rtld_hidden_def (_exit)
-weak_alias (_exit, _Exit)
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* V2 [PATCH 5/5] i386: Enable CET support in ucontext functions
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
` (3 preceding siblings ...)
2020-01-08 16:15 ` V2 [PATCH 4/5] i386: Remove _exit.S H.J. Lu
@ 2020-01-08 16:15 ` H.J. Lu
2020-02-01 14:09 ` V3 [PATCH] " H.J. Lu
4 siblings, 1 reply; 13+ messages in thread
From: H.J. Lu @ 2020-01-08 16:15 UTC (permalink / raw)
To: libc-alpha
1. getcontext and swapcontext are updated to save the caller's shadow
stack pointer and return address.
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 the helper code, L(exitcode).
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.
Tested on i386 CET/non-CET machines.
---
sysdeps/unix/sysv/linux/i386/getcontext.S | 56 ++++++++
sysdeps/unix/sysv/linux/i386/makecontext.S | 123 +++++++++++++++++
sysdeps/unix/sysv/linux/i386/setcontext.S | 101 +++++++++++++-
sysdeps/unix/sysv/linux/i386/swapcontext.S | 139 ++++++++++++++++++++
sysdeps/unix/sysv/linux/i386/sysdep.h | 5 +
sysdeps/unix/sysv/linux/i386/ucontext_i.sym | 1 +
6 files changed, 422 insertions(+), 3 deletions(-)
diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
index 9c1df9a2aa..0ebe572b94 100644
--- a/sysdeps/unix/sysv/linux/i386/getcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -42,6 +43,61 @@ ENTRY(__getcontext)
movw %fs, %dx
movl %edx, oFS(%eax)
+#if SHSTK_ENABLED
+ /* Check if shadow stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ /* Save EAX in EDX. */
+ movl %eax, %edx
+
+ xorl %eax, %eax
+ cmpl %gs:SSP_BASE_OFFSET, %eax
+ jnz L(shadow_stack_bound_recorded)
+
+ /* Save EBX in the unused EAX slot. */
+ movl %ebx, oEAX(%edx)
+
+ /* Get the base address and size of the default shadow stack
+ which must be the current shadow stack since nothing has
+ been recorded yet. */
+ sub $24, %esp
+ mov %esp, %ecx
+ movl $ARCH_CET_STATUS, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jz L(continue_no_err)
+
+ /* This should never happen. */
+ hlt
+
+L(continue_no_err):
+ /* Restore EBX from the EAX slot. */
+ movl oEAX(%edx), %ebx
+
+ /* Record the base of the current shadow stack. */
+ movl 8(%esp), %eax
+ movl %eax, %gs:SSP_BASE_OFFSET
+ add $24, %esp
+
+L(shadow_stack_bound_recorded):
+ /* Load address of the context data structure. */
+ movl 4(%esp), %eax
+
+ /* Get the current shadow stack pointer. */
+ rdsspd %edx
+ /* NB: Save the caller's shadow stack so that we can jump back
+ to the caller directly. */
+ addl $4, %edx
+ movl %edx, oSSP(%eax)
+
+ /* Save the current shadow stack base in ucontext. */
+ movl %gs:SSP_BASE_OFFSET, %edx
+ movl %edx, (oSSP + 4)(%eax)
+
+L(no_shstk):
+#endif
/* We have separate floating-point register content memory on the
stack. We use the __fpregs_mem block in the context. Set the
links up correctly. */
diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S
index ad9ce5f977..c4d78ee1b3 100644
--- a/sysdeps/unix/sysv/linux/i386/makecontext.S
+++ b/sysdeps/unix/sysv/linux/i386/makecontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -68,6 +69,127 @@ ENTRY(__makecontext)
jnz 1b
2:
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(skip_ssp)
+
+ /* Reload the pointer to ucontext. */
+ movl 4(%esp), %eax
+
+ /* Shadow stack is enabled. We need to allocate a new shadow
+ stack. */
+ subl oSS_SP(%eax), %edx
+ shrl $STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT, %edx
+
+ /* Align shadow stack size to 8 bytes. */
+ addl $7, %edx
+ andl $-8, %edx
+
+ /* Store shadow stack size in __ssp[2]. */
+ movl %edx, (oSSP + 8)(%eax)
+
+ /* Save ESI in the unused ECX slot. */
+ movl %esi, oECX(%eax)
+ /* Save EDI in the unused EDX slot. */
+ movl %edi, oEDX(%eax)
+
+ /* Save the pointer to ucontext. */
+ movl %eax, %edi
+
+ /* Get the original shadow stack pointer. */
+ rdsspd %esi
+
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+ /* Load the top of the new stack into EDX. */
+ movl oESP(%eax), %edx
+
+ /* We need to terminate the FDE here because the unwinder looks
+ at ra-1 for unwind information. */
+ cfi_endproc
+
+ /* Swap the original stack pointer with the top of the new
+ stack. */
+ xchgl %esp, %edx
+
+ /* Add 4 bytes since CALL will push the 4-byte return address
+ onto stack. */
+ addl $4, %esp
+
+ /* Allocate the new shadow stack. Save EBX in the unused EAX
+ slot. */
+ movl %ebx, oEAX(%eax)
+
+ /* CET syscall takes 64-bit sizes. */
+ subl $16, %esp
+ movl (oSSP + 8)(%eax), %ecx
+ movl %ecx, (%esp)
+ movl $0, 4(%esp)
+ movl %ecx, 8(%esp)
+ movl $0, 12(%esp)
+ movl %esp, %ecx
+
+ movl $ARCH_CET_ALLOC_SHSTK, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jne L(hlt) /* This should never happen. */
+
+ /* Copy the base address of the new shadow stack to __ssp[1]. */
+ movl (%esp), %eax
+ movl %eax, (oSSP + 4)(%edi)
+
+ addl $16, %esp
+
+ /* Restore EBX from the EAX slot. */
+ movl oEAX(%edi), %ebx
+
+ /* Get the size of the new shadow stack. */
+ movl (oSSP + 8)(%edi), %ecx
+
+ /* Use the restore stoken to restore the new shadow stack. */
+ rstorssp -8(%eax, %ecx)
+
+ /* Save the restore token at the next 8 byte aligned boundary
+ on the original shadow stack. */
+ saveprevssp
+
+ /* Push the address of "jmp exitcode" onto the new stack as
+ well as the new shadow stack. */
+ call 1f
+ jmp L(exitcode)
+1:
+
+ /* Get the new shadow stack pointer. */
+ rdsspd %eax
+
+ /* Use the restore stoken to restore the original shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the new shadow stack. */
+ saveprevssp
+
+ /* Store the new shadow stack pointer in __ssp[0]. */
+ movl %eax, oSSP(%edi)
+
+ /* Restore the original stack. */
+ mov %edx, %esp
+
+ cfi_startproc
+
+ /* Restore ESI from the ECX slot. */
+ movl oECX(%edi), %esi
+ /* Restore EDI from the EDX slot. */
+ movl oEDX(%edi), %edi
+
+ ret
+
+L(skip_ssp):
+#endif
+
/* If the function we call returns we must continue with the
context which is given in the uc_link element. To do this
set the return address for the function the user provides
@@ -123,6 +245,7 @@ L(call_exit):
call HIDDEN_JUMPTARGET(exit)
/* The 'exit' call should never return. In case it does cause
the process to terminate. */
+L(hlt):
hlt
cfi_startproc
END(__makecontext)
diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
index f042d80bf4..9bc7d68a1a 100644
--- a/sysdeps/unix/sysv/linux/i386/setcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -56,9 +57,6 @@ ENTRY(__setcontext)
movl oFS(%eax), %ecx
movw %cx, %fs
- /* Fetch the address to return to. */
- movl oEIP(%eax), %ecx
-
/* Load the new stack pointer. */
cfi_def_cfa (eax, 0)
cfi_offset (edi, oEDI)
@@ -67,6 +65,103 @@ ENTRY(__setcontext)
cfi_offset (ebx, oEBX)
movl oESP(%eax), %esp
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ /* If the base of the target shadow stack is the same as the
+ base of the current shadow stack, we unwind the shadow
+ stack. Otherwise it is a stack switch and we look for a
+ restore token. */
+ movl oSSP(%eax), %esi
+ movl %esi, %edi
+
+ /* Get the base of the target shadow stack. */
+ movl (oSSP + 4)(%eax), %ecx
+ cmpl %gs:SSP_BASE_OFFSET, %ecx
+ je L(unwind_shadow_stack)
+
+L(find_restore_token_loop):
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+ /* Look for a restore token. */
+ movl -8(%esi), %ebx
+ andl $-8, %ebx
+ cmpl %esi, %ebx
+ je L(restore_shadow_stack)
+
+ /* Try the next slot. */
+ subl $8, %esi
+ jmp L(find_restore_token_loop)
+
+L(restore_shadow_stack):
+ /* Pop return address from the shadow stack since setcontext
+ will not return. */
+ movl $1, %ebx
+ incsspd %ebx
+
+ /* Use the restore stoken to restore the target shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the old shadow stack. NB: This
+ restore token may be checked by setcontext or swapcontext
+ later. */
+ saveprevssp
+
+ /* Record the new shadow stack base that was switched to. */
+ movl (oSSP + 4)(%eax), %ebx
+ movl %ebx, %gs:SSP_BASE_OFFSET
+
+L(unwind_shadow_stack):
+ rdsspd %ebx
+ subl %edi, %ebx
+ je L(skip_unwind_shadow_stack)
+ negl %ebx
+ shrl $2, %ebx
+ movl $255, %esi
+L(loop):
+ cmpl %esi, %ebx
+ cmovb %ebx, %esi
+ incsspd %esi
+ subl %esi, %ebx
+ ja L(loop)
+
+L(skip_unwind_shadow_stack):
+
+ /* Load the values of all the preserved registers (except ESP). */
+ movl oEDI(%eax), %edi
+ movl oESI(%eax), %esi
+ movl oEBP(%eax), %ebp
+ movl oEBX(%eax), %ebx
+
+ /* Get the return address set with getcontext. */
+ movl oEIP(%eax), %ecx
+
+ /* Check if return address is valid for the case when setcontext
+ is invoked from L(exitcode) with linked context. */
+ rdsspd %eax
+ cmpl (%eax), %ecx
+ /* Clear EAX to indicate success. NB: Don't use xorl to keep
+ EFLAGS for jne. */
+ movl $0, %eax
+ jne L(jmp)
+ /* Return to the new context if return address valid. */
+ pushl %ecx
+ ret
+
+L(jmp):
+ /* Jump to the new context directly. */
+ jmp *%ecx
+
+L(no_shstk):
+#endif
+
+ /* Fetch the address to return to. */
+ movl oEIP(%eax), %ecx
+
/* Push the return address on the new stack so we can return there. */
pushl %ecx
diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
index 090c2d8c3e..28d057599e 100644
--- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -76,6 +77,144 @@ ENTRY(__swapcontext)
movl oFS(%eax), %edx
movw %dx, %fs
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ xorl %eax, %eax
+ cmpl %gs:SSP_BASE_OFFSET, %eax
+ jnz L(shadow_stack_bound_recorded)
+
+ /* Get the base address and size of the default shadow stack
+ which must be the current shadow stack since nothing has
+ been recorded yet. */
+ sub $24, %esp
+ mov %esp, %ecx
+ movl $ARCH_CET_STATUS, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jz L(continue_no_err)
+
+ /* This should never happen. */
+ hlt
+
+L(continue_no_err):
+ /* Record the base of the current shadow stack. */
+ movl 8(%esp), %eax
+ movl %eax, %gs:SSP_BASE_OFFSET
+ add $24, %esp
+
+L(shadow_stack_bound_recorded):
+ /* Load address of the context data structure we save in. */
+ movl 4(%esp), %eax
+
+ /* Load address of the context data structure we swap in */
+ movl 8(%esp), %edx
+
+ /* If we unwind the stack, we can't undo stack unwinding. Just
+ save the target shadow stack pointer as the current shadow
+ stack pointer. */
+ movl oSSP(%edx), %ecx
+ movl %ecx, oSSP(%eax)
+
+ /* Save the current shadow stack base in ucontext. */
+ movl %gs:SSP_BASE_OFFSET, %ecx
+ movl %ecx, (oSSP + 4)(%eax)
+
+ /* If the base of the target shadow stack is the same as the
+ base of the current shadow stack, we unwind the shadow
+ stack. Otherwise it is a stack switch and we look for a
+ restore token. */
+ movl oSSP(%edx), %esi
+ movl %esi, %edi
+
+ /* Get the base of the target shadow stack. */
+ movl (oSSP + 4)(%edx), %ecx
+ cmpl %gs:SSP_BASE_OFFSET, %ecx
+ je L(unwind_shadow_stack)
+
+L(find_restore_token_loop):
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+ /* Look for a restore token. */
+ movl -8(%esi), %ebx
+ andl $-8, %ebx
+ cmpl %esi, %ebx
+ je L(restore_shadow_stack)
+
+ /* Try the next slot. */
+ subl $8, %esi
+ jmp L(find_restore_token_loop)
+
+L(restore_shadow_stack):
+ /* The target shadow stack will be restored. Save the current
+ shadow stack pointer. */
+ rdsspd %ecx
+ movl %ecx, oSSP(%eax)
+
+ /* Use the restore stoken to restore the target shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the old shadow stack. NB: This
+ restore token may be checked by setcontext or swapcontext
+ later. */
+ saveprevssp
+
+ /* Record the new shadow stack base that was switched to. */
+ movl (oSSP + 4)(%edx), %ebx
+ movl %ebx, %gs:SSP_BASE_OFFSET
+
+L(unwind_shadow_stack):
+ rdsspd %ebx
+ subl %edi, %ebx
+ je L(skip_unwind_shadow_stack)
+ negl %ebx
+ shrl $2, %ebx
+ movl $255, %esi
+L(loop):
+ cmpl %esi, %ebx
+ cmovb %ebx, %esi
+ incsspd %esi
+ subl %esi, %ebx
+ ja L(loop)
+
+L(skip_unwind_shadow_stack):
+
+ /* Load the new stack pointer. */
+ movl oESP(%edx), %esp
+
+ /* Load the values of all the preserved registers (except ESP). */
+ movl oEDI(%edx), %edi
+ movl oESI(%edx), %esi
+ movl oEBP(%edx), %ebp
+ movl oEBX(%edx), %ebx
+
+ /* Get the return address set with getcontext. */
+ movl oEIP(%edx), %ecx
+
+ /* Check if return address is valid for the case when setcontext
+ is invoked from L(exitcode) with linked context. */
+ rdsspd %eax
+ cmpl (%eax), %ecx
+ /* Clear EAX to indicate success. NB: Don't use xorl to keep
+ EFLAGS for jne. */
+ movl $0, %eax
+ jne L(jmp)
+ /* Return to the new context if return address valid. */
+ pushl %ecx
+ ret
+
+L(jmp):
+ /* Jump to the new context directly. */
+ jmp *%ecx
+
+L(no_shstk):
+#endif
+
/* Fetch the address to return to. */
movl oEIP(%eax), %ecx
diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
index 4aa7bb496a..420b6a7912 100644
--- a/sysdeps/unix/sysv/linux/i386/sysdep.h
+++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
@@ -662,4 +662,9 @@ struct libc_do_syscall_args
# endif
#endif
+/* Each shadow stack slot takes 4 bytes. Assuming that each stack
+ frame takes 128 bytes, this is used to compute shadow stack size
+ from stack size. */
+#define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5
+
#endif /* linux/i386/sysdep.h */
diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
index b11a5509cd..933c1924eb 100644
--- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
+++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
@@ -28,3 +28,4 @@ oEIP mreg (EIP)
oFPREGS mcontext (fpregs)
oSIGMASK ucontext (uc_sigmask)
oFPREGSMEM ucontext (__fpregs_mem)
+oSSP ucontext (__ssp)
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY
2020-01-08 16:15 ` V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY H.J. Lu
@ 2020-01-09 21:13 ` Adhemerval Zanella
0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2020-01-09 21:13 UTC (permalink / raw)
To: libc-alpha
On 08/01/2020 13:15, H.J. Lu wrote:
> Add _CET_ENDBR to i386 assembly files which don't use ENTRY to add
> ENDBR32 at function entries when CET is enabled.
> ---
> sysdeps/i386/i386-mcount.S | 2 ++
> sysdeps/i386/nptl/pthread_spin_lock.S | 2 ++
> sysdeps/i386/nptl/pthread_spin_unlock.S | 3 +++
> sysdeps/i386/pthread_spin_trylock.S | 2 ++
> 4 files changed, 9 insertions(+)
>
> diff --git a/sysdeps/i386/i386-mcount.S b/sysdeps/i386/i386-mcount.S
> index 9516265bcb..8b60bd20fe 100644
> --- a/sysdeps/i386/i386-mcount.S
> +++ b/sysdeps/i386/i386-mcount.S
> @@ -30,6 +30,7 @@
> .type C_SYMBOL_NAME(_mcount), @function
> .align ALIGNARG(4)
> C_LABEL(_mcount)
> + _CET_ENDBR
> /* Save the caller-clobbered registers. */
> pushl %eax
> pushl %ecx
> @@ -58,6 +59,7 @@ weak_alias (_mcount, mcount)
> .type C_SYMBOL_NAME(__fentry__), @function
> .align ALIGNARG(4)
> C_LABEL(__fentry__)
> + _CET_ENDBR
> /* Save the caller-clobbered registers. */
> pushl %eax
> pushl %ecx
Ok, ENTRY will issue CALL_MCOUNT itself.
> diff --git a/sysdeps/i386/nptl/pthread_spin_lock.S b/sysdeps/i386/nptl/pthread_spin_lock.S
> index 5736c82078..7eb8f0e069 100644
> --- a/sysdeps/i386/nptl/pthread_spin_lock.S
> +++ b/sysdeps/i386/nptl/pthread_spin_lock.S
> @@ -15,12 +15,14 @@
> License along with the GNU C Library; if not, see
> <https://www.gnu.org/licenses/>. */
>
> +#include <sysdep.h>
> #include <lowlevellock.h>
>
> .globl pthread_spin_lock
> .type pthread_spin_lock,@function
> .align 16
> pthread_spin_lock:
> + _CET_ENDBR
> mov 4(%esp), %eax
> 1: LOCK
> decl 0(%eax)
I think we can use ENTRY/END here instead.
> diff --git a/sysdeps/i386/nptl/pthread_spin_unlock.S b/sysdeps/i386/nptl/pthread_spin_unlock.S
> index e7757d0a03..dac730af1f 100644
> --- a/sysdeps/i386/nptl/pthread_spin_unlock.S
> +++ b/sysdeps/i386/nptl/pthread_spin_unlock.S
> @@ -16,10 +16,13 @@
> License along with the GNU C Library; if not, see
> <https://www.gnu.org/licenses/>. */
>
> +#include <sysdep.h>
> +
> .globl pthread_spin_unlock
> .type pthread_spin_unlock,@function
> .align 16
> pthread_spin_unlock:
> + _CET_ENDBR
> movl 4(%esp), %eax
> movl $1, (%eax)
> xorl %eax, %eax
Ditto.
> diff --git a/sysdeps/i386/pthread_spin_trylock.S b/sysdeps/i386/pthread_spin_trylock.S
> index dd08d38f8d..b3965a150f 100644
> --- a/sysdeps/i386/pthread_spin_trylock.S
> +++ b/sysdeps/i386/pthread_spin_trylock.S
> @@ -16,6 +16,7 @@
> License along with the GNU C Library; if not, see
> <https://www.gnu.org/licenses/>. */
>
> +#include <sysdep.h>
> #include <pthread-errnos.h>
>
>
> @@ -29,6 +30,7 @@
> .type pthread_spin_trylock,@function
> .align 16
> pthread_spin_trylock:
> + _CET_ENDBR
> movl 4(%esp), %edx
> movl $1, %eax
> xorl %ecx, %ecx
>
Ditto.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262]
2020-01-08 16:15 ` V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] H.J. Lu
@ 2020-01-09 21:13 ` Adhemerval Zanella
0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2020-01-09 21:13 UTC (permalink / raw)
To: libc-alpha
On 08/01/2020 13:15, H.J. Lu wrote:
> On i386, since EAX, ECX and EDX are caller-saved, there are no need
> to save and restore EAX, ECX and EDX in getcontext, setcontext and
> swapcontext. They just need to clear EAX on success. The extra
> scratch registers are needed to enable CET.
>
> Tested on i386.
LGTM thanks. You might also remove the oE{A,C,D}X definitions from
Linux i386 ucontext_i.sym.
As a side note, Hurd might do the same on its implementations.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> ---
> sysdeps/unix/sysv/linux/i386/getcontext.S | 8 +-------
> sysdeps/unix/sysv/linux/i386/setcontext.S | 11 ++++-------
> sysdeps/unix/sysv/linux/i386/swapcontext.S | 17 +++++------------
> 3 files changed, 10 insertions(+), 26 deletions(-)
>
> diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
> index f86df4d555..9c1df9a2aa 100644
> --- a/sysdeps/unix/sysv/linux/i386/getcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
> @@ -26,13 +26,7 @@ ENTRY(__getcontext)
> /* Load address of the context data structure. */
> movl 4(%esp), %eax
>
> - /* Return value of getcontext. EAX is the only register whose
> - value is not preserved. */
> - movl $0, oEAX(%eax)
> -
> - /* Save the 32-bit register values and the return address. */
> - movl %ecx, oECX(%eax)
> - movl %edx, oEDX(%eax)
> + /* Save the preserved register values and the return address. */
> movl %edi, oEDI(%eax)
> movl %esi, oESI(%eax)
> movl %ebp, oEBP(%eax)
Ok.
> diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
> index b4b5c0298c..f042d80bf4 100644
> --- a/sysdeps/unix/sysv/linux/i386/setcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
> @@ -65,22 +65,19 @@ ENTRY(__setcontext)
> cfi_offset (esi, oESI)
> cfi_offset (ebp, oEBP)
> cfi_offset (ebx, oEBX)
> - cfi_offset (edx, oEDX)
> - cfi_offset (ecx, oECX)
> movl oESP(%eax), %esp
>
> /* Push the return address on the new stack so we can return there. */
> pushl %ecx
>
> - /* Load the values of all the 32-bit registers (except ESP).
> - Since we are loading from EAX, it must be last. */
> + /* Load the values of all the preserved registers (except ESP). */
> movl oEDI(%eax), %edi
> movl oESI(%eax), %esi
> movl oEBP(%eax), %ebp
> movl oEBX(%eax), %ebx
> - movl oEDX(%eax), %edx
> - movl oECX(%eax), %ecx
> - movl oEAX(%eax), %eax
> +
> + /* All done, return 0 for success. */
> + xorl %eax, %eax
>
> /* End FDE here, we fall into another context. */
> cfi_endproc
Ok.
> diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
> index 792bfdf7e6..090c2d8c3e 100644
> --- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
> @@ -26,13 +26,7 @@ ENTRY(__swapcontext)
> /* Load address of the context data structure we save in. */
> movl 4(%esp), %eax
>
> - /* Return value of swapcontext. EAX is the only register whose
> - value is not preserved. */
> - movl $0, oEAX(%eax)
> -
> - /* Save the 32-bit register values and the return address. */
> - movl %ecx, oECX(%eax)
> - movl %edx, oEDX(%eax)
> + /* Save the preserved register values and the return address. */
> movl %edi, oEDI(%eax)
> movl %esi, oESI(%eax)
> movl %ebp, oEBP(%eax)
> @@ -91,15 +85,14 @@ ENTRY(__swapcontext)
> /* Push the return address on the new stack so we can return there. */
> pushl %ecx
>
> - /* Load the values of all the 32-bit registers (except ESP).
> - Since we are loading from EAX, it must be last. */
> + /* Load the values of all the preserved registers (except ESP). */
> movl oEDI(%eax), %edi
> movl oESI(%eax), %esi
> movl oEBP(%eax), %ebp
> movl oEBX(%eax), %ebx
> - movl oEDX(%eax), %edx
> - movl oECX(%eax), %ecx
> - movl oEAX(%eax), %eax
> +
> + /* All done, return 0 for success. */
> + xorl %eax, %eax
>
> /* The following 'ret' will pop the address of the code and jump
> to it. */
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target
2020-01-08 16:15 ` V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target H.J. Lu
@ 2020-01-09 21:13 ` Adhemerval Zanella
0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2020-01-09 21:13 UTC (permalink / raw)
To: libc-alpha
LGTM thanks.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
On 08/01/2020 13:15, H.J. Lu wrote:
> diff --git a/sysdeps/i386/sub_n.S b/sysdeps/i386/sub_n.S
> index caac89177b..16ebd9169b 100644
> --- a/sysdeps/i386/sub_n.S
> +++ b/sysdeps/i386/sub_n.S
> @@ -91,6 +91,7 @@ L(oop): movl (%esi),%eax
> movl 8(%esi),%eax
> sbbl 8(%edx),%eax
> movl %eax,8(%edi)
> + _CET_ENDBR
> movl 12(%esi),%eax
> sbbl 12(%edx),%eax
> movl %eax,12(%edi)
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: V2 [PATCH 4/5] i386: Remove _exit.S
2020-01-08 16:15 ` V2 [PATCH 4/5] i386: Remove _exit.S H.J. Lu
@ 2020-01-09 21:14 ` Adhemerval Zanella
0 siblings, 0 replies; 13+ messages in thread
From: Adhemerval Zanella @ 2020-01-09 21:14 UTC (permalink / raw)
To: libc-alpha
LGTM thanks.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
On 08/01/2020 13:15, H.J. Lu wrote:
> The generic implementation is suffice since __NR_exit_group is always
> support and i386 does define ABORT_INSTRUCTION.
> ---
> sysdeps/unix/sysv/linux/i386/_exit.S | 44 ----------------------------
> 1 file changed, 44 deletions(-)
> delete mode 100644 sysdeps/unix/sysv/linux/i386/_exit.S
>
> diff --git a/sysdeps/unix/sysv/linux/i386/_exit.S b/sysdeps/unix/sysv/linux/i386/_exit.S
> deleted file mode 100644
> index 1f7bfeb7e7..0000000000
> --- a/sysdeps/unix/sysv/linux/i386/_exit.S
> +++ /dev/null
> @@ -1,44 +0,0 @@
> -/* Copyright (C) 2002-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/>. */
> -
> -#include <sysdep.h>
> -
> - .text
> - .type _exit,@function
> - .global _exit
> -_exit:
> - movl 4(%esp), %ebx
> -
> - /* Try the new syscall first. */
> -#ifdef __NR_exit_group
> - movl $__NR_exit_group, %eax
> - ENTER_KERNEL
> -#endif
> -
> - /* Not available. Now the old one. */
> - movl $__NR_exit, %eax
> - /* Don't bother using ENTER_KERNEL here. If the exit_group
> - syscall is not available AT_SYSINFO isn't either. */
> - int $0x80
> -
> - /* This must not fail. Be sure we don't return. */
> - hlt
> - .size _exit,.-_exit
> -
> -libc_hidden_def (_exit)
> -rtld_hidden_def (_exit)
> -weak_alias (_exit, _Exit)
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* V3 [PATCH] i386: Enable CET support in ucontext functions
2020-01-08 16:15 ` V2 [PATCH 5/5] i386: Enable CET support in ucontext functions H.J. Lu
@ 2020-02-01 14:09 ` H.J. Lu
2020-02-13 17:04 ` V4 " H.J. Lu
2020-02-14 22:10 ` V3 " Carlos O'Donell
0 siblings, 2 replies; 13+ messages in thread
From: H.J. Lu @ 2020-02-01 14:09 UTC (permalink / raw)
To: GNU C Library
[-- Attachment #1: Type: text/plain, Size: 1548 bytes --]
On Wed, Jan 8, 2020 at 8:15 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> 1. getcontext and swapcontext are updated to save the caller's shadow
> stack pointer and return address.
> 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 the helper code, L(exitcode).
>
> 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.
>
This is the updated patch. The only change is to use
+oSCRATCH1 mreg (EAX)
+oSCRATCH2 mreg (ECX)
+oSCRATCH3 mreg (EDX)
to replace oEAX, oECX and -oEDX.
OK for master?
Thanks.
--
H.J.
[-- Attachment #2: 0001-i386-Enable-CET-support-in-ucontext-functions.patch --]
[-- Type: application/x-patch, Size: 16299 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* V4 [PATCH] i386: Enable CET support in ucontext functions
2020-02-01 14:09 ` V3 [PATCH] " H.J. Lu
@ 2020-02-13 17:04 ` H.J. Lu
2020-02-14 22:10 ` V3 " Carlos O'Donell
1 sibling, 0 replies; 13+ messages in thread
From: H.J. Lu @ 2020-02-13 17:04 UTC (permalink / raw)
To: GNU C Library
[-- Attachment #1: Type: text/plain, Size: 2921 bytes --]
On Sat, Feb 1, 2020 at 6:09 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Wed, Jan 8, 2020 at 8:15 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > 1. getcontext and swapcontext are updated to save the caller's shadow
> > stack pointer and return address.
> > 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 the helper code, L(exitcode).
> >
> > 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.
> >
>
> This is the updated patch. The only change is to use
>
> +oSCRATCH1 mreg (EAX)
> +oSCRATCH2 mreg (ECX)
> +oSCRATCH3 mreg (EDX)
>
> to replace oEAX, oECX and -oEDX.
>
> OK for master?
>
A small update. The difference between old and new are:
--- sysdeps/unix/sysv/linux/i386/setcontext.S 2020-01-22
05:28:24.027273572 -0800
+++ sysdeps/unix/sysv/linux/i386/setcontext.S 2020-02-13
08:48:09.731359073 -0800
@@ -82,11 +82,11 @@ ENTRY(__setcontext)
cmpl %gs:SSP_BASE_OFFSET, %ecx
je L(unwind_shadow_stack)
-L(find_restore_token_loop):
/* Align the saved original shadow stack pointer to the next
8 byte aligned boundary. */
andl $-8, %esi
+L(find_restore_token_loop):
/* Look for a restore token. */
movl -8(%esi), %ebx
andl $-8, %ebx
--- sysdeps/unix/sysv/linux/i386/swapcontext.S 2020-01-22
05:28:24.028273573 -0800
+++ sysdeps/unix/sysv/linux/i386/swapcontext.S 2020-02-13
08:48:09.731359073 -0800
@@ -135,11 +135,11 @@ L(shadow_stack_bound_recorded):
cmpl %gs:SSP_BASE_OFFSET, %ecx
je L(unwind_shadow_stack)
-L(find_restore_token_loop):
/* Align the saved original shadow stack pointer to the next
8 byte aligned boundary. */
andl $-8, %esi
+L(find_restore_token_loop):
/* Look for a restore token. */
movl -8(%esi), %ebx
andl $-8, %ebx
We don't need to re-align shadow stack since shadow stack will always
be decremented
by 8 bytes after initial alignment to 8 bytes.
Thanks.
--
H.J.
[-- Attachment #2: 0001-i386-Enable-CET-support-in-ucontext-functions.patch --]
[-- Type: text/x-patch, Size: 16299 bytes --]
From 1b957edd0ed4ebee5ad401ac3f2de138d8495e79 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 6 Dec 2019 16:45:17 -0800
Subject: [PATCH] i386: Enable CET support in ucontext functions
1. getcontext and swapcontext are updated to save the caller's shadow
stack pointer and return address.
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 the helper code, L(exitcode).
4. Since we no longer save and restore EAX, ECX and EDX in getcontext,
setcontext and swapcontext, we can use them as scratch register slots
to enable CET in ucontext functions.
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.
Tested on i386 CET/non-CET machines.
---
sysdeps/unix/sysv/linux/i386/getcontext.S | 56 ++++++++
sysdeps/unix/sysv/linux/i386/makecontext.S | 123 +++++++++++++++++
sysdeps/unix/sysv/linux/i386/setcontext.S | 101 +++++++++++++-
sysdeps/unix/sysv/linux/i386/swapcontext.S | 139 ++++++++++++++++++++
sysdeps/unix/sysv/linux/i386/sysdep.h | 5 +
sysdeps/unix/sysv/linux/i386/ucontext_i.sym | 4 +
6 files changed, 425 insertions(+), 3 deletions(-)
diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
index 9c1df9a2aa..d91cfe4b1d 100644
--- a/sysdeps/unix/sysv/linux/i386/getcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -42,6 +43,61 @@ ENTRY(__getcontext)
movw %fs, %dx
movl %edx, oFS(%eax)
+#if SHSTK_ENABLED
+ /* Check if shadow stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ /* Save EAX in EDX. */
+ movl %eax, %edx
+
+ xorl %eax, %eax
+ cmpl %gs:SSP_BASE_OFFSET, %eax
+ jnz L(shadow_stack_bound_recorded)
+
+ /* Save EBX in the first scratch register slot. */
+ movl %ebx, oSCRATCH1(%edx)
+
+ /* Get the base address and size of the default shadow stack
+ which must be the current shadow stack since nothing has
+ been recorded yet. */
+ sub $24, %esp
+ mov %esp, %ecx
+ movl $ARCH_CET_STATUS, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jz L(continue_no_err)
+
+ /* This should never happen. */
+ hlt
+
+L(continue_no_err):
+ /* Restore EBX from the first scratch register slot. */
+ movl oSCRATCH1(%edx), %ebx
+
+ /* Record the base of the current shadow stack. */
+ movl 8(%esp), %eax
+ movl %eax, %gs:SSP_BASE_OFFSET
+ add $24, %esp
+
+L(shadow_stack_bound_recorded):
+ /* Load address of the context data structure. */
+ movl 4(%esp), %eax
+
+ /* Get the current shadow stack pointer. */
+ rdsspd %edx
+ /* NB: Save the caller's shadow stack so that we can jump back
+ to the caller directly. */
+ addl $4, %edx
+ movl %edx, oSSP(%eax)
+
+ /* Save the current shadow stack base in ucontext. */
+ movl %gs:SSP_BASE_OFFSET, %edx
+ movl %edx, (oSSP + 4)(%eax)
+
+L(no_shstk):
+#endif
/* We have separate floating-point register content memory on the
stack. We use the __fpregs_mem block in the context. Set the
links up correctly. */
diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S
index ad9ce5f977..91009675d1 100644
--- a/sysdeps/unix/sysv/linux/i386/makecontext.S
+++ b/sysdeps/unix/sysv/linux/i386/makecontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -68,6 +69,127 @@ ENTRY(__makecontext)
jnz 1b
2:
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(skip_ssp)
+
+ /* Reload the pointer to ucontext. */
+ movl 4(%esp), %eax
+
+ /* Shadow stack is enabled. We need to allocate a new shadow
+ stack. */
+ subl oSS_SP(%eax), %edx
+ shrl $STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT, %edx
+
+ /* Align shadow stack size to 8 bytes. */
+ addl $7, %edx
+ andl $-8, %edx
+
+ /* Store shadow stack size in __ssp[2]. */
+ movl %edx, (oSSP + 8)(%eax)
+
+ /* Save ESI in the second scratch register slot. */
+ movl %esi, oSCRATCH2(%eax)
+ /* Save EDI in the third scratch register slot. */
+ movl %edi, oSCRATCH3(%eax)
+
+ /* Save the pointer to ucontext. */
+ movl %eax, %edi
+
+ /* Get the original shadow stack pointer. */
+ rdsspd %esi
+
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+ /* Load the top of the new stack into EDX. */
+ movl oESP(%eax), %edx
+
+ /* We need to terminate the FDE here because the unwinder looks
+ at ra-1 for unwind information. */
+ cfi_endproc
+
+ /* Swap the original stack pointer with the top of the new
+ stack. */
+ xchgl %esp, %edx
+
+ /* Add 4 bytes since CALL will push the 4-byte return address
+ onto stack. */
+ addl $4, %esp
+
+ /* Allocate the new shadow stack. Save EBX in the first scratch
+ register slot. */
+ movl %ebx, oSCRATCH1(%eax)
+
+ /* CET syscall takes 64-bit sizes. */
+ subl $16, %esp
+ movl (oSSP + 8)(%eax), %ecx
+ movl %ecx, (%esp)
+ movl $0, 4(%esp)
+ movl %ecx, 8(%esp)
+ movl $0, 12(%esp)
+ movl %esp, %ecx
+
+ movl $ARCH_CET_ALLOC_SHSTK, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jne L(hlt) /* This should never happen. */
+
+ /* Copy the base address of the new shadow stack to __ssp[1]. */
+ movl (%esp), %eax
+ movl %eax, (oSSP + 4)(%edi)
+
+ addl $16, %esp
+
+ /* Restore EBX from the first scratch register slot. */
+ movl oSCRATCH1(%edi), %ebx
+
+ /* Get the size of the new shadow stack. */
+ movl (oSSP + 8)(%edi), %ecx
+
+ /* Use the restore stoken to restore the new shadow stack. */
+ rstorssp -8(%eax, %ecx)
+
+ /* Save the restore token at the next 8 byte aligned boundary
+ on the original shadow stack. */
+ saveprevssp
+
+ /* Push the address of "jmp exitcode" onto the new stack as
+ well as the new shadow stack. */
+ call 1f
+ jmp L(exitcode)
+1:
+
+ /* Get the new shadow stack pointer. */
+ rdsspd %eax
+
+ /* Use the restore stoken to restore the original shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the new shadow stack. */
+ saveprevssp
+
+ /* Store the new shadow stack pointer in __ssp[0]. */
+ movl %eax, oSSP(%edi)
+
+ /* Restore the original stack. */
+ mov %edx, %esp
+
+ cfi_startproc
+
+ /* Restore ESI from the second scratch register slot. */
+ movl oSCRATCH2(%edi), %esi
+ /* Restore EDI from the third scratch register slot. */
+ movl oSCRATCH3(%edi), %edi
+
+ ret
+
+L(skip_ssp):
+#endif
+
/* If the function we call returns we must continue with the
context which is given in the uc_link element. To do this
set the return address for the function the user provides
@@ -123,6 +245,7 @@ L(call_exit):
call HIDDEN_JUMPTARGET(exit)
/* The 'exit' call should never return. In case it does cause
the process to terminate. */
+L(hlt):
hlt
cfi_startproc
END(__makecontext)
diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
index f042d80bf4..332b5147bc 100644
--- a/sysdeps/unix/sysv/linux/i386/setcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -56,9 +57,6 @@ ENTRY(__setcontext)
movl oFS(%eax), %ecx
movw %cx, %fs
- /* Fetch the address to return to. */
- movl oEIP(%eax), %ecx
-
/* Load the new stack pointer. */
cfi_def_cfa (eax, 0)
cfi_offset (edi, oEDI)
@@ -67,6 +65,103 @@ ENTRY(__setcontext)
cfi_offset (ebx, oEBX)
movl oESP(%eax), %esp
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ /* If the base of the target shadow stack is the same as the
+ base of the current shadow stack, we unwind the shadow
+ stack. Otherwise it is a stack switch and we look for a
+ restore token. */
+ movl oSSP(%eax), %esi
+ movl %esi, %edi
+
+ /* Get the base of the target shadow stack. */
+ movl (oSSP + 4)(%eax), %ecx
+ cmpl %gs:SSP_BASE_OFFSET, %ecx
+ je L(unwind_shadow_stack)
+
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+L(find_restore_token_loop):
+ /* Look for a restore token. */
+ movl -8(%esi), %ebx
+ andl $-8, %ebx
+ cmpl %esi, %ebx
+ je L(restore_shadow_stack)
+
+ /* Try the next slot. */
+ subl $8, %esi
+ jmp L(find_restore_token_loop)
+
+L(restore_shadow_stack):
+ /* Pop return address from the shadow stack since setcontext
+ will not return. */
+ movl $1, %ebx
+ incsspd %ebx
+
+ /* Use the restore stoken to restore the target shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the old shadow stack. NB: This
+ restore token may be checked by setcontext or swapcontext
+ later. */
+ saveprevssp
+
+ /* Record the new shadow stack base that was switched to. */
+ movl (oSSP + 4)(%eax), %ebx
+ movl %ebx, %gs:SSP_BASE_OFFSET
+
+L(unwind_shadow_stack):
+ rdsspd %ebx
+ subl %edi, %ebx
+ je L(skip_unwind_shadow_stack)
+ negl %ebx
+ shrl $2, %ebx
+ movl $255, %esi
+L(loop):
+ cmpl %esi, %ebx
+ cmovb %ebx, %esi
+ incsspd %esi
+ subl %esi, %ebx
+ ja L(loop)
+
+L(skip_unwind_shadow_stack):
+
+ /* Load the values of all the preserved registers (except ESP). */
+ movl oEDI(%eax), %edi
+ movl oESI(%eax), %esi
+ movl oEBP(%eax), %ebp
+ movl oEBX(%eax), %ebx
+
+ /* Get the return address set with getcontext. */
+ movl oEIP(%eax), %ecx
+
+ /* Check if return address is valid for the case when setcontext
+ is invoked from L(exitcode) with linked context. */
+ rdsspd %eax
+ cmpl (%eax), %ecx
+ /* Clear EAX to indicate success. NB: Don't use xorl to keep
+ EFLAGS for jne. */
+ movl $0, %eax
+ jne L(jmp)
+ /* Return to the new context if return address valid. */
+ pushl %ecx
+ ret
+
+L(jmp):
+ /* Jump to the new context directly. */
+ jmp *%ecx
+
+L(no_shstk):
+#endif
+
+ /* Fetch the address to return to. */
+ movl oEIP(%eax), %ecx
+
/* Push the return address on the new stack so we can return there. */
pushl %ecx
diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
index 090c2d8c3e..203eafa2e7 100644
--- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
+++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
@@ -18,6 +18,7 @@
<https://www.gnu.org/licenses/>. */
#include <sysdep.h>
+#include <asm/prctl.h>
#include "ucontext_i.h"
@@ -76,6 +77,144 @@ ENTRY(__swapcontext)
movl oFS(%eax), %edx
movw %dx, %fs
+#if SHSTK_ENABLED
+ /* Check if Shadow Stack is enabled. */
+ testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
+ jz L(no_shstk)
+
+ xorl %eax, %eax
+ cmpl %gs:SSP_BASE_OFFSET, %eax
+ jnz L(shadow_stack_bound_recorded)
+
+ /* Get the base address and size of the default shadow stack
+ which must be the current shadow stack since nothing has
+ been recorded yet. */
+ sub $24, %esp
+ mov %esp, %ecx
+ movl $ARCH_CET_STATUS, %ebx
+ movl $__NR_arch_prctl, %eax
+ ENTER_KERNEL
+ testl %eax, %eax
+ jz L(continue_no_err)
+
+ /* This should never happen. */
+ hlt
+
+L(continue_no_err):
+ /* Record the base of the current shadow stack. */
+ movl 8(%esp), %eax
+ movl %eax, %gs:SSP_BASE_OFFSET
+ add $24, %esp
+
+L(shadow_stack_bound_recorded):
+ /* Load address of the context data structure we save in. */
+ movl 4(%esp), %eax
+
+ /* Load address of the context data structure we swap in */
+ movl 8(%esp), %edx
+
+ /* If we unwind the stack, we can't undo stack unwinding. Just
+ save the target shadow stack pointer as the current shadow
+ stack pointer. */
+ movl oSSP(%edx), %ecx
+ movl %ecx, oSSP(%eax)
+
+ /* Save the current shadow stack base in ucontext. */
+ movl %gs:SSP_BASE_OFFSET, %ecx
+ movl %ecx, (oSSP + 4)(%eax)
+
+ /* If the base of the target shadow stack is the same as the
+ base of the current shadow stack, we unwind the shadow
+ stack. Otherwise it is a stack switch and we look for a
+ restore token. */
+ movl oSSP(%edx), %esi
+ movl %esi, %edi
+
+ /* Get the base of the target shadow stack. */
+ movl (oSSP + 4)(%edx), %ecx
+ cmpl %gs:SSP_BASE_OFFSET, %ecx
+ je L(unwind_shadow_stack)
+
+ /* Align the saved original shadow stack pointer to the next
+ 8 byte aligned boundary. */
+ andl $-8, %esi
+
+L(find_restore_token_loop):
+ /* Look for a restore token. */
+ movl -8(%esi), %ebx
+ andl $-8, %ebx
+ cmpl %esi, %ebx
+ je L(restore_shadow_stack)
+
+ /* Try the next slot. */
+ subl $8, %esi
+ jmp L(find_restore_token_loop)
+
+L(restore_shadow_stack):
+ /* The target shadow stack will be restored. Save the current
+ shadow stack pointer. */
+ rdsspd %ecx
+ movl %ecx, oSSP(%eax)
+
+ /* Use the restore stoken to restore the target shadow stack. */
+ rstorssp -8(%esi)
+
+ /* Save the restore token on the old shadow stack. NB: This
+ restore token may be checked by setcontext or swapcontext
+ later. */
+ saveprevssp
+
+ /* Record the new shadow stack base that was switched to. */
+ movl (oSSP + 4)(%edx), %ebx
+ movl %ebx, %gs:SSP_BASE_OFFSET
+
+L(unwind_shadow_stack):
+ rdsspd %ebx
+ subl %edi, %ebx
+ je L(skip_unwind_shadow_stack)
+ negl %ebx
+ shrl $2, %ebx
+ movl $255, %esi
+L(loop):
+ cmpl %esi, %ebx
+ cmovb %ebx, %esi
+ incsspd %esi
+ subl %esi, %ebx
+ ja L(loop)
+
+L(skip_unwind_shadow_stack):
+
+ /* Load the new stack pointer. */
+ movl oESP(%edx), %esp
+
+ /* Load the values of all the preserved registers (except ESP). */
+ movl oEDI(%edx), %edi
+ movl oESI(%edx), %esi
+ movl oEBP(%edx), %ebp
+ movl oEBX(%edx), %ebx
+
+ /* Get the return address set with getcontext. */
+ movl oEIP(%edx), %ecx
+
+ /* Check if return address is valid for the case when setcontext
+ is invoked from L(exitcode) with linked context. */
+ rdsspd %eax
+ cmpl (%eax), %ecx
+ /* Clear EAX to indicate success. NB: Don't use xorl to keep
+ EFLAGS for jne. */
+ movl $0, %eax
+ jne L(jmp)
+ /* Return to the new context if return address valid. */
+ pushl %ecx
+ ret
+
+L(jmp):
+ /* Jump to the new context directly. */
+ jmp *%ecx
+
+L(no_shstk):
+#endif
+
/* Fetch the address to return to. */
movl oEIP(%eax), %ecx
diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
index 4aa7bb496a..420b6a7912 100644
--- a/sysdeps/unix/sysv/linux/i386/sysdep.h
+++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
@@ -662,4 +662,9 @@ struct libc_do_syscall_args
# endif
#endif
+/* Each shadow stack slot takes 4 bytes. Assuming that each stack
+ frame takes 128 bytes, this is used to compute shadow stack size
+ from stack size. */
+#define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5
+
#endif /* linux/i386/sysdep.h */
diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
index 1dfe03d2cc..1d8608eafc 100644
--- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
+++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
@@ -22,6 +22,10 @@ oEBP mreg (EBP)
oESP mreg (ESP)
oEBX mreg (EBX)
oEIP mreg (EIP)
+oSCRATCH1 mreg (EAX)
+oSCRATCH2 mreg (ECX)
+oSCRATCH3 mreg (EDX)
oFPREGS mcontext (fpregs)
oSIGMASK ucontext (uc_sigmask)
oFPREGSMEM ucontext (__fpregs_mem)
+oSSP ucontext (__ssp)
--
2.24.1
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: V3 [PATCH] i386: Enable CET support in ucontext functions
2020-02-01 14:09 ` V3 [PATCH] " H.J. Lu
2020-02-13 17:04 ` V4 " H.J. Lu
@ 2020-02-14 22:10 ` Carlos O'Donell
1 sibling, 0 replies; 13+ messages in thread
From: Carlos O'Donell @ 2020-02-14 22:10 UTC (permalink / raw)
To: H.J. Lu, GNU C Library
On 2/1/20 9:09 AM, H.J. Lu wrote:
> On Wed, Jan 8, 2020 at 8:15 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> 1. getcontext and swapcontext are updated to save the caller's shadow
>> stack pointer and return address.
>> 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 the helper code, L(exitcode).
>>
>> 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.
>>
LGTM.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
> This is the updated patch. The only change is to use
>
> +oSCRATCH1 mreg (EAX)
> +oSCRATCH2 mreg (ECX)
> +oSCRATCH3 mreg (EDX)
>
> to replace oEAX, oECX and -oEDX.
>
> OK for master?
>
> Thanks.
>
> From 33165282a09877e2da7b71745fb49b12c648acf1 Mon Sep 17 00:00:00 2001
> From: "H.J. Lu" <hjl.tools@gmail.com>
> Date: Fri, 6 Dec 2019 16:45:17 -0800
> Subject: [PATCH] i386: Enable CET support in ucontext functions
>
> 1. getcontext and swapcontext are updated to save the caller's shadow
> stack pointer and return address.
> 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 the helper code, L(exitcode).
> 4. Since we no longer save and restore EAX, ECX and EDX in getcontext,
> setcontext and swapcontext, we can use them as scratch register slots
> to enable CET in ucontext functions.
>
> 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.
>
> Tested on i386 CET/non-CET machines.
OK.
> ---
> sysdeps/unix/sysv/linux/i386/getcontext.S | 56 ++++++++
> sysdeps/unix/sysv/linux/i386/makecontext.S | 123 +++++++++++++++++
> sysdeps/unix/sysv/linux/i386/setcontext.S | 101 +++++++++++++-
> sysdeps/unix/sysv/linux/i386/swapcontext.S | 139 ++++++++++++++++++++
> sysdeps/unix/sysv/linux/i386/sysdep.h | 5 +
> sysdeps/unix/sysv/linux/i386/ucontext_i.sym | 4 +
> 6 files changed, 425 insertions(+), 3 deletions(-)
>
> diff --git a/sysdeps/unix/sysv/linux/i386/getcontext.S b/sysdeps/unix/sysv/linux/i386/getcontext.S
> index 9c1df9a2aa..d91cfe4b1d 100644
> --- a/sysdeps/unix/sysv/linux/i386/getcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/getcontext.S
> @@ -18,6 +18,7 @@
> <https://www.gnu.org/licenses/>. */
>
> #include <sysdep.h>
> +#include <asm/prctl.h>
>
> #include "ucontext_i.h"
>
> @@ -42,6 +43,61 @@ ENTRY(__getcontext)
> movw %fs, %dx
> movl %edx, oFS(%eax)
>
> +#if SHSTK_ENABLED
> + /* Check if shadow stack is enabled. */
> + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
> + jz L(no_shstk)
OK. Like x86_64
> +
> + /* Save EAX in EDX. */
> + movl %eax, %edx
> +
> + xorl %eax, %eax
> + cmpl %gs:SSP_BASE_OFFSET, %eax
> + jnz L(shadow_stack_bound_recorded)
OK. Check ssp_base from tcbhead_t.
> +
> + /* Save EBX in the first scratch register slot. */
> + movl %ebx, oSCRATCH1(%edx)
> +
> + /* Get the base address and size of the default shadow stack
> + which must be the current shadow stack since nothing has
> + been recorded yet. */
> + sub $24, %esp
> + mov %esp, %ecx
> + movl $ARCH_CET_STATUS, %ebx
> + movl $__NR_arch_prctl, %eax
> + ENTER_KERNEL
> + testl %eax, %eax
> + jz L(continue_no_err)
> +
> + /* This should never happen. */
> + hlt
OK. Might have been nice to put this in abort-instr.h as ABORT_INSTRUCTION_ASM,
but almost nobody does this.
> +
> +L(continue_no_err):
> + /* Restore EBX from the first scratch register slot. */
> + movl oSCRATCH1(%edx), %ebx
> +
> + /* Record the base of the current shadow stack. */
> + movl 8(%esp), %eax
> + movl %eax, %gs:SSP_BASE_OFFSET
> + add $24, %esp
> +
> +L(shadow_stack_bound_recorded):
> + /* Load address of the context data structure. */
> + movl 4(%esp), %eax
> +
> + /* Get the current shadow stack pointer. */
> + rdsspd %edx
> + /* NB: Save the caller's shadow stack so that we can jump back
> + to the caller directly. */
> + addl $4, %edx
> + movl %edx, oSSP(%eax)
> +
> + /* Save the current shadow stack base in ucontext. */
> + movl %gs:SSP_BASE_OFFSET, %edx
> + movl %edx, (oSSP + 4)(%eax)
> +
> +L(no_shstk):
> +#endif
> /* We have separate floating-point register content memory on the
> stack. We use the __fpregs_mem block in the context. Set the
> links up correctly. */
> diff --git a/sysdeps/unix/sysv/linux/i386/makecontext.S b/sysdeps/unix/sysv/linux/i386/makecontext.S
> index ad9ce5f977..91009675d1 100644
> --- a/sysdeps/unix/sysv/linux/i386/makecontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/makecontext.S
> @@ -18,6 +18,7 @@
> <https://www.gnu.org/licenses/>. */
>
> #include <sysdep.h>
> +#include <asm/prctl.h>
>
> #include "ucontext_i.h"
>
> @@ -68,6 +69,127 @@ ENTRY(__makecontext)
> jnz 1b
> 2:
>
> +#if SHSTK_ENABLED
OK.
> + /* Check if Shadow Stack is enabled. */
> + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
> + jz L(skip_ssp)
> +
> + /* Reload the pointer to ucontext. */
> + movl 4(%esp), %eax
> +
> + /* Shadow stack is enabled. We need to allocate a new shadow
> + stack. */
> + subl oSS_SP(%eax), %edx
> + shrl $STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT, %edx
> +
> + /* Align shadow stack size to 8 bytes. */
> + addl $7, %edx
> + andl $-8, %edx
> +
> + /* Store shadow stack size in __ssp[2]. */
> + movl %edx, (oSSP + 8)(%eax)
> +
> + /* Save ESI in the second scratch register slot. */
> + movl %esi, oSCRATCH2(%eax)
> + /* Save EDI in the third scratch register slot. */
> + movl %edi, oSCRATCH3(%eax)
> +
> + /* Save the pointer to ucontext. */
> + movl %eax, %edi
> +
> + /* Get the original shadow stack pointer. */
> + rdsspd %esi
> +
> + /* Align the saved original shadow stack pointer to the next
> + 8 byte aligned boundary. */
> + andl $-8, %esi
> +
> + /* Load the top of the new stack into EDX. */
> + movl oESP(%eax), %edx
> +
> + /* We need to terminate the FDE here because the unwinder looks
> + at ra-1 for unwind information. */
> + cfi_endproc
> +
> + /* Swap the original stack pointer with the top of the new
> + stack. */
> + xchgl %esp, %edx
> +
> + /* Add 4 bytes since CALL will push the 4-byte return address
> + onto stack. */
> + addl $4, %esp
> +
> + /* Allocate the new shadow stack. Save EBX in the first scratch
> + register slot. */
> + movl %ebx, oSCRATCH1(%eax)
> +
> + /* CET syscall takes 64-bit sizes. */
> + subl $16, %esp
> + movl (oSSP + 8)(%eax), %ecx
> + movl %ecx, (%esp)
> + movl $0, 4(%esp)
> + movl %ecx, 8(%esp)
> + movl $0, 12(%esp)
> + movl %esp, %ecx
> +
> + movl $ARCH_CET_ALLOC_SHSTK, %ebx
> + movl $__NR_arch_prctl, %eax
> + ENTER_KERNEL
> + testl %eax, %eax
> + jne L(hlt) /* This should never happen. */
> +
> + /* Copy the base address of the new shadow stack to __ssp[1]. */
> + movl (%esp), %eax
> + movl %eax, (oSSP + 4)(%edi)
> +
> + addl $16, %esp
> +
> + /* Restore EBX from the first scratch register slot. */
> + movl oSCRATCH1(%edi), %ebx
> +
> + /* Get the size of the new shadow stack. */
> + movl (oSSP + 8)(%edi), %ecx
> +
> + /* Use the restore stoken to restore the new shadow stack. */
> + rstorssp -8(%eax, %ecx)
> +
> + /* Save the restore token at the next 8 byte aligned boundary
> + on the original shadow stack. */
> + saveprevssp
> +
> + /* Push the address of "jmp exitcode" onto the new stack as
> + well as the new shadow stack. */
> + call 1f
> + jmp L(exitcode)
> +1:
> +
> + /* Get the new shadow stack pointer. */
> + rdsspd %eax
> +
> + /* Use the restore stoken to restore the original shadow stack. */
> + rstorssp -8(%esi)
> +
> + /* Save the restore token on the new shadow stack. */
> + saveprevssp
> +
> + /* Store the new shadow stack pointer in __ssp[0]. */
> + movl %eax, oSSP(%edi)
> +
> + /* Restore the original stack. */
> + mov %edx, %esp
> +
> + cfi_startproc
> +
> + /* Restore ESI from the second scratch register slot. */
> + movl oSCRATCH2(%edi), %esi
> + /* Restore EDI from the third scratch register slot. */
> + movl oSCRATCH3(%edi), %edi
> +
> + ret
> +
> +L(skip_ssp):
> +#endif
> +
> /* If the function we call returns we must continue with the
> context which is given in the uc_link element. To do this
> set the return address for the function the user provides
> @@ -123,6 +245,7 @@ L(call_exit):
> call HIDDEN_JUMPTARGET(exit)
> /* The 'exit' call should never return. In case it does cause
> the process to terminate. */
> +L(hlt):
> hlt
> cfi_startproc
> END(__makecontext)
> diff --git a/sysdeps/unix/sysv/linux/i386/setcontext.S b/sysdeps/unix/sysv/linux/i386/setcontext.S
> index f042d80bf4..9bc7d68a1a 100644
> --- a/sysdeps/unix/sysv/linux/i386/setcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/setcontext.S
> @@ -18,6 +18,7 @@
> <https://www.gnu.org/licenses/>. */
>
> #include <sysdep.h>
> +#include <asm/prctl.h>
>
> #include "ucontext_i.h"
>
> @@ -56,9 +57,6 @@ ENTRY(__setcontext)
> movl oFS(%eax), %ecx
> movw %cx, %fs
>
> - /* Fetch the address to return to. */
> - movl oEIP(%eax), %ecx
> -
> /* Load the new stack pointer. */
> cfi_def_cfa (eax, 0)
> cfi_offset (edi, oEDI)
> @@ -67,6 +65,103 @@ ENTRY(__setcontext)
> cfi_offset (ebx, oEBX)
> movl oESP(%eax), %esp
>
> +#if SHSTK_ENABLED
OK.
> + /* Check if Shadow Stack is enabled. */
> + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
> + jz L(no_shstk)
> +
> + /* If the base of the target shadow stack is the same as the
> + base of the current shadow stack, we unwind the shadow
> + stack. Otherwise it is a stack switch and we look for a
> + restore token. */
> + movl oSSP(%eax), %esi
> + movl %esi, %edi
> +
> + /* Get the base of the target shadow stack. */
> + movl (oSSP + 4)(%eax), %ecx
> + cmpl %gs:SSP_BASE_OFFSET, %ecx
> + je L(unwind_shadow_stack)
> +
> +L(find_restore_token_loop):
> + /* Align the saved original shadow stack pointer to the next
> + 8 byte aligned boundary. */
> + andl $-8, %esi
> +
> + /* Look for a restore token. */
> + movl -8(%esi), %ebx
> + andl $-8, %ebx
> + cmpl %esi, %ebx
> + je L(restore_shadow_stack)
> +
> + /* Try the next slot. */
> + subl $8, %esi
> + jmp L(find_restore_token_loop)
> +
> +L(restore_shadow_stack):
> + /* Pop return address from the shadow stack since setcontext
> + will not return. */
> + movl $1, %ebx
> + incsspd %ebx
> +
> + /* Use the restore stoken to restore the target shadow stack. */
> + rstorssp -8(%esi)
> +
> + /* Save the restore token on the old shadow stack. NB: This
> + restore token may be checked by setcontext or swapcontext
> + later. */
> + saveprevssp
> +
> + /* Record the new shadow stack base that was switched to. */
> + movl (oSSP + 4)(%eax), %ebx
> + movl %ebx, %gs:SSP_BASE_OFFSET
> +
> +L(unwind_shadow_stack):
> + rdsspd %ebx
> + subl %edi, %ebx
> + je L(skip_unwind_shadow_stack)
> + negl %ebx
> + shrl $2, %ebx
> + movl $255, %esi
> +L(loop):
> + cmpl %esi, %ebx
> + cmovb %ebx, %esi
> + incsspd %esi
> + subl %esi, %ebx
> + ja L(loop)
> +
> +L(skip_unwind_shadow_stack):
> +
> + /* Load the values of all the preserved registers (except ESP). */
> + movl oEDI(%eax), %edi
> + movl oESI(%eax), %esi
> + movl oEBP(%eax), %ebp
> + movl oEBX(%eax), %ebx
> +
> + /* Get the return address set with getcontext. */
> + movl oEIP(%eax), %ecx
> +
> + /* Check if return address is valid for the case when setcontext
> + is invoked from L(exitcode) with linked context. */
> + rdsspd %eax
> + cmpl (%eax), %ecx
> + /* Clear EAX to indicate success. NB: Don't use xorl to keep
> + EFLAGS for jne. */
> + movl $0, %eax
> + jne L(jmp)
> + /* Return to the new context if return address valid. */
> + pushl %ecx
> + ret
> +
> +L(jmp):
> + /* Jump to the new context directly. */
> + jmp *%ecx
> +
> +L(no_shstk):
> +#endif
> +
> + /* Fetch the address to return to. */
> + movl oEIP(%eax), %ecx
> +
> /* Push the return address on the new stack so we can return there. */
> pushl %ecx
>
> diff --git a/sysdeps/unix/sysv/linux/i386/swapcontext.S b/sysdeps/unix/sysv/linux/i386/swapcontext.S
> index 090c2d8c3e..28d057599e 100644
> --- a/sysdeps/unix/sysv/linux/i386/swapcontext.S
> +++ b/sysdeps/unix/sysv/linux/i386/swapcontext.S
> @@ -18,6 +18,7 @@
> <https://www.gnu.org/licenses/>. */
>
> #include <sysdep.h>
> +#include <asm/prctl.h>
>
> #include "ucontext_i.h"
>
> @@ -76,6 +77,144 @@ ENTRY(__swapcontext)
> movl oFS(%eax), %edx
> movw %dx, %fs
>
> +#if SHSTK_ENABLED
OK.
> + /* Check if Shadow Stack is enabled. */
> + testl $X86_FEATURE_1_SHSTK, %gs:FEATURE_1_OFFSET
> + jz L(no_shstk)
> +
> + xorl %eax, %eax
> + cmpl %gs:SSP_BASE_OFFSET, %eax
> + jnz L(shadow_stack_bound_recorded)
> +
> + /* Get the base address and size of the default shadow stack
> + which must be the current shadow stack since nothing has
> + been recorded yet. */
> + sub $24, %esp
> + mov %esp, %ecx
> + movl $ARCH_CET_STATUS, %ebx
> + movl $__NR_arch_prctl, %eax
> + ENTER_KERNEL
> + testl %eax, %eax
> + jz L(continue_no_err)
> +
> + /* This should never happen. */
> + hlt
> +
> +L(continue_no_err):
> + /* Record the base of the current shadow stack. */
> + movl 8(%esp), %eax
> + movl %eax, %gs:SSP_BASE_OFFSET
> + add $24, %esp
> +
> +L(shadow_stack_bound_recorded):
> + /* Load address of the context data structure we save in. */
> + movl 4(%esp), %eax
> +
> + /* Load address of the context data structure we swap in */
> + movl 8(%esp), %edx
> +
> + /* If we unwind the stack, we can't undo stack unwinding. Just
> + save the target shadow stack pointer as the current shadow
> + stack pointer. */
> + movl oSSP(%edx), %ecx
> + movl %ecx, oSSP(%eax)
> +
> + /* Save the current shadow stack base in ucontext. */
> + movl %gs:SSP_BASE_OFFSET, %ecx
> + movl %ecx, (oSSP + 4)(%eax)
> +
> + /* If the base of the target shadow stack is the same as the
> + base of the current shadow stack, we unwind the shadow
> + stack. Otherwise it is a stack switch and we look for a
> + restore token. */
> + movl oSSP(%edx), %esi
> + movl %esi, %edi
> +
> + /* Get the base of the target shadow stack. */
> + movl (oSSP + 4)(%edx), %ecx
> + cmpl %gs:SSP_BASE_OFFSET, %ecx
> + je L(unwind_shadow_stack)
> +
> +L(find_restore_token_loop):
> + /* Align the saved original shadow stack pointer to the next
> + 8 byte aligned boundary. */
> + andl $-8, %esi
> +
> + /* Look for a restore token. */
> + movl -8(%esi), %ebx
> + andl $-8, %ebx
> + cmpl %esi, %ebx
> + je L(restore_shadow_stack)
> +
> + /* Try the next slot. */
> + subl $8, %esi
> + jmp L(find_restore_token_loop)
> +
> +L(restore_shadow_stack):
> + /* The target shadow stack will be restored. Save the current
> + shadow stack pointer. */
> + rdsspd %ecx
> + movl %ecx, oSSP(%eax)
> +
> + /* Use the restore stoken to restore the target shadow stack. */
> + rstorssp -8(%esi)
> +
> + /* Save the restore token on the old shadow stack. NB: This
> + restore token may be checked by setcontext or swapcontext
> + later. */
> + saveprevssp
> +
> + /* Record the new shadow stack base that was switched to. */
> + movl (oSSP + 4)(%edx), %ebx
> + movl %ebx, %gs:SSP_BASE_OFFSET
> +
> +L(unwind_shadow_stack):
> + rdsspd %ebx
> + subl %edi, %ebx
> + je L(skip_unwind_shadow_stack)
> + negl %ebx
> + shrl $2, %ebx
> + movl $255, %esi
> +L(loop):
> + cmpl %esi, %ebx
> + cmovb %ebx, %esi
> + incsspd %esi
> + subl %esi, %ebx
> + ja L(loop)
> +
> +L(skip_unwind_shadow_stack):
> +
> + /* Load the new stack pointer. */
> + movl oESP(%edx), %esp
> +
> + /* Load the values of all the preserved registers (except ESP). */
> + movl oEDI(%edx), %edi
> + movl oESI(%edx), %esi
> + movl oEBP(%edx), %ebp
> + movl oEBX(%edx), %ebx
> +
> + /* Get the return address set with getcontext. */
> + movl oEIP(%edx), %ecx
> +
> + /* Check if return address is valid for the case when setcontext
> + is invoked from L(exitcode) with linked context. */
> + rdsspd %eax
> + cmpl (%eax), %ecx
> + /* Clear EAX to indicate success. NB: Don't use xorl to keep
> + EFLAGS for jne. */
> + movl $0, %eax
> + jne L(jmp)
> + /* Return to the new context if return address valid. */
> + pushl %ecx
> + ret
> +
> +L(jmp):
> + /* Jump to the new context directly. */
> + jmp *%ecx
> +
> +L(no_shstk):
> +#endif
> +
> /* Fetch the address to return to. */
> movl oEIP(%eax), %ecx
>
> diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
> index 4aa7bb496a..420b6a7912 100644
> --- a/sysdeps/unix/sysv/linux/i386/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
> @@ -662,4 +662,9 @@ struct libc_do_syscall_args
> # endif
> #endif
>
> +/* Each shadow stack slot takes 4 bytes. Assuming that each stack
> + frame takes 128 bytes, this is used to compute shadow stack size
> + from stack size. */
> +#define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5
OK.
> +
> #endif /* linux/i386/sysdep.h */
> diff --git a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
> index 1dfe03d2cc..1d8608eafc 100644
> --- a/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
> +++ b/sysdeps/unix/sysv/linux/i386/ucontext_i.sym
> @@ -22,6 +22,10 @@ oEBP mreg (EBP)
> oESP mreg (ESP)
> oEBX mreg (EBX)
> oEIP mreg (EIP)
> +oSCRATCH1 mreg (EAX)
> +oSCRATCH2 mreg (ECX)
> +oSCRATCH3 mreg (EDX)
OK. Just adding a macro.
> oFPREGS mcontext (fpregs)
> oSIGMASK ucontext (uc_sigmask)
> oFPREGSMEM ucontext (__fpregs_mem)
> +oSSP ucontext (__ssp)
OK. Already have __ssp present since 25123a1c5c96429d70e75b85a9749b405909d7f2.
> --
> 2.24.1
>
--
Cheers,
Carlos.
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2020-02-14 22:10 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-08 16:15 V2 [PATCH 0/5] i386: Finish CET support H.J. Lu
2020-01-08 16:15 ` V2 [PATCH 1/5] i386: Don't unnecessarily save and restore EAX, ECX and EDX [BZ# 25262] H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 2/5] i386/sub_n.S: Add a missing _CET_ENDBR to indirect jump target H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 3/5] i386: Add _CET_ENDBR to assembly files without ENTRY H.J. Lu
2020-01-09 21:13 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 4/5] i386: Remove _exit.S H.J. Lu
2020-01-09 21:14 ` Adhemerval Zanella
2020-01-08 16:15 ` V2 [PATCH 5/5] i386: Enable CET support in ucontext functions H.J. Lu
2020-02-01 14:09 ` V3 [PATCH] " H.J. Lu
2020-02-13 17:04 ` V4 " H.J. Lu
2020-02-14 22:10 ` V3 " Carlos O'Donell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).