xref: /linux/scripts/gcc-plugins/stackleak_plugin.c (revision b8265621f4888af9494e1d685620871ec81bc33d)
1 /*
2  * Copyright 2011-2017 by the PaX Team <pageexec@freemail.hu>
3  * Modified by Alexander Popov <alex.popov@linux.com>
4  * Licensed under the GPL v2
5  *
6  * Note: the choice of the license means that the compilation process is
7  * NOT 'eligible' as defined by gcc's library exception to the GPL v3,
8  * but for the kernel it doesn't matter since it doesn't link against
9  * any of the gcc libraries
10  *
11  * This gcc plugin is needed for tracking the lowest border of the kernel stack.
12  * It instruments the kernel code inserting stackleak_track_stack() calls:
13  *  - after alloca();
14  *  - for the functions with a stack frame size greater than or equal
15  *     to the "track-min-size" plugin parameter.
16  *
17  * This plugin is ported from grsecurity/PaX. For more information see:
18  *   https://grsecurity.net/
19  *   https://pax.grsecurity.net/
20  *
21  * Debugging:
22  *  - use fprintf() to stderr, debug_generic_expr(), debug_gimple_stmt(),
23  *     print_rtl() and print_simple_rtl();
24  *  - add "-fdump-tree-all -fdump-rtl-all" to the plugin CFLAGS in
25  *     Makefile.gcc-plugins to see the verbose dumps of the gcc passes;
26  *  - use gcc -E to understand the preprocessing shenanigans;
27  *  - use gcc with enabled CFG/GIMPLE/SSA verification (--enable-checking).
28  */
29 
30 #include "gcc-common.h"
31 
32 __visible int plugin_is_GPL_compatible;
33 
34 static int track_frame_size = -1;
35 static const char track_function[] = "stackleak_track_stack";
36 
37 /*
38  * Mark these global variables (roots) for gcc garbage collector since
39  * they point to the garbage-collected memory.
40  */
41 static GTY(()) tree track_function_decl;
42 
43 static struct plugin_info stackleak_plugin_info = {
44 	.version = "201707101337",
45 	.help = "track-min-size=nn\ttrack stack for functions with a stack frame size >= nn bytes\n"
46 		"disable\t\tdo not activate the plugin\n"
47 };
48 
49 static void stackleak_add_track_stack(gimple_stmt_iterator *gsi, bool after)
50 {
51 	gimple stmt;
52 	gcall *stackleak_track_stack;
53 	cgraph_node_ptr node;
54 	basic_block bb;
55 
56 	/* Insert call to void stackleak_track_stack(void) */
57 	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 	}
65 
66 	/* Update the cgraph */
67 	bb = gimple_bb(stackleak_track_stack);
68 	node = cgraph_get_create_node(track_function_decl);
69 	gcc_assert(node);
70 	cgraph_create_edge(cgraph_get_node(current_function_decl), node,
71 			stackleak_track_stack, bb->count,
72 			compute_call_stmt_bb_frequency(current_function_decl, bb));
73 }
74 
75 static bool is_alloca(gimple stmt)
76 {
77 	if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA))
78 		return true;
79 
80 #if BUILDING_GCC_VERSION >= 4007
81 	if (gimple_call_builtin_p(stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
82 		return true;
83 #endif
84 
85 	return false;
86 }
87 
88 /*
89  * Work with the GIMPLE representation of the code. Insert the
90  * stackleak_track_stack() call after alloca() and into the beginning
91  * of the function if it is not instrumented.
92  */
93 static unsigned int stackleak_instrument_execute(void)
94 {
95 	basic_block bb, entry_bb;
96 	bool prologue_instrumented = false, is_leaf = true;
97 	gimple_stmt_iterator gsi;
98 
99 	/*
100 	 * ENTRY_BLOCK_PTR is a basic block which represents possible entry
101 	 * point of a function. This block does not contain any code and
102 	 * has a CFG edge to its successor.
103 	 */
104 	gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
105 	entry_bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
106 
107 	/*
108 	 * Loop through the GIMPLE statements in each of cfun basic blocks.
109 	 * cfun is a global variable which represents the function that is
110 	 * currently processed.
111 	 */
112 	FOR_EACH_BB_FN(bb, cfun) {
113 		for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
114 			gimple stmt;
115 
116 			stmt = gsi_stmt(gsi);
117 
118 			/* Leaf function is a function which makes no calls */
119 			if (is_gimple_call(stmt))
120 				is_leaf = false;
121 
122 			if (!is_alloca(stmt))
123 				continue;
124 
125 			/* Insert stackleak_track_stack() call after alloca() */
126 			stackleak_add_track_stack(&gsi, true);
127 			if (bb == entry_bb)
128 				prologue_instrumented = true;
129 		}
130 	}
131 
132 	if (prologue_instrumented)
133 		return 0;
134 
135 	/*
136 	 * Special cases to skip the instrumentation.
137 	 *
138 	 * Taking the address of static inline functions materializes them,
139 	 * but we mustn't instrument some of them as the resulting stack
140 	 * alignment required by the function call ABI will break other
141 	 * assumptions regarding the expected (but not otherwise enforced)
142 	 * register clobbering ABI.
143 	 *
144 	 * Case in point: native_save_fl on amd64 when optimized for size
145 	 * clobbers rdx if it were instrumented here.
146 	 *
147 	 * TODO: any more special cases?
148 	 */
149 	if (is_leaf &&
150 	    !TREE_PUBLIC(current_function_decl) &&
151 	    DECL_DECLARED_INLINE_P(current_function_decl)) {
152 		return 0;
153 	}
154 
155 	if (is_leaf &&
156 	    !strncmp(IDENTIFIER_POINTER(DECL_NAME(current_function_decl)),
157 		     "_paravirt_", 10)) {
158 		return 0;
159 	}
160 
161 	/* Insert stackleak_track_stack() call at the function beginning */
162 	bb = entry_bb;
163 	if (!single_pred_p(bb)) {
164 		/* gcc_assert(bb_loop_depth(bb) ||
165 				(bb->flags & BB_IRREDUCIBLE_LOOP)); */
166 		split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
167 		gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
168 		bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
169 	}
170 	gsi = gsi_after_labels(bb);
171 	stackleak_add_track_stack(&gsi, false);
172 
173 	return 0;
174 }
175 
176 static bool large_stack_frame(void)
177 {
178 #if BUILDING_GCC_VERSION >= 8000
179 	return maybe_ge(get_frame_size(), track_frame_size);
180 #else
181 	return (get_frame_size() >= track_frame_size);
182 #endif
183 }
184 
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)
191 {
192 	rtx_insn *insn, *next;
193 
194 	if (cfun->calls_alloca)
195 		return 0;
196 
197 	if (large_stack_frame())
198 		return 0;
199 
200 	/*
201 	 * Find stackleak_track_stack() calls. Loop through the chain of insns,
202 	 * which is an RTL representation of the code for a function.
203 	 *
204 	 * The example of a matching insn:
205 	 *  (call_insn 8 4 10 2 (call (mem (symbol_ref ("stackleak_track_stack")
206 	 *  [flags 0x41] <function_decl 0x7f7cd3302a80 stackleak_track_stack>)
207 	 *  [0 stackleak_track_stack S1 A8]) (0)) 675 {*call} (expr_list
208 	 *  (symbol_ref ("stackleak_track_stack") [flags 0x41] <function_decl
209 	 *  0x7f7cd3302a80 stackleak_track_stack>) (expr_list (0) (nil))) (nil))
210 	 */
211 	for (insn = get_insns(); insn; insn = next) {
212 		rtx body;
213 
214 		next = NEXT_INSN(insn);
215 
216 		/* Check the expression code of the insn */
217 		if (!CALL_P(insn))
218 			continue;
219 
220 		/*
221 		 * Check the expression code of the insn body, which is an RTL
222 		 * Expression (RTX) describing the side effect performed by
223 		 * that insn.
224 		 */
225 		body = PATTERN(insn);
226 
227 		if (GET_CODE(body) == PARALLEL)
228 			body = XVECEXP(body, 0, 0);
229 
230 		if (GET_CODE(body) != CALL)
231 			continue;
232 
233 		/*
234 		 * Check the first operand of the call expression. It should
235 		 * be a mem RTX describing the needed subroutine with a
236 		 * symbol_ref RTX.
237 		 */
238 		body = XEXP(body, 0);
239 		if (GET_CODE(body) != MEM)
240 			continue;
241 
242 		body = XEXP(body, 0);
243 		if (GET_CODE(body) != SYMBOL_REF)
244 			continue;
245 
246 		if (SYMBOL_REF_DECL(body) != track_function_decl)
247 			continue;
248 
249 		/* Delete the stackleak_track_stack() call */
250 		delete_insn_and_edges(insn);
251 #if BUILDING_GCC_VERSION >= 4007 && BUILDING_GCC_VERSION < 8000
252 		if (GET_CODE(next) == NOTE &&
253 		    NOTE_KIND(next) == NOTE_INSN_CALL_ARG_LOCATION) {
254 			insn = next;
255 			next = NEXT_INSN(insn);
256 			delete_insn_and_edges(insn);
257 		}
258 #endif
259 	}
260 
261 	return 0;
262 }
263 
264 static bool stackleak_gate(void)
265 {
266 	tree section;
267 
268 	section = lookup_attribute("section",
269 				   DECL_ATTRIBUTES(current_function_decl));
270 	if (section && TREE_VALUE(section)) {
271 		section = TREE_VALUE(TREE_VALUE(section));
272 
273 		if (!strncmp(TREE_STRING_POINTER(section), ".init.text", 10))
274 			return false;
275 		if (!strncmp(TREE_STRING_POINTER(section), ".devinit.text", 13))
276 			return false;
277 		if (!strncmp(TREE_STRING_POINTER(section), ".cpuinit.text", 13))
278 			return false;
279 		if (!strncmp(TREE_STRING_POINTER(section), ".meminit.text", 13))
280 			return false;
281 	}
282 
283 	return track_frame_size >= 0;
284 }
285 
286 /* Build the function declaration for stackleak_track_stack() */
287 static void stackleak_start_unit(void *gcc_data __unused,
288 				 void *user_data __unused)
289 {
290 	tree fntype;
291 
292 	/* void stackleak_track_stack(void) */
293 	fntype = build_function_type_list(void_type_node, NULL_TREE);
294 	track_function_decl = build_fn_decl(track_function, fntype);
295 	DECL_ASSEMBLER_NAME(track_function_decl); /* for LTO */
296 	TREE_PUBLIC(track_function_decl) = 1;
297 	TREE_USED(track_function_decl) = 1;
298 	DECL_EXTERNAL(track_function_decl) = 1;
299 	DECL_ARTIFICIAL(track_function_decl) = 1;
300 	DECL_PRESERVE_P(track_function_decl) = 1;
301 }
302 
303 /*
304  * Pass gate function is a predicate function that gets executed before the
305  * corresponding pass. If the return value is 'true' the pass gets executed,
306  * otherwise, it is skipped.
307  */
308 static bool stackleak_instrument_gate(void)
309 {
310 	return stackleak_gate();
311 }
312 
313 #define PASS_NAME stackleak_instrument
314 #define PROPERTIES_REQUIRED PROP_gimple_leh | PROP_cfg
315 #define TODO_FLAGS_START TODO_verify_ssa | TODO_verify_flow | TODO_verify_stmts
316 #define TODO_FLAGS_FINISH TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func \
317 			| TODO_update_ssa | TODO_rebuild_cgraph_edges
318 #include "gcc-generate-gimple-pass.h"
319 
320 static bool stackleak_cleanup_gate(void)
321 {
322 	return stackleak_gate();
323 }
324 
325 #define PASS_NAME stackleak_cleanup
326 #define TODO_FLAGS_FINISH TODO_dump_func
327 #include "gcc-generate-rtl-pass.h"
328 
329 /*
330  * Every gcc plugin exports a plugin_init() function that is called right
331  * after the plugin is loaded. This function is responsible for registering
332  * the plugin callbacks and doing other required initialization.
333  */
334 __visible int plugin_init(struct plugin_name_args *plugin_info,
335 			  struct plugin_gcc_version *version)
336 {
337 	const char * const plugin_name = plugin_info->base_name;
338 	const int argc = plugin_info->argc;
339 	const struct plugin_argument * const argv = plugin_info->argv;
340 	int i = 0;
341 
342 	/* Extra GGC root tables describing our GTY-ed data */
343 	static const struct ggc_root_tab gt_ggc_r_gt_stackleak[] = {
344 		{
345 			.base = &track_function_decl,
346 			.nelt = 1,
347 			.stride = sizeof(track_function_decl),
348 			.cb = &gt_ggc_mx_tree_node,
349 			.pchw = &gt_pch_nx_tree_node
350 		},
351 		LAST_GGC_ROOT_TAB
352 	};
353 
354 	/*
355 	 * The stackleak_instrument pass should be executed before the
356 	 * "optimized" pass, which is the control flow graph cleanup that is
357 	 * performed just before expanding gcc trees to the RTL. In former
358 	 * versions of the plugin this new pass was inserted before the
359 	 * "tree_profile" pass, which is currently called "profile".
360 	 */
361 	PASS_INFO(stackleak_instrument, "optimized", 1,
362 						PASS_POS_INSERT_BEFORE);
363 
364 	/*
365 	 * The stackleak_cleanup pass should be executed before the "*free_cfg"
366 	 * pass. It's the moment when the stack frame size is already final,
367 	 * function prologues and epilogues are generated, and the
368 	 * machine-dependent code transformations are not done.
369 	 */
370 	PASS_INFO(stackleak_cleanup, "*free_cfg", 1, PASS_POS_INSERT_BEFORE);
371 
372 	if (!plugin_default_version_check(version, &gcc_version)) {
373 		error(G_("incompatible gcc/plugin versions"));
374 		return 1;
375 	}
376 
377 	/* Parse the plugin arguments */
378 	for (i = 0; i < argc; i++) {
379 		if (!strcmp(argv[i].key, "disable"))
380 			return 0;
381 
382 		if (!strcmp(argv[i].key, "track-min-size")) {
383 			if (!argv[i].value) {
384 				error(G_("no value supplied for option '-fplugin-arg-%s-%s'"),
385 					plugin_name, argv[i].key);
386 				return 1;
387 			}
388 
389 			track_frame_size = atoi(argv[i].value);
390 			if (track_frame_size < 0) {
391 				error(G_("invalid option argument '-fplugin-arg-%s-%s=%s'"),
392 					plugin_name, argv[i].key, argv[i].value);
393 				return 1;
394 			}
395 		} else {
396 			error(G_("unknown option '-fplugin-arg-%s-%s'"),
397 					plugin_name, argv[i].key);
398 			return 1;
399 		}
400 	}
401 
402 	/* Give the information about the plugin */
403 	register_callback(plugin_name, PLUGIN_INFO, NULL,
404 						&stackleak_plugin_info);
405 
406 	/* Register to be called before processing a translation unit */
407 	register_callback(plugin_name, PLUGIN_START_UNIT,
408 					&stackleak_start_unit, NULL);
409 
410 	/* Register an extra GCC garbage collector (GGC) root table */
411 	register_callback(plugin_name, PLUGIN_REGISTER_GGC_ROOTS, NULL,
412 					(void *)&gt_ggc_r_gt_stackleak);
413 
414 	/*
415 	 * Hook into the Pass Manager to register new gcc passes.
416 	 *
417 	 * The stack frame size info is available only at the last RTL pass,
418 	 * when it's too late to insert complex code like a function call.
419 	 * So we register two gcc passes to instrument every function at first
420 	 * and remove the unneeded instrumentation later.
421 	 */
422 	register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
423 					&stackleak_instrument_pass_info);
424 	register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL,
425 					&stackleak_cleanup_pass_info);
426 
427 	return 0;
428 }
429