2003-09-23 12:06:48 +00:00
|
|
|
/* Stack executability handling for GNU dynamic linker. Linux version.
|
2016-01-04 16:05:18 +00:00
|
|
|
Copyright (C) 2003-2016 Free Software Foundation, Inc.
|
2003-09-23 12:06:48 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-09 23:18:22 +00:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2003-09-23 12:06:48 +00:00
|
|
|
|
|
|
|
#include <ldsodefs.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <errno.h>
|
2004-03-27 03:40:33 +00:00
|
|
|
#include <libintl.h>
|
2003-09-23 12:06:48 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stackinfo.h>
|
2004-03-27 03:40:33 +00:00
|
|
|
#include <caller.h>
|
2005-02-08 08:25:22 +00:00
|
|
|
#include <sysdep.h>
|
2003-09-23 12:06:48 +00:00
|
|
|
|
2003-11-27 05:24:58 +00:00
|
|
|
|
2004-09-20 15:08:26 +00:00
|
|
|
extern int __stack_prot attribute_relro attribute_hidden;
|
2004-09-19 02:48:59 +00:00
|
|
|
|
|
|
|
|
2003-09-23 12:06:48 +00:00
|
|
|
int
|
|
|
|
internal_function
|
2004-01-13 15:35:20 +00:00
|
|
|
_dl_make_stack_executable (void **stack_endp)
|
2003-09-23 12:06:48 +00:00
|
|
|
{
|
2004-01-13 20:18:20 +00:00
|
|
|
/* This gives us the highest/lowest page that needs to be changed. */
|
2004-03-27 03:40:33 +00:00
|
|
|
uintptr_t page = ((uintptr_t) *stack_endp
|
2004-03-05 10:29:47 +00:00
|
|
|
& -(intptr_t) GLRO(dl_pagesize));
|
2005-02-08 08:25:22 +00:00
|
|
|
int result = 0;
|
2004-01-13 20:18:20 +00:00
|
|
|
|
2004-01-13 15:35:20 +00:00
|
|
|
/* Challenge the caller. */
|
2004-04-20 18:58:56 +00:00
|
|
|
if (__builtin_expect (__check_caller (RETURN_ADDRESS (0),
|
2004-03-27 03:40:33 +00:00
|
|
|
allow_ldso|allow_libpthread) != 0, 0)
|
|
|
|
|| __builtin_expect (*stack_endp != __libc_stack_end, 0))
|
2004-01-13 15:35:20 +00:00
|
|
|
return EPERM;
|
|
|
|
|
2012-08-07 23:03:35 +00:00
|
|
|
if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
|
|
|
|
__stack_prot) == 0, 1))
|
|
|
|
goto return_success;
|
|
|
|
result = errno;
|
|
|
|
goto out;
|
2003-09-23 12:06:48 +00:00
|
|
|
|
2003-11-27 05:24:58 +00:00
|
|
|
return_success:
|
2004-03-27 03:40:33 +00:00
|
|
|
/* Clear the address. */
|
|
|
|
*stack_endp = NULL;
|
|
|
|
|
2003-09-25 03:31:59 +00:00
|
|
|
/* Remember that we changed the permission. */
|
|
|
|
GL(dl_stack_flags) |= PF_X;
|
|
|
|
|
2005-02-08 08:25:22 +00:00
|
|
|
out:
|
|
|
|
#ifdef check_consistency
|
|
|
|
check_consistency ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
2003-09-23 12:06:48 +00:00
|
|
|
}
|
|
|
|
rtld_hidden_def (_dl_make_stack_executable)
|