Skip to contentSkip to conversation

Introduce the STACKLEAK feature and a test for it

RFC PATCH Superseded Alexander Popov 6 participants 24 messages Oct 22, 2017 – Nov 14, 2017

Subsystems (11)
Documentation x86 VFS Kbuild +7 more subsystems

v5 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v5 cover Introduce the STACKLEAK feature and a test for it

This is the 5th version of the patch series introducing STACKLEAK to the mainline kernel. STACKLEAK is a security feature developed by Grsecurity/PaX (kudos to them), which:
- reduces the information that can be revealed through kernel stack leak bugs;
- blocks some uninitialized stack variable attacks (e.g. CVE-2010-2963);
- introduces some runtime checks for kernel stack overflow detection.

STACKLEAK instrumentation statistics
====================================

These numbers were obtained for the 4th version of the patch series.

Size of vmlinux (x86_64_defconfig):
file size:
- STACKLEAK disabled: 35014784 bytes
- STACKLEAK enabled: 35044952 bytes (+0.086%)
.text section size (calculated by size utility):
- STACKLEAK disabled: 10752983
- STACKLEAK enabled: 11062221 (+2.876%)

The readelf utility shows 45602 functions in vmlinux. The STACKLEAK gcc plugin inserted 36 check_alloca() calls and 1265 track_stack() calls (42274 calls are inserted during GIMPLE pass and 41009 calls are deleted during RTL pass). So 2.853% of functions are instrumented.

Further work
=============

- Think of erasing stack on the way out of exception handlers (idea by Andy Lutomirski).
- Rewrite erase_kstack() in C (if Ingo Molnar insists).
- Think of erasing the kernel stack after invoking EFI runtime services (idea by Mark Rutland).
- Try to port STACKLEAK to arm64 (Laura Abbott is working on it).

Changes in v5 (mostly according to the feedback from Ingo Molnar)
=================================================================

1. Introduced the CONFIG_STACKLEAK_METRICS providing STACKLEAK information about tasks via the /proc file system. That information can be useful for estimating the STACKLEAK performance impact for different workloads. In particular, /proc/<pid>/lowest_stack shows the current lowest_stack value and its final value from the previous syscall.

2. Introduced a single check_alloca() implementation working for both x86_64 and x86_32.

3. Fixed coding style issues and did some refactoring in the STACKLEAK gcc plugin.

4. Put the gcc plugin and the kernel stack erasing into separate (working) patches.

Changes in v4
=============

1. Introduced the CONFIG_STACKLEAK_TRACK_MIN_SIZE parameter instead of hard-coded track-lowest-sp.

2. Carefully looked into the assertions in track_stack() and check_alloca().
- Fixed the incorrect BUG() condition in track_stack(), thanks to Tycho Andersen. Also disabled that check if CONFIG_VMAP_STACK is enabled.
- Fixed the surplus and erroneous code for calculating stack_left in check_alloca() on x86_64. That code repeats the work which is already done in get_stack_info() and it misses the fact that different exception stacks on x86_64 have different size.

3. Introduced two lkdtm tests for the STACKLEAK feature (developed together with Tycho Andersen).

4. Added info about STACKLEAK to Documentation/security/self-protection.rst.

5. Improved the comments.

6. Decided not to change "unsigned long sp = (unsigned long)&sp" to current_stack_pointer. The original variant is more platform independent since current_stack_pointer has different type on x86 and arm.

Changes in v3
=============

1. Added the detailed comments describing the STACKLEAK gcc plugin. Read the plugin from the bottom up, like you do for Linux kernel drivers. Additional information:
- gcc internals documentation available from gcc sources;
- wonderful slides by Diego Novillo
https://www.airs.com/dnovillo/200711-GCC-Internals/
- nice introduction to gcc plugins at LWN
https://lwn.net/Articles/457543/

2. Improved the commit message and Kconfig description according to the feedback from Kees Cook. Also added the original notice describing the performance impact of the STACKLEAK feature.

3. Removed the arch-specific ix86_cmodel check in stackleak_track_stack_gate(). It caused skipping the kernel code instrumentation for i386.

4. Fixed a minor mistake in stackleak_tree_instrument_execute(). First versions of the plugin used ENTRY_BLOCK_PTR_FOR_FN(cfun)->next_bb to get the basic block with the function prologue. That was not correct since the control flow graph edge from ENTRY_BLOCK_PTR doesn't always go to that basic block.

So later it was changed to single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)), but not completely. next_bb was still used for entry_bb assignment, which could cause the wrong value of the prologue_instrumented variable.

I've reported this issue to Grsecurity and proposed the fix for it, but unfortunately didn't get any reply.

5. Introduced the STACKLEAK_POISON macro and renamed the config option according to the feedback from Kees Cook.

Alexander Popov (5):
x86/entry: Add STACKLEAK erasing the kernel stack at the end of
syscalls
gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
lkdtm: Add a test for STACKLEAK
fs/proc: Show STACKLEAK metrics in the /proc file system
doc: self-protection: Add information about STACKLEAK feature

 Documentation/security/self-protection.rst |  23 +-
 arch/Kconfig                               |  50 +++
 arch/x86/Kconfig                           |   1 +
 arch/x86/entry/common.c                    |  17 +-
 arch/x86/entry/entry_32.S                  |  73 +++++
 arch/x86/entry/entry_64.S                  |  99 ++++++
 arch/x86/entry/entry_64_compat.S           |   8 +
 arch/x86/include/asm/processor.h           |   7 +
 arch/x86/kernel/asm-offsets.c              |  12 +
 arch/x86/kernel/dumpstack.c                |  15 +
 arch/x86/kernel/process_32.c               |   8 +
 arch/x86/kernel/process_64.c               |   8 +
 drivers/misc/Makefile                      |   3 +
 drivers/misc/lkdtm.h                       |   4 +
 drivers/misc/lkdtm_core.c                  |   2 +
 drivers/misc/lkdtm_stackleak.c             | 139 +++++++++
 fs/exec.c                                  |  30 ++
 fs/proc/base.c                             |  14 +
 include/linux/compiler.h                   |   4 +
 scripts/Makefile.gcc-plugins               |   3 +
 scripts/gcc-plugins/stackleak_plugin.c     | 470 +++++++++++++++++++++++++++++
 21 files changed, 978 insertions(+), 12 deletions(-)
 create mode 100644 drivers/misc/lkdtm_stackleak.c
 create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH v5 1/5 x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the architecture-specific code filling the used part of the kernel stack with a poison value before returning to the userspace. Full STACKLEAK feature also contains the gcc plugin which comes in a separate commit.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---
 arch/Kconfig                     | 27 ++++++++++++
 arch/x86/Kconfig                 |  1 +
 arch/x86/entry/common.c          | 17 +++++--
 arch/x86/entry/entry_32.S        | 69 +++++++++++++++++++++++++++++
 arch/x86/entry/entry_64.S        | 95 ++++++++++++++++++++++++++++++++++++++++
 arch/x86/entry/entry_64_compat.S |  8 ++++
 arch/x86/include/asm/processor.h |  4 ++
 arch/x86/kernel/asm-offsets.c    |  9 ++++
 arch/x86/kernel/process_32.c     |  5 +++
 arch/x86/kernel/process_64.c     |  5 +++
 include/linux/compiler.h         |  4 ++
 11 files changed, 241 insertions(+), 3 deletions(-)
arch/Kconfig +27 -0
index d789a89..e9ec94c 100644
@@ -386,6 +386,13 @@ config SECCOMP_FILTER
 
 	  See Documentation/prctl/seccomp_filter.txt for details.
 
+config HAVE_ARCH_STACKLEAK
+	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
+	  value before returning from system calls.
+
 config HAVE_GCC_PLUGINS
 	bool
 	help
@@ -516,6 +523,26 @@ config GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
 	  in structures.  This reduces the performance hit of RANDSTRUCT
 	  at the cost of weakened randomization.
 
