2 * Copyright 2013-2017 by PaX Team <pageexec@freemail.hu>
3 * Licensed under the GPL v2
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
10 * gcc plugin to forcibly initialize certain local variables that could
11 * otherwise leak kernel stack to userland if they aren't properly initialized
14 * Homepage: http://pax.grsecurity.net/
17 * -fplugin-arg-structleak_plugin-disable
18 * -fplugin-arg-structleak_plugin-verbose
21 * $ # for 4.5/4.6/C based 4.7
22 * $ 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
23 * $ # for C++ based 4.7/4.8+
24 * $ 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
25 * $ gcc -fplugin=./structleak_plugin.so test.c -O2
27 * TODO: eliminate redundant initializers
28 * increase type coverage
31 #include "gcc-common.h"
33 /* unused C type flag in all versions 4.5-6 */
34 #define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
36 __visible
int plugin_is_GPL_compatible
;
38 static struct plugin_info structleak_plugin_info
= {
39 .version
= "201607271510vanilla",
40 .help
= "disable\tdo not activate plugin\n"
41 "verbose\tprint all initialized variables\n",
46 static tree
handle_user_attribute(tree
*node
, tree name
, tree args
, int flags
, bool *no_add_attrs
)
50 /* check for types? for now accept everything linux has to offer */
51 if (TREE_CODE(*node
) != FIELD_DECL
)
54 *no_add_attrs
= false;
58 static struct attribute_spec user_attr
= {
62 .decl_required
= false,
63 .type_required
= false,
64 .function_type_required
= false,
65 .handler
= handle_user_attribute
,
66 #if BUILDING_GCC_VERSION >= 4007
67 .affects_type_identity
= true
71 static void register_attributes(void *event_data
, void *data
)
73 register_attribute(&user_attr
);
76 static tree
get_field_type(tree field
)
78 return strip_array_types(TREE_TYPE(field
));
81 static bool is_userspace_type(tree type
)
85 for (field
= TYPE_FIELDS(type
); field
; field
= TREE_CHAIN(field
)) {
86 tree fieldtype
= get_field_type(field
);
87 enum tree_code code
= TREE_CODE(fieldtype
);
89 if (code
== RECORD_TYPE
|| code
== UNION_TYPE
)
90 if (is_userspace_type(fieldtype
))
93 if (lookup_attribute("user", DECL_ATTRIBUTES(field
)))
99 static void finish_type(void *event_data
, void *data
)
101 tree type
= (tree
)event_data
;
103 if (type
== NULL_TREE
|| type
== error_mark_node
)
106 #if BUILDING_GCC_VERSION >= 5000
107 if (TREE_CODE(type
) == ENUMERAL_TYPE
)
111 if (TYPE_USERSPACE(type
))
114 if (is_userspace_type(type
))
115 TYPE_USERSPACE(type
) = 1;
118 static void initialize(tree var
)
121 gimple_stmt_iterator gsi
;
125 /* this is the original entry bb before the forced split */
126 bb
= single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun
));
128 /* first check if variable is already initialized, warn otherwise */
129 for (gsi
= gsi_start_bb(bb
); !gsi_end_p(gsi
); gsi_next(&gsi
)) {
130 gimple stmt
= gsi_stmt(gsi
);
133 /* we're looking for an assignment of a single rhs... */
134 if (!gimple_assign_single_p(stmt
))
136 rhs1
= gimple_assign_rhs1(stmt
);
137 #if BUILDING_GCC_VERSION >= 4007
138 /* ... of a non-clobbering expression... */
139 if (TREE_CLOBBER_P(rhs1
))
142 /* ... to our variable... */
143 if (gimple_get_lhs(stmt
) != var
)
145 /* if it's an initializer then we're good */
146 if (TREE_CODE(rhs1
) == CONSTRUCTOR
)
150 /* these aren't the 0days you're looking for */
152 inform(DECL_SOURCE_LOCATION(var
),
153 "userspace variable will be forcibly initialized");
155 /* build the initializer expression */
156 initializer
= build_constructor(TREE_TYPE(var
), NULL
);
158 /* build the initializer stmt */
159 init_stmt
= gimple_build_assign(var
, initializer
);
160 gsi
= gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun
)));
161 gsi_insert_before(&gsi
, init_stmt
, GSI_NEW_STMT
);
162 update_stmt(init_stmt
);
165 static unsigned int structleak_execute(void)
168 unsigned int ret
= 0;
172 /* split the first bb where we can put the forced initializers */
173 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun
)));
174 bb
= single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun
));
175 if (!single_pred_p(bb
)) {
176 split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun
)));
177 gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun
)));
180 /* enumerate all local variables and forcibly initialize our targets */
181 FOR_EACH_LOCAL_DECL(cfun
, i
, var
) {
182 tree type
= TREE_TYPE(var
);
184 gcc_assert(DECL_P(var
));
185 if (!auto_var_in_fn_p(var
, current_function_decl
))
188 /* only care about structure types */
189 if (TREE_CODE(type
) != RECORD_TYPE
&& TREE_CODE(type
) != UNION_TYPE
)
192 /* if the type is of interest, examine the variable */
193 if (TYPE_USERSPACE(type
))
200 #define PASS_NAME structleak
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"
206 __visible
int plugin_init(struct plugin_name_args
*plugin_info
, struct plugin_gcc_version
*version
)
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
;
214 PASS_INFO(structleak
, "early_optimizations", 1, PASS_POS_INSERT_BEFORE
);
216 if (!plugin_default_version_check(version
, &gcc_version
)) {
217 error(G_("incompatible gcc/plugin versions"));
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
);
226 for (i
= 0; i
< argc
; ++i
) {
227 if (!strcmp(argv
[i
].key
, "disable")) {
231 if (!strcmp(argv
[i
].key
, "verbose")) {
235 error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name
, argv
[i
].key
);
238 register_callback(plugin_name
, PLUGIN_INFO
, NULL
, &structleak_plugin_info
);
240 register_callback(plugin_name
, PLUGIN_PASS_MANAGER_SETUP
, NULL
, &structleak_pass_info
);
241 register_callback(plugin_name
, PLUGIN_FINISH_TYPE
, finish_type
, NULL
);
243 register_callback(plugin_name
, PLUGIN_ATTRIBUTES
, register_attributes
, NULL
);