Skip to contentSkip to conversation

Introduce the STACKLEAK feature and a test for it

RFC PATCH Superseded Alexander Popov 2 participants 15 messages Jan 12, 2018 – Feb 11, 2018

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

v7 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v7 cover Introduce the STACKLEAK feature and a test for it

This is the 7th 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.

Changes in v7
=============

1. Improved erase_kstack(). Now it detects which stack we are currently using. If we are on the thread stack, erase_kstack() writes the poison value up to the stack pointer. If we are not on the thread stack (we are on the trampoline stack introduced by Andy Lutomirski), erase_kstack() writes the poison value up to the cpu_current_top_of_stack.

N.B. Right now there are two situations when erase_kstack() is called on the thread stack:
- from sysret32_from_system_call in entry_64_compat.S;
- from syscall_trace_enter() (see the 3rd patch of this series).

2. Introduced STACKLEAK_POISON_CHECK_DEPTH macro and BUILD_BUG_ON() checking its consistency with CONFIG_STACKLEAK_TRACK_MIN_SIZE.

3. Added "disable" option for the STACKLEAK gcc plugin (asked by Laura Abbott).

4. Improved CONFIG_STACKLEAK_METRICS. Now /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 different workloads.

5. Removed redundant erase_kstack() calls from syscall_trace_enter(). There is no need to erase the stack in syscall_trace_enter() when it returns -1 (thanks to Dmitry Levin for noticing that).

6. Introduced MIN_STACK_LEFT to get rid of hardcoded numbers in check_alloca().

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.

STACKLEAK performance impact
============================

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".

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: 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 #2: 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%)

Previous changes
================

Changes in v6

1. Examined syscall entry/exit paths.
- Added missing erase_kstack() call at ret_from_fork() for x86_32.
- Added missing erase_kstack() call at syscall_trace_enter().
- Solved questions previously marked with TODO.

2. Rebased onto v4.15-rc2, which includes Andy Lutomirski's entry changes. Andy removed sp0 from thread_struct for x86_64, which was the only issue during rebasing.

3. Removed the recursive BUG() in track_stack() that was caused by the code instrumentation. Instead, CONFIG_GCC_PLUGIN_STACKLEAK now implies CONFIG_VMAP_STACK and CONFIG_SCHED_STACK_END_CHECK, which seems to be an optimal solution.

4. Put stack erasing in syscall_trace_enter() into a separate patch and fixed my mistake with secure_computing() (found by Tycho Andersen).

5. After some experiments, kept the asm implementation of erase_kstack(), because it gives a full control over the stack for clearing it neatly and doesn't offend KASAN.

6. Improved the comments describing STACKLEAK.

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.

Ideas for further work
======================

- Think of erasing stack on the way out of exception handlers (idea by Andy Lutomirski).
- 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).

Alexander Popov (6):
x86/entry: Add STACKLEAK erasing the kernel stack at the end of
syscalls
gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack x86/entry: Erase kernel stack in syscall_trace_enter()
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                               |  53 ++++
 arch/x86/Kconfig                           |   1 +
 arch/x86/entry/common.c                    |   7 +
 arch/x86/entry/entry_32.S                  |  69 +++++
 arch/x86/entry/entry_64.S                  | 113 +++++++
 arch/x86/entry/entry_64_compat.S           |  11 +
 arch/x86/include/asm/processor.h           |   7 +
 arch/x86/kernel/asm-offsets.c              |  11 +
 arch/x86/kernel/dumpstack.c                |  20 ++
 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                                  |  33 ++
 fs/proc/base.c                             |  18 ++
 include/linux/compiler.h                   |   6 +
 scripts/Makefile.gcc-plugins               |   3 +
 scripts/gcc-plugins/stackleak_plugin.c     | 471 +++++++++++++++++++++++++++++
 21 files changed, 1001 insertions(+), 9 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 v7 1/6 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/entry_32.S        |  65 +++++++++++++++++++++++
 arch/x86/entry/entry_64.S        | 109 +++++++++++++++++++++++++++++++++++++++
 arch/x86/entry/entry_64_compat.S |  11 ++++
 arch/x86/include/asm/processor.h |   4 ++
 arch/x86/kernel/asm-offsets.c    |   8 +++
 arch/x86/kernel/process_32.c     |   5 ++
 arch/x86/kernel/process_64.c     |   5 ++
 include/linux/compiler.h         |   6 +++
 10 files changed, 241 insertions(+)