+config GCC_PLUGIN_STACKLEAK
+	bool "Erase the kernel stack before returning from syscalls"
+	depends on GCC_PLUGINS
+	depends on HAVE_ARCH_STACKLEAK
+	help
+	  This option makes the kernel erase the kernel stack before it
+	  returns from a system call. That reduces the information which
+	  kernel stack leak bugs can reveal and blocks some uninitialized
+	  stack variable attacks. This option also provides runtime checks
+	  for kernel stack overflow detection.
+
+	  The tradeoff is the performance impact: on a single CPU system kernel
+	  compilation sees a 1% slowdown, other systems and workloads may vary
+	  and you are advised to test this feature on your expected workload
+	  before deploying it.
+
+	  This plugin was ported from grsecurity/PaX. More information at:
+	   * https://grsecurity.net/
+	   * https://pax.grsecurity.net/
+
 config HAVE_CC_STACKPROTECTOR
 	bool
 	help
arch/x86/Kconfig +1 -0
index 971feac..b7da58f 100644
@@ -114,6 +114,7 @@ config X86
 	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if MMU && COMPAT
 	select HAVE_ARCH_COMPAT_MMAP_BASES	if MMU && COMPAT
 	select HAVE_ARCH_SECCOMP_FILTER
+	select HAVE_ARCH_STACKLEAK
 	select HAVE_ARCH_TRACEHOOK
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64
arch/x86/entry/common.c +14 -3
index 03505ff..075487e 100644
@@ -45,6 +45,12 @@ __visible inline void enter_from_user_mode(void)
 static inline void enter_from_user_mode(void) {}
 #endif
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+asmlinkage void erase_kstack(void);
+#else
+static void erase_kstack(void) {}
+#endif
+
 static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
 {
 #ifdef CONFIG_X86_64
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs)
 		emulated = true;
 
 	if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
-	    tracehook_report_syscall_entry(regs))
+	    tracehook_report_syscall_entry(regs)) {
+		erase_kstack();
 		return -1L;
+	}
 
 	if (emulated)
 		return -1L;
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs)
 			sd.args[5] = regs->bp;
 		}
 
-		ret = __secure_computing(&sd);
-		if (ret == -1)
+		ret = secure_computing(&sd);
+		if (ret == -1) {
+			erase_kstack();
 			return ret;
+		}
 	}
 #endif
 
@@ -127,6 +137,7 @@ static long syscall_trace_enter(struct pt_regs *regs)
 
 	do_audit_syscall_entry(regs, arch);
 
+	erase_kstack();
 	return ret ?: regs->orig_ax;
 }
 
arch/x86/entry/entry_32.S +69 -0

87 lines not on this page · raw message

arch/x86/entry/entry_64.S +95 -0

125 lines not on this page · raw message

arch/x86/entry/entry_64_compat.S +8 -0
index e26c25c..f79cbf4 100644
@@ -18,6 +18,12 @@
 
 	.section .entry.text, "ax"
 
+	.macro erase_kstack
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	call erase_kstack
+#endif
+	.endm
+
 /*
  * 32-bit SYSENTER entry.
  *
@@ -228,6 +234,7 @@ GLOBAL(entry_SYSCALL_compat_after_hwframe)
 
 	/* Opportunistic SYSRET */
 sysret32_from_system_call:
+	erase_kstack
 	TRACE_IRQS_ON			/* User mode traces as IRQs on. */
 	movq	RBX(%rsp), %rbx		/* pt_regs->rbx */
 	movq	RBP(%rsp), %rbp		/* pt_regs->rbp */
@@ -335,6 +342,7 @@ ENTRY(entry_INT80_compat)
 .Lsyscall_32_done:
 
 	/* Go back to user mode. */
+	erase_kstack
 	TRACE_IRQS_ON
 	SWAPGS
 	jmp	restore_regs_and_iret
arch/x86/include/asm/processor.h +4 -0
index b390ff7..c6eaf2d 100644
@@ -477,6 +477,10 @@ struct thread_struct {
 
 	mm_segment_t		addr_limit;
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	unsigned long		lowest_stack;
+#endif
+
 	unsigned int		sig_on_uaccess_err:1;
 	unsigned int		uaccess_err:1;	/* uaccess failed */
 
arch/x86/kernel/asm-offsets.c +9 -0
index de827d6..4ed7451 100644
@@ -37,6 +37,10 @@ void common(void) {
 	BLANK();
 	OFFSET(TASK_TI_flags, task_struct, thread_info.flags);
 	OFFSET(TASK_addr_limit, task_struct, thread.addr_limit);
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	OFFSET(TASK_lowest_stack, task_struct, thread.lowest_stack);
+	OFFSET(TASK_thread_sp0, task_struct, thread.sp0);
+#endif
 
 	BLANK();
 	OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
@@ -73,6 +77,11 @@ void common(void) {
 	OFFSET(PV_MMU_read_cr2, pv_mmu_ops, read_cr2);
 #endif
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	BLANK();
+	DEFINE(THREAD_SIZE_asm, THREAD_SIZE);
+#endif
+
 #ifdef CONFIG_XEN
 	BLANK();
 	OFFSET(XEN_vcpu_info_mask, vcpu_info, evtchn_upcall_mask);
arch/x86/kernel/process_32.c +5 -0
index 1196625..c7345d2 100644
@@ -136,6 +136,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 	p->thread.sp0 = (unsigned long) (childregs+1);
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	p->thread.lowest_stack = (unsigned long)task_stack_page(p) +
+						2 * sizeof(unsigned long);
+#endif
+
 	if (unlikely(p->flags & PF_KTHREAD)) {
 		/* kernel thread */
 		memset(childregs, 0, sizeof(struct pt_regs));
arch/x86/kernel/process_64.c +5 -0
index 302e7b2..65ba73f 100644
@@ -283,6 +283,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 	p->thread.sp = (unsigned long) fork_frame;
 	p->thread.io_bitmap_ptr = NULL;
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	p->thread.lowest_stack = (unsigned long)task_stack_page(p) +
+						2 * sizeof(unsigned long);
+#endif
+
 	savesegment(gs, p->thread.gsindex);
 	p->thread.gsbase = p->thread.gsindex ? 0 : me->thread.gsbase;
 	savesegment(fs, p->thread.fsindex);
include/linux/compiler.h +4 -0
index e95a263..916f02d 100644
@@ -624,4 +624,8 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
 	(_________p1); \
 })
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+# define STACKLEAK_POISON -0xBEEF
+#endif
+
 #endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.7.4
#6 lore raw

RFC PATCH v5 2/5 gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

In reply to Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the STACKLEAK gcc plugin. It is needed for:
- tracking the lowest border of the kernel stack, which is important for the code erasing the used part of the kernel stack at the end of syscalls (comes in a separate commit);
- checking that alloca calls don't cause stack overflow.

So this plugin instruments the kernel code inserting:
- the check_alloca() call before alloca and the track_stack() call

    after it;

- the track_stack() call for the functions with a stack frame size greater than or equal to CONFIG_STACKLEAK_TRACK_MIN_SIZE.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---
 arch/Kconfig                           |  12 +
 arch/x86/kernel/dumpstack.c            |  15 ++
 fs/exec.c                              |  30 +++
 scripts/Makefile.gcc-plugins           |   3 +
 scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++++++
 5 files changed, 530 insertions(+)
 create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
arch/Kconfig +12 -0
index e9ec94c..f2de598 100644
@@ -543,6 +543,18 @@ config GCC_PLUGIN_STACKLEAK
 	   * https://grsecurity.net/
 	   * https://pax.grsecurity.net/
 
+config STACKLEAK_TRACK_MIN_SIZE
+	int "Minimum stack frame size of functions tracked by STACKLEAK"
+	default 100
+	range 0 4096
+	depends on GCC_PLUGIN_STACKLEAK
+	help
+	  The STACKLEAK gcc plugin instruments the kernel code for tracking
+	  the lowest border of the kernel stack (and for some other purposes).
+	  It inserts the 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 HAVE_CC_STACKPROTECTOR
 	bool
 	help
arch/x86/kernel/dumpstack.c +15 -0
index f13b4c0..5a9b6cc 100644
@@ -315,3 +315,18 @@ static int __init code_bytes_setup(char *s)
 	return 1;
 }
 __setup("code_bytes=", code_bytes_setup);
+
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+void __used check_alloca(unsigned long size)
+{
+	unsigned long sp = (unsigned long)&sp;
+	struct stack_info stack_info = {0};
+	unsigned long visit_mask = 0;
+	unsigned long stack_left;
+
+	BUG_ON(get_stack_info(&sp, current, &stack_info, &visit_mask));
+	stack_left = sp - (unsigned long)stack_info.begin;
+	BUG_ON(stack_left < 256 || size >= stack_left - 256);
+}
+EXPORT_SYMBOL(check_alloca);
+#endif
fs/exec.c +30 -0
index 3e14ba2..481ef4b 100644
@@ -1958,3 +1958,33 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
 				  argv, envp, flags);
 }
 #endif
