Skip to content

Commit feee1b8

Browse files
a13xp0p0vkees
authored andcommitted
gcc-plugins/stackleak: Use asm instrumentation to avoid useless register saving
The 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 00 proc_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 00 Let'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 Signed-off-by: Alexander Popov <[email protected]> Acked-by: Miguel Ojeda <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent ddfaf0e commit feee1b8

File tree

4 files changed

+196
-40
lines changed

4 files changed

+196
-40
lines changed

include/linux/compiler_attributes.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
# define __GCC4_has_attribute___copy__ 0
3838
# define __GCC4_has_attribute___designated_init__ 0
3939
# define __GCC4_has_attribute___externally_visible__ 1
40+
# define __GCC4_has_attribute___no_caller_saved_registers__ 0
4041
# define __GCC4_has_attribute___noclone__ 1
4142
# define __GCC4_has_attribute___nonstring__ 0
4243
# define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
@@ -175,6 +176,18 @@
175176
*/
176177
#define __mode(x) __attribute__((__mode__(x)))
177178

179+
/*
180+
* Optional: only supported since gcc >= 7
181+
*
182+
* gcc: https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html#index-no_005fcaller_005fsaved_005fregisters-function-attribute_002c-x86
183+
* clang: https://clang.llvm.org/docs/AttributeReference.html#no-caller-saved-registers
184+
*/
185+
#if __has_attribute(__no_caller_saved_registers__)
186+
# define __no_caller_saved_registers __attribute__((__no_caller_saved_registers__))
187+
#else
188+
# define __no_caller_saved_registers
189+
#endif
190+
178191
/*
179192
* Optional: not supported by clang
180193
*

kernel/stackleak.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,9 @@ asmlinkage void notrace stackleak_erase(void)
104104
}
105105
NOKPROBE_SYMBOL(stackleak_erase);
106106

107-
void __used notrace stackleak_track_stack(void)
107+
void __used __no_caller_saved_registers notrace stackleak_track_stack(void)
108108
{
109-
/*
110-
* N.B. stackleak_erase() fills the kernel stack with the poison value,
111-
* which has the register width. That code assumes that the value
112-
* of 'lowest_stack' is aligned on the register width boundary.
113-
*
114-
* That is true for x86 and x86_64 because of the kernel stack
115-
* alignment on these platforms (for details, see 'cc_stack_align' in
116-
* arch/x86/Makefile). Take care of that when you port STACKLEAK to
117-
* new platforms.
118-
*/
119-
unsigned long sp = (unsigned long)&sp;
109+
unsigned long sp = current_stack_pointer;
120110

121111
/*
122112
* Having CONFIG_STACKLEAK_TRACK_MIN_SIZE larger than
@@ -125,6 +115,8 @@ void __used notrace stackleak_track_stack(void)
125115
*/
126116
BUILD_BUG_ON(CONFIG_STACKLEAK_TRACK_MIN_SIZE > STACKLEAK_SEARCH_DEPTH);
127117

