]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
stackleak: Rename STACKLEAK to KSTACK_ERASE
authorKees Cook <kees@kernel.org>
Thu, 17 Jul 2025 23:25:06 +0000 (16:25 -0700)
committerKees Cook <kees@kernel.org>
Tue, 22 Jul 2025 04:35:01 +0000 (21:35 -0700)
In preparation for adding Clang sanitizer coverage stack depth tracking
that can support stack depth callbacks:

- Add the new top-level CONFIG_KSTACK_ERASE option which will be
  implemented either with the stackleak GCC plugin, or with the Clang
  stack depth callback support.
- Rename CONFIG_GCC_PLUGIN_STACKLEAK as needed to CONFIG_KSTACK_ERASE,
  but keep it for anything specific to the GCC plugin itself.
- Rename all exposed "STACKLEAK" names and files to "KSTACK_ERASE" (named
  for what it does rather than what it protects against), but leave as
  many of the internals alone as possible to avoid even more churn.

While here, also split "prev_lowest_stack" into CONFIG_KSTACK_ERASE_METRICS,
since that's the only place it is referenced from.

Suggested-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20250717232519.2984886-1-kees@kernel.org
Signed-off-by: Kees Cook <kees@kernel.org>
37 files changed:
Documentation/admin-guide/sysctl/kernel.rst
Documentation/arch/x86/x86_64/mm.rst
Documentation/security/self-protection.rst
Documentation/translations/zh_CN/security/self-protection.rst
MAINTAINERS
arch/Kconfig
arch/arm/Kconfig
arch/arm/boot/compressed/Makefile
arch/arm/kernel/entry-common.S
arch/arm64/Kconfig
arch/arm64/kernel/entry.S
arch/arm64/kernel/pi/Makefile
arch/arm64/kvm/hyp/nvhe/Makefile
arch/loongarch/Kconfig
arch/riscv/Kconfig
arch/riscv/kernel/entry.S
arch/riscv/kernel/pi/Makefile
arch/riscv/purgatory/Makefile
arch/s390/Kconfig
arch/s390/kernel/entry.S
arch/x86/Kconfig
arch/x86/entry/calling.h
arch/x86/purgatory/Makefile
drivers/firmware/efi/libstub/Makefile
drivers/misc/lkdtm/Makefile
drivers/misc/lkdtm/kstack_erase.c [moved from drivers/misc/lkdtm/stackleak.c with 89% similarity]
fs/proc/base.c
include/linux/kstack_erase.h [moved from include/linux/stackleak.h with 85% similarity]
include/linux/sched.h
kernel/Makefile
kernel/fork.c
kernel/kstack_erase.c [moved from kernel/stackleak.c with 90% similarity]
lib/Makefile
scripts/Makefile.gcc-plugins
security/Kconfig.hardening
tools/objtool/check.c
tools/testing/selftests/lkdtm/config

index dd49a89a62d3542fa1a599f318dff26589e1d57b..19224eeac1c28e275b5f101acf519cc016a2def4 100644 (file)
@@ -1465,7 +1465,7 @@ stack_erasing
 =============
 
 This parameter can be used to control kernel stack erasing at the end
-of syscalls for kernels built with ``CONFIG_GCC_PLUGIN_STACKLEAK``.
+of syscalls for kernels built with ``CONFIG_KSTACK_ERASE``.
 
 That erasing reduces the information which kernel stack leak bugs
 can reveal and blocks some uninitialized stack variable attacks.
@@ -1473,7 +1473,7 @@ The tradeoff is the performance impact: on a single CPU system kernel
 compilation sees a 1% slowdown, other systems and workloads may vary.
 
 = ====================================================================
-0 Kernel stack erasing is disabled, STACKLEAK_METRICS are not updated.
+0 Kernel stack erasing is disabled, KSTACK_ERASE_METRICS are not updated.
 1 Kernel stack erasing is enabled (default), it is performed before
   returning to the userspace at the end of syscalls.
 = ====================================================================
index f2db178b353f8ef763c2b8bf39cb55406898080a..a6cf05d51bd8c09626c2d316e60717d373439cbb 100644 (file)
@@ -176,5 +176,5 @@ Be very careful vs. KASLR when changing anything here. The KASLR address
 range must not overlap with anything except the KASAN shadow area, which is
 correct as KASAN disables KASLR.
 
-For both 4- and 5-level layouts, the STACKLEAK_POISON value in the last 2MB
+For both 4- and 5-level layouts, the KSTACK_ERASE_POISON value in the last 2MB
 hole: ffffffffffff4111
index 910668e665cbeff35be7727950a20af3840eecd1..a32ca23c21b0e3525ab67761ed3f199e12c02954 100644 (file)
@@ -303,7 +303,7 @@ Memory poisoning
 
 When releasing memory, it is best to poison the contents, to avoid reuse
 attacks that rely on the old contents of memory. E.g., clear stack on a
