[PATCH 2/2] nptl: Enable MTE for stacks created for threads.

Cupertino Miranda cupertino.miranda@oracle.com
Fri Mar 14 13:21:42 GMT 2025


This patch adds MPROT_MTE flag to newly mapped stacks for threads, if
DT_AARCH64_MEMTAG_STACK is present in the array of dynamic tags in the
executable.

---
 elf/dl-mte-stack.c                             |  1 +
 nptl/allocatestack.c                           |  3 ++-
 nptl/nptl-stack.c                              |  7 +++++++
 nptl/nptl-stack.h                              |  2 ++
 sysdeps/unix/sysv/linux/aarch64/Makefile       |  5 +++++
 sysdeps/unix/sysv/linux/aarch64/dl-mte-stack.c | 11 ++++++++++-
 6 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/elf/dl-mte-stack.c b/elf/dl-mte-stack.c
index f0063b54a0..f5b8293c7a 100644
--- a/elf/dl-mte-stack.c
+++ b/elf/dl-mte-stack.c
@@ -25,3 +25,4 @@ _dl_mte_stack_protect (void *stack_end)
   return ENOSYS;
 }
 rtld_hidden_def (_dl_mte_stack_protect)
+
diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
index 800ca89720..6155157063 100644
--- a/nptl/allocatestack.c
+++ b/nptl/allocatestack.c
@@ -153,7 +153,8 @@ static int allocate_stack_mode = ALLOCATE_GUARD_MADV_GUARD;
 static inline int stack_prot (void)
 {
   return (PROT_READ | PROT_WRITE
-	  | ((GL(dl_stack_flags) & PF_X) ? PROT_EXEC : 0));
+	  | ((GL(dl_stack_flags) & PF_X) ? PROT_EXEC : 0)
+	  | stack_mem_tagging_prot ());
 }
 
 static void *
diff --git a/nptl/nptl-stack.c b/nptl/nptl-stack.c
index c049c5133c..34fbba1cd1 100644
--- a/nptl/nptl-stack.c
+++ b/nptl/nptl-stack.c
@@ -145,3 +145,10 @@ __pthread_get_minstack (const pthread_attr_t *attr)
 	  + PTHREAD_STACK_MIN);
 }
 libc_hidden_def (__pthread_get_minstack)
+
+int
+__stack_mem_tagging_prot (void)
+{
+  return 0;
+}
+weak_alias (__stack_mem_tagging_prot, stack_mem_tagging_prot)
diff --git a/nptl/nptl-stack.h b/nptl/nptl-stack.h
index ac672e16cf..2bfcaf7252 100644
--- a/nptl/nptl-stack.h
+++ b/nptl/nptl-stack.h
@@ -61,4 +61,6 @@ __nptl_tls_static_size_for_stack (void)
   return roundup (GLRO (dl_tls_static_size), GLRO (dl_tls_static_align));
 }
 
+int stack_mem_tagging_prot (void);
+
 #endif /* _NPTL_STACK_H */
diff --git a/sysdeps/unix/sysv/linux/aarch64/Makefile b/sysdeps/unix/sysv/linux/aarch64/Makefile
index 8fb0e65b2a..e91a756836 100644
--- a/sysdeps/unix/sysv/linux/aarch64/Makefile
+++ b/sysdeps/unix/sysv/linux/aarch64/Makefile
@@ -10,6 +10,11 @@ sysdep-dl-routines += \
   dl-mte-stack
 endif
 
+ifeq ($(subdir),nptl)
+routines += \
+  dl-mte-stack
+endif
+
 ifeq ($(subdir),stdlib)
 gen-as-const-headers += ucontext_i.sym
 endif
diff --git a/sysdeps/unix/sysv/linux/aarch64/dl-mte-stack.c b/sysdeps/unix/sysv/linux/aarch64/dl-mte-stack.c
index a4bc82c99b..7ed92855ae 100644
--- a/sysdeps/unix/sysv/linux/aarch64/dl-mte-stack.c
+++ b/sysdeps/unix/sysv/linux/aarch64/dl-mte-stack.c
@@ -42,7 +42,7 @@ void
 _dl_mte_stack_protect (void *stack_end)
 {
   if ((GLRO(dl_aarch64_cpu_features).mte_state & AARCH64_CPU_FEATURE_MTE_STATE_STACK) == 0
-      ||  !_dl_mte_mode ())
+      || !_dl_mte_mode ())
     return;
 
   int errval = 0;
@@ -72,3 +72,12 @@ cannot set stack with PROT_MTE");
     }
 }
 
+int
+stack_mem_tagging_prot (void)
+{
+  if ((GLRO(dl_aarch64_cpu_features).mte_state & AARCH64_CPU_FEATURE_MTE_STATE_STACK) == 8
+      && _dl_mte_mode ())
+    return PROT_MTE;
+  else
+    return 0;
+}
-- 
2.39.5



More information about the Libc-alpha mailing list