+
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+void __used track_stack(void)
+{
+	/*
+	 * N.B. The arch-specific part of the STACKLEAK feature fills the
+	 * kernel stack with the poison value, which has the register width.
+	 * That code assumes that the value of thread.lowest_stack is aligned
+	 * on the register width boundary.
+	 *
+	 * That is true for x86 and x86_64 because of the kernel stack
+	 * alignment on these platforms (for details, see cc_stack_align in
+	 * arch/x86/Makefile). Take care of that when you port STACKLEAK to
+	 * new platforms.
+	 */
+	unsigned long sp = (unsigned long)&sp;
+
+	if (sp < current->thread.lowest_stack &&
+	    sp >= (unsigned long)task_stack_page(current) +
+					2 * sizeof(unsigned long)) {
+		current->thread.lowest_stack = sp;
+	}
+
+#ifndef CONFIG_VMAP_STACK
+	if (unlikely((sp & (THREAD_SIZE - 1)) < (THREAD_SIZE / 16)))
+		BUG();
+#endif /* !CONFIG_VMAP_STACK */
+}
+EXPORT_SYMBOL(track_stack);
+#endif /* CONFIG_GCC_PLUGIN_STACKLEAK */
scripts/Makefile.gcc-plugins +3 -0
index d1f7b0d..3793c41 100644
@@ -34,6 +34,9 @@ ifdef CONFIG_GCC_PLUGINS
   gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT)	+= -DRANDSTRUCT_PLUGIN
   gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE)	+= -fplugin-arg-randomize_layout_plugin-performance-mode
 
+  gcc-plugin-$(CONFIG_GCC_PLUGIN_STACKLEAK)	+= stackleak_plugin.so
+  gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK)	+= -DSTACKLEAK_PLUGIN -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE)
+
   GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))
 
   export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGIN GCC_PLUGIN_SUBDIR
scripts/gcc-plugins/stackleak_plugin.c new file +470 -0

470 lines not on this page · raw message

Signature · 1 line
2.7.4
#17 lore raw

RFC PATCH v5 3/5 lkdtm: Add a test for STACKLEAK

In reply to Alexander Popov

Introduce two lkdtm tests for the STACKLEAK feature: STACKLEAK_CHECK_ALLOCA and STACKLEAK_TRACK_STACK. Both of them check that the current task stack is properly erased (filled with STACKLEAK_POISON).

In addition, STACKLEAK_CHECK_ALLOCA tests that:
- check_alloca() allows alloca calls which don't exhaust the kernel stack;
- alloca calls which exhaust/overflow the kernel stack hit BUG() in

    check_alloca().

And STACKLEAK_TRACK_STACK checks that exhausting the thread stack with a recursion is detected:
- it hits the BUG() in track_stack() if CONFIG_VMAP_STACK is not enabled,
- or hits the guard page otherwise.

---
 drivers/misc/Makefile          |   3 +
 drivers/misc/lkdtm.h           |   4 ++
 drivers/misc/lkdtm_core.c      |   2 +
 drivers/misc/lkdtm_stackleak.c | 139 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 148 insertions(+)
 create mode 100644 drivers/misc/lkdtm_stackleak.c
drivers/misc/Makefile +3 -0
index d84819d..105e16e 100644
@@ -63,6 +63,9 @@ lkdtm-$(CONFIG_LKDTM)		+= lkdtm_perms.o
 lkdtm-$(CONFIG_LKDTM)		+= lkdtm_refcount.o
 lkdtm-$(CONFIG_LKDTM)		+= lkdtm_rodata_objcopy.o
 lkdtm-$(CONFIG_LKDTM)		+= lkdtm_usercopy.o
+lkdtm-$(CONFIG_LKDTM)		+= lkdtm_stackleak.o
+
+KASAN_SANITIZE_lkdtm_stackleak.o := n
 
 KCOV_INSTRUMENT_lkdtm_rodata.o	:= n
 
drivers/misc/lkdtm.h +4 -0
index bfb6c45..5952339 100644
@@ -82,4 +82,8 @@ void lkdtm_USERCOPY_STACK_FRAME_FROM(void);
 void lkdtm_USERCOPY_STACK_BEYOND(void);
 void lkdtm_USERCOPY_KERNEL(void);
 
+/* lkdtm_stackleak.c */
+void lkdtm_STACKLEAK_CHECK_ALLOCA(void);
+void lkdtm_STACKLEAK_TRACK_STACK(void);
+
 #endif
drivers/misc/lkdtm_core.c +2 -0
index 981b3ef..264d948 100644
@@ -251,6 +251,8 @@ struct crashtype crashtypes[] = {
 	CRASHTYPE(USERCOPY_STACK_FRAME_FROM),
 	CRASHTYPE(USERCOPY_STACK_BEYOND),
 	CRASHTYPE(USERCOPY_KERNEL),
+	CRASHTYPE(STACKLEAK_CHECK_ALLOCA),
+	CRASHTYPE(STACKLEAK_TRACK_STACK),
 };
 
 
drivers/misc/lkdtm_stackleak.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.7.4
#18 lore raw

RFC PATCH v5 4/5 fs/proc: Show STACKLEAK metrics in the /proc file system

In reply to Alexander Popov

Introduce CONFIG_STACKLEAK_METRICS providing STACKLEAK information about tasks via the /proc file system. In particular, /proc/<pid>/lowest_stack shows the current lowest_stack value and its final value from the previous syscall. That information can be useful for estimating the STACKLEAK performance impact for different workloads.

---
 arch/Kconfig                     | 11 +++++++++++
 arch/x86/entry/entry_32.S        |  4 ++++
 arch/x86/entry/entry_64.S        |  4 ++++
 arch/x86/include/asm/processor.h |  3 +++
 arch/x86/kernel/asm-offsets.c    |  3 +++
 arch/x86/kernel/process_32.c     |  3 +++
 arch/x86/kernel/process_64.c     |  3 +++
 fs/proc/base.c                   | 14 ++++++++++++++
 8 files changed, 45 insertions(+)
arch/Kconfig +11 -0
index f2de598..c48d828 100644
@@ -555,6 +555,17 @@ config STACKLEAK_TRACK_MIN_SIZE
 	  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
+	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>/lowest_stack
+	  shows the current lowest_stack value and its final value from the
+	  previous syscall. That information can be useful for estimating
+	  the STACKLEAK performance impact for your workloads.
+
 config HAVE_CC_STACKPROTECTOR
 	bool
 	help
arch/x86/entry/entry_32.S +4 -0
index a7b0c52..4f3f2ff 100644
@@ -115,6 +115,10 @@ ENTRY(erase_kstack)
 	mov	%esp, %ecx
 	sub	%edi, %ecx
 
+#ifdef CONFIG_STACKLEAK_METRICS
+	mov	%edi, TASK_prev_lowest_stack(%ebp)
+#endif
+
 	cmp	$THREAD_SIZE_asm, %ecx
 	jb	3f
 	ud2
arch/x86/entry/entry_64.S +4 -0
index 189d843..fbf7f1c 100644
@@ -116,6 +116,10 @@ ENTRY(erase_kstack)
 	mov	%esp, %ecx
 	sub	%edi, %ecx
 
+#ifdef CONFIG_STACKLEAK_METRICS
+	mov	%rdi, TASK_prev_lowest_stack(%r11)
+#endif
+
 	/* Check that the counter value is sane. */
 	cmp	$THREAD_SIZE_asm, %rcx
 	jb	3f
