1*e4b17023SJohn Marino /* Implement __enable_execute_stack using mprotect(2).
2*e4b17023SJohn Marino Copyright (C) 2011, 2012 Free Software Foundation, Inc.
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino This file is part of GCC.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
7*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
8*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*e4b17023SJohn Marino for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino #include <sys/mman.h>
26*e4b17023SJohn Marino #include <unistd.h>
27*e4b17023SJohn Marino #include <stdlib.h>
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino #define STACK_PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
30*e4b17023SJohn Marino
31*e4b17023SJohn Marino static int need_enable_exec_stack;
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino static void check_enabling (void) __attribute__ ((unused));
34*e4b17023SJohn Marino extern void __enable_execute_stack (void *);
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino #if defined __FreeBSD__
37*e4b17023SJohn Marino #include <sys/sysctl.h>
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino static void __attribute__ ((constructor))
check_enabling(void)40*e4b17023SJohn Marino check_enabling (void)
41*e4b17023SJohn Marino {
42*e4b17023SJohn Marino int prot = 0;
43*e4b17023SJohn Marino size_t len = sizeof (prot);
44*e4b17023SJohn Marino
45*e4b17023SJohn Marino sysctlbyname ("kern.stackprot", &prot, &len, NULL, 0);
46*e4b17023SJohn Marino if (prot != STACK_PROT_RWX)
47*e4b17023SJohn Marino need_enable_exec_stack = 1;
48*e4b17023SJohn Marino }
49*e4b17023SJohn Marino #elif defined __sun__ && defined __svr4__
50*e4b17023SJohn Marino static void __attribute__ ((constructor))
check_enabling(void)51*e4b17023SJohn Marino check_enabling (void)
52*e4b17023SJohn Marino {
53*e4b17023SJohn Marino int prot = (int) sysconf (_SC_STACK_PROT);
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino if (prot != STACK_PROT_RWX)
56*e4b17023SJohn Marino need_enable_exec_stack = 1;
57*e4b17023SJohn Marino }
58*e4b17023SJohn Marino #else
59*e4b17023SJohn Marino /* There is no way to query the execute permission of the stack, so
60*e4b17023SJohn Marino we always issue the mprotect() call. */
61*e4b17023SJohn Marino
62*e4b17023SJohn Marino static int need_enable_exec_stack = 1;
63*e4b17023SJohn Marino #endif
64*e4b17023SJohn Marino
65*e4b17023SJohn Marino /* Attempt to turn on access permissions for the stack. Unfortunately it
66*e4b17023SJohn Marino is not possible to make this namespace-clean.*/
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino void
__enable_execute_stack(void * addr)69*e4b17023SJohn Marino __enable_execute_stack (void *addr)
70*e4b17023SJohn Marino {
71*e4b17023SJohn Marino if (!need_enable_exec_stack)
72*e4b17023SJohn Marino return;
73*e4b17023SJohn Marino else
74*e4b17023SJohn Marino {
75*e4b17023SJohn Marino static long size, mask;
76*e4b17023SJohn Marino
77*e4b17023SJohn Marino if (size == 0) {
78*e4b17023SJohn Marino size = getpagesize ();
79*e4b17023SJohn Marino mask = ~(size - 1);
80*e4b17023SJohn Marino }
81*e4b17023SJohn Marino
82*e4b17023SJohn Marino char *page = (char *) (((long) addr) & mask);
83*e4b17023SJohn Marino char *end = (char *)
84*e4b17023SJohn Marino ((((long) (addr + __LIBGCC_TRAMPOLINE_SIZE__)) & mask) + size);
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino if (mprotect (page, end - page, STACK_PROT_RWX) < 0)
87*e4b17023SJohn Marino /* Note that no errors should be emitted by this code; it is
88*e4b17023SJohn Marino considered dangerous for library calls to send messages to
89*e4b17023SJohn Marino stdout/stderr. */
90*e4b17023SJohn Marino abort ();
91*e4b17023SJohn Marino }
92*e4b17023SJohn Marino }
93