118+
/* 'lowest_stack' should be aligned on the register width boundary */
119+
sp = ALIGN(sp, sizeof(unsigned long));
128120
if (sp < current->lowest_stack &&
129121
sp >= (unsigned long)task_stack_page(current) +
130122
sizeof(unsigned long)) {

scripts/Makefile.gcc-plugins

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
3333
+= -DSTACKLEAK_PLUGIN
3434
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
3535
+= -fplugin-arg-stackleak_plugin-track-min-size=$(CONFIG_STACKLEAK_TRACK_MIN_SIZE)
36+
gcc-plugin-cflags-$(CONFIG_GCC_PLUGIN_STACKLEAK) \
37+
+= -fplugin-arg-stackleak_plugin-arch=$(SRCARCH)
3638
ifdef CONFIG_GCC_PLUGIN_STACKLEAK
3739
DISABLE_STACKLEAK_PLUGIN += -fplugin-arg-stackleak_plugin-disable
3840
endif

scripts/gcc-plugins/stackleak_plugin.c

Lines changed: 177 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* Debugging:
2222
* - use fprintf() to stderr, debug_generic_expr(), debug_gimple_stmt(),
23-
* print_rtl() and print_simple_rtl();
23+
* print_rtl_single() and debug_rtx();
2424
* - add "-fdump-tree-all -fdump-rtl-all" to the plugin CFLAGS in
2525
* Makefile.gcc-plugins to see the verbose dumps of the gcc passes;
2626
* - use gcc -E to understand the preprocessing shenanigans;
@@ -32,6 +32,7 @@
3232
__visible int plugin_is_GPL_compatible;
3333

3434
static int track_frame_size = -1;
35+
static bool build_for_x86 = false;
3536
static const char track_function[] = "stackleak_track_stack";
3637

3738
/*
@@ -43,32 +44,31 @@ static GTY(()) tree track_function_decl;
4344
static struct plugin_info stackleak_plugin_info = {
4445
.version = "201707101337",
4546
.help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
47+
"arch=target_arch\tspecify target build arch\n"
4648
"disable\t\tdo not activate the plugin\n"
4749
};
4850

49-
static void stackleak_add_track_stack(gimple_stmt_iterator *gsi, bool after)
51+
static void add_stack_tracking_gcall(gimple_stmt_iterator *gsi, bool after)
5052
{
5153
gimple stmt;
52-
gcall *stackleak_track_stack;
54+
gcall *gimple_call;
5355
cgraph_node_ptr node;
5456
basic_block bb;
5557

56-
/* Insert call to void stackleak_track_stack(void) */
58+
/* Insert calling stackleak_track_stack() */
5759
stmt = gimple_build_call(track_function_decl, 0);
58-
stackleak_track_stack = as_a_gcall(stmt);
59-
if (after) {
60-
gsi_insert_after(gsi, stackleak_track_stack,
61-
GSI_CONTINUE_LINKING);
62-
} else {
63-
gsi_insert_before(gsi, stackleak_track_stack, GSI_SAME_STMT);
64-
}
60+
gimple_call = as_a_gcall(stmt);
61+
if (after)
62+
gsi_insert_after(gsi, gimple_call, GSI_CONTINUE_LINKING);
63+
else
64+
gsi_insert_before(gsi, gimple_call, GSI_SAME_STMT);
6565

6666
/* Update the cgraph */
67-
bb = gimple_bb(stackleak_track_stack);
67+
bb = gimple_bb(gimple_call);
6868
node = cgraph_get_create_node(track_function_decl);
6969
gcc_assert(node);
7070
cgraph_create_edge(cgraph_get_node(current_function_decl), node,
71-
stackleak_track_stack, bb->count,
71+
gimple_call, bb->count,
7272
compute_call_stmt_bb_frequency(current_function_decl, bb));
7373
}
7474

@@ -85,6 +85,79 @@ static bool is_alloca(gimple stmt)
8585
return false;
8686
}
8787

