]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/dl-execstack.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / dl-execstack.c
1 /* Stack executability handling for GNU dynamic linker. Linux version.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <ldsodefs.h>
21 #include <sys/mman.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdbool.h>
25 #include <stackinfo.h>
26 #include <caller.h>
27
28 #include "kernel-features.h"
29
30
31 extern int __stack_prot attribute_relro attribute_hidden;
32
33
34 int
35 internal_function
36 _dl_make_stack_executable (void **stack_endp)
37 {
38 /* This gives us the highest/lowest page that needs to be changed. */
39 uintptr_t page = ((uintptr_t) *stack_endp
40 & -(intptr_t) GLRO(dl_pagesize));
41
42 /* Challenge the caller. */
43 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0),
44 allow_ldso|allow_libpthread) != 0, 0)
45 || __builtin_expect (*stack_endp != __libc_stack_end, 0))
46 return EPERM;
47
48 /* Newer Linux kernels support a flag to make our job easy. */
49 #if defined PROT_GROWSDOWN || defined PROT_GROWSUP
50 # if __ASSUME_PROT_GROWSUPDOWN == 0
51 static bool no_growsupdown;
52 if (! no_growsupdown)
53 # endif
54 {
55 if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
56 __stack_prot) == 0, 1))
57 goto return_success;
58 # if __ASSUME_PROT_GROWSUPDOWN == 0
59 if (errno == EINVAL)
60 no_growsupdown = true;
61 else
62 # endif
63 return errno;
64 }
65 #endif
66
67 /* There is always a hole in the address space below the bottom of the
68 stack. So when we make an mprotect call that starts below the bottom
69 of the stack, it will include the hole and fail with ENOMEM.
70
71 We start with a random guess at how deep the stack might have gotten
72 so as to have extended the GROWSDOWN mapping to lower pages. */
73
74 #if __ASSUME_PROT_GROWSUPDOWN == 0
75 size_t size = GLRO(dl_pagesize) * 8;
76
77 # if _STACK_GROWS_DOWN
78 page = page + GLRO(dl_pagesize) - size;
79 while (1)
80 {
81 if (__mprotect ((void *) page, size,
82 __stack_prot & ~PROT_GROWSDOWN) == 0)
83 /* We got this chunk changed; loop to do another chunk below. */
84 page -= size;
85 else
86 {
87 if (errno != ENOMEM) /* Unexpected failure mode. */
88 return errno;
89
90 if (size == GLRO(dl_pagesize))
91 /* We just tried to mprotect the top hole page and failed.
92 We are done. */
93 break;
94
95 /* Our mprotect call failed because it started below the lowest
96 stack page. Try again on just the top half of that region. */
97 size /= 2;
98 page += size;
99 }
100 }
101
102 # elif _STACK_GROWS_UP
103 while (1)
104 {
105 if (__mprotect ((void *) page, size, __stack_prot & ~PROT_GROWSUP) == 0)
106 /* We got this chunk changed; loop to do another chunk below. */
107 page += size;
108 else
109 {
110 if (errno != ENOMEM) /* Unexpected failure mode. */
111 return errno;
112
113 if (size == GLRO(dl_pagesize))
114 /* We just tried to mprotect the lowest hole page and failed.
115 We are done. */
116 break;
117
118 /* Our mprotect call failed because it extended past the highest
119 stack page. Try again on just the bottom half of that region. */
120 size /= 2;
121 }
122 }
123
124 # else
125 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
126 # endif
127 #endif
128
129 return_success:
130 /* Clear the address. */
131 *stack_endp = NULL;
132
133 /* Remember that we changed the permission. */
134 GL(dl_stack_flags) |= PF_X;
135
136 return 0;
137 }
138 rtld_hidden_def (_dl_make_stack_executable)