diff options
author | Huacai Chen <chenhuacai@loongson.cn> | 2022-12-10 22:40:15 +0800 |
---|---|---|
committer | Huacai Chen <chenhuacai@loongson.cn> | 2022-12-14 08:41:53 +0800 |
commit | 09f33601bf940f955c10a6e75a1c1b7bcadee5e2 (patch) | |
tree | 423542be6f130680ebf31b6f679af26cb764775a /arch/loongarch/include | |
parent | 7db54bfe44a662c8f2c10277bccfa02c2f4c719c (diff) | |
download | lwn-09f33601bf940f955c10a6e75a1c1b7bcadee5e2.tar.gz lwn-09f33601bf940f955c10a6e75a1c1b7bcadee5e2.zip |
LoongArch: Add basic STACKPROTECTOR support
Add basic stack protector support similar to other architectures. A
constant canary value is set at boot time, and with help of compiler's
-fstack-protector we can detect stack corruption.
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Diffstat (limited to 'arch/loongarch/include')
-rw-r--r-- | arch/loongarch/include/asm/stackprotector.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/arch/loongarch/include/asm/stackprotector.h b/arch/loongarch/include/asm/stackprotector.h new file mode 100644 index 000000000000..a1a965751a7b --- /dev/null +++ b/arch/loongarch/include/asm/stackprotector.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * GCC stack protector support. + * + * Stack protector works by putting predefined pattern at the start of + * the stack frame and verifying that it hasn't been overwritten when + * returning from the function. The pattern is called stack canary and + * on LoongArch gcc expects it to be defined by a global variable called + * "__stack_chk_guard". + */ + +#ifndef _ASM_STACKPROTECTOR_H +#define _ASM_STACKPROTECTOR_H + +#include <linux/random.h> +#include <linux/version.h> + +extern unsigned long __stack_chk_guard; + +/* + * Initialize the stackprotector canary value. + * + * NOTE: this must only be called from functions that never return, + * and it must always be inlined. + */ +static __always_inline void boot_init_stack_canary(void) +{ + unsigned long canary; + + /* Try to get a semi random initial value. */ + get_random_bytes(&canary, sizeof(canary)); + canary ^= LINUX_VERSION_CODE; + + current->stack_canary = canary; + __stack_chk_guard = current->stack_canary; +} + +#endif /* _ASM_STACKPROTECTOR_H */ |