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