arch/Kconfig +27 -0
index 400b9e1..721fdae 100644
@@ -387,6 +387,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
@@ -517,6 +524,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 d4fc98c..4981cb6 100644
@@ -116,6 +116,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/entry_32.S +65 -0

89 lines not on this page · raw message

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

127 lines not on this page · raw message

arch/x86/entry/entry_64_compat.S +11 -0
index 98d5358..727dde2 100644
@@ -19,6 +19,12 @@
 
 	.section .entry.text, "ax"
 
+	.macro erase_kstack
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+	call erase_kstack
+#endif
+	.endm
+
 /*
  * 32-bit SYSENTER entry.
  *
@@ -238,6 +244,11 @@ GLOBAL(entry_SYSCALL_compat_after_hwframe)
 
 	/* Opportunistic SYSRET */
 sysret32_from_system_call:
+	/*
+	 * We are not going to return to the userspace from the trampoline
+	 * stack. So let's erase the thread stack right now.
+	 */
+	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 */
arch/x86/include/asm/processor.h +4 -0
index d3a67fb..9736829 100644
@@ -496,6 +496,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 +8 -0
index 76417a9..ef5d260 100644
@@ -39,6 +39,9 @@ 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);
+#endif
 
 	BLANK();
 	OFFSET(crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
@@ -75,6 +78,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 5224c60..6d256ab 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 c754662..4345fdf 100644
@@ -281,6 +281,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 +6 -0
index 52e611a..001c3e2 100644
@@ -327,4 +327,10 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s
 	compiletime_assert(__native_word(t),				\
 		"Need native word sized stores/loads for atomicity.")
 
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+/* Poison value points to the unused hole in the virtual memory map */
+# define STACKLEAK_POISON -0xBEEF
+# define STACKLEAK_POISON_CHECK_DEPTH 128
+#endif
+
 #endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.7.4
#2 lore raw

RFC PATCH v7 2/6 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                           |  14 +
 arch/x86/kernel/dumpstack.c            |  20 ++
 fs/exec.c                              |  33 +++
 scripts/Makefile.gcc-plugins           |   3 +
 scripts/gcc-plugins/stackleak_plugin.c | 471 +++++++++++++++++++++++++++++++++
 5 files changed, 541 insertions(+)
 create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
arch/Kconfig +14 -0
index 721fdae..6fcb632 100644
@@ -528,6 +528,8 @@ config GCC_PLUGIN_STACKLEAK
 	bool "Erase the kernel stack before returning from syscalls"
 	depends on GCC_PLUGINS
 	depends on HAVE_ARCH_STACKLEAK
+	imply VMAP_STACK
+	imply SCHED_STACK_END_CHECK
 	help
 	  This option makes the kernel erase the kernel stack before it
 	  returns from a system call. That reduces the information which
@@ -544,6 +546,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 +20 -0
index afbecff..2fbbaa9 100644
@@ -375,3 +375,23 @@ static int __init code_bytes_setup(char *s)
 	return 1;
 }
 __setup("code_bytes=", code_bytes_setup);
+
+#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
+
+#define MIN_STACK_LEFT 256
+
+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 < MIN_STACK_LEFT ||
+				size >= stack_left - MIN_STACK_LEFT);
+}
+EXPORT_SYMBOL(check_alloca);
+#endif
fs/exec.c +33 -0
index 7eb8d21..9865612 100644
@@ -1962,3 +1962,36 @@ 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;
+
+	/*
+	 * Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
+	 * STACKLEAK_POISON_CHECK_DEPTH makes the poison search in
+	 * erase_kstack() unreliable. Let's prevent that.
+	 */
+	BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE >
+						STACKLEAK_POISON_CHECK_DEPTH);
+
+	if (sp < current->thread.lowest_stack &&
+	    sp >= (unsigned long)task_stack_page(current) +
+					2 * sizeof(unsigned long)) {
+		current->thread.lowest_stack = sp;
+	}
+}
+EXPORT_SYMBOL(track_stack);
+#endif /* CONFIG_GCC_PLUGIN_STACKLEAK */
scripts/Makefile.gcc-plugins +3 -0
index b2a95af..8d6070f 100644
@@ -35,6 +35,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 +471 -0

471 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

