at v6.19-rc6 55 lines 1.6 kB view raw
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 7#ifndef _NOLIBC_STACKPROTECTOR_H 8#define _NOLIBC_STACKPROTECTOR_H 9 10#include "compiler.h" 11 12#ifndef NOLIBC_NO_RUNTIME 13#if defined(_NOLIBC_STACKPROTECTOR) 14 15#include "sys.h" 16#include "stdlib.h" 17 18/* The functions in this header are using raw syscall macros to avoid 19 * triggering stack protector errors themselves 20 */ 21 22void __stack_chk_fail(void); 23__attribute__((weak,used,noreturn,section(".text.nolibc_stack_chk"))) 24void __stack_chk_fail(void) 25{ 26 pid_t pid; 27 my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28); 28 pid = my_syscall0(__NR_getpid); 29 my_syscall2(__NR_kill, pid, SIGABRT); 30 for (;;); 31} 32 33void __stack_chk_fail_local(void); 34__attribute__((weak,noreturn,section(".text.nolibc_stack_chk"))) 35void __stack_chk_fail_local(void) 36{ 37 __stack_chk_fail(); 38} 39 40__attribute__((weak,used,section(".data.nolibc_stack_chk"))) 41uintptr_t __stack_chk_guard; 42 43static __no_stack_protector void __stack_chk_init(void) 44{ 45 my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0); 46 /* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */ 47 if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard) 48 __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard; 49} 50#else /* !defined(_NOLIBC_STACKPROTECTOR) */ 51static void __stack_chk_init(void) {} 52#endif /* defined(_NOLIBC_STACKPROTECTOR) */ 53#endif /* NOLIBC_NO_RUNTIME */ 54 55#endif /* _NOLIBC_STACKPROTECTOR_H */