]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aarch64: Enable GCS in dynamic linked exe
authorSzabolcs Nagy <szabolcs.nagy@arm.com>
Fri, 14 Jul 2023 14:49:11 +0000 (15:49 +0100)
committerYury Khrustalev <yury.khrustalev@arm.com>
Mon, 20 Jan 2025 09:31:47 +0000 (09:31 +0000)
Use the dynamic linker start code to enable GCS in the dynamic linked
case after _dl_start returns and before _dl_start_user which marks
the point after which user code may run.

Like in the static linked case this ensures that GCS is enabled on a
top level stack frame.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
sysdeps/aarch64/Makefile
sysdeps/aarch64/dl-gcs.c [new file with mode: 0644]
sysdeps/aarch64/dl-start.S
sysdeps/aarch64/rtld-global-offsets.sym

index 141d7d9cc2bbbce75f47213c07c44a6e49e78fe6..74479604f2b127f58f8aec28b8e3787ea0f55540 100644 (file)
@@ -9,7 +9,9 @@ LDFLAGS-rtld += -Wl,-z,force-bti,--fatal-warnings
 endif
 
 ifeq ($(subdir),elf)
-sysdep-dl-routines += dl-bti
+sysdep-dl-routines += \
+  dl-bti \
+  dl-gcs
 
 tests += tst-audit26 \
         tst-audit27
@@ -35,7 +37,9 @@ endif
 ifeq ($(subdir),elf)
 sysdep-rtld-routines += dl-start
 sysdep-dl-routines += tlsdesc dl-tlsdesc
-gen-as-const-headers += dl-link.sym
+gen-as-const-headers += \
+  dl-link.sym \
+  rtld-global-offsets.sym
 
 tests-internal += tst-ifunc-arg-1 tst-ifunc-arg-2
 
diff --git a/sysdeps/aarch64/dl-gcs.c b/sysdeps/aarch64/dl-gcs.c
new file mode 100644 (file)
index 0000000..870336f
--- /dev/null
@@ -0,0 +1,25 @@
+/* AArch64 GCS functions.
+   Copyright (C) 2024-2025 Free Software Foundation, Inc.
+
+   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 <ldsodefs.h>
+
+/* Used to report error when prctl system call to enabled GCS fails.  */
+
+void _dl_gcs_enable_failed (int code)
+{
+  _dl_fatal_printf ("failed to enable GCS: %d\n", -code);
+}
index 3b8eff0acdc3467c955f9039c4a84fc00971a1b3..a249fda21c1063a21e4f39dd4463727d0ab72229 100644 (file)
@@ -18,6 +18,7 @@
    <https://www.gnu.org/licenses/>.  */
 
 #include <sysdep.h>
+#include <rtld-global-offsets.h>
 
 ENTRY (_start)
        /* Create an initial frame with 0 LR and FP */
@@ -25,11 +26,33 @@ ENTRY (_start)
        mov     x29, #0
        mov     x30, #0
 
+       /* Load and relocate all library dependencies.  */
        mov     x0, sp
        PTR_ARG (0)
        bl      _dl_start
        /* Returns user entry point in x0.  */
        mov     PTR_REG (21), PTR_REG (0)
+
+       /* Use GL(dl_aarch64_gcs) to set the shadow stack status.  */
+       adrp    x16, _rtld_local
+       add     PTR_REG (16), PTR_REG (16), :lo12:_rtld_local
+       ldr     x1, [x16, GL_DL_AARCH64_GCS_OFFSET]
+       cbz     x1, L(skip_gcs_enable)
+
+       /* Enable GCS before user code runs.  Note that IFUNC resolvers and
+          LD_AUDIT hooks may run before, but should not create threads.  */
+#define PR_SET_SHADOW_STACK_STATUS  75
+#define PR_SHADOW_STACK_ENABLE      (1UL << 0)
+       mov     x0, PR_SET_SHADOW_STACK_STATUS
+       mov     x1, PR_SHADOW_STACK_ENABLE
+       mov     x2, 0
+       mov     x3, 0
+       mov     x4, 0
+       mov     x8, #SYS_ify(prctl)
+       svc     0x0
+       cbnz    w0, L(failed_gcs_enable)
+L(skip_gcs_enable):
+
 .globl _dl_start_user
 .type _dl_start_user, %function
 _dl_start_user:
@@ -40,14 +63,17 @@ _dl_start_user:
        /* Compute envp.  */
        add     PTR_REG (3), PTR_REG (2), PTR_REG (1), lsl PTR_LOG_SIZE
        add     PTR_REG (3), PTR_REG (3), PTR_SIZE
-       adrp    x16, _rtld_local
-       add     PTR_REG (16), PTR_REG (16), :lo12:_rtld_local
+       /* Run the init functions of the loaded modules.  */
        ldr     PTR_REG (0), [x16]
        bl      _dl_init
        /* Load the finalizer function.  */
        adrp    x0, _dl_fini
        add     PTR_REG (0), PTR_REG (0), :lo12:_dl_fini
        /* Jump to the user's entry point.  */
-       mov     x16, x21
-       br      x16
+       mov     x16, x21
+       br      x16
+
+L(failed_gcs_enable):
+       b       _dl_gcs_enable_failed
+
 END (_start)
index 23cdaf7d9e1c78e054c5a347d8314de9341e7799..6c0690bb95fa6847a25fd568de9c900c1ba7e11b 100644 (file)
@@ -3,8 +3,13 @@
 #include <ldsodefs.h>
 
 #define GLRO_offsetof(name) offsetof (struct rtld_global_ro, _##name)
+#define GL_offsetof(name) offsetof (struct rtld_global, _##name)
 
 -- Offsets of _rtld_global_ro in libc.so
 
 GLRO_DL_HWCAP_OFFSET   GLRO_offsetof (dl_hwcap)
 GLRO_DL_HWCAP2_OFFSET  GLRO_offsetof (dl_hwcap2)
+
+-- Offsets of _rtld_global in libc.so
+
+GL_DL_AARCH64_GCS_OFFSET       GL_offsetof (dl_aarch64_gcs)