xref: /OK3568_Linux_fs/kernel/scripts/gcc-plugins/structleak_plugin.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2013-2017 by PaX Team <pageexec@freemail.hu>
3*4882a593Smuzhiyun  * Licensed under the GPL v2
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Note: the choice of the license means that the compilation process is
6*4882a593Smuzhiyun  *       NOT 'eligible' as defined by gcc's library exception to the GPL v3,
7*4882a593Smuzhiyun  *       but for the kernel it doesn't matter since it doesn't link against
8*4882a593Smuzhiyun  *       any of the gcc libraries
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * gcc plugin to forcibly initialize certain local variables that could
11*4882a593Smuzhiyun  * otherwise leak kernel stack to userland if they aren't properly initialized
12*4882a593Smuzhiyun  * by later code
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Homepage: https://pax.grsecurity.net/
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * Options:
17*4882a593Smuzhiyun  * -fplugin-arg-structleak_plugin-disable
18*4882a593Smuzhiyun  * -fplugin-arg-structleak_plugin-verbose
19*4882a593Smuzhiyun  * -fplugin-arg-structleak_plugin-byref
20*4882a593Smuzhiyun  * -fplugin-arg-structleak_plugin-byref-all
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Usage:
23*4882a593Smuzhiyun  * $ # for 4.5/4.6/C based 4.7
24*4882a593Smuzhiyun  * $ gcc -I`gcc -print-file-name=plugin`/include -I`gcc -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
25*4882a593Smuzhiyun  * $ # for C++ based 4.7/4.8+
26*4882a593Smuzhiyun  * $ g++ -I`g++ -print-file-name=plugin`/include -I`g++ -print-file-name=plugin`/include/c-family -fPIC -shared -O2 -o structleak_plugin.so structleak_plugin.c
27*4882a593Smuzhiyun  * $ gcc -fplugin=./structleak_plugin.so test.c -O2
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * TODO: eliminate redundant initializers
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #include "gcc-common.h"
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* unused C type flag in all versions 4.5-6 */
35*4882a593Smuzhiyun #define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun __visible int plugin_is_GPL_compatible;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static struct plugin_info structleak_plugin_info = {
40*4882a593Smuzhiyun 	.version	= "20190125vanilla",
41*4882a593Smuzhiyun 	.help		= "disable\tdo not activate plugin\n"
42*4882a593Smuzhiyun 			  "byref\tinit structs passed by reference\n"
43*4882a593Smuzhiyun 			  "byref-all\tinit anything passed by reference\n"
44*4882a593Smuzhiyun 			  "verbose\tprint all initialized variables\n",
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define BYREF_STRUCT	1
48*4882a593Smuzhiyun #define BYREF_ALL	2
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun static bool verbose;
51*4882a593Smuzhiyun static int byref;
52*4882a593Smuzhiyun 
handle_user_attribute(tree * node,tree name,tree args,int flags,bool * no_add_attrs)53*4882a593Smuzhiyun static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	*no_add_attrs = true;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* check for types? for now accept everything linux has to offer */
58*4882a593Smuzhiyun 	if (TREE_CODE(*node) != FIELD_DECL)
59*4882a593Smuzhiyun 		return NULL_TREE;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	*no_add_attrs = false;
62*4882a593Smuzhiyun 	return NULL_TREE;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun static struct attribute_spec user_attr = { };
66*4882a593Smuzhiyun 
register_attributes(void * event_data,void * data)67*4882a593Smuzhiyun static void register_attributes(void *event_data, void *data)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	user_attr.name			= "user";
70*4882a593Smuzhiyun 	user_attr.handler		= handle_user_attribute;
71*4882a593Smuzhiyun #if BUILDING_GCC_VERSION >= 4007
72*4882a593Smuzhiyun 	user_attr.affects_type_identity	= true;
73*4882a593Smuzhiyun #endif
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	register_attribute(&user_attr);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
get_field_type(tree field)78*4882a593Smuzhiyun static tree get_field_type(tree field)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	return strip_array_types(TREE_TYPE(field));
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
is_userspace_type(tree type)83*4882a593Smuzhiyun static bool is_userspace_type(tree type)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	tree field;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {
88*4882a593Smuzhiyun 		tree fieldtype = get_field_type(field);
89*4882a593Smuzhiyun 		enum tree_code code = TREE_CODE(fieldtype);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		if (code == RECORD_TYPE || code == UNION_TYPE)
92*4882a593Smuzhiyun 			if (is_userspace_type(fieldtype))
93*4882a593Smuzhiyun 				return true;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 		if (lookup_attribute("user", DECL_ATTRIBUTES(field)))
96*4882a593Smuzhiyun 			return true;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 	return false;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
finish_type(void * event_data,void * data)101*4882a593Smuzhiyun static void finish_type(void *event_data, void *data)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	tree type = (tree)event_data;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	if (type == NULL_TREE || type == error_mark_node)
106*4882a593Smuzhiyun 		return;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun #if BUILDING_GCC_VERSION >= 5000
109*4882a593Smuzhiyun 	if (TREE_CODE(type) == ENUMERAL_TYPE)
110*4882a593Smuzhiyun 		return;
111*4882a593Smuzhiyun #endif
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (TYPE_USERSPACE(type))
114*4882a593Smuzhiyun 		return;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (is_userspace_type(type))
117*4882a593Smuzhiyun 		TYPE_USERSPACE(type) = 1;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
initialize(tree var)120*4882a593Smuzhiyun static void initialize(tree var)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	basic_block bb;
123*4882a593Smuzhiyun 	gimple_stmt_iterator gsi;
124*4882a593Smuzhiyun 	tree initializer;
125*4882a593Smuzhiyun 	gimple init_stmt;
126*4882a593Smuzhiyun 	tree type;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* this is the original entry bb before the forced split */
129*4882a593Smuzhiyun 	bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* first check if variable is already initialized, warn otherwise */
132*4882a593Smuzhiyun 	for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
133*4882a593Smuzhiyun 		gimple stmt = gsi_stmt(gsi);
134*4882a593Smuzhiyun 		tree rhs1;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		/* we're looking for an assignment of a single rhs... */
137*4882a593Smuzhiyun 		if (!gimple_assign_single_p(stmt))
138*4882a593Smuzhiyun 			continue;
139*4882a593Smuzhiyun 		rhs1 = gimple_assign_rhs1(stmt);
140*4882a593Smuzhiyun #if BUILDING_GCC_VERSION >= 4007
141*4882a593Smuzhiyun 		/* ... of a non-clobbering expression... */
142*4882a593Smuzhiyun 		if (TREE_CLOBBER_P(rhs1))
143*4882a593Smuzhiyun 			continue;
144*4882a593Smuzhiyun #endif
145*4882a593Smuzhiyun 		/* ... to our variable... */
146*4882a593Smuzhiyun 		if (gimple_get_lhs(stmt) != var)
147*4882a593Smuzhiyun 			continue;
148*4882a593Smuzhiyun 		/* if it's an initializer then we're good */
149*4882a593Smuzhiyun 		if (TREE_CODE(rhs1) == CONSTRUCTOR)
150*4882a593Smuzhiyun 			return;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	/* these aren't the 0days you're looking for */
154*4882a593Smuzhiyun 	if (verbose)
155*4882a593Smuzhiyun 		inform(DECL_SOURCE_LOCATION(var),
156*4882a593Smuzhiyun 			"%s variable will be forcibly initialized",
157*4882a593Smuzhiyun 			(byref && TREE_ADDRESSABLE(var)) ? "byref"
158*4882a593Smuzhiyun 							 : "userspace");
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* build the initializer expression */
161*4882a593Smuzhiyun 	type = TREE_TYPE(var);
162*4882a593Smuzhiyun 	if (AGGREGATE_TYPE_P(type))
163*4882a593Smuzhiyun 		initializer = build_constructor(type, NULL);
164*4882a593Smuzhiyun 	else
165*4882a593Smuzhiyun 		initializer = fold_convert(type, integer_zero_node);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* build the initializer stmt */
168*4882a593Smuzhiyun 	init_stmt = gimple_build_assign(var, initializer);
169*4882a593Smuzhiyun 	gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
170*4882a593Smuzhiyun 	gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);
171*4882a593Smuzhiyun 	update_stmt(init_stmt);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
structleak_execute(void)174*4882a593Smuzhiyun static unsigned int structleak_execute(void)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	basic_block bb;
177*4882a593Smuzhiyun 	unsigned int ret = 0;
178*4882a593Smuzhiyun 	tree var;
179*4882a593Smuzhiyun 	unsigned int i;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* split the first bb where we can put the forced initializers */
182*4882a593Smuzhiyun 	gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
183*4882a593Smuzhiyun 	bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
184*4882a593Smuzhiyun 	if (!single_pred_p(bb)) {
185*4882a593Smuzhiyun 		split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
186*4882a593Smuzhiyun 		gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* enumerate all local variables and forcibly initialize our targets */
190*4882a593Smuzhiyun 	FOR_EACH_LOCAL_DECL(cfun, i, var) {
191*4882a593Smuzhiyun 		tree type = TREE_TYPE(var);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		gcc_assert(DECL_P(var));
194*4882a593Smuzhiyun 		if (!auto_var_in_fn_p(var, current_function_decl))
195*4882a593Smuzhiyun 			continue;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		/* only care about structure types unless byref-all */
198*4882a593Smuzhiyun 		if (byref != BYREF_ALL && TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
199*4882a593Smuzhiyun 			continue;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		/* if the type is of interest, examine the variable */
202*4882a593Smuzhiyun 		if (TYPE_USERSPACE(type) ||
203*4882a593Smuzhiyun 		    (byref && TREE_ADDRESSABLE(var)))
204*4882a593Smuzhiyun 			initialize(var);
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	return ret;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun #define PASS_NAME structleak
211*4882a593Smuzhiyun #define NO_GATE
212*4882a593Smuzhiyun #define PROPERTIES_REQUIRED PROP_cfg
213*4882a593Smuzhiyun #define TODO_FLAGS_FINISH TODO_verify_il | TODO_verify_ssa | TODO_verify_stmts | TODO_dump_func | TODO_remove_unused_locals | TODO_update_ssa | TODO_ggc_collect | TODO_verify_flow
214*4882a593Smuzhiyun #include "gcc-generate-gimple-pass.h"
215*4882a593Smuzhiyun 
plugin_init(struct plugin_name_args * plugin_info,struct plugin_gcc_version * version)216*4882a593Smuzhiyun __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int i;
219*4882a593Smuzhiyun 	const char * const plugin_name = plugin_info->base_name;
220*4882a593Smuzhiyun 	const int argc = plugin_info->argc;
221*4882a593Smuzhiyun 	const struct plugin_argument * const argv = plugin_info->argv;
222*4882a593Smuzhiyun 	bool enable = true;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (!plugin_default_version_check(version, &gcc_version)) {
227*4882a593Smuzhiyun 		error(G_("incompatible gcc/plugin versions"));
228*4882a593Smuzhiyun 		return 1;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
232*4882a593Smuzhiyun 		inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
233*4882a593Smuzhiyun 		enable = false;
234*4882a593Smuzhiyun 	}
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	for (i = 0; i < argc; ++i) {
237*4882a593Smuzhiyun 		if (!strcmp(argv[i].key, "disable")) {
238*4882a593Smuzhiyun 			enable = false;
239*4882a593Smuzhiyun 			continue;
240*4882a593Smuzhiyun 		}
241*4882a593Smuzhiyun 		if (!strcmp(argv[i].key, "verbose")) {
242*4882a593Smuzhiyun 			verbose = true;
243*4882a593Smuzhiyun 			continue;
244*4882a593Smuzhiyun 		}
245*4882a593Smuzhiyun 		if (!strcmp(argv[i].key, "byref")) {
246*4882a593Smuzhiyun 			byref = BYREF_STRUCT;
247*4882a593Smuzhiyun 			continue;
248*4882a593Smuzhiyun 		}
249*4882a593Smuzhiyun 		if (!strcmp(argv[i].key, "byref-all")) {
250*4882a593Smuzhiyun 			byref = BYREF_ALL;
251*4882a593Smuzhiyun 			continue;
252*4882a593Smuzhiyun 		}
253*4882a593Smuzhiyun 		error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);
257*4882a593Smuzhiyun 	if (enable) {
258*4882a593Smuzhiyun 		register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);
259*4882a593Smuzhiyun 		register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 	register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	return 0;
264*4882a593Smuzhiyun }
265