.. | .. |
---|
11 | 11 | * otherwise leak kernel stack to userland if they aren't properly initialized |
---|
12 | 12 | * by later code |
---|
13 | 13 | * |
---|
14 | | - * Homepage: http://pax.grsecurity.net/ |
---|
| 14 | + * Homepage: https://pax.grsecurity.net/ |
---|
15 | 15 | * |
---|
16 | 16 | * Options: |
---|
17 | 17 | * -fplugin-arg-structleak_plugin-disable |
---|
18 | 18 | * -fplugin-arg-structleak_plugin-verbose |
---|
| 19 | + * -fplugin-arg-structleak_plugin-byref |
---|
19 | 20 | * -fplugin-arg-structleak_plugin-byref-all |
---|
20 | 21 | * |
---|
21 | 22 | * Usage: |
---|
.. | .. |
---|
26 | 27 | * $ gcc -fplugin=./structleak_plugin.so test.c -O2 |
---|
27 | 28 | * |
---|
28 | 29 | * TODO: eliminate redundant initializers |
---|
29 | | - * increase type coverage |
---|
30 | 30 | */ |
---|
31 | 31 | |
---|
32 | 32 | #include "gcc-common.h" |
---|
.. | .. |
---|
37 | 37 | __visible int plugin_is_GPL_compatible; |
---|
38 | 38 | |
---|
39 | 39 | static struct plugin_info structleak_plugin_info = { |
---|
40 | | - .version = "201607271510vanilla", |
---|
| 40 | + .version = "20190125vanilla", |
---|
41 | 41 | .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", |
---|
43 | 45 | }; |
---|
44 | 46 | |
---|
| 47 | +#define BYREF_STRUCT 1 |
---|
| 48 | +#define BYREF_ALL 2 |
---|
| 49 | + |
---|
45 | 50 | static bool verbose; |
---|
46 | | -static bool byref_all; |
---|
| 51 | +static int byref; |
---|
47 | 52 | |
---|
48 | 53 | static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs) |
---|
49 | 54 | { |
---|
.. | .. |
---|
118 | 123 | gimple_stmt_iterator gsi; |
---|
119 | 124 | tree initializer; |
---|
120 | 125 | gimple init_stmt; |
---|
| 126 | + tree type; |
---|
121 | 127 | |
---|
122 | 128 | /* this is the original entry bb before the forced split */ |
---|
123 | 129 | bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)); |
---|
.. | .. |
---|
148 | 154 | if (verbose) |
---|
149 | 155 | inform(DECL_SOURCE_LOCATION(var), |
---|
150 | 156 | "%s variable will be forcibly initialized", |
---|
151 | | - (byref_all && TREE_ADDRESSABLE(var)) ? "byref" |
---|
152 | | - : "userspace"); |
---|
| 157 | + (byref && TREE_ADDRESSABLE(var)) ? "byref" |
---|
| 158 | + : "userspace"); |
---|
153 | 159 | |
---|
154 | 160 | /* 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); |
---|
156 | 166 | |
---|
157 | 167 | /* build the initializer stmt */ |
---|
158 | 168 | init_stmt = gimple_build_assign(var, initializer); |
---|
.. | .. |
---|
184 | 194 | if (!auto_var_in_fn_p(var, current_function_decl)) |
---|
185 | 195 | continue; |
---|
186 | 196 | |
---|
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) |
---|
189 | 199 | continue; |
---|
190 | 200 | |
---|
191 | 201 | /* if the type is of interest, examine the variable */ |
---|
192 | 202 | if (TYPE_USERSPACE(type) || |
---|
193 | | - (byref_all && TREE_ADDRESSABLE(var))) |
---|
| 203 | + (byref && TREE_ADDRESSABLE(var))) |
---|
194 | 204 | initialize(var); |
---|
195 | 205 | } |
---|
196 | 206 | |
---|
.. | .. |
---|
232 | 242 | verbose = true; |
---|
233 | 243 | continue; |
---|
234 | 244 | } |
---|
| 245 | + if (!strcmp(argv[i].key, "byref")) { |
---|
| 246 | + byref = BYREF_STRUCT; |
---|
| 247 | + continue; |
---|
| 248 | + } |
---|
235 | 249 | if (!strcmp(argv[i].key, "byref-all")) { |
---|
236 | | - byref_all = true; |
---|
| 250 | + byref = BYREF_ALL; |
---|
237 | 251 | continue; |
---|
238 | 252 | } |
---|
239 | 253 | error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key); |
---|