88+
static tree get_current_stack_pointer_decl(void)
89+
{
90+
varpool_node_ptr node;
91+
92+
FOR_EACH_VARIABLE(node) {
93+
tree var = NODE_DECL(node);
94+
tree name = DECL_NAME(var);
95+
96+
if (DECL_NAME_LENGTH(var) != sizeof("current_stack_pointer") - 1)
97+
continue;
98+
99+
if (strcmp(IDENTIFIER_POINTER(name), "current_stack_pointer"))
100+
continue;
101+
102+
return var;
103+
}
104+
105+
return NULL_TREE;
106+
}
107+
108+
static void add_stack_tracking_gasm(gimple_stmt_iterator *gsi, bool after)
109+
{
110+
gasm *asm_call = NULL;
111+
tree sp_decl, input;
112+
vec<tree, va_gc> *inputs = NULL;
113+
114+
/* 'no_caller_saved_registers' is currently supported only for x86 */
115+
gcc_assert(build_for_x86);
116+
117+
/*
118+
* Insert calling stackleak_track_stack() in asm:
119+
* asm volatile("call stackleak_track_stack"
120+
* :: "r" (current_stack_pointer))
121+
* Use ASM_CALL_CONSTRAINT trick from arch/x86/include/asm/asm.h.
122+
* This constraint is taken into account during gcc shrink-wrapping
123+
* optimization. It is needed to be sure that stackleak_track_stack()
124+
* call is inserted after the prologue of the containing function,
125+
* when the stack frame is prepared.
126+
*/
127+
sp_decl = get_current_stack_pointer_decl();
128+
if (sp_decl == NULL_TREE) {
129+
add_stack_tracking_gcall(gsi, after);
130+
return;
131+
}
132+
input = build_tree_list(NULL_TREE, build_const_char_string(2, "r"));
133+
input = chainon(NULL_TREE, build_tree_list(input, sp_decl));
134+
vec_safe_push(inputs, input);
135+
asm_call = gimple_build_asm_vec("call stackleak_track_stack",
136+
inputs, NULL, NULL, NULL);
137+
gimple_asm_set_volatile(asm_call, true);
138+
if (after)
139+
gsi_insert_after(gsi, asm_call, GSI_CONTINUE_LINKING);
140+
else
141+
gsi_insert_before(gsi, asm_call, GSI_SAME_STMT);
142+
update_stmt(asm_call);
143+
}
144+
145+
static void add_stack_tracking(gimple_stmt_iterator *gsi, bool after)
146+
{
147+
/*
148+
* The 'no_caller_saved_registers' attribute is used for
149+
* stackleak_track_stack(). If the compiler supports this attribute for
150+
* the target arch, we can add calling stackleak_track_stack() in asm.
151+
* That improves performance: we avoid useless operations with the
152+
* caller-saved registers in the functions from which we will remove
153+
* stackleak_track_stack() call during the stackleak_cleanup pass.
154+
*/
155+
if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
156+
add_stack_tracking_gasm(gsi, after);
157+
else
158+
add_stack_tracking_gcall(gsi, after);
159+
}
160+
88161
/*
89162
* Work with the GIMPLE representation of the code. Insert the
90163
* stackleak_track_stack() call after alloca() and into the beginning
@@ -94,7 +167,7 @@ static unsigned int stackleak_instrument_execute(void)
94167
{
95168
basic_block bb, entry_bb;
96169
bool prologue_instrumented = false, is_leaf = true;
97-
gimple_stmt_iterator gsi;
170+
gimple_stmt_iterator gsi = { 0 };
98171

99172
/*
100173
* ENTRY_BLOCK_PTR is a basic block which represents possible entry
@@ -123,7 +196,7 @@ static unsigned int stackleak_instrument_execute(void)
123196
continue;
124197

125198
/* Insert stackleak_track_stack() call after alloca() */
126-
stackleak_add_track_stack(&gsi, true);
199+
add_stack_tracking(&gsi, true);
127200
if (bb == entry_bb)
128201
prologue_instrumented = true;
129202
}
@@ -168,7 +241,7 @@ static unsigned int stackleak_instrument_execute(void)
168241
bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
169242
}
170243
gsi = gsi_after_labels(bb);
171-
stackleak_add_track_stack(&gsi, false);
244+
add_stack_tracking(&gsi, false);
172245

173246
return 0;
174247
}
@@ -182,21 +255,10 @@ static bool large_stack_frame(void)
182255
#endif
183256
}
184257

