chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / sysdeps / unix / sysv / linux / dl-execstack.c
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.
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 #include <sysdep.h>
28
29 #include <kernel-features.h>
30
31
32 extern int __stack_prot attribute_relro attribute_hidden;
33
34
35 int
36 internal_function
37 _dl_make_stack_executable (void **stack_endp)
38 {
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));
42   int result = 0;
43
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))
48     return EPERM;
49
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;
54   if (! no_growsupdown)
55 # endif
56     {
57       if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
58                                         __stack_prot) == 0, 1))
59         goto return_success;
60 # if __ASSUME_PROT_GROWSUPDOWN == 0
61       if (errno == EINVAL)
62         no_growsupdown = true;
63       else
64 # endif
65         {
66           result = errno;
67           goto out;
68         }
69     }
70 #endif
71
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.
75
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.  */
78
79 #if __ASSUME_PROT_GROWSUPDOWN == 0
80   size_t size = GLRO(dl_pagesize) * 8;
81
82 # if _STACK_GROWS_DOWN
83   page = page + GLRO(dl_pagesize) - size;
84   while (1)
85     {
86       if (__mprotect ((void *) page, size,
87                       __stack_prot & ~PROT_GROWSDOWN) == 0)
88         /* We got this chunk changed; loop to do another chunk below.  */
89         page -= size;
90       else
91         {
92           if (errno != ENOMEM)  /* Unexpected failure mode.  */
93             {
94               result = errno;
95               goto out;
96             }
97
98           if (size == GLRO(dl_pagesize))
99             /* We just tried to mprotect the top hole page and failed.
100                We are done.  */
101             break;
102
103           /* Our mprotect call failed because it started below the lowest
104              stack page.  Try again on just the top half of that region.  */
105           size /= 2;
106           page += size;
107         }
108     }
109
110 # elif _STACK_GROWS_UP
111   while (1)
112     {
113       if (__mprotect ((void *) page, size, __stack_prot & ~PROT_GROWSUP) == 0)
114         /* We got this chunk changed; loop to do another chunk below.  */
115         page += size;
116       else
117         {
118           if (errno != ENOMEM)  /* Unexpected failure mode.  */
119             {
120               result = errno;
121               goto out;
122             }
123
124           if (size == GLRO(dl_pagesize))
125             /* We just tried to mprotect the lowest hole page and failed.
126                We are done.  */
127             break;
128
129           /* Our mprotect call failed because it extended past the highest
130              stack page.  Try again on just the bottom half of that region.  */
131           size /= 2;
132         }
133     }
134
135 # else
136 #  error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
137 # endif
138 #endif
139
140  return_success:
141   /* Clear the address.  */
142   *stack_endp = NULL;
143
144   /* Remember that we changed the permission.  */
145   GL(dl_stack_flags) |= PF_X;
146
147  out:
148 #ifdef check_consistency
149   check_consistency ();
150 #endif
151
152   return result;
153 }
154 rtld_hidden_def (_dl_make_stack_executable)