-syscall return (``CONFIG_GCC_PLUGIN_STACKLEAK``), wipe heap memory on a
+syscall return (``CONFIG_KSTACK_ERASE``), wipe heap memory on a
 free. This frustrates many uninitialized variable attacks, stack content
 exposures, heap content exposures, and use-after-free attacks.
 
index 3c8a68b1e1be930d80e7e740399cacf07cc150a0..93de9cee5c1a34b0e3ad8719d5f4c6cd36299d36 100644 (file)
@@ -259,7 +259,7 @@ KALLSYSM,则会直接打印原始地址。
 --------
 
 在释放内存时,最好对内存内容进行清除处理,以防止攻击者重用内存中以前
-的内容。例如,在系统调用返回时清除堆栈(CONFIG_GCC_PLUGIN_STACKLEAK),
+的内容。例如,在系统调用返回时清除堆栈(CONFIG_KSTACK_ERASE),
 在释放堆内容是清除其内容。这有助于防止许多未初始化变量攻击、堆栈内容
 泄露、堆内容泄露以及使用后释放攻击(user-after-free)。
 
index 0c1d245bf7b84f8a78b811e0c9c5a3edc09edc22..470d159d8fea4d150fc6492928220c0e3ea86645 100644 (file)
@@ -9992,8 +9992,6 @@ L:        linux-hardening@vger.kernel.org
 S:     Maintained
 T:     git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening
 F:     Documentation/kbuild/gcc-plugins.rst
-F:     include/linux/stackleak.h
-F:     kernel/stackleak.c
 F:     scripts/Makefile.gcc-plugins
 F:     scripts/gcc-plugins/
 
