stackprotector.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
  2. /*
  3. * Stack protector support for NOLIBC
  4. * Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
  5. */
  6. #ifndef _NOLIBC_STACKPROTECTOR_H
  7. #define _NOLIBC_STACKPROTECTOR_H
  8. #include "compiler.h"
  9. #if defined(_NOLIBC_STACKPROTECTOR)
  10. #include "sys.h"
  11. #include "stdlib.h"
  12. /* The functions in this header are using raw syscall macros to avoid
  13. * triggering stack protector errors themselves
  14. */
  15. __attribute__((weak,used,noreturn,section(".text.nolibc_stack_chk")))
  16. void __stack_chk_fail(void)
  17. {
  18. pid_t pid;
  19. my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28);
  20. pid = my_syscall0(__NR_getpid);
  21. my_syscall2(__NR_kill, pid, SIGABRT);
  22. for (;;);
  23. }
  24. __attribute__((weak,noreturn,section(".text.nolibc_stack_chk")))
  25. void __stack_chk_fail_local(void)
  26. {
  27. __stack_chk_fail();
  28. }
  29. __attribute__((weak,used,section(".data.nolibc_stack_chk")))
  30. uintptr_t __stack_chk_guard;
  31. static __no_stack_protector void __stack_chk_init(void)
  32. {
  33. my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
  34. /* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */
  35. if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard)
  36. __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
  37. }
  38. #else /* !defined(_NOLIBC_STACKPROTECTOR) */
  39. static void __stack_chk_init(void) {}
  40. #endif /* defined(_NOLIBC_STACKPROTECTOR) */
  41. #endif /* _NOLIBC_STACKPROTECTOR_H */