RFC PATCH v7 3/6 x86/entry: Erase kernel stack in syscall_trace_enter()

In reply to Alexander Popov

Make STACKLEAK erase kernel stack after ptrace/seccomp/auditing not to leave any sensitive information on the stack for the syscall code.

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/x86/entry/common.c | 7 +++++++
 1 file changed, 7 insertions(+)
arch/x86/entry/common.c +7 -0
index d7d3cc2..cd38727 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
@@ -127,6 +133,7 @@ static long syscall_trace_enter(struct pt_regs *regs)
 
 	do_audit_syscall_entry(regs, arch);
 
+	erase_kstack();
 	return ret ?: regs->orig_ax;
 }
 
Signature · 1 line
2.7.4
#4 lore raw

RFC PATCH v7 4/6 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).

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().

STACKLEAK_TRACK_STACK tests that exhausting the thread stack with a recursion hits the guard page provided by CONFIG_VMAP_STACK.

---
 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 5ca5f64..66c3605 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 687a0db..2a0cd0d 100644
@@ -83,4 +83,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 ba92291..3a22ffa 100644
@@ -183,6 +183,8 @@ static const 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
#5 lore raw

RFC PATCH v7 5/6 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>/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.

---
 arch/Kconfig                     | 12 ++++++++++++
 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                   | 18 ++++++++++++++++++
 8 files changed, 50 insertions(+)
arch/Kconfig +12 -0
index 6fcb632..0f5a59d 100644
@@ -558,6 +558,18 @@ 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>/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.
+
 config HAVE_CC_STACKPROTECTOR
 	bool
 	help
arch/x86/entry/entry_32.S +4 -0
index f400d8f..c22d190 100644
@@ -116,6 +116,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 3ff0802..40e4ba5 100644
@@ -118,6 +118,10 @@ ENTRY(erase_kstack)
 	 */
 	or	$2 * 8, %rdi
 
+#ifdef CONFIG_STACKLEAK_METRICS
+	mov	%rdi, TASK_prev_lowest_stack(%r11)
+#endif
+
 	/*
 	 * Check whether we are on the thread stack to prepare the counter
 	 * for stack poisoning.
arch/x86/include/asm/processor.h +3 -0
index 9736829..4741f26 100644
@@ -498,6 +498,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 ef5d260..f48197a 100644
@@ -41,6 +41,9 @@ void common(void) {
 	OFFSET(TASK_addr_limit, task_struct, thread.addr_limit);
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	OFFSET(TASK_lowest_stack, task_struct, thread.lowest_stack);
+# 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 6d256ab..48993fe 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 4345fdf..faf9fae 100644
@@ -284,6 +284,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 +18 -0
index 60316b5..3106265 100644
@@ -2884,6 +2884,21 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 #endif /* CONFIG_LIVEPATCH */
 
+#ifdef CONFIG_STACKLEAK_METRICS
+static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
+				struct pid *pid, struct task_struct *task)
+{
+	unsigned long prev_depth = THREAD_SIZE -
+			(task->thread.prev_lowest_stack & (THREAD_SIZE - 1));
+	unsigned long depth = THREAD_SIZE -
+			(task->thread.lowest_stack & (THREAD_SIZE - 1));
+
+	seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
+							prev_depth, depth);
+	return 0;
+}
+#endif /* CONFIG_STACKLEAK_METRICS */
+
 /*
  * Thread groups
  */
@@ -2988,6 +3003,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("stack_depth", S_IRUGO, proc_stack_depth),
+#endif
 };
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Signature · 1 line
2.7.4
#6 lore raw

RFC PATCH v7 6/6 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
#7 lore raw
In reply to Alexander Popov
Quoted 6 lines · Alexander Popov

This is the 7th 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.

I think this is really looking good. I had some thoughts while reading through the patches:

There are really three features in this series, and it might make sense to separate them a bit more clearly (at least with CONFIG choices):

1) stack clearing (with depth searching)

2) runtime stack depth tracking (making 1 much more efficient)

3) alloca checking (an additional feature, not strictly part of clearing, but needs the same plugin infrastructure)

It seems like it should be possible to get 1 without 2 and 3 (both of which happen in the gcc plugin), and might be good to separate for builds that don't have gcc plugins.

Once compilers are doing alloca checking (or all VLAs are removed from the kernel), it'd be nice to be able to avoid the redundancy of 3.

Thanks for continuing to work on this!