@@ -13087,10 +13085,12 @@ T:    git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/har
 F:     Documentation/ABI/testing/sysfs-kernel-oops_count
 F:     Documentation/ABI/testing/sysfs-kernel-warn_count
 F:     arch/*/configs/hardening.config
+F:     include/linux/kstack_erase.h
 F:     include/linux/overflow.h
 F:     include/linux/randomize_kstack.h
 F:     include/linux/ucopysize.h
 F:     kernel/configs/hardening.config
+F:     kernel/kstack_erase.c
 F:     lib/tests/randstruct_kunit.c
 F:     lib/tests/usercopy_kunit.c
 F:     mm/usercopy.c
index a3308a220f86413085cdd101611b689078b998be..4d1908f6f0849129d6738eb826ba925d5e1f6953 100644 (file)
@@ -630,11 +630,11 @@ config SECCOMP_CACHE_DEBUG
 
          If unsure, say N.
 
-config HAVE_ARCH_STACKLEAK
+config HAVE_ARCH_KSTACK_ERASE
        bool
        help
          An architecture should select this if it has the code which
-         fills the used part of the kernel stack with the STACKLEAK_POISON
+         fills the used part of the kernel stack with the KSTACK_ERASE_POISON
          value before returning from system calls.
 
 config HAVE_STACKPROTECTOR
index 3072731fe09c5911996d2e5fcc384c424f72f638..cb0b2e2211ca1322d0271d9f3945bff6c1054ad0 100644 (file)
@@ -87,11 +87,11 @@ config ARM
        select HAVE_ARCH_KGDB if !CPU_ENDIAN_BE32 && MMU
        select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
        select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_MMAP_RND_BITS if MMU
        select HAVE_ARCH_PFN_VALID
        select HAVE_ARCH_SECCOMP
        select HAVE_ARCH_SECCOMP_FILTER if AEABI && !OABI_COMPAT
-       select HAVE_ARCH_STACKLEAK
        select HAVE_ARCH_THREAD_STRUCT_WHITELIST
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE if ARM_LPAE
index d61369b1eabe8e3d6d1830156faa30f74bd1586c..f9075edfd773a696f783c61c5e616f7f5e930fa4 100644 (file)
@@ -9,7 +9,7 @@ OBJS            =
 
 HEAD   = head.o
 OBJS   += misc.o decompress.o
-CFLAGS_decompress.o += $(DISABLE_STACKLEAK_PLUGIN)
+CFLAGS_decompress.o += $(DISABLE_KSTACK_ERASE)
 ifeq ($(CONFIG_DEBUG_UNCOMPRESS),y)
 OBJS   += debug.o
 AFLAGS_head.o += -DDEBUG
index f379c852dcb72fa5e960bba5621cc2152f0df0d3..88336a1292bb79107e66b49a2b722fedaca32eea 100644 (file)
@@ -119,7 +119,7 @@ no_work_pending:
 
        ct_user_enter save = 0
 
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        bl      stackleak_erase_on_task_stack
 #endif
        restore_user_regs fast = 0, offset = 0
index 55fc331af3371660ede7f83d43e1c4425dcf6b10..e2a9e013b6a99262dc9fb260fdf018670d88ce77 100644 (file)
@@ -187,12 +187,12 @@ config ARM64
        select HAVE_ARCH_KCSAN if EXPERT
        select HAVE_ARCH_KFENCE
        select HAVE_ARCH_KGDB
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_MMAP_RND_BITS
        select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
        select HAVE_ARCH_PREL32_RELOCATIONS
        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
index 5ae2a34b50bda55a6972a412e284faa482388f95..67331437b2aab998372811d6e715c3ab05a82e00 100644 (file)
@@ -614,7 +614,7 @@ SYM_CODE_END(ret_to_kernel)
 SYM_CODE_START_LOCAL(ret_to_user)
        ldr     x19, [tsk, #TSK_TI_FLAGS]       // re-check for single-step
        enable_step_tsk x19, x2
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        bl      stackleak_erase_on_task_stack
 #endif
        kernel_exit 0
index 4d11a8c29181640f5bf71c1766f8a9e5c3d715a8..f440bf57b1a5f5315f5015c6e3c102171d472c60 100644 (file)
@@ -2,7 +2,7 @@
 # Copyright 2022 Google LLC
 
 KBUILD_CFLAGS  := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
-                  -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
+                  -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_KSTACK_ERASE) \
                   $(DISABLE_LATENT_ENTROPY_PLUGIN) \
                   $(call cc-option,-mbranch-protection=none) \
                   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
index a76522d63c3e630795db5972a99abc3d24bc5e26..0b0a68b663d4bd202a7036384bf8a1748cc97ca5 100644 (file)
@@ -12,7 +12,7 @@ asflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS
 ccflags-y := -D__KVM_NVHE_HYPERVISOR__ -D__DISABLE_EXPORTS -D__DISABLE_TRACE_MMIO__
 ccflags-y += -fno-stack-protector      \
             -DDISABLE_BRANCH_PROFILING \
-            $(DISABLE_STACKLEAK_PLUGIN)
+            $(DISABLE_KSTACK_ERASE)
 
 hostprogs := gen-hyprel
 HOST_EXTRACFLAGS += -I$(objtree)/include
index 4b19f93379a15336e0d13626accf57a5884306a3..1514789bea4a197104a47aa93d7a59088bdde30b 100644 (file)
@@ -120,11 +120,11 @@ config LOONGARCH
        select HAVE_ARCH_KASAN
        select HAVE_ARCH_KFENCE
        select HAVE_ARCH_KGDB if PERF_EVENTS
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_MMAP_RND_BITS if MMU
        select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
        select HAVE_ARCH_SECCOMP
        select HAVE_ARCH_SECCOMP_FILTER
-       select HAVE_ARCH_STACKLEAK
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE
        select HAVE_ARCH_USERFAULTFD_MINOR if USERFAULTFD
index 36061f4732b7496a9c68a9a10f9959849dc2a95c..cfc084fc9e6f72586256159096feb8df4c710132 100644 (file)
@@ -135,13 +135,13 @@ config RISCV
        select HAVE_ARCH_KASAN if MMU && 64BIT
        select HAVE_ARCH_KASAN_VMALLOC if MMU && 64BIT
        select HAVE_ARCH_KFENCE if MMU && 64BIT
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_KGDB if !XIP_KERNEL
        select HAVE_ARCH_KGDB_QXFER_PKT
        select HAVE_ARCH_MMAP_RND_BITS if MMU
        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
index 75656afa2d6be8ca5f2c4711455567c7f8fc0b97..3a0ec6fd595691c873717ae1e6af5b3ed9854ca2 100644 (file)
@@ -220,7 +220,7 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
 #endif
        bnez s0, 1f
 
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        call    stackleak_erase_on_task_stack
 #endif
 
index 81d69d45c06c33887b4795d1f226c476005c3826..7dd15be69c9007fccc5cb2fb79d6870364e1e650 100644 (file)
@@ -2,7 +2,7 @@
 # This file was copied from arm64/kernel/pi/Makefile.
 
 KBUILD_CFLAGS  := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
-                  -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
+                  -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_KSTACK_ERASE) \
                   $(call cc-option,-mbranch-protection=none) \
                   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
                   -include $(srctree)/include/linux/hidden.h \
index fb9c917c9b45735d35be5166455331fddda65ef9..240592e3f5c2f5beddf8f34f3cff00b25b727947 100644 (file)
@@ -53,7 +53,7 @@ targets += purgatory.ro purgatory.chk
 
 PURGATORY_CFLAGS_REMOVE := -mcmodel=kernel
 PURGATORY_CFLAGS := -mcmodel=medany -ffreestanding -fno-zero-initialized-in-bss
-PURGATORY_CFLAGS += $(DISABLE_STACKLEAK_PLUGIN) -DDISABLE_BRANCH_PROFILING
+PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
 PURGATORY_CFLAGS += -fno-stack-protector -g0
 
 # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
index 0c16dc443e2f65fc6bd71352e1cc8d7aa60a7c6a..a8e74ed8e3ccf5a2faedbcb09d152e7393b36887 100644 (file)
@@ -176,10 +176,10 @@ config S390
        select HAVE_ARCH_KCSAN
        select HAVE_ARCH_KMSAN
        select HAVE_ARCH_KFENCE
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_RANDOMIZE_KSTACK_OFFSET
        select HAVE_ARCH_SECCOMP_FILTER
        select HAVE_ARCH_SOFT_DIRTY
-       select HAVE_ARCH_STACKLEAK
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE
        select HAVE_ARCH_VMAP_STACK
index 0f00f4b06d51bb00a7c9f07d3f00fe787dd935b7..75b0fbb236d05f420b20cac6bac925e8ac36fa68 100644 (file)
@@ -124,7 +124,7 @@ _LPP_OFFSET = __LC_LPP
 #endif
 
        .macro STACKLEAK_ERASE
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        brasl   %r14,stackleak_erase_on_task_stack
 #endif
        .endm
index 340e5468980e070092990efb537dd37f24ddee69..bc3708cad46b3f8ed4ddff8b5dba5bddd7d816cf 100644 (file)
@@ -204,13 +204,13 @@ config X86
        select HAVE_ARCH_KFENCE
        select HAVE_ARCH_KMSAN                  if X86_64
        select HAVE_ARCH_KGDB
+       select HAVE_ARCH_KSTACK_ERASE
        select HAVE_ARCH_MMAP_RND_BITS          if MMU
        select HAVE_ARCH_MMAP_RND_COMPAT_BITS   if MMU && COMPAT
        select HAVE_ARCH_COMPAT_MMAP_BASES      if MMU && COMPAT
        select HAVE_ARCH_PREL32_RELOCATIONS
        select HAVE_ARCH_SECCOMP_FILTER
        select HAVE_ARCH_THREAD_STRUCT_WHITELIST
-       select HAVE_ARCH_STACKLEAK
        select HAVE_ARCH_TRACEHOOK
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE
        select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64
index d83236b96f2254cc99060c4e06df86a8399b6900..94519688b0071d34a679cd80ff1255dada739a0b 100644 (file)
@@ -369,7 +369,7 @@ For 32-bit we have the following conventions - kernel is built with
 .endm
 
 .macro STACKLEAK_ERASE_NOCLOBBER
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        PUSH_AND_CLEAR_REGS
        call stackleak_erase
        POP_REGS
@@ -388,7 +388,7 @@ For 32-bit we have the following conventions - kernel is built with
 #endif /* !CONFIG_X86_64 */
 
 .macro STACKLEAK_ERASE
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        call stackleak_erase
 #endif
 .endm