arch/x86/include/asm/processor.h +3 -0
index c6eaf2d..8e3f2ef 100644
@@ -479,6 +479,9 @@ struct thread_struct {
 
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	unsigned long		lowest_stack;
+# ifdef CONFIG_STACKLEAK_METRICS
+	unsigned long		prev_lowest_stack;
+# endif
 #endif
 
 	unsigned int		sig_on_uaccess_err:1;
arch/x86/kernel/asm-offsets.c +3 -0
index 4ed7451..673495d 100644
@@ -40,6 +40,9 @@ void common(void) {
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	OFFSET(TASK_lowest_stack, task_struct, thread.lowest_stack);
 	OFFSET(TASK_thread_sp0, task_struct, thread.sp0);
+# ifdef CONFIG_STACKLEAK_METRICS
+	OFFSET(TASK_prev_lowest_stack, task_struct, thread.prev_lowest_stack);
+# endif
 #endif
 
 	BLANK();
arch/x86/kernel/process_32.c +3 -0
index c7345d2..0e4fa3c 100644
@@ -139,6 +139,9 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	p->thread.lowest_stack = (unsigned long)task_stack_page(p) +
 						2 * sizeof(unsigned long);
+# ifdef CONFIG_STACKLEAK_METRICS
+	p->thread.prev_lowest_stack = p->thread.lowest_stack;
+# endif
 #endif
 
 	if (unlikely(p->flags & PF_KTHREAD)) {
arch/x86/kernel/process_64.c +3 -0
index 65ba73f..50d019c 100644
@@ -286,6 +286,9 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	p->thread.lowest_stack = (unsigned long)task_stack_page(p) +
 						2 * sizeof(unsigned long);
+# ifdef CONFIG_STACKLEAK_METRICS
+	p->thread.prev_lowest_stack = p->thread.lowest_stack;
+# endif
 #endif
 
 	savesegment(gs, p->thread.gsindex);
fs/proc/base.c +14 -0
index ad3b076..7c3e127 100644
@@ -2884,6 +2884,17 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_LIVEPATCH */
 
+#ifdef CONFIG_STACKLEAK_METRICS
+static int proc_lowest_stack(struct seq_file *m, struct pid_namespace *ns,
+				struct pid *pid, struct task_struct *task)
+{
+	seq_printf(m, "prev_lowest_stack: %pK\nlowest_stack: %pK\n",
+		   (void *)task->thread.prev_lowest_stack,
+		   (void *)task->thread.lowest_stack);
+	return 0;
+}
+#endif /* CONFIG_STACKLEAK_METRICS */
+
 /*
  * Thread groups
  */
@@ -2988,6 +2999,9 @@ 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
+	ONE("lowest_stack", S_IRUGO, proc_lowest_stack),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Signature · 1 line
2.7.4
#19 lore raw

RFC PATCH v5 5/5 doc: self-protection: Add information about STACKLEAK feature

In reply to Alexander Popov

Add information about STACKLEAK feature to "Stack depth overflow" and "Memory poisoning" sections of self-protection.rst.

---
 Documentation/security/self-protection.rst | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)
Documentation/security/self-protection.rst +14 -9
index 60c8bd8..9693a90 100644
@@ -165,10 +165,15 @@ Stack depth overflow
 A less well understood attack is using a bug that triggers the
 kernel to consume stack memory with deep function calls or large stack
 allocations. With this attack it is possible to write beyond the end of
-the kernel's preallocated stack space and into sensitive structures. Two
-important changes need to be made for better protections: moving the
-sensitive thread_info structure elsewhere, and adding a faulting memory
-hole at the bottom of the stack to catch these overflows.
+the kernel's preallocated stack space and into sensitive structures.
+The combination of the following measures gives better protection:
+
+* moving the sensitive thread_info structure off the stack
+  (``CONFIG_THREAD_INFO_IN_TASK``);
+* adding a faulting memory hole at the bottom of the stack to catch
+  these overflows (``CONFIG_VMAP_STACK``);
+* runtime checking that alloca() calls don't overstep the stack boundary
+  (``CONFIG_GCC_PLUGIN_STACKLEAK``).
 
 Heap memory integrity
 ---------------------
@@ -287,11 +292,11 @@ sure structure holes are cleared.
 Memory poisoning
 ----------------
 
-When releasing memory, it is best to poison the contents (clear stack on
-syscall return, wipe heap memory on a free), to avoid reuse attacks that
-rely on the old contents of memory. This frustrates many uninitialized
-variable attacks, stack content exposures, heap content exposures, and
-use-after-free attacks.
+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
+free. This frustrates many uninitialized variable attacks, stack content
+exposures, heap content exposures, and use-after-free attacks.
 
 Destination tracking
 --------------------
Signature · 1 line
2.7.4
maintainer
#20 lore raw
In reply to Alexander Popov
Quoted 9 lines · Alexander Popov

These numbers were obtained for the 4th version of the patch series.

Size of vmlinux (x86_64_defconfig):
file size:
- STACKLEAK disabled: 35014784 bytes
- STACKLEAK enabled: 35044952 bytes (+0.086%)
.text section size (calculated by size utility):
- STACKLEAK disabled: 10752983
- STACKLEAK enabled: 11062221 (+2.876%)

Why no runtime costs? This should not be hard to measure.

Quoted 4 lines

Further work
=============

- Rewrite erase_kstack() in C (if Ingo Molnar insists).

Aside from legacy, is there any sane reason that stuff is in ASM? That is, I too will insist it being in C unless you can provide good arguments on why it needs be asm. All it does is memzero() right? And I would think architectures already have fairly optimized implementations of that around.

#21 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Alexander Popov

Further work
=============

- Rewrite erase_kstack() in C (if Ingo Molnar insists).

Aside from legacy, is there any sane reason that stuff is in ASM? That is, I too will insist it being in C unless you can provide good arguments on why it needs be asm. All it does is memzero() right? And I would think architectures already have fairly optimized implementations of that around.

One argument for this being in asm is that you can't control the stack usage of a C implementation of erase_kstack(), which leaves a (small) portion of the stack potentially not cleared depending on what exactly the compiler does with the stack.

If it's just a call to memzero(), the asm should be very simple, and I'd think it's better to err on the side of clearing as much of the stack as possible.

Thanks,
Mark.

#23 lore raw
In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 9 lines · Alexander Popov

These numbers were obtained for the 4th version of the patch series.

Size of vmlinux (x86_64_defconfig):
file size:
- STACKLEAK disabled: 35014784 bytes
- STACKLEAK enabled: 35044952 bytes (+0.086%)
.text section size (calculated by size utility):
- STACKLEAK disabled: 10752983
- STACKLEAK enabled: 11062221 (+2.876%)

Quoted 1 line

Why no runtime costs? This should not be hard to measure.

Hello Peter,

Here are the results of a brief performance test on x86_64 (for the 2nd version of the patch). The numbers are very different because the STACKLEAK performance penalty depends on the workloads.

Hardware: Intel Core i7-4770, 16 GB RAM

Test #1: hackbench -s 4096 -l 2000 -g 15 -f 25 -P Average result on 4.11-rc8: 8.71s
Average result on 4.11-rc8+stackleak: 9.08s (+4.29%)

Test #2: building the Linux kernel with Ubuntu config (time make -j9) Result on 4.11-rc8:
real 32m14.893s
user 237m30.886s
sys 11m12.273s
Result on 4.11-rc8+stackleak:
real 32m26.881s (+0.62%)
user 238m38.926s (+0.48%)
sys 11m36.426s (+3.59%)

Test #3: stress-ng --cpu 4 --io 2 --vm 2 --vm-bytes 2G --timeout 300s
--metrics-brief
Result on 4.11-rc8:
cpu bogo ops 269955
iosync bogo ops 9809985
vm bogo ops 17093608
Result on 4.11-rc8+stackleak:
cpu bogo ops 270106 (+0.6%)
iosync bogo ops 9474535 (-3.42%)
vm bogo ops 17093608 (the same)

So the STACKLEAK description in Kconfig includes:
"The tradeoff is the performance impact: on a single CPU system kernel compilation sees a 1% slowdown, other systems and workloads may vary and you are advised to test this feature on your expected workload before deploying it".

Best regards,
Alexander

#22 lore raw
In reply to Mark Rutland

Hello Peter and Mark,

Quoted 5 lines · Mark Rutland
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Alexander Popov

Further work
=============

Quoted 7 lines
Quoted 7 lines
Quoted 1 line

- Rewrite erase_kstack() in C (if Ingo Molnar insists).

Aside from legacy, is there any sane reason that stuff is in ASM? That is, I too will insist it being in C unless you can provide good arguments on why it needs be asm. All it does is memzero() right? And I would think architectures already have fairly optimized implementations of that around.

I added very detailed comments describing erase_kstack() on x86_64. It is quite tricky for being fast.

It starts from the lowest_stack address and goes down to the bottom of the stack searching for the poison value (-0xBEEF). If it reaches the stack bottom or finds 17 poison values in a row, it starts to fill the memory between the current position and the stack pointer with a poison value. Finally it resets the lowest_stack value. So only used part of the kernel stack is actually erased.

I really like the PaX Team's code in erase_kstack().

Quoted 8 lines

One argument for this being in asm is that you can't control the stack usage of a C implementation of erase_kstack(), which leaves a (small) portion of the stack potentially not cleared depending on what exactly the compiler does with the stack.

If it's just a call to memzero(), the asm should be very simple, and I'd think it's better to err on the side of clearing as much of the stack as possible.

Laura Abbott wrote about her issues with erase_kstack() and gcc in the previous
thread: http://www.openwall.com/lists/kernel-hardening/2017/10/10/3

Anyway, I'll obey the final decision.

Best regards,
Alexander

#2 lore raw

[kernel-hardening] Re: [PATCH RFC v5 1/5] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

In reply to Alexander Popov
Quoted 121 lines · Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the architecture-specific code filling the used part of the kernel stack with a poison value before returning to the userspace. Full STACKLEAK feature also contains the gcc plugin which comes in a separate commit.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---
 arch/Kconfig                     | 27 ++++++++++++
 arch/x86/Kconfig                 |  1 +
 arch/x86/entry/common.c          | 17 +++++--
 arch/x86/entry/entry_32.S        | 69 +++++++++++++++++++++++++++++
 arch/x86/entry/entry_64.S        | 95 ++++++++++++++++++++++++++++++++++++++++
 arch/x86/entry/entry_64_compat.S |  8 ++++
 arch/x86/include/asm/processor.h |  4 ++
 arch/x86/kernel/asm-offsets.c    |  9 ++++
 arch/x86/kernel/process_32.c     |  5 +++
 arch/x86/kernel/process_64.c     |  5 +++
 include/linux/compiler.h         |  4 ++
 11 files changed, 241 insertions(+), 3 deletions(-)
arch/Kconfig +27 -0
index d789a89..e9ec94c 100644
@@ -386,6 +386,13 @@ config SECCOMP_FILTER
 
 	  See Documentation/prctl/seccomp_filter.txt for details.
 
+config HAVE_ARCH_STACKLEAK
+	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
+	  value before returning from system calls.
+
 config HAVE_GCC_PLUGINS
 	bool
 	help
@@ -516,6 +523,26 @@ config GCC_PLUGIN_RANDSTRUCT_PERFORMANCE
 	  in structures.  This reduces the performance hit of RANDSTRUCT
 	  at the cost of weakened randomization.
 
+config GCC_PLUGIN_STACKLEAK
+	bool "Erase the kernel stack before returning from syscalls"
+	depends on GCC_PLUGINS
+	depends on HAVE_ARCH_STACKLEAK
+	help
+	  This option makes the kernel erase the kernel stack before it
+	  returns from a system call. That reduces the information which
+	  kernel stack leak bugs can reveal and blocks some uninitialized
+	  stack variable attacks. This option also provides runtime checks
+	  for kernel stack overflow detection.
+
+	  The tradeoff is the performance impact: on a single CPU system kernel
+	  compilation sees a 1% slowdown, other systems and workloads may vary
+	  and you are advised to test this feature on your expected workload
+	  before deploying it.
+
+	  This plugin was ported from grsecurity/PaX. More information at:
+	   * https://grsecurity.net/
+	   * https://pax.grsecurity.net/
+
 config HAVE_CC_STACKPROTECTOR
 	bool
 	help
arch/x86/Kconfig +1 -0
index 971feac..b7da58f 100644
@@ -114,6 +114,7 @@ config X86
 	select HAVE_ARCH_MMAP_RND_COMPAT_BITS	if MMU && COMPAT
 	select HAVE_ARCH_COMPAT_MMAP_BASES	if MMU && COMPAT
 	select HAVE_ARCH_SECCOMP_FILTER
+	select HAVE_ARCH_STACKLEAK
 	select HAVE_ARCH_TRACEHOOK
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE
 	select HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD if X86_64
arch/x86/entry/common.c +9 -1
index 03505ff..075487e 100644
@@ -45,6 +45,12 @@ __visible inline void enter_from_user_mode(void)
 static inline void enter_from_user_mode(void) {}
 #endif
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+asmlinkage void erase_kstack(void);
+#else
+static void erase_kstack(void) {}
+#endif
+
 static void do_audit_syscall_entry(struct pt_regs *regs, u32 arch)
 {
 #ifdef CONFIG_X86_64
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs)
 		emulated = true;
 
 	if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
-	    tracehook_report_syscall_entry(regs))
+	    tracehook_report_syscall_entry(regs)) {
+		erase_kstack();
 		return -1L;
+	}
 
 	if (emulated)
 		return -1L;

arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 127

Quoted 6 lines
arch/x86/entry/common.c +1 -2
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs)
 			sd.args[5] = regs->bp;
 		}
 
-		ret = __secure_computing(&sd);
-		if (ret == -1)
+		ret = secure_computing(&sd);

Is there any reason to switch this from the __ version? This basically adds an additional check on the TIF_SECCOMP flag, but I'm not sure that's intentional with this patch.

Cheers,

Tycho

#3 lore raw

[kernel-hardening] Re: [PATCH RFC v5 1/5] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

Quoted 22 lines · Tycho Andersen
Quoted 21 lines · Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the architecture-specific code filling the used part of the kernel stack with a poison value before returning to the userspace. Full STACKLEAK feature also contains the gcc plugin which comes in a separate commit.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---

[...]

Quoted 16 lines
Quoted 16 lines
arch/x86/entry/common.c +3 -1
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs)
 		emulated = true;
 
 	if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
-	    tracehook_report_syscall_entry(regs))
+	    tracehook_report_syscall_entry(regs)) {
+		erase_kstack();
 		return -1L;
+	}
 
 	if (emulated)
 		return -1L;
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs)
 			sd.args[5] = regs->bp;
 		}
 
Quoted 7 lines

arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 127

Quoted 3 lines
arch/x86/entry/common.c +1 -2
-		ret = __secure_computing(&sd);
-		if (ret == -1)
+		ret = secure_computing(&sd);

Is there any reason to switch this from the __ version? This basically adds an additional check on the TIF_SECCOMP flag, but I'm not sure that's intentional with this patch.

Hello Tycho, thanks for your remark!

Initially I took this change from the grsecurity patch, because it looked reasonable for me at that time. But now I doubt, thank you.

Kees and Andy (Lutomirski), you are the authors of syscall_trace_enter(). Could you please have a look at this change?

By the way, it seems that one erase_kstack() call is missing in that function. Could you please have a glance at the places where erase_kstack() is called?

Thanks in advance.
Best regards,
Alexander

#7 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

In reply to Alexander Popov
Quoted 36 lines · Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the STACKLEAK gcc plugin. It is needed for:
- tracking the lowest border of the kernel stack, which is important for the code erasing the used part of the kernel stack at the end of syscalls (comes in a separate commit);
- checking that alloca calls don't cause stack overflow.

So this plugin instruments the kernel code inserting:
- the check_alloca() call before alloca and the track_stack() call

    after it;

- the track_stack() call for the functions with a stack frame size greater than or equal to CONFIG_STACKLEAK_TRACK_MIN_SIZE.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---
 arch/Kconfig                           |  12 +
 arch/x86/kernel/dumpstack.c            |  15 ++
 fs/exec.c                              |  30 +++
 scripts/Makefile.gcc-plugins           |   3 +
 scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++++++
 5 files changed, 530 insertions(+)
 create mode 100644 scripts/gcc-plugins/stackleak_plugin.c

[...]

Quoted 24 lines
fs/exec.c +16 -0
index 3e14ba2..481ef4b 100644
@@ -1958,3 +1958,33 @@ COMPAT_SYSCALL_DEFINE5(execveat, int, fd,
 				  argv, envp, flags);
 }
 #endif
