* [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK
@ 2024-06-23 23:53 Jisheng Zhang
2024-06-24 3:23 ` Charlie Jenkins
2024-07-25 13:20 ` patchwork-bot+linux-riscv
0 siblings, 2 replies; 3+ messages in thread
From: Jisheng Zhang @ 2024-06-23 23:53 UTC (permalink / raw)
To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Ard Biesheuvel,
Charlie Jenkins
Cc: linux-riscv, linux-kernel, linux-efi
Add support for the stackleak feature. Whenever the kernel returns to user
space the kernel stack is filled with a poison value.
At the same time, disables the plugin in EFI stub code because EFI stub
is out of scope for the protection.
Tested on qemu and milkv duo:
/ # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT
[ 38.675575] lkdtm: Performing direct entry STACKLEAK_ERASING
[ 38.678448] lkdtm: stackleak stack usage:
[ 38.678448] high offset: 288 bytes
[ 38.678448] current: 496 bytes
[ 38.678448] lowest: 1328 bytes
[ 38.678448] tracked: 1328 bytes
[ 38.678448] untracked: 448 bytes
[ 38.678448] poisoned: 14312 bytes
[ 38.678448] low offset: 8 bytes
[ 38.689887] lkdtm: OK: the rest of the thread stack is properly erased
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
---
since v2:
- add #include <linux/sizes.h> to fixing compiler error found by
Charlie
arch/riscv/Kconfig | 1 +
arch/riscv/include/asm/thread_info.h | 1 +
arch/riscv/kernel/entry.S | 4 ++++
drivers/firmware/efi/libstub/Makefile | 3 ++-
4 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 0525ee2d63c7..9cbfdffec96c 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -118,6 +118,7 @@ config RISCV
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
select HAVE_ARCH_SECCOMP_FILTER
+ select HAVE_ARCH_STACKLEAK
select HAVE_ARCH_THREAD_STRUCT_WHITELIST
select HAVE_ARCH_TRACEHOOK
select HAVE_ARCH_TRANSPARENT_HUGEPAGE if 64BIT && MMU
diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
index 5d473343634b..fca5c6be2b81 100644
--- a/arch/riscv/include/asm/thread_info.h
+++ b/arch/riscv/include/asm/thread_info.h
@@ -10,6 +10,7 @@
#include <asm/page.h>
#include <linux/const.h>
+#include <linux/sizes.h>
/* thread information allocation */
#define THREAD_SIZE_ORDER CONFIG_THREAD_SIZE_ORDER
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 68a24cf9481a..80ff55a26d13 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -130,6 +130,10 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
#endif
bnez s0, 1f
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+ call stackleak_erase_on_task_stack
+#endif
+
/* Save unwound kernel stack pointer in thread_info */
addi s0, sp, PT_SIZE_ON_STACK
REG_S s0, TASK_TI_KERNEL_SP(tp)
diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
index 06f0428a723c..3a9521c57641 100644
--- a/drivers/firmware/efi/libstub/Makefile
+++ b/drivers/firmware/efi/libstub/Makefile
@@ -28,7 +28,8 @@ cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
-DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
-DEFI_HAVE_STRCMP -fno-builtin -fpic \
$(call cc-option,-mno-single-pic-base)
-cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax
+cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax \
+ $(DISABLE_STACKLEAK_PLUGIN)
cflags-$(CONFIG_LOONGARCH) += -fpie
cflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK
2024-06-23 23:53 [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK Jisheng Zhang
@ 2024-06-24 3:23 ` Charlie Jenkins
2024-07-25 13:20 ` patchwork-bot+linux-riscv
1 sibling, 0 replies; 3+ messages in thread
From: Charlie Jenkins @ 2024-06-24 3:23 UTC (permalink / raw)
To: Jisheng Zhang
Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Ard Biesheuvel,
linux-riscv, linux-kernel, linux-efi
On Mon, Jun 24, 2024 at 07:53:16AM +0800, Jisheng Zhang wrote:
> Add support for the stackleak feature. Whenever the kernel returns to user
> space the kernel stack is filled with a poison value.
>
> At the same time, disables the plugin in EFI stub code because EFI stub
> is out of scope for the protection.
>
> Tested on qemu and milkv duo:
> / # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT
> [ 38.675575] lkdtm: Performing direct entry STACKLEAK_ERASING
> [ 38.678448] lkdtm: stackleak stack usage:
> [ 38.678448] high offset: 288 bytes
> [ 38.678448] current: 496 bytes
> [ 38.678448] lowest: 1328 bytes
> [ 38.678448] tracked: 1328 bytes
> [ 38.678448] untracked: 448 bytes
> [ 38.678448] poisoned: 14312 bytes
> [ 38.678448] low offset: 8 bytes
> [ 38.689887] lkdtm: OK: the rest of the thread stack is properly erased
>
> Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
> ---
>
> since v2:
> - add #include <linux/sizes.h> to fixing compiler error found by
> Charlie
Thanks!
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
>
> arch/riscv/Kconfig | 1 +
> arch/riscv/include/asm/thread_info.h | 1 +
> arch/riscv/kernel/entry.S | 4 ++++
> drivers/firmware/efi/libstub/Makefile | 3 ++-
> 4 files changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index 0525ee2d63c7..9cbfdffec96c 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -118,6 +118,7 @@ config RISCV
> select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
> select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
> select HAVE_ARCH_SECCOMP_FILTER
> + select HAVE_ARCH_STACKLEAK
> select HAVE_ARCH_THREAD_STRUCT_WHITELIST
> select HAVE_ARCH_TRACEHOOK
> select HAVE_ARCH_TRANSPARENT_HUGEPAGE if 64BIT && MMU
> diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
> index 5d473343634b..fca5c6be2b81 100644
> --- a/arch/riscv/include/asm/thread_info.h
> +++ b/arch/riscv/include/asm/thread_info.h
> @@ -10,6 +10,7 @@
>
> #include <asm/page.h>
> #include <linux/const.h>
> +#include <linux/sizes.h>
>
> /* thread information allocation */
> #define THREAD_SIZE_ORDER CONFIG_THREAD_SIZE_ORDER
> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 68a24cf9481a..80ff55a26d13 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -130,6 +130,10 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
> #endif
> bnez s0, 1f
>
> +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
> + call stackleak_erase_on_task_stack
> +#endif
> +
> /* Save unwound kernel stack pointer in thread_info */
> addi s0, sp, PT_SIZE_ON_STACK
> REG_S s0, TASK_TI_KERNEL_SP(tp)
> diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile
> index 06f0428a723c..3a9521c57641 100644
> --- a/drivers/firmware/efi/libstub/Makefile
> +++ b/drivers/firmware/efi/libstub/Makefile
> @@ -28,7 +28,8 @@ cflags-$(CONFIG_ARM) += -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
> -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
> -DEFI_HAVE_STRCMP -fno-builtin -fpic \
> $(call cc-option,-mno-single-pic-base)
> -cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax
> +cflags-$(CONFIG_RISCV) += -fpic -DNO_ALTERNATIVE -mno-relax \
> + $(DISABLE_STACKLEAK_PLUGIN)
> cflags-$(CONFIG_LOONGARCH) += -fpie
>
> cflags-$(CONFIG_EFI_PARAMS_FROM_FDT) += -I$(srctree)/scripts/dtc/libfdt
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK
2024-06-23 23:53 [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK Jisheng Zhang
2024-06-24 3:23 ` Charlie Jenkins
@ 2024-07-25 13:20 ` patchwork-bot+linux-riscv
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+linux-riscv @ 2024-07-25 13:20 UTC (permalink / raw)
To: Jisheng Zhang
Cc: linux-riscv, paul.walmsley, palmer, aou, ardb, charlie,
linux-kernel, linux-efi
Hello:
This patch was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:
On Mon, 24 Jun 2024 07:53:16 +0800 you wrote:
> Add support for the stackleak feature. Whenever the kernel returns to user
> space the kernel stack is filled with a poison value.
>
> At the same time, disables the plugin in EFI stub code because EFI stub
> is out of scope for the protection.
>
> Tested on qemu and milkv duo:
> / # echo STACKLEAK_ERASING > /sys/kernel/debug/provoke-crash/DIRECT
> [ 38.675575] lkdtm: Performing direct entry STACKLEAK_ERASING
> [ 38.678448] lkdtm: stackleak stack usage:
> [ 38.678448] high offset: 288 bytes
> [ 38.678448] current: 496 bytes
> [ 38.678448] lowest: 1328 bytes
> [ 38.678448] tracked: 1328 bytes
> [ 38.678448] untracked: 448 bytes
> [ 38.678448] poisoned: 14312 bytes
> [ 38.678448] low offset: 8 bytes
> [ 38.689887] lkdtm: OK: the rest of the thread stack is properly erased
>
> [...]
Here is the summary with links:
- [v2] riscv: enable HAVE_ARCH_STACKLEAK
https://git.kernel.org/riscv/c/0059748ce289
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-07-25 13:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-23 23:53 [PATCH v2] riscv: enable HAVE_ARCH_STACKLEAK Jisheng Zhang
2024-06-24 3:23 ` Charlie Jenkins
2024-07-25 13:20 ` patchwork-bot+linux-riscv
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).