~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/gcc-plugins/structleak_plugin.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/gcc-plugins/structleak_plugin.c (Version linux-6.12-rc7) and /scripts/gcc-plugins/structleak_plugin.c (Version linux-4.19.323)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php