Improvements of the stackleak gcc plugin
Subsystems (5)
v2 cover Improvements of the stackleak gcc plugin
This is the v2 of the patch series with various improvements of the stackleak gcc plugin.
The first three patches disable unneeded gcc plugin instrumentation for some files.
The fourth patch is the main improvement. It eliminates an unwanted side-effect of kernel code instrumentation performed by stackleak gcc plugin. This patch is a deep reengineering of the idea described on grsecurity blog:
https://grsecurity.net/resolving_an_unfortunate_stackleak_interaction
The final patch adds 'verbose' stackleak parameter for printing additional info about the kernel code instrumentation during kernel building.
I would like to thank Alexander Monakov <[email protected]> for his advisory on gcc internals.
This patch series was tested for gcc version 4.8, 5, 6, 7, 8, 9, and 10 on x86_64, i386 and arm64.
That was done using the project 'kernel-build-containers':
https://github.com/a13xp0p0v/kernel-build-containers
Changes from v1:
- rebase onto 5.8.0-rc2;
- don't exclude alloca() from the instrumentation logic, because it will be used in kernel stack offset randomization;
- reorder patches in the series;
- don't use gcc plugins for building vgettimeofday.c in arm and arm64 vDSO;
- follow alphabetic order in include/linux/compiler_attributes.h.
Link to v1:
https://lore.kernel.org/lkml/[email protected]/
Alexander Popov (5):
gcc-plugins/stackleak: Don't instrument itself
ARM: vdso: Don't use gcc plugins for building vgettimeofday.c
arm64: vdso: Don't use gcc plugins for building vgettimeofday.c gcc-plugins/stackleak: Use asm instrumentation to avoid useless
register saving
gcc-plugins/stackleak: Add 'verbose' plugin parameter
arch/arm/vdso/Makefile | 2 +- arch/arm64/kernel/vdso/Makefile | 2 +- include/linux/compiler_attributes.h | 13 ++ kernel/Makefile | 1 + kernel/stackleak.c | 16 +- scripts/Makefile.gcc-plugins | 2 + scripts/gcc-plugins/stackleak_plugin.c | 248 +++++++++++++++++++++---- 7 files changed, 239 insertions(+), 45 deletions(-)
Signature · 1 line
2.25.4
PATCH v2 1/5 gcc-plugins/stackleak: Don't instrument itself
There is no need to try instrumenting functions in kernel/stackleak.c. Otherwise that can cause issues if the cleanup pass of stackleak gcc plugin is disabled.
--- kernel/Makefile | 1 + 1 file changed, 1 insertion(+)
kernel/Makefile +1 -0
@@ -125,6 +125,7 @@ obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o +CFLAGS_stackleak.o += $(DISABLE_STACKLEAK_PLUGIN) obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o KASAN_SANITIZE_stackleak.o := n KCSAN_SANITIZE_stackleak.o := n
Signature · 1 line
2.25.4
Kees Cook
AppliedRe: [PATCH v2 1/5] gcc-plugins/stackleak: Don't instrument itself
Quoted 6 lines · Alexander Popov
There is no need to try instrumenting functions in
kernel/stackleak.c. Otherwise that can cause issues if the cleanup pass of stackleak gcc plugin is disabled.Signed-off-by Alexander Popov <[email protected]>Acked-by Kees Cook <[email protected]>
Thanks! Applied to for-next/gcc-plugins.
Signature · 1 line
Kees Cook
PATCH v2 2/5 ARM: vdso: Don't use gcc plugins for building vgettimeofday.c
In reply to Alexander PopovDon't use gcc plugins for building arch/arm/vdso/vgettimeofday.c to avoid unneeded instrumentation.
--- arch/arm/vdso/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/vdso/Makefile +1 -1
@@ -29,7 +29,7 @@ CPPFLAGS_vdso.lds += -P -C -U$(ARCH) CFLAGS_REMOVE_vdso.o = -pg # Force -O2 to avoid libgcc dependencies -CFLAGS_REMOVE_vgettimeofday.o = -pg -Os +CFLAGS_REMOVE_vgettimeofday.o = -pg -Os $(GCC_PLUGINS_CFLAGS) ifeq ($(c-gettimeofday-y),) CFLAGS_vgettimeofday.o = -O2 else
Signature · 1 line
2.25.4
Re: [PATCH v2 2/5] ARM: vdso: Don't use gcc plugins for building vgettimeofday.c
Quoted 4 lines · Alexander Popov
Don't use gcc plugins for building
arch/arm/vdso/vgettimeofday.cto avoid unneeded instrumentation.Signed-off-by Alexander Popov <[email protected]>
But why is skipping it safe?
Luis
Re: [PATCH v2 2/5] ARM: vdso: Don't use gcc plugins for building vgettimeofday.c
Quoted 7 lines · Luis Chamberlain
Quoted 4 lines · Alexander Popov
Don't use gcc plugins for building
arch/arm/vdso/vgettimeofday.cto avoid unneeded instrumentation.Signed-off-by Alexander Popov <[email protected]>But why is skipping it safe?
Hello Luis,
Kees and Will discussed that in detail in v1 of the series:
https://lore.kernel.org/lkml/20200610073046.GA15939@willie-the-truck/
Best regards,
Alexander
Kees Cook
AppliedRe: [PATCH v2 2/5] ARM: vdso: Don't use gcc plugins for building vgettimeofday.c
In reply to Alexander PopovQuoted 4 lines · Alexander Popov
Don't use gcc plugins for building
arch/arm/vdso/vgettimeofday.cto avoid unneeded instrumentation.Signed-off-by Alexander Popov <[email protected]>
Applied to for-next/gcc-plugins.
Signature · 1 line
Kees Cook
PATCH v2 3/5 arm64: vdso: Don't use gcc plugins for building vgettimeofday.c
In reply to Alexander PopovDon't use gcc plugins for building arch/arm64/kernel/vdso/vgettimeofday.c to avoid unneeded instrumentation.
--- arch/arm64/kernel/vdso/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kernel/vdso/Makefile +1 -1
@@ -29,7 +29,7 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \ ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18 ccflags-y += -DDISABLE_BRANCH_PROFILING -CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) +CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) KBUILD_CFLAGS += $(DISABLE_LTO) KASAN_SANITIZE := n UBSAN_SANITIZE := n
Signature · 1 line
2.25.4
Will Deacon
maintainerRe: [PATCH v2 3/5] arm64: vdso: Don't use gcc plugins for building vgettimeofday.c
Quoted 23 lines · Alexander Popov
Don't use gcc plugins for building
arch/arm64/kernel/vdso/vgettimeofday.cto avoid unneeded instrumentation.Signed-off-by Alexander Popov <[email protected]>--- arch/arm64/kernel/vdso/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/arm64/kernel/vdso/Makefile +1
-1@@ -29,7 +29,7 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \ ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18 ccflags-y += -DDISABLE_BRANCH_PROFILING -CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) +CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) KBUILD_CFLAGS += $(DISABLE_LTO) KASAN_SANITIZE := n UBSAN_SANITIZE := nSignature · 1 line
2.25.4
I'll pick this one up as a fix for 5.8, please let me know if that's a problem.
Will
Re: [PATCH v2 3/5] arm64: vdso: Don't use gcc plugins for building vgettimeofday.c
In reply to Alexander PopovQuoted 4 lines · Alexander Popov
Don't use gcc plugins for building
arch/arm64/kernel/vdso/vgettimeofday.cto avoid unneeded instrumentation.Signed-off-by Alexander Popov <[email protected]>
It looks like Will has taken this already, but:
Signature · 1 line
Kees Cook
PATCH v2 4/5 gcc-plugins/stackleak: Use asm instrumentation to avoid useless register saving
In reply to Alexander PopovThe kernel code instrumentation in stackleak gcc plugin works in two stages. At first, stack tracking is added to GIMPLE representation of every function (except some special cases). And later, when stack frame size info is available, stack tracking is removed from the RTL representation of the functions with small stack frame. There is an unwanted side-effect for these
functions: some of them do useless work with caller-saved registers.
As an example of such case, proc_sys_write without() instrumentation:
55 push %rbp
41 b8 01 00 00 00 mov $0x1,%r8d
48 89 e5 mov %rsp,%rbp
e8 11 ff ff ff callq ffffffff81284610 <proc_sys_call_handler>
5d pop %rbp
c3 retq
0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
00 00 00proc_sys_write() with instrumentation:
55 push %rbp
48 89 e5 mov %rsp,%rbp
41 56 push %r14
41 55 push %r13
41 54 push %r12
53 push %rbx
49 89 f4 mov %rsi,%r12
48 89 fb mov %rdi,%rbx
49 89 d5 mov %rdx,%r13
49 89 ce mov %rcx,%r14
4c 89 f1 mov %r14,%rcx
4c 89 ea mov %r13,%rdx
4c 89 e6 mov %r12,%rsi
48 89 df mov %rbx,%rdi
41 b8 01 00 00 00 mov $0x1,%r8d
e8 f2 fe ff ff callq ffffffff81298e80 <proc_sys_call_handler>
5b pop %rbx
41 5c pop %r12
41 5d pop %r13
41 5e pop %r14
5d pop %rbp
c3 retq
66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
00 00Let's improve the instrumentation to avoid this:
1. Make stackleak_track_stack() save all register that it works with. Use no_caller_saved_registers attribute for that function. This attribute is available for x86_64 and i386 starting from gcc-7.
2. Insert calling stackleak_track_stack() in asm:
asm volatile("call stackleak_track_stack" :: "r" (current_stack_pointer))
Here we use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h. The input constraint is taken into account during gcc shrink-wrapping optimization. It is needed to be sure that stackleak_track_stack() call is inserted after the prologue of the containing function, when the stack frame is prepared.
This work is a deep reengineering of the idea described on grsecurity blog
https://grsecurity.net/resolving_an_unfortunate_stackleak_interaction
--- include/linux/compiler_attributes.h | 13 ++ kernel/stackleak.c | 16 +- scripts/Makefile.gcc-plugins | 2 + scripts/gcc-plugins/stackleak_plugin.c | 205 +++++++++++++++++++++---- 4 files changed, 196 insertions(+), 40 deletions(-)
include/linux/compiler_attributes.h +13 -0
@@ -37,6 +37,7 @@# define __GCC4_has_attribute___copy__ 0# define __GCC4_has_attribute___designated_init__ 0# define __GCC4_has_attribute___externally_visible__ 1 +# define __GCC4_has_attribute___no_caller_saved_registers__ 0# define __GCC4_has_attribute___noclone__ 1# define __GCC4_has_attribute___nonstring__ 0# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
@@ -175,6 +176,18 @@*/ #define __mode (x) __attribute__((__mode__(x))) +/* +* Optional: only supported since gcc >= 7 +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-no_005fcaller_005fsaved_005fregisters-function-attribute_002c-x86 +* clang: https://clang.llvm.org/docs/AttributeReference.html#no-caller-saved-registers +*/ +#if __has_attribute (__no_caller_saved_registers__) +# define __no_caller_saved_registers __attribute__((__no_caller_saved_registers__)) +#else +# define __no_caller_saved_registers +#endif +/* * Optional: not supported by clang *
kernel/stackleak.c +4 -12
@@ -104,19 +104,9 @@ asmlinkage void notrace stackleak_erase(void) }NOKPROBE_SYMBOL (stackleak_erase); -void __used notrace stackleak_track_stack(void) +void __used __no_caller_saved_registers notrace stackleak_track_stack(void) { -/* -* N.B. stackleak_erase() fills the kernel stack with the poison value, -* which has the register width. That code assumes that the value -* of '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; +unsigned long sp = current_stack_pointer;/* * Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
@@ -125,6 +115,8 @@ void __used notrace stackleak_track_stack(void)*/ BUILD_BUG_ON (CONFIG_STACKLEAK_TRACK_MIN_SIZE >STACKLEAK_SEARCH_DEPTH ); +/* 'lowest_stack' should be aligned on the register width boundary */ + sp =ALIGN (sp,sizeof (unsigned long ));if (sp < current->lowest_stack && sp >= (unsigned long )task_stack_page (current) +sizeof (unsigned long )) {
scripts/Makefile.gcc-plugins +2 -0
@@ -33,6 +33,8 @@ gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -DSTACKLEAK_PLUGIN gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE) +gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ + += -fplugin-arg-stackleak_plugin-arch=$(SRCARCH) ifdef CONFIG_GCC_PLUGIN_STACKLEAK DISABLE_STACKLEAK_PLUGIN += -fplugin-arg-stackleak_plugin-disable endif
scripts/gcc-plugins/stackleak_plugin.c +177 -28
282 lines not on this page · raw message
Signature · 1 line
2.25.4
PATCH v2 5/5 gcc-plugins/stackleak: Add 'verbose' plugin parameter
In reply to Alexander PopovAdd 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation.
For using it add the following to scripts/Makefile.gcc-plugins:
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
+= -fplugin-arg-stackleak_plugin-verbose--- scripts/gcc-plugins/stackleak_plugin.c | 47 +++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-)
scripts/gcc-plugins/stackleak_plugin.c +42 -5
@@ -34,6 +34,8 @@ __visible int plugin_is_GPL_compatible;static int track_frame_size =-1 ;static bool build_for_x86 = false;static const char track_function[] ="stackleak_track_stack" ; +static bool disable = false; +static bool verbose = false;/* * Mark these global variables (roots) for gcc garbage collector since
@@ -46,6 +48,7 @@ static struct plugin_info stackleak_plugin_info = { .help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n""arch=target_arch\tspecify target build arch\n" "disable\t\tdo not activate the plugin\n" +"verbose\t\tprint info about the instrumentation\n" };static void add_stack_tracking_gcall (gimple_stmt_iterator *gsi,bool after)
@@ -102,6 +105,10 @@ static tree get_current_stack_pointer_decl(void)return var; } +if (verbose) { +fprintf (stderr,"stackleak: missing current_stack_pointer in %s()\n" , +DECL_NAME_POINTER (current_function_decl)); + }return NULL_TREE ; }
@@ -195,6 +202,11 @@ static unsigned int stackleak_instrument_execute(void)if (!is_alloca (stmt))continue ; +if (verbose) { +fprintf (stderr,"stackleak: be careful, alloca() in %s()\n" , +DECL_NAME_POINTER (current_function_decl)); + } +/* Insert stackleak_track_stack() call after alloca() */ add_stack_tracking (&gsi, true);if (bb == entry_bb)
@@ -384,13 +396,31 @@ static bool remove_stack_tracking_gasm(void)*/ static unsigned int stackleak_cleanup_execute (void ) { +const char *fn =DECL_NAME_POINTER (current_function_decl);bool removed = false; -if (cfun->calls_alloca) +/* +* Leave stack tracking in functions that call alloca(). +* Additional case: +* gcc before version 7 called allocate_dynamic_stack_space() from +* expand_stack_vars() for runtime alignment of constant-sized stack +* variables. That caused cfun->calls_alloca to be set for functions +* that in fact don't use alloca(). +* For more info see gcc commit 7072df0aae0c59ae437e. +* Let's leave such functions instrumented as well. +*/ +if (cfun->calls_alloca) { +if (verbose) +fprintf (stderr,"stackleak: instrument %s(): calls_alloca\n" , fn);return 0 ; + } -if (large_stack_frame ()) +/* Leave stack tracking in functions with large stack frame */ +if (large_stack_frame ()) { +if (verbose) +fprintf (stderr,"stackleak: instrument %s()\n" , fn);return 0 ; + }if (lookup_attribute_spec (get_identifier ("no_caller_saved_registers" ))) removed =remove_stack_tracking_gasm ();
@@ -516,9 +546,6 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,/* Parse the plugin arguments */ for (i =0 ; i < argc; i++) { -if (!strcmp (argv[i].key,"disable" )) -return 0 ; -if (!strcmp (argv[i].key,"track-min-size" )) {if (!argv[i].value) { error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
@@ -541,6 +568,10 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,if (!strcmp (argv[i].value,"x86" )) build_for_x86 = true; + }else if (!strcmp (argv[i].key,"disable" )) { + disable = true; + }else if (!strcmp (argv[i].key,"verbose" )) { + verbose = true; }else {error (G_ ("unknown option '-fplugin-arg-%s-%s'" ), plugin_name, argv[i].key);
@@ -548,6 +579,12 @@ __visible int plugin_init(struct plugin_name_args *plugin_info, } } +if (disable) { +if (verbose) +fprintf (stderr,"stackleak: disabled for this translation unit\n" ); +return 0 ; + } +/* Give the information about the plugin */ register_callback (plugin_name,PLUGIN_INFO ,NULL , &stackleak_plugin_info);
Signature · 1 line
2.25.4
Re: [PATCH v2 5/5] gcc-plugins/stackleak: Add 'verbose' plugin parameter
Quoted 7 lines · Alexander Popov
Add 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation.
For using it add the following to
scripts/Makefile.gcc-plugins:gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-verbose
Would be nice if we instead could pass an argument to make which lets us enable this.
Luis
Re: [PATCH v2 5/5] gcc-plugins/stackleak: Add 'verbose' plugin parameter
Quoted 11 lines · Luis Chamberlain
Quoted 7 lines · Alexander Popov
Add 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation.
For using it add the following to
scripts/Makefile.gcc-plugins:gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-verboseWould be nice if we instead could pass an argument to make which lets us enable this.
This feature is useful only for debugging stackleak gcc plugin.
The cflag that enables it is similar to -fplugin-arg-structleak_plugin-verbose, which is used for debugging the structleak plugin.
This debugging feature clutters the kernel build output, I don't think that many people will use it. So IMO creating a separate argument for make is not really needed.
Thanks!
Best regards,
Alexander
Re: [PATCH v2 5/5] gcc-plugins/stackleak: Add 'verbose' plugin parameter
Quoted 13 lines · Alexander Popov
Quoted 11 lines · Luis Chamberlain
Quoted 7 lines · Alexander Popov
Add 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation.
For using it add the following to
scripts/Makefile.gcc-plugins:gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-verboseWould be nice if we instead could pass an argument to make which lets us enable this.
Quoted 8 lines
This feature is useful only for debugging stackleak gcc plugin.
The cflag that enables it is similar to -fplugin-arg-structleak_plugin-verbose, which is used for debugging the structleak plugin.
This debugging feature clutters the kernel build output, I don't think that many people will use it. So IMO creating a separate argument for make is not really needed.
Yup, agreed. The precedent for plugin verbosity is via CONFIGs. They're not really general purpose enough to justify a "make" argument.
Signature · 1 line
Kees Cook
Kees Cook
AppliedRe: [PATCH v2 5/5] gcc-plugins/stackleak: Add 'verbose' plugin parameter
In reply to Alexander PopovQuoted 9 lines · Alexander Popov
Add 'verbose' plugin parameter for stackleak gcc plugin. It can be used for printing additional info about the kernel code instrumentation.
For using it add the following to
scripts/Makefile.gcc-plugins:gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \ += -fplugin-arg-stackleak_plugin-verboseSigned-off-by Alexander Popov <[email protected]>
Applied to for-next/gcc-plugins.
Signature · 1 line
Kees Cook
Quoted 6 lines · Alexander Popov
This is the v2 of the patch series with various improvements of the stackleak gcc plugin.
The first three patches disable unneeded gcc plugin instrumentation for some files.
Quoted 7 lines
The fourth patch is the main improvement. It eliminates an unwanted side-effect of kernel code instrumentation performed by stackleak gcc plugin. This patch is a deep reengineering of the idea described on grsecurity blog:
https://grsecurity.net/resolving_an_unfortunate_stackleak_interaction[...]
Applied to arm64 (for-next/fixes), thanks!
[1/1] arm64: vdso: Don't use gcc plugins for building vgettimeofday.c
https://git.kernel.org/arm64/c/e56404e8e475
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev