Skip to content

Commit 10e9ae9

Browse files
a13xp0p0vkees
authored andcommitted
gcc-plugins: Add STACKLEAK plugin for tracking the kernel stack
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. 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). 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 <alex.popov@linux.com> Tested-by: Laura Abbott <labbott@redhat.com> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent afaef01 commit 10e9ae9

4 files changed

Lines changed: 477 additions & 0 deletions

File tree

‎kernel/stackleak.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,31 @@ asmlinkage void stackleak_erase(void)
6060
current->lowest_stack = current_top_of_stack() - THREAD_SIZE/64;
6161
}
6262

63+
void __used stackleak_track_stack(void)
64+
{
65+
/*
66+
* N.B. stackleak_erase() fills the kernel stack with the poison value,
67+
* which has the register width. That code assumes that the value
68+
* of 'lowest_stack' is aligned on the register width boundary.
69+
*
70+
* That is true for x86 and x86_64 because of the kernel stack
71+
* alignment on these platforms (for details, see 'cc_stack_align' in
72+
* arch/x86/Makefile). Take care of that when you port STACKLEAK to
73+
* new platforms.
74+
*/
75+
unsigned long sp = (unsigned long)&sp;
76+
77+
/*
78+
* Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
79+
* STACKLEAK_SEARCH_DEPTH makes the poison search in
80+
* stackleak_erase() unreliable. Let's prevent that.
81+
*/
82+
BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE > STACKLEAK_SEARCH_DEPTH);
83+
84+
if (sp < current->lowest_stack &&
85+
sp >= (unsigned long)task_stack_page(current) +
86+
sizeof(unsigned long)) {
87+
current->lowest_stack = sp;
88+
}
89+
}
90+
EXPORT_SYMBOL(stackleak_track_stack);

‎scripts/Makefile.gcc-plugins‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT) \
2626
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_RANDSTRUCT_PERFORMANCE) \
2727
+= -fplugin-arg-randomize_layout_plugin-performance-mode
2828

29+
gcc-plugin-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak_plugin.so
30+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
31+
+= -DSTACKLEAK_PLUGIN
32+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
33+
+= -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE)
34+
ifdef CONFIG_GCC_PLUGIN_STACKLEAK
35+
DISABLE_STACKLEAK_PLUGIN += -fplugin-arg-stackleak_plugin-disable
36+
endif
37+
export DISABLE_STACKLEAK_PLUGIN
38+
2939
# All the plugin CFLAGS are collected here in case a build target needs to
3040
# filter them out of the KBUILD_CFLAGS.
3141
GCC_PLUGINS_CFLAGS := $(strip $(addprefix -fplugin=$(objtree)/scripts/gcc-plugins/, $(gcc-plugin-y)) $(gcc-plugin-cflags-y))

‎scripts/gcc-plugins/Kconfig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,16 @@ config GCC_PLUGIN_STACKLEAK
158158
* https://grsecurity.net/
159159
* https://pax.grsecurity.net/
160160

161+
config STACKLEAK_TRACK_MIN_SIZE
162+
int "Minimum stack frame size of functions tracked by STACKLEAK"
163+
default 100
164+
range 0 4096
165+
depends on GCC_PLUGIN_STACKLEAK
166+
help
167+
The STACKLEAK gcc plugin instruments the kernel code for tracking
168+
the lowest border of the kernel stack (and for some other purposes).
169+
It inserts the stackleak_track_stack() call for the functions with
170+
a stack frame size greater than or equal to this parameter.
171+
If unsure, leave the default value 100.
172+
161173
endif

0 commit comments

Comments
 (0)