at v6.4 1.4 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 "arch.h" 11 12#if defined(NOLIBC_STACKPROTECTOR) 13 14#if !defined(__ARCH_SUPPORTS_STACK_PROTECTOR) 15#error "nolibc does not support stack protectors on this arch" 16#endif 17 18#include "sys.h" 19#include "stdlib.h" 20 21/* The functions in this header are using raw syscall macros to avoid 22 * triggering stack protector errors themselves 23 */ 24 25__attribute__((weak,noreturn,section(".text.nolibc_stack_chk"))) 26void __stack_chk_fail(void) 27{ 28 pid_t pid; 29 my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28); 30 pid = my_syscall0(__NR_getpid); 31 my_syscall2(__NR_kill, pid, SIGABRT); 32 for (;;); 33} 34 35__attribute__((weak,noreturn,section(".text.nolibc_stack_chk"))) 36void __stack_chk_fail_local(void) 37{ 38 __stack_chk_fail(); 39} 40 41__attribute__((weak,section(".data.nolibc_stack_chk"))) 42uintptr_t __stack_chk_guard; 43 44__attribute__((weak,no_stack_protector,section(".text.nolibc_stack_chk"))) 45void __stack_chk_init(void) 46{ 47 my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0); 48 /* a bit more randomness in case getrandom() fails */ 49 __stack_chk_guard ^= (uintptr_t) &__stack_chk_guard; 50} 51#endif // defined(NOLIBC_STACKPROTECTOR) 52 53#endif // _NOLIBC_STACKPROTECTOR_H