[v4,12/17] riscv/cfi: Store shadow stack information in TLS

Message ID 20260526061703.2188042-13-jesse.huang@sifive.com (mailing list archive)
State New
Headers
Series Support RISC-V Control Flow Integrifty (CFI) |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Jesse Huang May 26, 2026, 6:16 a.m. UTC
  Switching between different ucontexts involves two cases, one is both
sharing a same shadow stack, which requires a unwinding, and the other
is both using a different shadow stack, which require a stack switch
using shadow stack restore token.

By storing shadow stack base in TLS and compare to the one stored in the
context, we can tell the difference and perform the right action.
---
 sysdeps/riscv/nptl/Makefile        | 1 +
 sysdeps/riscv/nptl/tcb-offsets.sym | 5 +++++
 sysdeps/riscv/nptl/tls.h           | 2 ++
 3 files changed, 8 insertions(+)
 create mode 100644 sysdeps/riscv/nptl/Makefile
 create mode 100644 sysdeps/riscv/nptl/tcb-offsets.sym
  

Patch

diff --git a/sysdeps/riscv/nptl/Makefile b/sysdeps/riscv/nptl/Makefile
new file mode 100644
index 0000000000..2b7bf43403
--- /dev/null
+++ b/sysdeps/riscv/nptl/Makefile
@@ -0,0 +1 @@ 
+gen-as-const-headers += tcb-offsets.sym
diff --git a/sysdeps/riscv/nptl/tcb-offsets.sym b/sysdeps/riscv/nptl/tcb-offsets.sym
new file mode 100644
index 0000000000..5257acccec
--- /dev/null
+++ b/sysdeps/riscv/nptl/tcb-offsets.sym
@@ -0,0 +1,5 @@ 
+#include <sysdep.h>
+#include <tls.h>
+#include <kernel-features.h>
+
+TLS_SSP_BASE_OFFSET     (offsetof (tcbhead_t, ssp_base) - sizeof (tcbhead_t))
diff --git a/sysdeps/riscv/nptl/tls.h b/sysdeps/riscv/nptl/tls.h
index b2e0f3c249..f618b32b54 100644
--- a/sysdeps/riscv/nptl/tls.h
+++ b/sysdeps/riscv/nptl/tls.h
@@ -44,6 +44,8 @@  typedef struct
 {
   dtv_t *dtv;
   void *private;
+  /* The marker for the current shadow stack.  */
+  unsigned long long int ssp_base;
 } tcbhead_t;
 
 /* This is the size of the initial TCB.  Because our TCB is before the thread