-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/nolibc: add support for stack protector
This is useful when using nolibc for security-critical tools. Using nolibc has the advantage that the code is easily auditable and sandboxable with seccomp as no unexpected syscalls are used. Using compiler-assistent stack protection provides another security mechanism. For this to work the compiler and libc have to collaborate. This patch adds the following parts to nolibc that are required by the compiler: * __stack_chk_guard: random sentinel value * __stack_chk_fail: handler for detected stack smashes In addition an initialization function is added that randomizes the sentinel value. Only support for global guards is implemented. Register guards are useful in multi-threaded context which nolibc does not provide support for. Link: https://lwn.net/Articles/584225/ 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>
- Loading branch information
Thomas Weißschuh
authored and
Paul E. McKenney
committed
Mar 27, 2023
1 parent
c4560bd
commit 7188d46
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* 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 "arch.h" | ||
|
||
#if defined(NOLIBC_STACKPROTECTOR) | ||
|
||
#if !defined(__ARCH_SUPPORTS_STACK_PROTECTOR) | ||
#error "nolibc does not support stack protectors on this arch" | ||
#endif | ||
|
||
#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 */ | ||
__stack_chk_guard ^= (uintptr_t) &__stack_chk_guard; | ||
} | ||
#endif // defined(NOLIBC_STACKPROTECTOR) | ||
|
||
#endif // _NOLIBC_STACKPROTECTOR_H |