x86: In ld.so, diagnose missing APX support in APX-only builds
[glibc.git] / sysdeps / unix / sysv / linux / dl-execstack.c
blob3d8f3938daaeab8b1cef3f809e0a01e4906c0bfc
1 /* Stack executability handling for GNU dynamic linker. Linux version.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <ldsodefs.h>
21 #include <libintl.h>
22 #include <list.h>
23 #include <pthreadP.h>
24 #include <stackinfo.h>
25 #include <stdbool.h>
26 #include <sys/mman.h>
27 #include <sysdep.h>
28 #include <unistd.h>
30 extern int __stack_prot attribute_relro attribute_hidden;
32 static int
33 make_main_stack_executable (void **stack_endp)
35 /* This gives us the highest/lowest page that needs to be changed. */
36 uintptr_t page = ((uintptr_t) *stack_endp
37 & -(intptr_t) GLRO(dl_pagesize));
38 int result = 0;
40 if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
41 __stack_prot) == 0, 1))
42 goto return_success;
43 result = errno;
44 goto out;
46 return_success:
47 /* Clear the address. */
48 *stack_endp = NULL;
50 /* Remember that we changed the permission. */
51 GL(dl_stack_flags) |= PF_X;
53 out:
54 #ifdef check_consistency
55 check_consistency ();
56 #endif
58 return result;
61 int
62 _dl_make_stacks_executable (void **stack_endp)
64 /* First the main thread's stack. */
65 int err = make_main_stack_executable (stack_endp);
66 if (err != 0)
67 return err;
69 lll_lock (GL (dl_stack_cache_lock), LLL_PRIVATE);
71 list_t *runp;
72 list_for_each (runp, &GL (dl_stack_used))
74 err = __nptl_change_stack_perm (list_entry (runp, struct pthread, list));
75 if (err != 0)
76 break;
79 /* Also change the permission for the currently unused stacks. This
80 might be wasted time but better spend it here than adding a check
81 in the fast path. */
82 if (err == 0)
83 list_for_each (runp, &GL (dl_stack_cache))
85 err = __nptl_change_stack_perm (list_entry (runp, struct pthread,
86 list));
87 if (err != 0)
88 break;
91 lll_unlock (GL (dl_stack_cache_lock), LLL_PRIVATE);
93 return err;
96 int
97 __nptl_change_stack_perm (struct pthread *pd)
99 #if _STACK_GROWS_DOWN
100 void *stack = pd->stackblock + pd->guardsize;
101 size_t len = pd->stackblock_size - pd->guardsize;
102 #elif _STACK_GROWS_UP
103 void *stack = pd->stackblock;
104 size_t len = (uintptr_t) pd - pd->guardsize - (uintptr_t) pd->stackblock;
105 #else
106 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
107 #endif
108 if (__mprotect (stack, len, PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
109 return errno;
111 return 0;
113 rtld_hidden_def (__nptl_change_stack_perm)