generic_entry: Add stackleak support
Subsystems (5)
RFC PATCH 1/1 generic_entry: Add stackleak support
From: Guo Ren <[email protected]>
Make generic_entry supports basic STACKLEAK, and no arch custom code is needed.
--- drivers/firmware/efi/libstub/Makefile | 4 +++- include/linux/stackleak.h | 3 +++ kernel/entry/common.c | 5 +++++ security/Kconfig.hardening | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-)
drivers/firmware/efi/libstub/Makefile +3 -1
@@ -19,7 +19,7 @@ cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly # disable the stackleak plugin cflags-$(CONFIG_ARM64) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ - -fpie $(DISABLE_STACKLEAK_PLUGIN) \ + -fpie \ $(call cc-option,-mbranch-protection=none) cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fno-builtin -fpic \
@@ -27,6 +27,8 @@ cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ cflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fpic +cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) += $(DISABLE_STACKLEAK_PLUGIN) + cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
include/linux/stackleak.h +3 -0
@@ -76,8 +76,11 @@ static inline void stackleak_task_init(struct task_struct *t)# endif } +void noinstr stackleak_erase(void); +#else /* !CONFIG_GCC_PLUGIN_STACKLEAK */ static inline void stackleak_task_init (struct task_struct *t) { } +static inline void stackleak_erase (void ) {}#endif #endif
kernel/entry/common.c +5 -0
@@ -8,6 +8,7 @@#include <linux/livepatch.h> #include <linux/audit.h> #include <linux/tick.h> +#include <linux/stackleak.h> #include "common.h"
@@ -194,6 +195,10 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)lockdep_assert_irqs_disabled (); +#ifndef CONFIG_HAVE_ARCH_STACKLEAK +stackleak_erase (); +#endif +/* Flush pending rcuog wakeup before the last need_resched() check */ tick_nohz_user_enter_prepare ();
security/Kconfig.hardening +1 -1
@@ -152,7 +152,7 @@ config GCC_PLUGIN_STRUCTLEAK_VERBOSE config GCC_PLUGIN_STACKLEAK bool "Poison kernel stack before returning from syscalls" depends on GCC_PLUGINS - depends on HAVE_ARCH_STACKLEAK + depends on HAVE_ARCH_STACKLEAK || GENERIC_ENTRY help This option makes the kernel erase the kernel stack before returning from system calls. This has the effect of leaving
Signature · 1 line
2.36.1
Quoted 4 lines · On Tue, Sep 06, 2022 at 09:48:09PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
Make
generic_entrysupports basic STACKLEAK, and no arch custom code is needed.
IIUC, this change is going to cause redundant work to be done on x86 (since it erases the stack in its entry assembly). It also means any arch relying upon this will not clear some stack contents that could be cleared from assembly later in the return to userspace path, after the C entry code stack frames are gone.
I assume you're adding this so that riscv can use stackleak? WHy can't it call stackleak_erase*() later in the return-to-userspce path?
Quoted 23 lines
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- drivers/firmware/efi/libstub/Makefile | 4 +++- include/linux/stackleak.h | 3 +++ kernel/entry/common.c | 5 +++++ security/Kconfig.hardening | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/Makefile +1
-1@@ -19,7 +19,7 @@ cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly # disable the stackleak plugin cflags-$(CONFIG_ARM64) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ - -fpie $(DISABLE_STACKLEAK_PLUGIN) \ + -fpie \ $(call cc-option,-mbranch-protection=none) cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fno-builtin -fpic \
drivers/firmware/efi/libstub/Makefile cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ · line 34
Quoted 8 lines
drivers/firmware/efi/libstub/Makefile +2
-0@@ -27,6 +27,8 @@ cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ cflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fpic +cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) += $(DISABLE_STACKLEAK_PLUGIN) + cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
Huh; is there a latent bug here where x86's EFI stub is instrumented with stackleak?
Thanks,
Mark.
Quoted 55 lines
include/linux/stackleak.h +3
-0@@ -76,8 +76,11 @@ static inline void stackleak_task_init(struct task_struct *t)# endif } +void noinstr stackleak_erase(void); +#else /* !CONFIG_GCC_PLUGIN_STACKLEAK */ static inline void stackleak_task_init (struct task_struct *t) { } +static inline void stackleak_erase (void ) {}#endif #endif kernel/entry/common.c +5
-0@@ -8,6 +8,7 @@#include <linux/livepatch.h> #include <linux/audit.h> #include <linux/tick.h> +#include <linux/stackleak.h> #include "common.h" @@ -194,6 +195,10 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)lockdep_assert_irqs_disabled (); +#ifndef CONFIG_HAVE_ARCH_STACKLEAK +stackleak_erase (); +#endif +/* Flush pending rcuog wakeup before the last need_resched() check */ tick_nohz_user_enter_prepare ();security/Kconfig.hardening +1
-1@@ -152,7 +152,7 @@ config GCC_PLUGIN_STRUCTLEAK_VERBOSE config GCC_PLUGIN_STACKLEAK bool "Poison kernel stack before returning from syscalls" depends on GCC_PLUGINS - depends on HAVE_ARCH_STACKLEAK + depends on HAVE_ARCH_STACKLEAK || GENERIC_ENTRY help This option makes the kernel erase the kernel stack before returning from system calls. This has the effect of leavingSignature · 1 line
2.36.1
Quoted 4 lines · Mark Rutland
Quoted 2 lines · On Tue, Sep 06, 2022 at 09:48:09PM -0400, [email protected] wrote:
From: Guo Ren <[email protected]>
Quoted 8 lines
Quoted 2 lines
Make
generic_entrysupports basic STACKLEAK, and no arch custom code is needed.IIUC, this change is going to cause redundant work to be done on x86 (since it erases the stack in its entry assembly). It also means any arch relying upon this will not clear some stack contents that could be cleared from assembly later in the return to userspace path, after the C entry code stack frames are gone.
Yeah, it's a point, Thx.
Quoted 3 lines
I assume you're adding this so that riscv can use stackleak? WHy can't it call stackleak_erase*() later in the return-to-userspce path?
Okay, I would move stackleak_erase back to riscv code and call it in ret_from_exception of entry.S.
arch/riscv/kernel/entry.S +3 -1
@@ -130,7 +130,6 @@ END(handle_exception) ENTRY(ret_from_exception) REG_L s0, PT_STATUS(sp) - csrc CSR_STATUS, SR_IE #ifdef CONFIG_RISCV_M_MODE /* the MPP value is too large to be used as an immediate arg for addi */ li t0, SR_MPP
@@ -139,6 +138,8 @@ ENTRY(ret_from_exception) andi s0, s0, SR_SPP #endif bnez s0, 1f + call stackleak_erase + csrc CSR_STATUS, SR_IE /* Save unwound kernel stack pointer in thread_info */ addi s0, sp, PT_SIZE_ON_STACK
@@ -150,6 +151,7 @@ ENTRY(ret_from_exception) */ csrw CSR_SCRATCH, tp 1: + csrc CSR_STATUS, SR_IE
Quoted 27 lines
Quoted 27 lines
Signed-off-by Guo Ren <[email protected]>Signed-off-by Guo Ren <[email protected]>--- drivers/firmware/efi/libstub/Makefile | 4 +++- include/linux/stackleak.h | 3 +++ kernel/entry/common.c | 5 +++++ security/Kconfig.hardening | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-)drivers/firmware/efi/libstub/Makefile +1
-1@@ -19,7 +19,7 @@ cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly # disable the stackleak plugin cflags-$(CONFIG_ARM64) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ - -fpie $(DISABLE_STACKLEAK_PLUGIN) \ + -fpie \ $(call cc-option,-mbranch-protection=none) cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fno-builtin -fpic \@@ -27,6 +27,8 @@ cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ cflags-$(CONFIG_RISCV) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ -fpic
Quoted 8 lines
drivers/firmware/efi/libstub/Makefile cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ · line 34
Quoted 5 lines
drivers/firmware/efi/libstub/Makefile +2
-0+cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) += $(DISABLE_STACKLEAK_PLUGIN) + cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \Huh; is there a latent bug here where x86's EFI stub is instrumented with stackleak?
Oops, I forgot x86. Thank you for reminding.
Quoted 43 lines
Thanks,
Mark.Quoted 39 lines
include/linux/stackleak.h +3
-0@@ -76,8 +76,11 @@ static inline void stackleak_task_init(struct task_struct *t)# endif } +void noinstr stackleak_erase(void); +#else /* !CONFIG_GCC_PLUGIN_STACKLEAK */ static inline void stackleak_task_init (struct task_struct *t) { } +static inline void stackleak_erase (void ) {}#endif #endif kernel/entry/common.c +5
-0@@ -8,6 +8,7 @@#include <linux/livepatch.h> #include <linux/audit.h> #include <linux/tick.h> +#include <linux/stackleak.h> #include "common.h" @@ -194,6 +195,10 @@ static void exit_to_user_mode_prepare(struct pt_regs *regs)lockdep_assert_irqs_disabled (); +#ifndef CONFIG_HAVE_ARCH_STACKLEAK +stackleak_erase (); +#endif +/* Flush pending rcuog wakeup before the last need_resched() check */ tick_nohz_user_enter_prepare ();
Quoted 16 lines
Quoted 14 lines
security/Kconfig.hardening +1
-2@@ -152,7 +152,7 @@ config GCC_PLUGIN_STRUCTLEAK_VERBOSE config GCC_PLUGIN_STACKLEAK bool "Poison kernel stack before returning from syscalls" depends on GCC_PLUGINS - depends on HAVE_ARCH_STACKLEAK + depends on HAVE_ARCH_STACKLEAK || GENERIC_ENTRY help This option makes the kernel erase the kernel stack before returning from system calls. This has the effect of leaving --Quoted 2 lines
2.36.1
--
Best Regards
Guo Ren