Introduce the STACKLEAK feature and a test for it
Subsystems (11)
v6 cover Introduce the STACKLEAK feature and a test for it
This is the 6th 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.
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%)
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 | 19 +- arch/x86/entry/entry_32.S | 69 +++++ arch/x86/entry/entry_64.S | 93 ++++++ arch/x86/entry/entry_64_compat.S | 8 + arch/x86/include/asm/processor.h | 7 + arch/x86/kernel/asm-offsets.c | 14 + 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 | 25 ++ fs/proc/base.c | 14 + include/linux/compiler.h | 5 + scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++ 21 files changed, 971 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
RFC PATCH v6 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 | 89 ++++++++++++++++++++++++++++++++++++++++ arch/x86/entry/entry_64_compat.S | 8 ++++ arch/x86/include/asm/processor.h | 4 ++ arch/x86/kernel/asm-offsets.c | 11 +++++ arch/x86/kernel/process_32.c | 5 +++ arch/x86/kernel/process_64.c | 5 +++ include/linux/compiler.h | 5 +++ 10 files changed, 220 insertions(+)
arch/Kconfig +27 -0
@@ -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
@@ -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 +89 -0
113 lines not on this page · raw message
arch/x86/entry/entry_64_compat.S +8 -0
@@ -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. *
@@ -229,6 +235,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 */
@@ -336,6 +343,7 @@ ENTRY(entry_INT80_compat) .Lsyscall_32_done: /* Go back to user mode. */ + erase_kstack TRACE_IRQS_ON jmp swapgs_restore_regs_and_return_to_usermode END(entry_INT80_compat)
arch/x86/include/asm/processor.h +4 -0
@@ -481,6 +481,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 +11 -0
@@ -38,6 +38,12 @@ 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); +# ifdef CONFIG_X86_32 +OFFSET (TASK_thread_sp0, task_struct, thread.sp0); +# endif +#endif BLANK ();OFFSET (crypto_tfm_ctx_offset, crypto_tfm, __crt_ctx);
@@ -74,6 +80,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
@@ -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
@@ -282,6 +282,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 +5 -0
@@ -352,4 +352,9 @@ static __always_inline void __write_once_size(volatile void *p, void *res, int s (volatile typeof(x) *)&(x); })#define ACCESS_ONCE (x) (*__ACCESS_ONCE(x)) +#ifdef CONFIG_GCC_PLUGIN_STACKLEAK +/* It points to the unused hole in the virtual memory map */ +# define STACKLEAK_POISON -0xBEEF +#endif +#endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.7.4
RFC PATCH v6 2/6 gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
In reply to Alexander PopovThe 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 | 15 ++ arch/x86/kernel/dumpstack.c | 15 ++ fs/exec.c | 25 ++ scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++++++ 5 files changed, 528 insertions(+) create mode 100644 scripts/gcc-plugins/stackleak_plugin.c
arch/Kconfig +15 -0
@@ -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,19 @@ 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. Be careful with + this setting, don't break the poison search in erase_kstack. + If unsure, leave the default value 100. + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/kernel/dumpstack.c +15 -0
@@ -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 +25 -0
@@ -1963,3 +1963,28 @@ 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; + } +} +EXPORT_SYMBOL (track_stack); +#endif /* CONFIG_GCC_PLUGIN_STACKLEAK */
scripts/Makefile.gcc-plugins +3 -0
@@ -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 +470 -0
470 lines not on this page · raw message
Signature · 1 line
2.7.4
RFC PATCH v6 3/6 x86/entry: Erase kernel stack in syscall_trace_enter()
In reply to Alexander PopovMake 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 | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
arch/x86/entry/common.c +16 -3
@@ -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,11 +87,15 @@ 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) +if (emulated) { +erase_kstack ();return -1L ; + }#ifdef CONFIG_SECCOMP /*
@@ -117,8 +127,10 @@ static long syscall_trace_enter(struct pt_regs *regs) } ret =__secure_computing (&sd); -if (ret ==-1 ) +if (ret ==-1 ) { +erase_kstack ();return ret; + } }#endif
@@ -127,6 +139,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
RFC PATCH v6 4/6 lkdtm: Add a test for STACKLEAK
In reply to Alexander PopovIntroduce 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
@@ -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
@@ -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
@@ -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
RFC PATCH v6 5/6 fs/proc: Show STACKLEAK metrics in the /proc file system
In reply to Alexander PopovIntroduce 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
@@ -559,6 +559,17 @@ config STACKLEAK_TRACK_MIN_SIZE this setting, don't break the poison search in erase_kstack. 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
@@ -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
@@ -121,6 +121,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
@@ -483,6 +483,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
@@ -43,6 +43,9 @@ void common(void) {# ifdef CONFIG_X86_32 OFFSET (TASK_thread_sp0, task_struct, thread.sp0);# endif +# 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
@@ -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
@@ -285,6 +285,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
@@ -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
RFC PATCH v6 6/6 doc: self-protection: Add information about STACKLEAK feature
In reply to Alexander PopovAdd 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
@@ -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
[kernel-hardening] Re: [PATCH RFC v6 2/6] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
In reply to Alexander PopovQuoted 62 lines · Alexander Popov
scripts/gcc-plugins/stackleak_plugin.c new file +62
-0+__visible int plugin_init(struct plugin_name_args *plugin_info, +struct plugin_gcc_version *version) +{ +const char *const plugin_name = plugin_info->base_name; +const int argc = plugin_info->argc; +const struct plugin_argument *const argv = plugin_info->argv; +int i; + +/* Extra GGC root tables describing our GTY-ed data */ +static const struct ggc_root_tab gt_ggc_r_gt_stackleak[] = { + { + .base = &track_function_decl, + .nelt =1 , + .stride =sizeof (track_function_decl), + .cb = >_ggc_mx_tree_node, + .pchw = >_pch_nx_tree_node + }, + { + .base = &check_function_decl, + .nelt =1 , + .stride =sizeof (check_function_decl), + .cb = >_ggc_mx_tree_node, + .pchw = >_pch_nx_tree_node + }, +LAST_GGC_ROOT_TAB + }; + +/* +* The stackleak_tree_instrument pass should be executed before the +* "optimized" pass, which is the control flow graph cleanup that is +* performed just before expanding gcc trees to the RTL. In former +* versions of the plugin this new pass was inserted before the +* "tree_profile" pass, which is currently called "profile". +*/ +PASS_INFO (stackleak_tree_instrument,"optimized" ,1 , +PASS_POS_INSERT_BEFORE ); + +/* +* The stackleak_final pass should be executed before the "final" pass, +* which turns the RTL (Register Transfer Language) into assembly. +*/ +PASS_INFO (stackleak_final,"final" ,1 ,PASS_POS_INSERT_BEFORE ); + +if (!plugin_default_version_check (version, &gcc_version)) { +error (G_ ("incompatible gcc/plugin versions" )); +return 1 ; + } + +/* Parse the plugin arguments */ +if (argc !=1 ) { +error (G_ ("bad number of the plugin arguments: %d" ), argc); +return 1 ; + } + +if (strcmp (argv[i].key,"track-min-size" )) { +error (G_ ("unknown option '-fplugin-arg-%s-%s'" ), + plugin_name, argv[i].key); +return 1 ; + } + +if (!argv[i].value) { + error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
scripts/gcc-plugins/stackleak_plugin.c line 442
Quoted 10 lines
scripts/gcc-plugins/stackleak_plugin.c new file +10
-0+ plugin_name, argv[i].key); +return 1 ; + } + + track_frame_size =atoi (argv[i].value); +if (track_frame_size <0 ) { +error (G_ ("invalid option argument '-fplugin-arg-%s-%s=%s'" ), + plugin_name, argv[i].key, argv[i].value); +return 1 ; + }
I don't see i getting updated anywhere, which seems to be an artifact of removing the for loop. I'd prefer if you just kept the loop since the arm64 version requires a --disable option like structleak.
Thanks,
Laura
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
In reply to Alexander PopovQuoted 135 lines · Alexander Popov
Introduce
CONFIG_STACKLEAK_METRICSproviding STACKLEAK information about tasks via the /proc file system. In particular, /proc/<pid>/lowest_stack shows the currentlowest_stackvalue and its final value from the previous syscall. That information can be useful for estimating the STACKLEAK performance impact for different workloads.Signed-off-by Alexander Popov <[email protected]>--- 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@@ -559,6 +559,17 @@ config STACKLEAK_TRACK_MIN_SIZE this setting, don't break the poison search in erase_kstack. 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 helparch/x86/entry/entry_32.S +4
-0@@ -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 ud2arch/x86/entry/entry_64.S +4
-0@@ -121,6 +121,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 3farch/x86/include/asm/processor.h +3
-0@@ -483,6 +483,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@@ -43,6 +43,9 @@ void common(void) {# ifdef CONFIG_X86_32 OFFSET (TASK_thread_sp0, task_struct, thread.sp0);# endif +# 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@@ -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@@ -285,6 +285,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 +1
-0@@ -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
fs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, · line 2897
Quoted 10 lines
fs/proc/base.c +10
-0+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 */ +
This just prints the hashed value with the new pointer leak work. I don't think we want to print the fully exposed value via %px so it's not clear how valuable this proc file is now.
Thanks,
Laura
Quoted 14 lines
fs/proc/base.c +3
-0/* * 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)
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
Quoted 18 lines · Laura Abbott
Quoted 17 lines · Alexander Popov
fs/proc/base.c +7
-0@@ -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;
Quoted 8 lines
fs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, · line 2897
Quoted 3 lines
fs/proc/base.c +3
-0+} +#endif /* CONFIG_STACKLEAK_METRICS */ +This just prints the hashed value with the new pointer leak work. I don't think we want to print the fully exposed value via %px so it's not clear how valuable this proc file is now.
Maybe print the size, not the location?
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH RFC v6 3/6] x86/entry: Erase kernel stack in syscall_trace_enter()
In reply to Alexander PopovHi,
Quoted 61 lines · 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.
Signed-off-by Alexander Popov <[email protected]>--- arch/x86/entry/common.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)arch/x86/entry/common.c +15
-3@@ -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,11 +87,15 @@ 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) +if (emulated) { +erase_kstack ();return -1L ; + }#ifdef CONFIG_SECCOMP /* @@ -117,8 +127,10 @@ static long syscall_trace_enter(struct pt_regs *regs) } ret =__secure_computing (&sd); -if (ret ==-1 ) +if (ret ==-1 ) { +erase_kstack ();return ret; + } }#endif
arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 144
Quoted 6 lines
arch/x86/entry/common.c +1
-0@@ -127,6 +139,7 @@ static long syscall_trace_enter(struct pt_regs *regs)do_audit_syscall_entry (regs, arch); +erase_kstack ();return ret ?: regs->orig_ax; }
wrt adding erase_kstack() calls to syscall_trace_enter(), I think the only case where this would be appropriate is that still has a chance of executing syscall code. In all cases where syscall_trace_enter() returns -1 no syscall code is going to be executed and the stack will be erased on exiting syscall anyway.
In other words, only the last hunk of this patch seems to be useful, all others look redundant.
P.S. I've trimmed the Cc list to those who took part in earlier rounds of this discussion.
Signature · 1 line
ldv
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
In reply to Kees CookQuoted 28 lines · Kees Cook
Quoted 18 lines · Laura Abbott
Quoted 17 lines · Alexander Popov
fs/proc/base.c +7
-0@@ -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;Quoted 8 lines
fs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, · line 2897
Quoted 3 lines
fs/proc/base.c +3
-0+} +#endif /* CONFIG_STACKLEAK_METRICS */ +This just prints the hashed value with the new pointer leak work. I don't think we want to print the fully exposed value via %px so it's not clear how valuable this proc file is now.
Quoted 4 lines
Maybe print the size, not the location?
-Kees
Hmmmmm, that starts to overlap with CONFIG_DEBUG_STACK_USAGE. That's not a bad thing but it would be good to clarify what this is tracking vs. CONFIG_DEBUG_STACK_USAGE.
Thanks,
Laura
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
In reply to Laura AbbottHello Laura and Kees,
[adding Tobin C. Harding]
Quoted 18 lines · Laura Abbott
Quoted 17 lines · Alexander Popov
Introduce
CONFIG_STACKLEAK_METRICSproviding STACKLEAK information about tasks via the /proc file system. In particular, /proc/<pid>/lowest_stack shows the currentlowest_stackvalue and its final value from the previous syscall. That information can be useful for estimating the STACKLEAK performance impact for different workloads.Signed-off-by Alexander Popov <[email protected]>--- 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(+)
[...]
Quoted 7 lines
Quoted 7 lines
fs/proc/base.c +7
-0+#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);
Quoted 8 lines
fs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, · line 2897
Quoted 4 lines
fs/proc/base.c +4
-0+ return 0; +} +#endif /* CONFIG_STACKLEAK_METRICS */ +This just prints the hashed value with the new pointer leak work. I don't think we want to print the fully exposed value via %px so it's not clear how valuable this proc file is now.
Yes, I tested that before sending the patch. I was confused when I saw the hashed values. But setting kptr_restrict to 1 fixed that for me:
root@hobbit:~# cat /proc/2627/lowest_stack prev_lowest_stack: 00000000ed8ca991 lowest_stack: 0000000040579d76 root@hobbit:~# echo 1 > /proc/sys/kernel/kptr_restrict root@hobbit:~# cat /proc/2627/lowest_stack prev_lowest_stack: ffffc9000094fdb8 lowest_stack: ffffc9000094f9e0
However, Documentation/printk-formats.txt and Documentation/sysctl/kernel.txt don't specify that behaviour.
Best regards,
Alexander
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
Quoted 53 lines · Alexander Popov
Hello Laura and Kees,
[adding Tobin C. Harding]
Quoted 18 lines · Laura Abbott
Quoted 17 lines · Alexander Popov
Introduce
CONFIG_STACKLEAK_METRICSproviding STACKLEAK information about tasks via the /proc file system. In particular, /proc/<pid>/lowest_stack shows the currentlowest_stackvalue and its final value from the previous syscall. That information can be useful for estimating the STACKLEAK performance impact for different workloads.Signed-off-by Alexander Popov <[email protected]>--- 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(+)[...]
Quoted 7 lines
Quoted 7 lines
fs/proc/base.c +7
-0+#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);Quoted 8 lines
fs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, · line 2897
Quoted 4 lines
fs/proc/base.c +4
-0+ return 0; +} +#endif /* CONFIG_STACKLEAK_METRICS */ +This just prints the hashed value with the new pointer leak work. I don't think we want to print the fully exposed value via %px so it's not clear how valuable this proc file is now.
Yes, I tested that before sending the patch. I was confused when I saw the hashed values. But setting
kptr_restrictto 1 fixed that for me:root@hobbit:~# cat /proc/2627/lowest_stack prev_lowest_stack: 00000000ed8ca991 lowest_stack: 0000000040579d76 root@hobbit:~# echo 1 > /proc/sys/kernel/kptr_restrict root@hobbit:~# cat /proc/2627/lowest_stack prev_lowest_stack: ffffc9000094fdb8 lowest_stack: ffffc9000094f9e0
Quoted 2 lines
However,
Documentation/printk-formats.txtandDocumentation/sysctl/kernel.txtdon't specify that behaviour.
thanks for pointing this out. Yes, %pK hashes the address now when kptr_restrict==0
And yes, I forgot to update the docs :( will amend.
thanks,
Tobin.
[kernel-hardening] Re: [PATCH RFC v6 5/6] fs/proc: Show STACKLEAK metrics in the /proc file system
In reply to Laura AbbottQuoted 28 lines · Laura Abbott
Quoted 27 lines · Kees Cook
Quoted 25 lines · Laura Abbott
Quoted 10 lines · Alexander Popov
fs/proc/base.c +1
-0@@ -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_namespacefs/proc/base.c static int proc_pid_patch_state(struct seq_file *m, · line 2898
Quoted 10 lines
fs/proc/base.c +9
-0*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 */ +This just prints the hashed value with the new pointer leak work.
I don't think we want to print thefprev_lowest_stackullyexposed value via %px so it's not clear how valuable this proc file is now.
Quoted 1 line
Quoted 1 line
Maybe print the size, not the location?
Yes, I think I can print: THREAD_SIZE - (addr & (THREAD_SIZE - 1)). I can call it "stack_depth", do you like it?
N.B. this value is not a really precise stack depth, because:
- we don't instrument all kernel functions, so a lot of them don't update thelowest_stack value;
- prev_lowest_stack is a final point of the poison search in erase_kstack(), it
is not an actual stack depth.
Or should I dwell on the current version and rely on non-zero kptr_restrict?
Quoted 3 lines
Hmmmmm, that starts to overlap with
CONFIG_DEBUG_STACK_USAGE. That's not a bad thing but it would be good to clarify what this is tracking vs.CONFIG_DEBUG_STACK_USAGE.
Thanks, Laura, I didn't know about CONFIG_DEBUG_STACK_USAGE. After testing it I think that it should remain independent, because:
- it works on sysrq;
- it dumps information about all tasks in the system at once;
- it provides precise information (in contrast to my metrics).
In addition, I guess, modifying sysrq output format might break the workflow of users, who parse it (but I'm not sure).
Best regards,
Alexander
[kernel-hardening] Re: [PATCH RFC v6 2/6] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
In reply to Laura AbbottQuoted 21 lines · Laura Abbott
Quoted 20 lines · Alexander Popov
scripts/gcc-plugins/stackleak_plugin.c new file +20
-0+/* Parse the plugin arguments */ +if (argc !=1 ) { +error (G_ ("bad number of the plugin arguments: %d" ), argc); +return 1 ; + } + +if (strcmp (argv[i].key,"track-min-size" )) { +error (G_ ("unknown option '-fplugin-arg-%s-%s'" ), + plugin_name, argv[i].key); +return 1 ; + } + +if (!argv[i].value) { +error (G_ ("no value supplied for option '-fplugin-arg-%s-%s'" ), + plugin_name, argv[i].key); +return 1 ; + } + + track_frame_size =atoi (argv[i].value); +if (track_frame_size <0 ) {
Quoted 8 lines
scripts/gcc-plugins/stackleak_plugin.c line 442
Quoted 4 lines
scripts/gcc-plugins/stackleak_plugin.c new file +4
-0+error (G_ ("invalid option argument '-fplugin-arg-%s-%s=%s'" ), + plugin_name, argv[i].key, argv[i].value); +return 1 ; + }I don't see i getting updated anywhere, which seems to be an artifact of removing the for loop. I'd prefer if you just kept the loop since the arm64 version requires a --disable option like structleak.
Ok, no problem, I'll return the loop and add --disable option in v7.
Best regards,
Alexander
Peter Zijlstra
maintainer[kernel-hardening] Re: [PATCH RFC v6 1/6] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls
In reply to Alexander PopovQuoted 9 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.
Have you looked at the entry rework in this series:
[kernel-hardening] Re: [PATCH RFC v6 1/6] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls
Hello Peter,
Quoted 6 lines · Peter Zijlstra
Quoted 5 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.
Quoted 8 lines
Quoted 4 lines
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.
Have you looked at the entry rework in this series:
Thanks for the link. I briefly looked through WIP.x86/pti branch. Should I rebase STACKLEAK patch series onto it?
Although I don't fully understand some of the commits, I can suppose that STACKLEAK stack erasing must be modified because of this trampoline stack introduction:
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?h=WIP.x86/pti&id=813b4125a835f2eb9aa6fb08dac0bc8eeadd69a1
Am I right? Are there other changes which I should consider?
May I also ask for your feedback on this version of the STACKLEAK patch series? Thanks!
Best regards,
Alexander
Peter Zijlstra
maintainer[kernel-hardening] Re: [PATCH RFC v6 1/6] x86/entry: Add STACKLEAK erasing the kernel stack at the end of syscalls
Quoted 14 lines · Alexander Popov
Hello Peter,
Quoted 11 lines · Peter Zijlstra
Quoted 9 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.
Quoted 6 lines
Quoted 3 lines
Have you looked at the entry rework in this series:
Thanks for the link. I briefly looked through WIP.x86/pti branch. Should I rebase STACKLEAK patch series onto it?
Probably a good idea; the tail end of that series is still somewhat in flux but the entry rework is fairly stable at this point.
Quoted 6 lines
Although I don't fully understand some of the commits, I can suppose that STACKLEAK stack erasing must be modified because of this trampoline stack introduction:
https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/commit/?h=WIP.x86/pti&id=813b4125a835f2eb9aa6fb08dac0bc8eeadd69a1Am I right? Are there other changes which I should consider?
You're right; the trampoline stack is what I was thinking of. You can run the erase thing when we're on the trampoline back out.
Quoted 1 line
May I also ask for your feedback on this version of the STACKLEAK patch series?
I meant to have a look, but have not yet found the time for it, its on the todo list.
Thanks!
Re: [kernel-hardening] [PATCH RFC v6 3/6] x86/entry: Erase kernel stack in syscall_trace_enter()
In reply to Dmitry V. LevinQuoted 61 lines · Dmitry V. Levin
Quoted 60 lines · 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.
Signed-off-by Alexander Popov <[email protected]>--- arch/x86/entry/common.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)arch/x86/entry/common.c +15
-3@@ -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,11 +87,15 @@ 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) +if (emulated) { +erase_kstack ();return -1L ; + }#ifdef CONFIG_SECCOMP /* @@ -117,8 +127,10 @@ static long syscall_trace_enter(struct pt_regs *regs) } ret =__secure_computing (&sd); -if (ret ==-1 ) +if (ret ==-1 ) { +erase_kstack ();return ret; + } }#endif
Quoted 7 lines
arch/x86/entry/common.c static long syscall_trace_enter(struct pt_regs *regs) · line 144
Quoted 7 lines
arch/x86/entry/common.c +1
-0@@ -127,6 +139,7 @@ static long syscall_trace_enter(struct pt_regs *regs)do_audit_syscall_entry (regs, arch); +erase_kstack ();return ret ?: regs->orig_ax; }
Hello Dmitry,
Thanks for the review.
Quoted 8 lines
wrt adding
erase_kstack()calls tosyscall_trace_enter(), I think the only case where this would be appropriate is that still has a chance of executing syscall code. In all cases wheresyscall_trace_enter()returns -1 no syscall code is going to be executed and the stack will be erased on exiting syscall anyway.In other words, only the last hunk of this patch seems to be useful, all others look redundant.
I agree with your point. I'll fix it in v7.
Quoted 2 lines
P.S. I've trimmed the Cc list to those who took part in earlier rounds of this discussion.
Excuse me, I've returned everybody back to CC list again :)
Best regards,
Alexander
Re: [kernel-hardening] [PATCH RFC v6 2/6] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
In reply to Alexander PopovQuoted 56 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:
- thecheck_alloca()call before alloca and thetrack_stack()callafter it;- the
track_stack()call for the functions with a stack frame size greater than or equal toCONFIG_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.
Signed-off-by Alexander Popov <[email protected]>--- arch/Kconfig | 15 ++ arch/x86/kernel/dumpstack.c | 15 ++ fs/exec.c | 25 ++ scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++++++ 5 files changed, 528 insertions(+) create mode 100644 scripts/gcc-plugins/stackleak_plugin.carch/Kconfig +5
-0@@ -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,19 @@ 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
arch/Kconfig config GCC_PLUGIN_STACKLEAK · line 561
Quoted 10 lines
arch/Kconfig +10
-0+ 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. Be careful with + this setting, don't break the poison search in erase_kstack. + If unsure, leave the default value 100. +
I don't think the warning is scaring enough. As erase_kstack (both 64-bit and 32-bit versions) checks for 128 consequent bytes of STACKLEAK_POISON, it would be a bad idea to raise STACKLEAK_TRACK_MIN_SIZE to a value higher than 120. Perhaps there has to be a consistency check that STACKLEAK_TRACK_MIN_SIZE does not break assumptions made in erase_kstack.
Quoted 16 lines
arch/Kconfig +0
-0config HAVE_CC_STACKPROTECTOR bool helparch/x86/kernel/dumpstack.c +5
-0@@ -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;
arch/x86/kernel/dumpstack.c static int __init code_bytes_setup(char *s) · line 332
Quoted 10 lines
arch/x86/kernel/dumpstack.c +10
-0+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
I think some rationale has to be given why 256 was chosen as the minimal size of stack space left after alloca.
Signature · 1 line
ldv
Re: [kernel-hardening] [PATCH RFC v6 2/6] gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
Quoted 65 lines · Dmitry V. Levin
Quoted 64 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:
- thecheck_alloca()call before alloca and thetrack_stack()callafter it;- the
track_stack()call for the functions with a stack frame size greater than or equal toCONFIG_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.
Signed-off-by Alexander Popov <[email protected]>--- arch/Kconfig | 15 ++ arch/x86/kernel/dumpstack.c | 15 ++ fs/exec.c | 25 ++ scripts/Makefile.gcc-plugins | 3 + scripts/gcc-plugins/stackleak_plugin.c | 470 +++++++++++++++++++++++++++++++++ 5 files changed, 528 insertions(+) create mode 100644 scripts/gcc-plugins/stackleak_plugin.carch/Kconfig +13
-0@@ -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,19 @@ 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. Be careful with + this setting, don't break the poison search in erase_kstack.
Quoted 8 lines
arch/Kconfig config GCC_PLUGIN_STACKLEAK · line 561
Quoted 2 lines
arch/Kconfig +2
-0+ If unsure, leave the default value 100. +I don't think the warning is scaring enough. As
erase_kstack(both 64-bit and 32-bit versions) checks for 128 consequent bytes ofSTACKLEAK_POISON, it would be a bad idea to raiseSTACKLEAK_TRACK_MIN_SIZEto a value higher than 120. Perhaps there has to be a consistency check thatSTACKLEAK_TRACK_MIN_SIZEdoes not break assumptions made inerase_kstack.
Thanks, I agree. In v7 I'll introduce a macro for the poison search depth and add a BUILD_BUG_ON(). It would be better than this scary remark in Kconfig.
Quoted 21 lines
Quoted 21 lines
arch/Kconfig +0
-0config HAVE_CC_STACKPROTECTOR bool helparch/x86/kernel/dumpstack.c +10
-0@@ -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));
Quoted 8 lines
arch/x86/kernel/dumpstack.c static int __init code_bytes_setup(char *s) · line 332
Quoted 5 lines
arch/x86/kernel/dumpstack.c +5
-0+ stack_left = sp - (unsigned long )stack_info.begin; + BUG_ON(stack_left < 256 || size >= stack_left - 256); +} +EXPORT_SYMBOL (check_alloca); +#endif I think some rationale has to be given why 256 was chosen as the minimal size of stack space left after alloca.
Unfortunately, I can't provide such a rationale. This value just looks sane to me. I'll introduce a macro instead of the hardcoded number.
Thanks again for the review!
Alexander