hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/scripts/gcc-plugins/structleak_plugin.c
....@@ -11,11 +11,12 @@
1111 * otherwise leak kernel stack to userland if they aren't properly initialized
1212 * by later code
1313 *
14
- * Homepage: http://pax.grsecurity.net/
14
+ * Homepage: https://pax.grsecurity.net/
1515 *
1616 * Options:
1717 * -fplugin-arg-structleak_plugin-disable
1818 * -fplugin-arg-structleak_plugin-verbose
19
+ * -fplugin-arg-structleak_plugin-byref
1920 * -fplugin-arg-structleak_plugin-byref-all
2021 *
2122 * Usage:
....@@ -26,7 +27,6 @@
2627 * $ gcc -fplugin=./structleak_plugin.so test.c -O2
2728 *
2829 * TODO: eliminate redundant initializers
29
- * increase type coverage
3030 */
3131
3232 #include "gcc-common.h"
....@@ -37,13 +37,18 @@
3737 __visible int plugin_is_GPL_compatible;
3838
3939 static struct plugin_info structleak_plugin_info = {
40
- .version = "201607271510vanilla",
40
+ .version = "20190125vanilla",
4141 .help = "disable\tdo not activate plugin\n"
42
- "verbose\tprint all initialized variables\n",
42
+ "byref\tinit structs passed by reference\n"
43
+ "byref-all\tinit anything passed by reference\n"
44
+ "verbose\tprint all initialized variables\n",
4345 };
4446
47
+#define BYREF_STRUCT 1
48
+#define BYREF_ALL 2
49
+
4550 static bool verbose;
46
-static bool byref_all;
51
+static int byref;
4752
4853 static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
4954 {
....@@ -118,6 +123,7 @@
118123 gimple_stmt_iterator gsi;
119124 tree initializer;
120125 gimple init_stmt;
126
+ tree type;
121127
122128 /* this is the original entry bb before the forced split */
123129 bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
....@@ -148,11 +154,15 @@
148154 if (verbose)
149155 inform(DECL_SOURCE_LOCATION(var),
150156 "%s variable will be forcibly initialized",
151
- (byref_all && TREE_ADDRESSABLE(var)) ? "byref"
152
- : "userspace");
157
+ (byref && TREE_ADDRESSABLE(var)) ? "byref"
158
+ : "userspace");
153159
154160 /* build the initializer expression */
155
- initializer = build_constructor(TREE_TYPE(var), NULL);
161
+ type = TREE_TYPE(var);
162
+ if (AGGREGATE_TYPE_P(type))
163
+ initializer = build_constructor(type, NULL);
164
+ else
165
+ initializer = fold_convert(type, integer_zero_node);
156166
157167 /* build the initializer stmt */
158168 init_stmt = gimple_build_assign(var, initializer);
....@@ -184,13 +194,13 @@
184194 if (!auto_var_in_fn_p(var, current_function_decl))
185195 continue;
186196
187
- /* only care about structure types */
188
- if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
197
+ /* only care about structure types unless byref-all */
198
+ if (byref != BYREF_ALL && TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
189199 continue;
190200
191201 /* if the type is of interest, examine the variable */
192202 if (TYPE_USERSPACE(type) ||
193
- (byref_all && TREE_ADDRESSABLE(var)))
203
+ (byref && TREE_ADDRESSABLE(var)))
194204 initialize(var);
195205 }
196206
....@@ -232,8 +242,12 @@
232242 verbose = true;
233243 continue;
234244 }
245
+ if (!strcmp(argv[i].key, "byref")) {
246
+ byref = BYREF_STRUCT;
247
+ continue;
248
+ }
235249 if (!strcmp(argv[i].key, "byref-all")) {
236
- byref_all = true;
250
+ byref = BYREF_ALL;
237251 continue;
238252 }
239253 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);