index ebdfd7b84feb3eb4d1cc0fd958a97aa2c4f344e8..e0a607a14e7ed76397ad6b61268054a8808a0249 100644 (file)
@@ -35,7 +35,7 @@ targets += purgatory.ro purgatory.chk
 PURGATORY_CFLAGS_REMOVE := -mcmodel=kernel
 PURGATORY_CFLAGS := -mcmodel=small -ffreestanding -fno-zero-initialized-in-bss -g0
 PURGATORY_CFLAGS += -fpic -fvisibility=hidden
-PURGATORY_CFLAGS += $(DISABLE_STACKLEAK_PLUGIN) -DDISABLE_BRANCH_PROFILING
+PURGATORY_CFLAGS += $(DISABLE_KSTACK_ERASE) -DDISABLE_BRANCH_PROFILING
 PURGATORY_CFLAGS += -fno-stack-protector
 
 # Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
index 939a4955e00badde3ab0bd66cfc4e864c6489bc6..94b05e4451dd3f992da629220b8be97c3247d4d2 100644 (file)
@@ -22,16 +22,16 @@ cflags-$(CONFIG_X86)                += -m$(BITS) -D__KERNEL__ -std=gnu11 \
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-cflags-$(CONFIG_ARM64)         += -fpie $(DISABLE_STACKLEAK_PLUGIN) \
+cflags-$(CONFIG_ARM64)         += -fpie $(DISABLE_KSTACK_ERASE) \
                                   -fno-unwind-tables -fno-asynchronous-unwind-tables
 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) \
-                                  $(DISABLE_STACKLEAK_PLUGIN)
+                                  $(DISABLE_KSTACK_ERASE)
 cflags-$(CONFIG_RISCV)         += -fpic -DNO_ALTERNATIVE -mno-relax \
-                                  $(DISABLE_STACKLEAK_PLUGIN)
-cflags-$(CONFIG_LOONGARCH)     += -fpie $(DISABLE_STACKLEAK_PLUGIN)
+                                  $(DISABLE_KSTACK_ERASE)
+cflags-$(CONFIG_LOONGARCH)     += -fpie $(DISABLE_KSTACK_ERASE)
 
 cflags-$(CONFIG_EFI_PARAMS_FROM_FDT)   += -I$(srctree)/scripts/dtc/libfdt
 
index 39468bd27b85f771fc2afc2d2855b08fce676f45..03ebe33185f9d0f32a3a47392db9b9b593a2c78c 100644 (file)
@@ -8,7 +8,7 @@ lkdtm-$(CONFIG_LKDTM)           += perms.o
 lkdtm-$(CONFIG_LKDTM)          += refcount.o
 lkdtm-$(CONFIG_LKDTM)          += rodata_objcopy.o
 lkdtm-$(CONFIG_LKDTM)          += usercopy.o
