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.
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>
28 #include "kernel-features.h"
31 extern int __stack_prot attribute_relro attribute_hidden
;
36 _dl_make_stack_executable (void **stack_endp
)
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
));
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))
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
;
55 if (__builtin_expect (__mprotect ((void *) page
, GLRO(dl_pagesize
),
56 __stack_prot
) == 0, 1))
58 # if __ASSUME_PROT_GROWSUPDOWN == 0
60 no_growsupdown
= true;
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.
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. */
74 #if __ASSUME_PROT_GROWSUPDOWN == 0
75 size_t size
= GLRO(dl_pagesize
) * 8;
77 # if _STACK_GROWS_DOWN
78 page
= page
+ GLRO(dl_pagesize
) - size
;
81 if (__mprotect ((void *) page
, size
,
82 __stack_prot
& ~PROT_GROWSDOWN
) == 0)
83 /* We got this chunk changed; loop to do another chunk below. */
87 if (errno
!= ENOMEM
) /* Unexpected failure mode. */
90 if (size
== GLRO(dl_pagesize
))
91 /* We just tried to mprotect the top hole page and failed.
95 /* Our mprotect call failed because it started below the lowest
96 stack page. Try again on just the top half of that region. */
102 # elif _STACK_GROWS_UP
105 if (__mprotect ((void *) page
, size
, __stack_prot
& ~PROT_GROWSUP
) == 0)
106 /* We got this chunk changed; loop to do another chunk below. */
110 if (errno
!= ENOMEM
) /* Unexpected failure mode. */
113 if (size
== GLRO(dl_pagesize
))
114 /* We just tried to mprotect the lowest hole page and failed.
118 /* Our mprotect call failed because it extended past the highest
119 stack page. Try again on just the bottom half of that region. */
125 # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
130 /* Clear the address. */
133 /* Remember that we changed the permission. */
134 GL(dl_stack_flags
) |= PF_X
;
138 rtld_hidden_def (_dl_make_stack_executable
)