dl-execstack.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Stack executability handling for GNU dynamic linker. Linux version.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <ldsodefs.h>
  16. #include <sys/mman.h>
  17. #include <errno.h>
  18. #include <libintl.h>
  19. #include <stdbool.h>
  20. #include <stackinfo.h>
  21. #include <sysdep.h>
  22. extern int __stack_prot attribute_relro attribute_hidden;
  23. int
  24. _dl_make_stack_executable (void **stack_endp)
  25. {
  26. /* This gives us the highest/lowest page that needs to be changed. */
  27. uintptr_t page = ((uintptr_t) *stack_endp
  28. & -(intptr_t) GLRO(dl_pagesize));
  29. int result = 0;
  30. if (__builtin_expect (__mprotect ((void *) page, GLRO(dl_pagesize),
  31. __stack_prot) == 0, 1))
  32. goto return_success;
  33. result = errno;
  34. goto out;
  35. return_success:
  36. /* Clear the address. */
  37. *stack_endp = NULL;
  38. /* Remember that we changed the permission. */
  39. GL(dl_stack_flags) |= PF_X;
  40. out:
  41. #ifdef check_consistency
  42. check_consistency ();
  43. #endif
  44. return result;
  45. }
  46. rtld_hidden_def (_dl_make_stack_executable)