-lkdtm-$(CONFIG_LKDTM)          += stackleak.o
+lkdtm-$(CONFIG_LKDTM)          += kstack_erase.o
 lkdtm-$(CONFIG_LKDTM)          += cfi.o
 lkdtm-$(CONFIG_LKDTM)          += fortify.o
 lkdtm-$(CONFIG_PPC_64S_HASH_MMU)       += powerpc.o
similarity index 89%
rename from drivers/misc/lkdtm/stackleak.c
rename to drivers/misc/lkdtm/kstack_erase.c
index f1d02216091385d626703a7b6254c9d706341d99..4fd9b0bfb87492f37f7f4c0b247da0c8c03681d2 100644 (file)
@@ -1,7 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
  * This code tests that the current task stack is properly erased (filled
- * with STACKLEAK_POISON).
+ * with KSTACK_ERASE_POISON).
  *
  * Authors:
  *   Alexander Popov <alex.popov@linux.com>
@@ -9,9 +9,9 @@
  */
 
 #include "lkdtm.h"
-#include <linux/stackleak.h>
+#include <linux/kstack_erase.h>
 
-#if defined(CONFIG_GCC_PLUGIN_STACKLEAK)
+#if defined(CONFIG_KSTACK_ERASE)
 /*
  * Check that stackleak tracks the lowest stack pointer and erases the stack
  * below this as expected.
@@ -85,7 +85,7 @@ static void noinstr check_stackleak_irqoff(void)
        while (poison_low > task_stack_low) {
                poison_low -= sizeof(unsigned long);
 
-               if (*(unsigned long *)poison_low == STACKLEAK_POISON)
+               if (*(unsigned long *)poison_low == KSTACK_ERASE_POISON)
                        continue;
 
                instrumentation_begin();
@@ -96,7 +96,7 @@ static void noinstr check_stackleak_irqoff(void)
        }
 
        instrumentation_begin();
-       pr_info("stackleak stack usage:\n"
+       pr_info("kstack erase stack usage:\n"
                "  high offset: %lu bytes\n"
                "  current:     %lu bytes\n"
                "  lowest:      %lu bytes\n"
@@ -121,7 +121,7 @@ out:
        instrumentation_end();
 }
 
-static void lkdtm_STACKLEAK_ERASING(void)
+static void lkdtm_KSTACK_ERASE(void)
 {
        unsigned long flags;
 
@@ -129,19 +129,19 @@ static void lkdtm_STACKLEAK_ERASING(void)
        check_stackleak_irqoff();
        local_irq_restore(flags);
 }
-#else /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
-static void lkdtm_STACKLEAK_ERASING(void)
+#else /* defined(CONFIG_KSTACK_ERASE) */
+static void lkdtm_KSTACK_ERASE(void)
 {
-       if (IS_ENABLED(CONFIG_HAVE_ARCH_STACKLEAK)) {
-               pr_err("XFAIL: stackleak is not enabled (CONFIG_GCC_PLUGIN_STACKLEAK=n)\n");
+       if (IS_ENABLED(CONFIG_HAVE_ARCH_KSTACK_ERASE)) {
+               pr_err("XFAIL: stackleak is not enabled (CONFIG_KSTACK_ERASE=n)\n");
        } else {
-               pr_err("XFAIL: stackleak is not supported on this arch (HAVE_ARCH_STACKLEAK=n)\n");
+               pr_err("XFAIL: stackleak is not supported on this arch (HAVE_ARCH_KSTACK_ERASE=n)\n");
        }
 }
-#endif /* defined(CONFIG_GCC_PLUGIN_STACKLEAK) */
+#endif /* defined(CONFIG_KSTACK_ERASE) */
 
 static struct crashtype crashtypes[] = {
-       CRASHTYPE(STACKLEAK_ERASING),
+       CRASHTYPE(KSTACK_ERASE),
 };
 
 struct crashtype_category stackleak_crashtypes = {
index c667702dc69b8ca2531e88e12ed7a18533f294dd..be34612af8b6d2267026d35cdc01022a8143aed6 100644 (file)
@@ -3291,7 +3291,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_KSM */
 
-#ifdef CONFIG_STACKLEAK_METRICS
+#ifdef CONFIG_KSTACK_ERASE_METRICS
 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
                                struct pid *pid, struct task_struct *task)
 {
@@ -3304,7 +3304,7 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
                                                        prev_depth, depth);
        return 0;
 }