-Kees

Quoted 227 lines

Changes in v7
=============

1. Improved erase_kstack(). Now it detects which stack we are currently using. If we are on the thread stack, erase_kstack() writes the poison value up to the stack pointer. If we are not on the thread stack (we are on the trampoline stack introduced by Andy Lutomirski), erase_kstack() writes the poison value up to the cpu_current_top_of_stack.

N.B. Right now there are two situations when erase_kstack() is called on the thread stack:
- from sysret32_from_system_call in entry_64_compat.S;
- from syscall_trace_enter() (see the 3rd patch of this series).

2. Introduced STACKLEAK_POISON_CHECK_DEPTH macro and BUILD_BUG_ON() checking its consistency with CONFIG_STACKLEAK_TRACK_MIN_SIZE.

3. Added "disable" option for the STACKLEAK gcc plugin (asked by Laura Abbott).

4. Improved CONFIG_STACKLEAK_METRICS. Now /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 different workloads.

5. Removed redundant erase_kstack() calls from syscall_trace_enter(). There is no need to erase the stack in syscall_trace_enter() when it returns -1 (thanks to Dmitry Levin for noticing that).

6. Introduced MIN_STACK_LEFT to get rid of hardcoded numbers in check_alloca().

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.

STACKLEAK performance impact
============================

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".

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: 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 #2: 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%)

Previous changes
================

Changes in v6

1. Examined syscall entry/exit paths.
- Added missing erase_kstack() call at ret_from_fork() for x86_32.
- Added missing erase_kstack() call at syscall_trace_enter().
- Solved questions previously marked with TODO.

2. Rebased onto v4.15-rc2, which includes Andy Lutomirski's entry changes. Andy removed sp0 from thread_struct for x86_64, which was the only issue during rebasing.

3. Removed the recursive BUG() in track_stack() that was caused by the code instrumentation. Instead, CONFIG_GCC_PLUGIN_STACKLEAK now implies CONFIG_VMAP_STACK and CONFIG_SCHED_STACK_END_CHECK, which seems to be an optimal solution.

4. Put stack erasing in syscall_trace_enter() into a separate patch and fixed my mistake with secure_computing() (found by Tycho Andersen).

5. After some experiments, kept the asm implementation of erase_kstack(), because it gives a full control over the stack for clearing it neatly and doesn't offend KASAN.

6. Improved the comments describing STACKLEAK.

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.

Ideas for further work
======================

- Think of erasing stack on the way out of exception handlers (idea by Andy Lutomirski).
- 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).

Alexander Popov (6):
x86/entry: Add STACKLEAK erasing the kernel stack at the end of
syscalls
gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack x86/entry: Erase kernel stack in syscall_trace_enter()
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                               |  53 ++++
 arch/x86/Kconfig                           |   1 +
 arch/x86/entry/common.c                    |   7 +
 arch/x86/entry/entry_32.S                  |  69 +++++
 arch/x86/entry/entry_64.S                  | 113 +++++++
 arch/x86/entry/entry_64_compat.S           |  11 +
 arch/x86/include/asm/processor.h           |   7 +
 arch/x86/kernel/asm-offsets.c              |  11 +
 arch/x86/kernel/dumpstack.c                |  20 ++
 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                                  |  33 ++
 fs/proc/base.c                             |  18 ++
 include/linux/compiler.h                   |   6 +
 scripts/Makefile.gcc-plugins               |   3 +
 scripts/gcc-plugins/stackleak_plugin.c     | 471 +++++++++++++++++++++++++++++
 21 files changed, 1001 insertions(+), 9 deletions(-)
 create mode 100644 drivers/misc/lkdtm_stackleak.c
 create mode 100644 scripts/gcc-plugins/stackleak_plugin.c

--
2.7.4

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

Hello Kees,

Thanks for the review.

Quoted 22 lines · Kees Cook
Quoted 6 lines · Alexander Popov

This is the 7th 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.

I think this is really looking good. I had some thoughts while reading through the patches:

There are really three features in this series, and it might make sense to separate them a bit more clearly (at least with CONFIG choices):

1) stack clearing (with depth searching)

2) runtime stack depth tracking (making 1 much more efficient)

3) alloca checking (an additional feature, not strictly part of clearing, but needs the same plugin infrastructure)

Quoted 6 lines