+
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+void __used track_stack(void)
+{
+	/*
+	 * N.B. The arch-specific part of the STACKLEAK feature fills the
+	 * kernel stack with the poison value, which has the register width.
+	 * That code assumes that the value of thread.lowest_stack is aligned
+	 * on the register width boundary.
+	 *
+	 * That is true for x86 and x86_64 because of the kernel stack
+	 * alignment on these platforms (for details, see cc_stack_align in
+	 * arch/x86/Makefile). Take care of that when you port STACKLEAK to
+	 * new platforms.
+	 */
+	unsigned long sp = (unsigned long)&sp;

fs/exec.c COMPAT_SYSCALL_DEFINE5(execveat, int, fd, · line 1986

Quoted 10 lines
fs/exec.c +10 -0
+
+	if (sp < current->thread.lowest_stack &&
+	    sp >= (unsigned long)task_stack_page(current) +
+					2 * sizeof(unsigned long)) {
+		current->thread.lowest_stack = sp;
+	}
+
+#ifndef CONFIG_VMAP_STACK
+	if (unlikely((sp & (THREAD_SIZE - 1)) < (THREAD_SIZE / 16)))
+		BUG();

Hello, I need your help! I'm trying to solve the problem with the recursive BUG() here (currently on x86_64).

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Finally the stack pointer oversteps the stack bottom and the bug is not hit anymore, because (sp & (THREAD_SIZE - 1)) is big again. And we have such an oops message printed:

[   17.529075] ------------[ cut here ]------------
[   17.529075] kernel BUG at fs/exec.c:1986!
[   17.529075] invalid opcode: 0000 [#1] SMP
[   17.529075] Dumping ftrace buffer:
[   17.529075]    (ftrace buffer empty)
[   17.529075] Modules linked in: lkdtm
[   17.529075] CPU: 0 PID: 2651 Comm: sh Not tainted 4.14.0-rc5+ #10
[   17.529075] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
Ubuntu-1.8.2-1ubuntu1 04/01/2014
[   17.529075] task: ffff880079950040 task.stack: ffff88007a904000
[   17.529075] RIP: 0010:track_stack+0x52/0x60
[   17.529075] RSP: 0018:ffff88007a904078 EFLAGS: 00010193
[   17.529075] RAX: 0000000000000078 RBX: 0000000000000006 RCX: ffff88007a904010
[   17.529075] RDX: ffff880079950040 RSI: ffff88007a904000 RDI: ffff88007a904168
[   17.529075] RBP: ffff88007a904080 R08: 0000000000000004 R09: 000000000000018c
[   17.529075] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88007a904168
[   17.529075] R13: 0000000000000004 R14: ffffffff81bd33c9 R15: 0000000000000000
[   17.529075] FS:  00007f469977e700(0000) GS:ffff88007fc00000(0000)
knlGS:0000000000000000
[   17.529075] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   17.529075] CR2: 00000000021ab618 CR3: 000000007a404000 CR4: 00000000000006f0
[   17.529075] Call Trace:
[   17.529075]  do_error_trap+0x25/0xe0
[   17.529075]  do_invalid_op+0x2a/0x30
[   17.529075]  invalid_op+0x18/0x20
[   17.529075] RIP: 0010:track_stack+0x52/0x60
[   17.529075] RSP: 0018:ffff88007a904218 EFLAGS: 00010193
[   17.529075] RAX: 0000000000000218 RBX: 0000000000000006 RCX: ffff88007a904010
[   17.529075] RDX: ffff880079950040 RSI: ffff88007a904000 RDI: ffff88007a904308
[   17.529075] RBP: ffff88007a904220 R08: 0000000000000004 R09: 000000000000018c
[   17.529075] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88007a904308
[   17.529075] R13: 0000000000000004 R14: ffffffff81bd33c9 R15: 0000000000000000
[   17.529075]  do_error_trap+0x25/0xe0
[   17.529075]  do_invalid_op+0x2a/0x30
[   17.529075]  invalid_op+0x18/0x20
[   17.529075] RIP: 0010:track_stack+0x52/0x60
[   17.529075] RSP: 0018:ffff88007a9043b8 EFLAGS: 00010393
[   17.529075] RAX: 00000000000003b8 RBX: ffff88007a907d90 RCX: ffff88007a904010
[   17.529075] RDX: ffff880079950040 RSI: ffff88007a904000 RDI: ffff88007a907d90
[   17.529075] RBP: ffff88007a9043c0 R08: 0000000000000000 R09: 000000000000018c
[   17.529075] R10: 0000000000000000 R11: 0000000000000000 R12: 00000000000003d0
[   17.529075] R13: ffffffffc0007020 R14: 0000000000000016 R15: 000000000000003d
[   17.529075]  recursion+0x14/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  ? put_dec+0x24/0xb0
[   17.529075]  ? put_dec+0x24/0xb0
[   17.529075]  ? number+0x2c5/0x2e0
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  ? vsnprintf+0xd1/0x4b0
[   17.529075]  ? wait_for_xmitr+0x32/0x90
[   17.529075]  ? serial8250_console_putchar+0x25/0x30
[   17.529075]  ? wait_for_xmitr+0x90/0x90
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  ? up+0x30/0x50
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  ? vprintk_emit+0x24a/0x2d0
[   17.529075]  recursion+0x58/0x70 [lkdtm]
[   17.529075]  ? vprintk_func+0x31/0x80
[   17.529075]  ? printk+0x4a/0x55
[   17.529075]  lkdtm_STACKLEAK_TRACK_STACK+0x39/0x4d [lkdtm]
[   17.529075]  lkdtm_do_action+0x18/0x20 [lkdtm]
[   17.529075]  direct_entry+0xcc/0x130 [lkdtm]
[   17.529075]  full_proxy_write+0x4f/0x90
[   17.529075]  __vfs_write+0x36/0x140
[   17.529075]  ? security_file_permission+0x36/0xb0
[   17.529075]  vfs_write+0xb1/0x1a0
[   17.529075]  SyS_write+0x46/0xa0
[   17.529075]  entry_SYSCALL_64_fastpath+0x1a/0xaa
[   17.529075] RIP: 0033:0x7f46992a1600
[   17.529075] RSP: 002b:00007fffe4466838 EFLAGS: 00000246 ORIG_RAX:
0000000000000001
[   17.529075] RAX: ffffffffffffffda RBX: 000000000000006b RCX: 00007f46992a1600
[   17.529075] RDX: 0000000000000016 RSI: 00000000021a9610 RDI: 0000000000000001
[   17.529075] RBP: 0000000000002710 R08: 0000000000000003 R09: 0000000000002010
[   17.529075] R10: 0000000000000871 R11: 0000000000000246 R12: 00007f469955eb58
[   17.529075] R13: 0000000000002010 R14: 00000000021a9600 R15: 00007f469955eb00
[   17.529075] Code: 8d 4e 10 48 39 c8 73 0f 25 ff 3f 00 00 48 3d ff 03 00 00 76
16 c9 c3 48 89 82 30 0a 00 00 25 ff 3f 00 00 48 3d ff 03 00 00 77 ea <0f> 0b 66
90 66 2e 0f 1f 84 00 00 00 00 00 55 48 89 e5 41 54 53
[   17.529075] RIP: track_stack+0x52/0x60 RSP: ffff88007a904078
[   17.529075] ---[ end trace 3f0d8f585f4dcc08 ]---

But printing this message spoils the memory below the exhausted stack, right?

Can we somehow handle this BUG() in another stack to avoid the recursion?

Thanks!

fs/exec.c COMPAT_SYSCALL_DEFINE5(execveat, int, fd, · line 1990

Quoted 4 lines
fs/exec.c +4 -0
+#endif /* !CONFIG_VMAP_STACK */
+}
+EXPORT_SYMBOL(track_stack);
+#endif /* CONFIG_GCC_PLUGIN_STACKLEAK */

[...]

Best regards,
Alexander

maintainer
#8 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Quoted 4 lines · Alexander Popov

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Add a __attribute__((nostacktrack)) on it?

#9 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Hello Peter,

Thanks for your reply.

Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Alexander Popov

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Add a __attribute__((nostacktrack)) on it?

Yes, I already tried some blacklisting in the plugin, but it didn't really help, because:

1. there are other (more than 5) instrumented functions, that are called during BUG() handling too;

2. decreasing CONFIG_STACKLEAK_TRACK_MIN_SIZE would add more instrumented functions, which should be manually blacklisted (not good).

I guess handling BUG() in another stack would be a solution. For example, Andy Lutomirski calls handle_stack_overflow in the DOUBLEFAULT_STACK (arch/x86/mm/fault.c). Should I do something similar?

Thanks!

Best regards,
Alexander

#4 lore raw

[kernel-hardening] Re: [PATCH RFC v5 1/5] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

In reply to Alexander Popov
Quoted 50 lines · Alexander Popov
Quoted 22 lines · Tycho Andersen
Quoted 21 lines · Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the architecture-specific code filling the used part of the kernel stack with a poison value before returning to the userspace. Full STACKLEAK feature also contains the gcc plugin which comes in a separate commit.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---

[...]

Quoted 16 lines
Quoted 16 lines
arch/x86/entry/common.c +3 -1
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs)
             emulated = true;
 
     if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
-        tracehook_report_syscall_entry(regs))
+        tracehook_report_syscall_entry(regs)) {
+            erase_kstack();
             return -1L;
+    }
 
     if (emulated)
             return -1L;
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs)
                     sd.args[5] = regs->bp;
             }
 