-#endif /* CONFIG_STACKLEAK_METRICS */
+#endif /* CONFIG_KSTACK_ERASE_METRICS */
 
 /*
  * Thread groups
@@ -3411,7 +3411,7 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_LIVEPATCH
        ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
 #endif
-#ifdef CONFIG_STACKLEAK_METRICS
+#ifdef CONFIG_KSTACK_ERASE_METRICS
        ONE("stack_depth", S_IRUGO, proc_stack_depth),
 #endif
 #ifdef CONFIG_PROC_PID_ARCH_STATUS
similarity index 85%
rename from include/linux/stackleak.h
rename to include/linux/kstack_erase.h
index 3be2cb564710b5a7be3de43903c5786e15f704ad..4e432eefa4d04f4041c421a480e848d299f0f523 100644 (file)
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _LINUX_STACKLEAK_H
-#define _LINUX_STACKLEAK_H
+#ifndef _LINUX_KSTACK_ERASE_H
+#define _LINUX_KSTACK_ERASE_H
 
 #include <linux/sched.h>
 #include <linux/sched/task_stack.h>
@@ -9,10 +9,10 @@
  * Check that the poison value points to the unused hole in the
  * virtual memory map for your platform.
  */
-#define STACKLEAK_POISON -0xBEEF
-#define STACKLEAK_SEARCH_DEPTH 128
+#define KSTACK_ERASE_POISON -0xBEEF
+#define KSTACK_ERASE_SEARCH_DEPTH 128
 
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
 #include <asm/stacktrace.h>
 #include <linux/linkage.h>
 