It seems like it should be possible to get 1 without 2 and 3 (both of which happen in the gcc plugin), and might be good to separate for builds that don't have gcc plugins.

Once compilers are doing alloca checking (or all VLAs are removed from the kernel), it'd be nice to be able to avoid the redundancy of 3.

Agree with your point. I'll make this separation in the next version.

Moreover, I'll create separate tests for these 3 features. Currently lkdtm_stackleak combines the stack clearing check with the alloca test and the deep recursion test.

Quoted 1 line

Thanks for continuing to work on this!

Doing my best!

Hope that x86 maintainers will have a look at this patch series too (when they are less busy).

Best regards,
Alexander

#9 lore raw

Hello Kees,

Quoted 23 lines · Alexander Popov
Quoted 22 lines · Kees Cook
Quoted 6 lines · Alexander Popov

This is the 7th 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.

I think this is really looking good. I had some thoughts while reading through the patches:

There are really three features in this series, and it might make sense to separate them a bit more clearly (at least with CONFIG choices):

1) stack clearing (with depth searching)

2) runtime stack depth tracking (making 1 much more efficient)

3) alloca checking (an additional feature, not strictly part of clearing, but needs the same plugin infrastructure)

Quoted 8 lines
Quoted 6 lines

It seems like it should be possible to get 1 without 2 and 3 (both of which happen in the gcc plugin), and might be good to separate for builds that don't have gcc plugins.

Once compilers are doing alloca checking (or all VLAs are removed from the kernel), it'd be nice to be able to avoid the redundancy of 3.

Agree with your point. I'll make this separation in the next version.

I have more thoughts about this separation.

Splitting (1) from (2)
----------------------

It makes the stack erasing not only slow but also unreliable. For example, an attacker can craft some STACKLEAK_POISON values on the thread stack to deceive the poison search during the erasing.

Of course, I can increase STACKLEAK_POISON_CHECK_DEPTH or change the default value of lowest_stack. It will make the stack erasing even slower, but will not give guarantees.

So I don't think that (1) without (2) is actually a good feature. I would propose to refrain from separating the stack erasing and the lowest_stack tracking.

Splitting (2) and (3)
---------------------

The STACKLEAK gcc plugin needs to search for alloca anyway (for the correct lowest_stack tracking). But I can introduce the "no-check-alloca" plugin parameter for disabling the check_alloca() call insertion.

Is it worth providing something like CONFIG_GCC_PLUGIN_STACKLEAK_NO_CHECK_ALLOCA in the Kconfig?

Thanks!

Best regards,
Alexander

#10 lore raw
Quoted 40 lines · Alexander Popov

Hello Kees,

Quoted 23 lines · Alexander Popov
Quoted 22 lines · Kees Cook
Quoted 6 lines · Alexander Popov

This is the 7th 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.

I think this is really looking good. I had some thoughts while reading through the patches:

There are really three features in this series, and it might make sense to separate them a bit more clearly (at least with CONFIG choices):

1) stack clearing (with depth searching)

2) runtime stack depth tracking (making 1 much more efficient)

3) alloca checking (an additional feature, not strictly part of clearing, but needs the same plugin infrastructure)

Quoted 8 lines
Quoted 6 lines

It seems like it should be possible to get 1 without 2 and 3 (both of which happen in the gcc plugin), and might be good to separate for builds that don't have gcc plugins.

Once compilers are doing alloca checking (or all VLAs are removed from the kernel), it'd be nice to be able to avoid the redundancy of 3.

Agree with your point. I'll make this separation in the next version.

I have more thoughts about this separation.

Splitting (1) from (2)
----------------------

Quoted 7 lines

It makes the stack erasing not only slow but also unreliable. For example, an attacker can craft some STACKLEAK_POISON values on the thread stack to deceive the poison search during the erasing.

Of course, I can increase STACKLEAK_POISON_CHECK_DEPTH or change the default value of lowest_stack. It will make the stack erasing even slower, but will not give guarantees.

Yeah, that's true. I was hoping the depth check was sufficient.

Quoted 2 lines

So I don't think that (1) without (2) is actually a good feature. I would propose to refrain from separating the stack erasing and the lowest_stack tracking.

How about an option to clear the _entire_ stack, then, when the plugin isn't available? That gives us a range of options and provides an easy way to compare the performance of the tracking. i.e. can compare off, full, and smart.

Quoted 9 lines