Quoted 7 lines

arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 127

Quoted 3 lines
arch/x86/entry/common.c +1 -2
-            ret = __secure_computing(&sd);
-            if (ret == -1)
+            ret = secure_computing(&sd);

Is there any reason to switch this from the __ version? This basically adds an additional check on the TIF_SECCOMP flag, but I'm not sure that's intentional with this patch.

Quoted 4 lines

Hello Tycho, thanks for your remark!

Initially I took this change from the grsecurity patch, because it looked reasonable for me at that time. But now I doubt, thank you.

Yeah, I'd prefer this stay __secure_computing().

Quoted 5 lines

Kees and Andy (Lutomirski), you are the authors of syscall_trace_enter(). Could you please have a look at this change?

By the way, it seems that one erase_kstack() call is missing in that function. Could you please have a glance at the places where erase_kstack() is called?

Errr, wouldn't erase_kstack() get called outside of seccomp? (i.e. via syscall_return_slowpath() or something later in the return path?)

Or is there some reason for erasing the stack after seccomp processing but before running the syscall?

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw

[kernel-hardening] Re: [PATCH RFC v5 1/5] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls

Hello Kees,

Quoted 51 lines · Kees Cook
Quoted 50 lines · Alexander Popov
Quoted 22 lines · Tycho Andersen
Quoted 21 lines · Alexander Popov

The STACKLEAK feature erases the kernel stack before returning from syscalls. That reduces the information which kernel stack leak bugs can reveal and blocks some uninitialized stack variable attacks. Moreover, STACKLEAK provides runtime checks for kernel stack overflow detection.

This commit introduces the architecture-specific code filling the used part of the kernel stack with a poison value before returning to the userspace. Full STACKLEAK feature also contains the gcc plugin which comes in a separate commit.

The STACKLEAK feature is ported from grsecurity/PaX. More information at:
https://grsecurity.net/
https://pax.grsecurity.net/

This code is modified from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on our understanding of the code. Changes or omissions from the original code are ours and don't reflect the original grsecurity/PaX code.

---

[...]

Quoted 23 lines
Quoted 13 lines
arch/x86/entry/common.c +3 -1
@@ -81,8 +87,10 @@ static long syscall_trace_enter(struct pt_regs *regs)
             emulated = true;
 
     if ((emulated || (work & _TIF_SYSCALL_TRACE)) &&
-        tracehook_report_syscall_entry(regs))
+        tracehook_report_syscall_entry(regs)) {
+            erase_kstack();
             return -1L;
+    }
 
     if (emulated)
             return -1L;

arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 127

Quoted 6 lines
arch/x86/entry/common.c +1 -2
@@ -116,9 +124,11 @@ static long syscall_trace_enter(struct pt_regs *regs)
                     sd.args[5] = regs->bp;
             }
 
-            ret = __secure_computing(&sd);
-            if (ret == -1)
+            ret = secure_computing(&sd);

Is there any reason to switch this from the __ version? This basically adds an additional check on the TIF_SECCOMP flag, but I'm not sure that's intentional with this patch.

Quoted 6 lines
Quoted 4 lines

Hello Tycho, thanks for your remark!

Initially I took this change from the grsecurity patch, because it looked reasonable for me at that time. But now I doubt, thank you.

Yeah, I'd prefer this stay __secure_computing().

Ok.

Quoted 8 lines
Quoted 5 lines

Kees and Andy (Lutomirski), you are the authors of syscall_trace_enter(). Could you please have a look at this change?

By the way, it seems that one erase_kstack() call is missing in that function. Could you please have a glance at the places where erase_kstack() is called?

Errr, wouldn't erase_kstack() get called outside of seccomp? (i.e. via syscall_return_slowpath() or something later in the return path?)

Yes, erase_kstack() is called later in entry_SYSCALL_64 just after do_syscall_64.

Quoted 2 lines

Or is there some reason for erasing the stack after seccomp processing but before running the syscall?

Ah, it seems that PaX Team is doing that intentionally. I guess I've found the proof in his slides for H2HC conference (slide 38):
https://pax.grsecurity.net/docs/PaXTeam-H2HC13-PaX-gcc-plugins.pdf

"- Special paths for ptrace/auditing
-- Low-level kernel entry/exit paths can diverge for ptrace/auditing and leave interesting information on the stack for the actual syscall code"

Do you know which kind of interesting information is mentioned?

And again, erase_kstack() is not called before 1 of 4 return statements in syscall_trace_enter(). IMHO it is a bug. Could you please verify that? Thanks!

Best regards,
Alexander

#10 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

In reply to Alexander Popov
Quoted 15 lines · Alexander Popov
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Alexander Popov

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Add a __attribute__((nostacktrack)) on it?

Yes, I already tried some blacklisting in the plugin, but it didn't really help, because:

1. there are other (more than 5) instrumented functions, that are called during BUG() handling too;

Quoted 6 lines

2. decreasing CONFIG_STACKLEAK_TRACK_MIN_SIZE would add more instrumented functions, which should be manually blacklisted (not good).

I guess handling BUG() in another stack would be a solution. For example, Andy Lutomirski calls handle_stack_overflow in the DOUBLEFAULT_STACK (arch/x86/mm/fault.c). Should I do something similar?

Hello Andy! May I ask your advice?

When CONFIG_VMAP_STACK is disabled and STACKLEAK is enabled (for example, on x86_32), we need another way to detect stack depth overflow. That is the reason of having this BUG() in track_stack(). But it turns out to be recursive since track_stack() will be called again during BUG() handling.

We can avoid that recursion by handling oops in another stack. It looks similar to the way you call handle_stack_overflow() in arch/x86/mm/fault.c. But it seems that I can't reuse that code, am I right?

How should I do it properly?

By the way, you wrote that you have some entry code changes which conflict with STACKLEAK. May I ask for more details?

Best regards,
Alexander

maintainer
#11 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Quoted 23 lines · Alexander Popov
Quoted 15 lines · Alexander Popov
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Alexander Popov

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Add a __attribute__((nostacktrack)) on it?

Yes, I already tried some blacklisting in the plugin, but it didn't really help, because:

1. there are other (more than 5) instrumented functions, that are called during BUG() handling too;

Quoted 6 lines

2. decreasing CONFIG_STACKLEAK_TRACK_MIN_SIZE would add more instrumented functions, which should be manually blacklisted (not good).

I guess handling BUG() in another stack would be a solution. For example, Andy Lutomirski calls handle_stack_overflow in the DOUBLEFAULT_STACK (arch/x86/mm/fault.c). Should I do something similar?

Quoted 6 lines

Hello Andy! May I ask your advice?

When CONFIG_VMAP_STACK is disabled and STACKLEAK is enabled (for example, on x86_32), we need another way to detect stack depth overflow. That is the reason of having this BUG() in track_stack(). But it turns out to be recursive since track_stack() will be called again during BUG() handling.

