Skip to content

Commit a45728f

Browse files
tangyoulingchenhuacai
authored andcommitted
LoongArch: Enable HAVE_ARCH_STACKLEAK
Add support for the stackleak feature. It initializes the stack with the poison value before returning from system calls which improves the kernel security. At the same time, disables the plugin in EFI stub code because EFI stub is out of scope for the protection. Tested on Loongson-3A5000 (enable GCC_PLUGIN_STACKLEAK and LKDTM): # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT # dmesg lkdtm: Performing direct entry STACKLEAK_ERASING lkdtm: stackleak stack usage: high offset: 320 bytes current: 448 bytes lowest: 1264 bytes tracked: 1264 bytes untracked: 208 bytes poisoned: 14528 bytes low offset: 64 bytes lkdtm: OK: the rest of the thread stack is properly erased Signed-off-by: Youling Tang <tangyouling@kylinos.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent b37981c commit a45728f

File tree

6 files changed

+17
-8
lines changed

6 files changed

+17
-8
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ config LOONGARCH
124124
select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
125125
select HAVE_ARCH_SECCOMP
126126
select HAVE_ARCH_SECCOMP_FILTER
127+
select HAVE_ARCH_STACKLEAK
127128
select HAVE_ARCH_TRACEHOOK
128129
select HAVE_ARCH_TRANSPARENT_HUGEPAGE
129130
select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD

arch/loongarch/include/asm/entry-common.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
#ifndef ARCH_LOONGARCH_ENTRY_COMMON_H
33
#define ARCH_LOONGARCH_ENTRY_COMMON_H
44

5-
#include <linux/sched.h>
6-
#include <linux/processor.h>
7-
8-
static inline bool on_thread_stack(void)
9-
{
10-
return !(((unsigned long)(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE - 1));
11-
}
5+
#include <asm/stacktrace.h> /* For on_thread_stack() */
126

137
#endif

arch/loongarch/include/asm/stackframe.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
jirl zero, \temp1, 0xc
5858
.endm
5959

60+
.macro STACKLEAK_ERASE
61+
#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
62+
bl stackleak_erase_on_task_stack
63+
#endif
64+
.endm
65+
6066
.macro BACKUP_T0T1
6167
csrwr t0, EXCEPTION_KS0
6268
csrwr t1, EXCEPTION_KS1

arch/loongarch/include/asm/stacktrace.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ bool in_irq_stack(unsigned long stack, struct stack_info *info);
3131
bool in_task_stack(unsigned long stack, struct task_struct *task, struct stack_info *info);
3232
int get_stack_info(unsigned long stack, struct task_struct *task, struct stack_info *info);
3333

34+
static __always_inline bool on_thread_stack(void)
35+
{
36+
return !(((unsigned long)(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE - 1));
37+
}
38+
3439
#define STR_LONG_L __stringify(LONG_L)
3540
#define STR_LONG_S __stringify(LONG_S)
3641
#define STR_LONGSIZE __stringify(LONGSIZE)

arch/loongarch/kernel/entry.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ SYM_CODE_START(handle_syscall)
7373
move a0, sp
7474
bl do_syscall
7575

76+
STACKLEAK_ERASE
7677
RESTORE_ALL_AND_RET
7778
SYM_CODE_END(handle_syscall)
7879
_ASM_NOKPROBE(handle_syscall)
@@ -81,6 +82,7 @@ SYM_CODE_START(ret_from_fork_asm)
8182
UNWIND_HINT_REGS
8283
move a1, sp
8384
bl ret_from_fork
85+
STACKLEAK_ERASE
8486
RESTORE_STATIC
8587
RESTORE_SOME
8688
RESTORE_SP_AND_RET
@@ -92,6 +94,7 @@ SYM_CODE_START(ret_from_kernel_thread_asm)
9294
move a2, s0
9395
move a3, s1
9496
bl ret_from_kernel_thread
97+
STACKLEAK_ERASE
9598
RESTORE_STATIC
9699
RESTORE_SOME
97100
RESTORE_SP_AND_RET

drivers/firmware/efi/libstub/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
3131
$(DISABLE_STACKLEAK_PLUGIN)
3232
cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax \
3333
$(DISABLE_STACKLEAK_PLUGIN)
34-
cflags-$(CONFIG_LOONGARCH) += -fpie
34+
cflags-$(CONFIG_LOONGARCH) += -fpie $(DISABLE_STACKLEAK_PLUGIN)
3535

3636
cflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt
3737

0 commit comments

Comments
 (0)