xref: /netbsd-src/external/gpl3/gcc/dist/libgcc/enable-execute-stack-mprotect.c (revision b1e838363e3c6fc78a55519254d99869742dd33c)
148fb7bfaSmrg /* Implement __enable_execute_stack using mprotect(2).
2*b1e83836Smrg    Copyright (C) 2011-2022 Free Software Foundation, Inc.
348fb7bfaSmrg 
448fb7bfaSmrg    This file is part of GCC.
548fb7bfaSmrg 
648fb7bfaSmrg    GCC is free software; you can redistribute it and/or modify it under
748fb7bfaSmrg    the terms of the GNU General Public License as published by the Free
848fb7bfaSmrg    Software Foundation; either version 3, or (at your option) any later
948fb7bfaSmrg    version.
1048fb7bfaSmrg 
1148fb7bfaSmrg    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
1248fb7bfaSmrg    WARRANTY; without even the implied warranty of MERCHANTABILITY or
1348fb7bfaSmrg    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1448fb7bfaSmrg    for more details.
1548fb7bfaSmrg 
1648fb7bfaSmrg    Under Section 7 of GPL version 3, you are granted additional
1748fb7bfaSmrg    permissions described in the GCC Runtime Library Exception, version
1848fb7bfaSmrg    3.1, as published by the Free Software Foundation.
1948fb7bfaSmrg 
2048fb7bfaSmrg    You should have received a copy of the GNU General Public License and
2148fb7bfaSmrg    a copy of the GCC Runtime Library Exception along with this program;
2248fb7bfaSmrg    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2348fb7bfaSmrg    <http://www.gnu.org/licenses/>.  */
2448fb7bfaSmrg 
2548fb7bfaSmrg #include <sys/mman.h>
2648fb7bfaSmrg #include <unistd.h>
2748fb7bfaSmrg #include <stdlib.h>
2848fb7bfaSmrg 
2948fb7bfaSmrg #define STACK_PROT_RWX (PROT_READ | PROT_WRITE | PROT_EXEC)
3048fb7bfaSmrg 
3148fb7bfaSmrg static int need_enable_exec_stack;
3248fb7bfaSmrg 
3348fb7bfaSmrg static void check_enabling (void) __attribute__ ((unused));
3448fb7bfaSmrg extern void __enable_execute_stack (void *);
3548fb7bfaSmrg 
364d5abbe8Smrg #if defined __sun__ && defined __svr4__
3748fb7bfaSmrg static void __attribute__ ((constructor))
check_enabling(void)3848fb7bfaSmrg check_enabling (void)
3948fb7bfaSmrg {
4048fb7bfaSmrg   int prot = (int) sysconf (_SC_STACK_PROT);
4148fb7bfaSmrg 
4248fb7bfaSmrg   if (prot != STACK_PROT_RWX)
4348fb7bfaSmrg     need_enable_exec_stack = 1;
4448fb7bfaSmrg }
4548fb7bfaSmrg #else
4648fb7bfaSmrg /* There is no way to query the execute permission of the stack, so
4748fb7bfaSmrg    we always issue the mprotect() call.  */
4848fb7bfaSmrg 
4948fb7bfaSmrg static int need_enable_exec_stack = 1;
5048fb7bfaSmrg #endif
5148fb7bfaSmrg 
5248fb7bfaSmrg /* Attempt to turn on access permissions for the stack.  Unfortunately it
5348fb7bfaSmrg    is not possible to make this namespace-clean.*/
5448fb7bfaSmrg 
5548fb7bfaSmrg void
__enable_execute_stack(void * addr)5648fb7bfaSmrg __enable_execute_stack (void *addr)
5748fb7bfaSmrg {
5848fb7bfaSmrg   if (!need_enable_exec_stack)
5948fb7bfaSmrg     return;
6048fb7bfaSmrg   else
6148fb7bfaSmrg     {
6248fb7bfaSmrg       static long size, mask;
6348fb7bfaSmrg 
6448fb7bfaSmrg       if (size == 0) {
6548fb7bfaSmrg 	size = getpagesize ();
6648fb7bfaSmrg 	mask = ~(size - 1);
6748fb7bfaSmrg       }
6848fb7bfaSmrg 
6948fb7bfaSmrg       char *page = (char *) (((long) addr) & mask);
7048fb7bfaSmrg       char *end  = (char *)
7148fb7bfaSmrg 	((((long) (addr + __LIBGCC_TRAMPOLINE_SIZE__)) & mask) + size);
7248fb7bfaSmrg 
7348fb7bfaSmrg       if (mprotect (page, end - page, STACK_PROT_RWX) < 0)
7448fb7bfaSmrg 	/* Note that no errors should be emitted by this code; it is
7548fb7bfaSmrg 	   considered dangerous for library calls to send messages to
7648fb7bfaSmrg 	   stdout/stderr.  */
7748fb7bfaSmrg 	abort ();
7848fb7bfaSmrg     }
7948fb7bfaSmrg }
80