What does the STEAKLACK plugin actually do? I haven't followed this enough.

Quoted 4 lines

We can avoid that recursion by handling oops in another stack. It looks similar to the way you call handle_stack_overflow() in arch/x86/mm/fault.c. But it seems that I can't reuse that code, am I right?

You'd probably have to make 32-bit compatible, which means making a 32-bit variant of this thingy:

                asm volatile ("movq %[stack], %%rsp\n\t"
                              "call handle_stack_overflow\n\t"
                              "1: jmp 1b"
                              : ASM_CALL_CONSTRAINT
                              : "D" ("kernel stack overflow (page fault)"),
                                "S" (regs), "d" (address),
                                [stack] "rm" (stack));

Or you could force a double-fault.

Quoted 5 lines

How should I do it properly?

By the way, you wrote that you have some entry code changes which conflict with STACKLEAK. May I ask for more details?

It's this thing:

https://git.kernel.org/pub/scm/linux/kernel/git/luto/linux.git/log/?h=x86/entry_stack.wip

and I'll probably drop the ".wip" from the name shortly.

Quoted 3 lines

Best regards,
Alexander

#12 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Quoted 1 line · Andy Lutomirski

What does the STEAKLACK plugin actually do? I haven't followed this enough.

The plugin adds instrumentation to track the maximum stack depth, though only functions with a sufficiently large stackframe are instrumented.

The basic idea is that this can then be used to lazily zero stack after use (just before return to userspace), to minimize the risk of problems resulting from uninitialized variables (either copied to userspace and leaking secrets, or controlled by userspace and influencing the kernel).

That, and catching some stack overflows in the absence of VMAP'd stacks.

Thanks,
Mark.

#13 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Thanks, Mark!

Please see my comments below.

Quoted 5 lines · Mark Rutland
Quoted 1 line · Andy Lutomirski

What does the STEAKLACK plugin actually do? I haven't followed this enough.

The plugin adds instrumentation to track the maximum stack depth, though only functions with a sufficiently large stackframe are instrumented.

Yes. Functions with a big stack frame call track_stack() to update the lowest_stack value. If CONFIG_VMAP_STACK is disabled, track_stack() is compiled with a check for detecting stack depth overflow. This check is what I'm asking about.

Quoted 4 lines

The basic idea is that this can then be used to lazily zero stack after use (just before return to userspace), to minimize the risk of problems resulting from uninitialized variables (either copied to userspace and leaking secrets, or controlled by userspace and influencing the kernel).

Yes.

Actually the used part of the kernel stack is not zeroed. It is filled by STACKLEAK_POISON (-0xBEEF) which points to the unused hole in x86_64 virtual memory map.

Quoted 1 line

That, and catching some stack overflows in the absence of VMAP'd stacks.

The STACKLEAK plugin also adds check_alloca() call before each alloca to block "Stack Clash" attack against kernel stack.

More details and statistics are available in the cover letter and commit messages in this patch series.

Best regards,
Alexander

maintainer
#14 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

Quoted 8 lines · Alexander Popov

Thanks, Mark!

Please see my comments below.

Quoted 3 lines · Mark Rutland
Quoted 1 line · Andy Lutomirski

What does the STEAKLACK plugin actually do? I haven't followed this enough.

Quoted 7 lines
Quoted 2 lines

The plugin adds instrumentation to track the maximum stack depth, though only functions with a sufficiently large stackframe are instrumented.

Yes. Functions with a big stack frame call track_stack() to update the lowest_stack value. If CONFIG_VMAP_STACK is disabled, track_stack() is compiled with a check for detecting stack depth overflow. This check is what I'm asking about.

Then you'll probably have to do something like what I did in the VMAP_STACK code.

That being said, I don't entirely see the point. If you want a hardened kernel, you're going to enable VMAP_STACK. Are there really users of hardened 32-bit kernels?

#16 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

In reply to Andy Lutomirski

Hello Andy,

Thanks for your prompt reply!

Quoted 24 lines · Andy Lutomirski
Quoted 23 lines · Alexander Popov
Quoted 21 lines · Alexander Popov
Quoted 7 lines · Peter Zijlstra
Quoted 4 lines · Alexander Popov

When the thread stack is exhausted, this BUG() is hit. But do_error_trap(), which handles the exception, calls track_stack() itself again (since it is instrumented by the gcc plugin). So this recursion proceeds with exhausting the thread stack.

Add a __attribute__((nostacktrack)) on it?

Yes, I already tried some blacklisting in the plugin, but it didn't really help, because:

1. there are other (more than 5) instrumented functions, that are called during BUG() handling too;

2. decreasing CONFIG_STACKLEAK_TRACK_MIN_SIZE would add more instrumented functions, which should be manually blacklisted (not good).

I guess handling BUG() in another stack would be a solution. For example, Andy Lutomirski calls handle_stack_overflow in the DOUBLEFAULT_STACK (arch/x86/mm/fault.c). Should I do something similar?

Quoted 8 lines
Quoted 6 lines

Hello Andy! May I ask your advice?

When CONFIG_VMAP_STACK is disabled and STACKLEAK is enabled (for example, on x86_32), we need another way to detect stack depth overflow. That is the reason of having this BUG() in track_stack(). But it turns out to be recursive since track_stack() will be called again during BUG() handling.

What does the STEAKLACK plugin actually do? I haven't followed this enough.

I've just replied to Mark's explanation.

Quoted 7 lines
Quoted 3 lines

We can avoid that recursion by handling oops in another stack. It looks similar to the way you call handle_stack_overflow() in arch/x86/mm/fault.c. But it seems that I can't reuse that code, am I right?

You'd probably have to make 32-bit compatible, which means making a 32-bit variant of this thingy:

Quoted 7 lines
                asm volatile ("movq %[stack], %%rsp\n\t"
                              "call handle_stack_overflow\n\t"
                              "1: jmp 1b"
                              : ASM_CALL_CONSTRAINT
                              : "D" ("kernel stack overflow (page fault)"),
                                "S" (regs), "d" (address),
                                [stack] "rm" (stack));

Hm, I don't have these pt_regs in track_stack(). That is why I think I can't reuse your handle_stack_overflow(). I guess manually crafting pt_regs will not be good-looking.

Quoted 1 line

Or you could force a double-fault.

Could you elaborate on that?

The goal is to have a verbose oops message and kill the offending process (if we work on behalf of a process). Can I do that?

Quoted 10 lines
Quoted 4 lines

How should I do it properly?

By the way, you wrote that you have some entry code changes which conflict with STACKLEAK. May I ask for more details?

It's this thing:

https://git.kernel.org/pub/scm/linux/kernel/git/luto/linux.git/log/?h=x86/entry_stack.wip

and I'll probably drop the ".wip" from the name shortly.

Wow, it's big. I'll look into it and maybe return with questions.

Best regards,
Alexander

#15 lore raw

[kernel-hardening] Re: [PATCH RFC v5 2/5] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack

In reply to Andy Lutomirski
Quoted 12 lines · Andy Lutomirski
Quoted 11 lines · Alexander Popov

Thanks, Mark!

Please see my comments below.

Quoted 5 lines · Mark Rutland
Quoted 1 line · Andy Lutomirski

What does the STEAKLACK plugin actually do? I haven't followed this enough.

The plugin adds instrumentation to track the maximum stack depth, though only functions with a sufficiently large stackframe are instrumented.

Quoted 7 lines
Quoted 4 lines

Yes. Functions with a big stack frame call track_stack() to update the lowest_stack value. If CONFIG_VMAP_STACK is disabled, track_stack() is compiled with a check for detecting stack depth overflow. This check is what I'm asking about.

Then you'll probably have to do something like what I did in the VMAP_STACK code.

Yes!

Quoted 3 lines

That being said, I don't entirely see the point. If you want a hardened kernel, you're going to enable VMAP_STACK. Are there really users of hardened 32-bit kernels?

You know, STACKLEAK already supports x86_32. It's a pity for me to make STACKLEAK dependent on VMAP_STACK and hence to drop STACKLEAK support for this platform.

I hope there is a way to add a good-looking check to track_stack() and have at least some profit (although it will not catch all overflow cases).

Best regards,
Alexander