1 /* Stack executability handling for GNU dynamic linker. Linux version.
2 Copyright (C) 2003, 2004, 2005, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <stackinfo.h>
29 #include <kernel-features.h>
32 extern int __stack_prot attribute_relro attribute_hidden
;
37 _dl_make_stack_executable (void **stack_endp
)
39 /* This gives us the highest/lowest page that needs to be changed. */
40 uintptr_t page
= ((uintptr_t) *stack_endp
41 & -(intptr_t) GLRO(dl_pagesize
));
44 /* Challenge the caller. */
45 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0),
46 allow_ldso
|allow_libpthread
) != 0, 0)
47 || __builtin_expect (*stack_endp
!= __libc_stack_end
, 0))
50 /* Newer Linux kernels support a flag to make our job easy. */
51 #if defined PROT_GROWSDOWN || defined PROT_GROWSUP
52 # if __ASSUME_PROT_GROWSUPDOWN == 0
53 static bool no_growsupdown
;
57 if (__builtin_expect (__mprotect ((void *) page
, GLRO(dl_pagesize
),
58 __stack_prot
) == 0, 1))
60 # if __ASSUME_PROT_GROWSUPDOWN == 0
62 no_growsupdown
= true;
72 /* There is always a hole in the address space below the bottom of the
73 stack. So when we make an mprotect call that starts below the bottom
74 of the stack, it will include the hole and fail with ENOMEM.
76 We start with a random guess at how deep the stack might have gotten
77 so as to have extended the GROWSDOWN mapping to lower pages. */
79 #if __ASSUME_PROT_GROWSUPDOWN == 0
80 size_t size
= GLRO(dl_pagesize
) * 8;
82 # if _STACK_GROWS_DOWN
83 page
= page
+ GLRO(dl_pagesize
) - size
;
86 if (__mprotect ((void *) page
, size
,
87 __stack_prot
& ~PROT_GROWSDOWN
) == 0)
88 /* We got this chunk changed; loop to do another chunk below. */
92 if (errno
!= ENOMEM
) /* Unexpected failure mode. */
98 if (size
== GLRO(dl_pagesize
))
99 /* We just tried to mprotect the top hole page and failed.
103 /* Our mprotect call failed because it started below the lowest
104 stack page. Try again on just the top half of that region. */
110 # elif _STACK_GROWS_UP
113 if (__mprotect ((void *) page
, size
, __stack_prot
& ~PROT_GROWSUP
) == 0)
114 /* We got this chunk changed; loop to do another chunk below. */
118 if (errno
!= ENOMEM
) /* Unexpected failure mode. */
124 if (size
== GLRO(dl_pagesize
))
125 /* We just tried to mprotect the lowest hole page and failed.
129 /* Our mprotect call failed because it extended past the highest
130 stack page. Try again on just the bottom half of that region. */
136 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
141 /* Clear the address. */
144 /* Remember that we changed the permission. */
145 GL(dl_stack_flags
) |= PF_X
;
148 #ifdef check_consistency
149 check_consistency ();
154 rtld_hidden_def (_dl_make_stack_executable
)