@@ -50,7 +50,7 @@ stackleak_task_high_bound(const struct task_struct *tsk)
 static __always_inline unsigned long
 stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
 {
-       const unsigned int depth = STACKLEAK_SEARCH_DEPTH / sizeof(unsigned long);
+       const unsigned int depth = KSTACK_ERASE_SEARCH_DEPTH / sizeof(unsigned long);
        unsigned int poison_count = 0;
        unsigned long poison_high = high;
        unsigned long sp = high;
@@ -58,7 +58,7 @@ stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
        while (sp > low && poison_count < depth) {
                sp -= sizeof(unsigned long);
 
-               if (*(unsigned long *)sp == STACKLEAK_POISON) {
+               if (*(unsigned long *)sp == KSTACK_ERASE_POISON) {
                        poison_count++;
                } else {
                        poison_count = 0;
@@ -72,7 +72,7 @@ stackleak_find_top_of_poison(const unsigned long low, const unsigned long high)
 static inline void stackleak_task_init(struct task_struct *t)
 {
        t->lowest_stack = stackleak_task_low_bound(t);
-# ifdef CONFIG_STACKLEAK_METRICS
+# ifdef CONFIG_KSTACK_ERASE_METRICS
        t->prev_lowest_stack = t->lowest_stack;
 # endif
 }
@@ -82,7 +82,7 @@ asmlinkage void noinstr stackleak_erase_on_task_stack(void);
 asmlinkage void noinstr stackleak_erase_off_task_stack(void);
 void __no_caller_saved_registers noinstr stackleak_track_stack(void);
 
-#else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
+#else /* !CONFIG_KSTACK_ERASE */
 static inline void stackleak_task_init(struct task_struct *t) { }
 #endif
 
index 4f78a64beb52c425e46c66427b2f608ba30b3d99..b7d2f2fd4cd489b06961c04cef7f52e100950a54 100644 (file)
@@ -1607,8 +1607,10 @@ struct task_struct {
        /* Used by BPF for per-TASK xdp storage */
        struct bpf_net_context          *bpf_net_context;
 
-#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+#ifdef CONFIG_KSTACK_ERASE
        unsigned long                   lowest_stack;
+#endif
+#ifdef CONFIG_KSTACK_ERASE_METRICS
        unsigned long                   prev_lowest_stack;
 #endif
 
index 32e80dd626af07d0c43290e3f5c64af5bff07b51..e4f01f1d4d0cc93c28493c615b48e4c81e6a098a 100644 (file)
@@ -139,11 +139,11 @@ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
 obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o
 obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o
 
-CFLAGS_stackleak.o += $(DISABLE_STACKLEAK_PLUGIN)
-obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o
-KASAN_SANITIZE_stackleak.o := n
-KCSAN_SANITIZE_stackleak.o := n
-KCOV_INSTRUMENT_stackleak.o := n
+CFLAGS_kstack_erase.o += $(DISABLE_KSTACK_ERASE)
+obj-$(CONFIG_KSTACK_ERASE) += kstack_erase.o
+KASAN_SANITIZE_kstack_erase.o := n
+KCSAN_SANITIZE_kstack_erase.o := n
+KCOV_INSTRUMENT_kstack_erase.o := n
 
 obj-$(CONFIG_SCF_TORTURE_TEST) += scftorture.o
 
index 1ee8eb11f38bae1d2eb6de9494aea94b7a19e6c3..1ec66911f6f654e2d15d6f2e0c09c782fce98dd5 100644 (file)
@@ -93,7 +93,7 @@
 #include <linux/kcov.h>
 #include <linux/livepatch.h>
 #include <linux/thread_info.h>
-#include <linux/stackleak.h>
+#include <linux/kstack_erase.h>
 #include <linux/kasan.h>
 #include <linux/scs.h>
 #include <linux/io_uring.h>
similarity index 90%
rename from kernel/stackleak.c
rename to kernel/kstack_erase.c
index bb65321761b43c0a27f8fe3ca1ae6b42dd648ca5..201b846f83453ff5eab22f6d24530b8c679e28ac 100644 (file)
@@ -6,14 +6,14 @@
  *
  * Author: Alexander Popov <alex.popov@linux.com>
  *
- * STACKLEAK reduces the information which kernel stack leak bugs can
+ * KSTACK_ERASE reduces the information which kernel stack leak bugs can
  * reveal and blocks some uninitialized stack variable attacks.
  */
 
-#include <linux/stackleak.h>
+#include <linux/kstack_erase.h>
 #include <linux/kprobes.h>
 
-#ifdef CONFIG_STACKLEAK_RUNTIME_DISABLE
+#ifdef CONFIG_KSTACK_ERASE_RUNTIME_DISABLE
 #include <linux/jump_label.h>
 #include <linux/string_choices.h>
 #include <linux/sysctl.h>
@@ -68,7 +68,7 @@ late_initcall(stackleak_sysctls_init);
 #define skip_erasing() static_branch_unlikely(&stack_erasing_bypass)
 #else
 #define skip_erasing() false
-#endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */
+#endif /* CONFIG_KSTACK_ERASE_RUNTIME_DISABLE */
 
 #ifndef __stackleak_poison
 static __always_inline void __stackleak_poison(unsigned long erase_low,
@@ -91,7 +91,7 @@ static __always_inline void __stackleak_erase(bool on_task_stack)
        erase_low = stackleak_find_top_of_poison(task_stack_low,
                                                 current->lowest_stack);
 
-#ifdef CONFIG_STACKLEAK_METRICS
+#ifdef CONFIG_KSTACK_ERASE_METRICS
        current->prev_lowest_stack = erase_low;
 #endif
 
@@ -113,7 +113,7 @@ static __always_inline void __stackleak_erase(bool on_task_stack)
        else
                erase_high = task_stack_high;
 
-       __stackleak_poison(erase_low, erase_high, STACKLEAK_POISON);
+       __stackleak_poison(erase_low, erase_high, KSTACK_ERASE_POISON);
 
        /* Reset the 'lowest_stack' value for the next syscall */
        current->lowest_stack = task_stack_high;
@@ -161,11 +161,11 @@ void __used __no_caller_saved_registers noinstr stackleak_track_stack(void)
        unsigned long sp = current_stack_pointer;
 
        /*
-        * Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
-        * STACKLEAK_SEARCH_DEPTH makes the poison search in
+        * Having CONFIG_KSTACK_ERASE_TRACK_MIN_SIZE larger than
+        * KSTACK_ERASE_SEARCH_DEPTH makes the poison search in
         * stackleak_erase() unreliable. Let's prevent that.
         */
-       BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE > STACKLEAK_SEARCH_DEPTH);
+       BUILD_BUG_ON(CONFIG_KSTACK_ERASE_TRACK_MIN_SIZE > KSTACK_ERASE_SEARCH_DEPTH);
 
        /* 'lowest_stack' should be aligned on the register width boundary */
        sp = ALIGN(sp, sizeof(unsigned long));
index c38582f187dd81916113319072e5cfef26f26c84..632e69d25feb121d4ea7630789202bc965cb3a2c 100644 (file)
@@ -337,7 +337,7 @@ obj-$(CONFIG_UBSAN) += ubsan.o
 UBSAN_SANITIZE_ubsan.o := n
 KASAN_SANITIZE_ubsan.o := n
 KCSAN_SANITIZE_ubsan.o := n
-CFLAGS_ubsan.o := -fno-stack-protector $(DISABLE_STACKLEAK_PLUGIN)
+CFLAGS_ubsan.o := -fno-stack-protector $(DISABLE_KSTACK_ERASE)
 
 obj-$(CONFIG_SBITMAP) += sbitmap.o
 
index 435ab3f0ec44de13f4f06a64606dc78198856fdd..28b8867c4e84a5c57945941517359e79fb222aa3 100644 (file)
@@ -12,15 +12,15 @@ gcc-plugin-$(CONFIG_GCC_PLUGIN_STACKLEAK)   += stackleak_plugin.so
 gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK)               \
                += -DSTACKLEAK_PLUGIN
 gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK)               \
-               += -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE)
+               += -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_KSTACK_ERASE_TRACK_MIN_SIZE)
 gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK)               \
                += -fplugin-arg-stackleak_plugin-arch=$(SRCARCH)
 gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK_VERBOSE)       \
                += -fplugin-arg-stackleak_plugin-verbose
 ifdef CONFIG_GCC_PLUGIN_STACKLEAK
-    DISABLE_STACKLEAK_PLUGIN += -fplugin-arg-stackleak_plugin-disable
+    DISABLE_KSTACK_ERASE += -fplugin-arg-stackleak_plugin-disable
 endif
