mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

The stackprotector support in nolibc should be enabled iff it is also enabled in the compiler. Use the preprocessor defines added by gcc and clang if stackprotector support is enable to automatically do so in nolibc. This completely removes the need for any user-visible API. To avoid inlining the lengthy preprocessor check into every user introduce a new header compiler.h that abstracts the logic away. As the define NOLIBC_STACKPROTECTOR is now not user-relevant anymore prefix it with an underscore. Suggested-by: Willy Tarreau <w@1wt.eu> Link: https://lore.kernel.org/lkml/20230520133237.GA27501@1wt.eu/ Signed-off-by: Thomas Weißschuh <linux@weissschuh.net> Signed-off-by: Willy Tarreau <w@1wt.eu> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
50 lines
1.4 KiB
C
50 lines
1.4 KiB
C
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
|
/*
|
|
* Stack protector support for NOLIBC
|
|
* Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
|
|
*/
|
|
|
|
#ifndef _NOLIBC_STACKPROTECTOR_H
|
|
#define _NOLIBC_STACKPROTECTOR_H
|
|
|
|
#include "compiler.h"
|
|
|
|
#if defined(_NOLIBC_STACKPROTECTOR)
|
|
|
|
#include "sys.h"
|
|
#include "stdlib.h"
|
|
|
|
/* The functions in this header are using raw syscall macros to avoid
|
|
* triggering stack protector errors themselves
|
|
*/
|
|
|
|
__attribute__((weak,noreturn,section(".text.nolibc_stack_chk")))
|
|
void __stack_chk_fail(void)
|
|
{
|
|
pid_t pid;
|
|
my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28);
|
|
pid = my_syscall0(__NR_getpid);
|
|
my_syscall2(__NR_kill, pid, SIGABRT);
|
|
for (;;);
|
|
}
|
|
|
|
__attribute__((weak,noreturn,section(".text.nolibc_stack_chk")))
|
|
void __stack_chk_fail_local(void)
|
|
{
|
|
__stack_chk_fail();
|
|
}
|
|
|
|
__attribute__((weak,section(".data.nolibc_stack_chk")))
|
|
uintptr_t __stack_chk_guard;
|
|
|
|
__attribute__((weak,no_stack_protector,section(".text.nolibc_stack_chk")))
|
|
void __stack_chk_init(void)
|
|
{
|
|
my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
|
|
/* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */
|
|
if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard)
|
|
__stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
|
|
}
|
|
#endif /* defined(_NOLIBC_STACKPROTECTOR) */
|
|
|
|
#endif /* _NOLIBC_STACKPROTECTOR_H */
|