185-
/*
186-
* Work with the RTL representation of the code.
187-
* Remove the unneeded stackleak_track_stack() calls from the functions
188-
* which don't call alloca() and don't have a large enough stack frame size.
189-
*/
190-
static unsigned int stackleak_cleanup_execute(void)
258+
static void remove_stack_tracking_gcall(void)
191259
{
192260
rtx_insn *insn, *next;
193261

194-
if (cfun->calls_alloca)
195-
return 0;
196-
197-
if (large_stack_frame())
198-
return 0;
199-
200262
/*
201263
* Find stackleak_track_stack() calls. Loop through the chain of insns,
202264
* which is an RTL representation of the code for a function.
@@ -257,6 +319,84 @@ static unsigned int stackleak_cleanup_execute(void)
257319
}
258320
#endif
259321
}
322+
}
323+
324+
static bool remove_stack_tracking_gasm(void)
325+
{
326+
bool removed = false;
327+
rtx_insn *insn, *next;
328+
329+
/* 'no_caller_saved_registers' is currently supported only for x86 */
330+
gcc_assert(build_for_x86);
331+
332+
/*
333+
* Find stackleak_track_stack() asm calls. Loop through the chain of
334+
* insns, which is an RTL representation of the code for a function.
335+
*
336+
* The example of a matching insn:
337+
* (insn 11 5 12 2 (parallel [ (asm_operands/v
338+
* ("call stackleak_track_stack") ("") 0
339+
* [ (reg/v:DI 7 sp [ current_stack_pointer ]) ]
340+
* [ (asm_input:DI ("r")) ] [])
341+
* (clobber (reg:CC 17 flags)) ]) -1 (nil))
342+
*/
343+
for (insn = get_insns(); insn; insn = next) {
344+
rtx body;
345+
346+
next = NEXT_INSN(insn);
347+
348+
/* Check the expression code of the insn */
349+
if (!NONJUMP_INSN_P(insn))
350+
continue;
351+
352+
/*
353+
* Check the expression code of the insn body, which is an RTL
354+
* Expression (RTX) describing the side effect performed by
355+
* that insn.
356+
*/
357+
body = PATTERN(insn);
358+
359+
if (GET_CODE(body) != PARALLEL)
360+
continue;
361+
362+
body = XVECEXP(body, 0, 0);
363+
364+
if (GET_CODE(body) != ASM_OPERANDS)
365+
continue;
366+
367+
if (strcmp(ASM_OPERANDS_TEMPLATE(body),
368+
"call stackleak_track_stack")) {
369+
continue;
370+
}
371+
372+
delete_insn_and_edges(insn);
373+
gcc_assert(!removed);
374+
removed = true;
375+
}
376+
377+
return removed;
378+
}
379+
380+
/*
381+
* Work with the RTL representation of the code.
382+
* Remove the unneeded stackleak_track_stack() calls from the functions
383+
* which don't call alloca() and don't have a large enough stack frame size.
384+
*/
385+
static unsigned int stackleak_cleanup_execute(void)
386+
{
387+
bool removed = false;
388+
389+
if (cfun->calls_alloca)
390+
return 0;
391+
392+
if (large_stack_frame())
393+
return 0;
394+
395+
if (lookup_attribute_spec(get_identifier("no_caller_saved_registers")))
396+
removed = remove_stack_tracking_gasm();
397+
398+
if (!removed)
399+
remove_stack_tracking_gcall();
260400

261401
return 0;
262402
}
@@ -392,6 +532,15 @@ __visible int plugin_init(struct plugin_name_args *plugin_info,
392532
plugin_name, argv[i].key, argv[i].value);
393533
return 1;
394534
}
535+
} else if (!strcmp(argv[i].key, "arch")) {
536+
if (!argv[i].value) {
537+
error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
538+
plugin_name, argv[i].key);
539+
return 1;
540+
}
541+
542+
if (!strcmp(argv[i].value, "x86"))
543+
build_for_x86 = true;
395544
} else {
396545
error(G_("unknown option '-fplugin-arg-%s-%s'"),
397546
plugin_name, argv[i].key);

0 commit comments

Comments
 (0)