-export DISABLE_STACKLEAK_PLUGIN
+export DISABLE_KSTACK_ERASE
 
 # All the plugin CFLAGS are collected here in case a build target needs to
 # filter them out of the KBUILD_CFLAGS.
index fd1238753cadb61fbd1c5cc37940268d16218e41..125b35e2ef0ff0bbb1028e9f327972c826a5f8ed 100644 (file)
@@ -82,10 +82,10 @@ choice
 
 endchoice
 
-config GCC_PLUGIN_STACKLEAK
+config KSTACK_ERASE
        bool "Poison kernel stack before returning from syscalls"
+       depends on HAVE_ARCH_KSTACK_ERASE
        depends on GCC_PLUGINS
-       depends on HAVE_ARCH_STACKLEAK
        help
          This option makes the kernel erase the kernel stack before
          returning from system calls. This has the effect of leaving
@@ -103,6 +103,10 @@ config GCC_PLUGIN_STACKLEAK
          are advised to test this feature on your expected workload before
          deploying it.
 
+config GCC_PLUGIN_STACKLEAK
+       def_bool KSTACK_ERASE
+       depends on GCC_PLUGINS
+       help
          This plugin was ported from grsecurity/PaX. More information at:
           * https://grsecurity.net/
           * https://pax.grsecurity.net/
@@ -117,37 +121,37 @@ config GCC_PLUGIN_STACKLEAK_VERBOSE
          instrumented. This is useful for comparing coverage between
          builds.
 
-config STACKLEAK_TRACK_MIN_SIZE
-       int "Minimum stack frame size of functions tracked by STACKLEAK"
+config KSTACK_ERASE_TRACK_MIN_SIZE
+       int "Minimum stack frame size of functions tracked by KSTACK_ERASE"
        default 100
        range 0 4096
-       depends on GCC_PLUGIN_STACKLEAK
+       depends on KSTACK_ERASE
        help
-         The STACKLEAK gcc plugin instruments the kernel code for tracking
+         The KSTACK_ERASE option instruments the kernel code for tracking
          the lowest border of the kernel stack (and for some other purposes).
          It inserts the stackleak_track_stack() call for the functions with
          a stack frame size greater than or equal to this parameter.
          If unsure, leave the default value 100.
 
-config STACKLEAK_METRICS
-       bool "Show STACKLEAK metrics in the /proc file system"
-       depends on GCC_PLUGIN_STACKLEAK
+config KSTACK_ERASE_METRICS
+       bool "Show KSTACK_ERASE metrics in the /proc file system"
+       depends on KSTACK_ERASE
        depends on PROC_FS
        help
-         If this is set, STACKLEAK metrics for every task are available in
-         the /proc file system. In particular, /proc/<pid>/stack_depth
+         If this is set, KSTACK_ERASE metrics for every task are available
+         in the /proc file system. In particular, /proc/<pid>/stack_depth
          shows the maximum kernel stack consumption for the current and
          previous syscalls. Although this information is not precise, it
-         can be useful for estimating the STACKLEAK performance impact for
-         your workloads.
+         can be useful for estimating the KSTACK_ERASE performance impact
+         for your workloads.
 
-config STACKLEAK_RUNTIME_DISABLE
+config KSTACK_ERASE_RUNTIME_DISABLE
        bool "Allow runtime disabling of kernel stack erasing"
-       depends on GCC_PLUGIN_STACKLEAK
+       depends on KSTACK_ERASE
        help
          This option provides 'stack_erasing' sysctl, which can be used in
          runtime to control kernel stack erasing for kernels built with
-         CONFIG_GCC_PLUGIN_STACKLEAK.
+         CONFIG_KSTACK_ERASE.
 
 config INIT_ON_ALLOC_DEFAULT_ON
        bool "Enable heap memory zeroing on allocation by default"
index f23bdda737aaa5e0a104540402d18e8861573ab9..5451bdbcf84ad3e58ee5cfaf35b4f8a5eb6d4cd2 100644 (file)
@@ -1192,7 +1192,7 @@ static const char *uaccess_safe_builtin[] = {
        "__ubsan_handle_type_mismatch_v1",
        "__ubsan_handle_shift_out_of_bounds",
        "__ubsan_handle_load_invalid_value",
-       /* STACKLEAK */
+       /* KSTACK_ERASE */
        "stackleak_track_stack",
        /* TRACE_BRANCH_PROFILING */
        "ftrace_likely_update",
index 7afe05e8c4d7926cc0cb638504c9bf9cf211e887..bd09fdaf53e0a5cc4f11fd66b3cb270d0ad4a953 100644 (file)
@@ -2,7 +2,7 @@ CONFIG_LKDTM=y
 CONFIG_DEBUG_LIST=y
 CONFIG_SLAB_FREELIST_HARDENED=y
 CONFIG_FORTIFY_SOURCE=y
-CONFIG_GCC_PLUGIN_STACKLEAK=y
+CONFIG_KSTACK_ERASE=y
 CONFIG_HARDENED_USERCOPY=y
 CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT=y
 CONFIG_INIT_ON_FREE_DEFAULT_ON=y