Splitting (2) and (3)
---------------------

The STACKLEAK gcc plugin needs to search for alloca anyway (for the correct lowest_stack tracking). But I can introduce the "no-check-alloca" plugin parameter for disabling the check_alloca() call insertion.

Is it worth providing something like CONFIG_GCC_PLUGIN_STACKLEAK_NO_CHECK_ALLOCA in the Kconfig?

Nah, we can cross that bridge when the compilers have sane alloca checking.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#11 lore raw
Quoted 8 lines · Kees Cook
Quoted 2 lines · Alexander Popov

So I don't think that (1) without (2) is actually a good feature. I would propose to refrain from separating the stack erasing and the lowest_stack tracking.

How about an option to clear the _entire_ stack, then, when the plugin isn't available? That gives us a range of options and provides an easy way to compare the performance of the tracking. i.e. can compare off, full, and smart.

Yes, I should try it. I'll return with the results of the performance tests. We'll discuss them; if full stack erasing is not too slow, I'll introduce it in the 8'th version of the patch series.

Thanks!

Best regards,
Alexander

#12 lore raw
Quoted 5 lines · Alexander Popov
Quoted 4 lines · Kees Cook
Quoted 2 lines · Alexander Popov

So I don't think that (1) without (2) is actually a good feature. I would propose to refrain from separating the stack erasing and the lowest_stack tracking.

Quoted 8 lines
Quoted 4 lines

How about an option to clear the _entire_ stack, then, when the plugin isn't available? That gives us a range of options and provides an easy way to compare the performance of the tracking. i.e. can compare off, full, and smart.

Yes, I should try it. I'll return with the results of the performance tests. We'll discuss them; if full stack erasing is not too slow, I'll introduce it in the 8'th version of the patch series.

I've made a brief performance test on x86_64 (similar to the test described in the cover letter). I guess there might be workloads with higher performance penalty.

Hardware: Intel Core i7-4770, 16 GB RAM
Test: hackbench -s 4096 -l 2000 -g 15 -f 25 -P

Average time on v4.14.15: 9.194s
Average time on v4.14.15-stackleak-with-plugin: 9.490 (+3.22%) Average time on v4.14.15-stackleak-full-erasing: 12.149 (+32.14%)

Honestly, I think, it is not worth having this full stack erasing as a separate feature. Moreover, it brings some #ifdef conditionals to the erase_kstack() code, which don't look nice.

May I ask for your opinion?

Best regards,
Alexander

#14 lore raw
In reply to Kees Cook
Quoted 9 lines · Kees Cook
Quoted 6 lines · Alexander Popov

This is the 7th 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.

I think this is really looking good.

Hello! May I ask whether dear x86 maintainers think so too?

Best regards,
Alexander

#13 lore raw
In reply to Alexander Popov
Quoted 18 lines · Alexander Popov
Quoted 5 lines · Alexander Popov
Quoted 4 lines · Kees Cook
Quoted 2 lines · Alexander Popov

So I don't think that (1) without (2) is actually a good feature. I would propose to refrain from separating the stack erasing and the lowest_stack tracking.

Quoted 8 lines
Quoted 4 lines

How about an option to clear the _entire_ stack, then, when the plugin isn't available? That gives us a range of options and provides an easy way to compare the performance of the tracking. i.e. can compare off, full, and smart.

Yes, I should try it. I'll return with the results of the performance tests. We'll discuss them; if full stack erasing is not too slow, I'll introduce it in the 8'th version of the patch series.

I've made a brief performance test on x86_64 (similar to the test described in the cover letter). I guess there might be workloads with higher performance penalty.

Quoted 6 lines

Hardware: Intel Core i7-4770, 16 GB RAM
Test: hackbench -s 4096 -l 2000 -g 15 -f 25 -P

Average time on v4.14.15: 9.194s
Average time on v4.14.15-stackleak-with-plugin: 9.490 (+3.22%) Average time on v4.14.15-stackleak-full-erasing: 12.149 (+32.14%)

Yeeeowch. Okay, I'm convinced. :)

Quoted 8 lines

Honestly, I think, it is not worth having this full stack erasing as a separate feature. Moreover, it brings some #ifdef conditionals to the erase_kstack() code, which don't look nice.

May I ask for your opinion?

Best regards,
Alexander

-Kees

Signature · 2 lines
Kees Cook
Pixel Security