···410410 * https://grsecurity.net/411411 * https://pax.grsecurity.net/412412413413+config GCC_PLUGIN_STRUCTLEAK414414+ bool "Force initialization of variables containing userspace addresses"415415+ depends on GCC_PLUGINS416416+ help417417+ This plugin zero-initializes any structures that containing a418418+ __user attribute. This can prevent some classes of information419419+ exposures.420420+421421+ This plugin was ported from grsecurity/PaX. More information at:422422+ * https://grsecurity.net/423423+ * https://pax.grsecurity.net/424424+425425+config GCC_PLUGIN_STRUCTLEAK_VERBOSE426426+ bool "Report forcefully initialized variables"427427+ depends on GCC_PLUGIN_STRUCTLEAK428428+ depends on !COMPILE_TEST429429+ help430430+ This option will cause a warning to be printed each time the431431+ structleak plugin finds a variable it thinks needs to be432432+ initialized. Since not all existing initializers are detected433433+ by the plugin, this can produce false positive warnings.434434+413435config HAVE_CC_STACKPROTECTOR414436 bool415437 help
···11+/*22+ * Copyright 2013-2017 by PaX Team <pageexec@freemail.hu>33+ * Licensed under the GPL v244+ *55+ * Note: the choice of the license means that the compilation process is66+ * NOT 'eligible' as defined by gcc's library exception to the GPL v3,77+ * but for the kernel it doesn't matter since it doesn't link against88+ * any of the gcc libraries99+ *1010+ * gcc plugin to forcibly initialize certain local variables that could1111+ * otherwise leak kernel stack to userland if they aren't properly initialized1212+ * by later code1313+ *1414+ * Homepage: http://pax.grsecurity.net/1515+ *1616+ * Options:1717+ * -fplugin-arg-structleak_plugin-disable1818+ * -fplugin-arg-structleak_plugin-verbose1919+ *2020+ * Usage:2121+ * $ # for 4.5/4.6/C based 4.72222+ * $ 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.c2323+ * $ # for C++ based 4.7/4.8+2424+ * $ 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.c2525+ * $ gcc -fplugin=./structleak_plugin.so test.c -O22626+ *2727+ * TODO: eliminate redundant initializers2828+ * increase type coverage2929+ */3030+3131+#include "gcc-common.h"3232+3333+/* unused C type flag in all versions 4.5-6 */3434+#define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)3535+3636+__visible int plugin_is_GPL_compatible;3737+3838+static struct plugin_info structleak_plugin_info = {3939+ .version = "201607271510vanilla",4040+ .help = "disable\tdo not activate plugin\n"4141+ "verbose\tprint all initialized variables\n",4242+};4343+4444+static bool verbose;4545+4646+static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)4747+{4848+ *no_add_attrs = true;4949+5050+ /* check for types? for now accept everything linux has to offer */5151+ if (TREE_CODE(*node) != FIELD_DECL)5252+ return NULL_TREE;5353+5454+ *no_add_attrs = false;5555+ return NULL_TREE;5656+}5757+5858+static struct attribute_spec user_attr = {5959+ .name = "user",6060+ .min_length = 0,6161+ .max_length = 0,6262+ .decl_required = false,6363+ .type_required = false,6464+ .function_type_required = false,6565+ .handler = handle_user_attribute,6666+#if BUILDING_GCC_VERSION >= 40076767+ .affects_type_identity = true6868+#endif6969+};7070+7171+static void register_attributes(void *event_data, void *data)7272+{7373+ register_attribute(&user_attr);7474+}7575+7676+static tree get_field_type(tree field)7777+{7878+ return strip_array_types(TREE_TYPE(field));7979+}8080+8181+static bool is_userspace_type(tree type)8282+{8383+ tree field;8484+8585+ for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {8686+ tree fieldtype = get_field_type(field);8787+ enum tree_code code = TREE_CODE(fieldtype);8888+8989+ if (code == RECORD_TYPE || code == UNION_TYPE)9090+ if (is_userspace_type(fieldtype))9191+ return true;9292+9393+ if (lookup_attribute("user", DECL_ATTRIBUTES(field)))9494+ return true;9595+ }9696+ return false;9797+}9898+9999+static void finish_type(void *event_data, void *data)100100+{101101+ tree type = (tree)event_data;102102+103103+ if (type == NULL_TREE || type == error_mark_node)104104+ return;105105+106106+#if BUILDING_GCC_VERSION >= 5000107107+ if (TREE_CODE(type) == ENUMERAL_TYPE)108108+ return;109109+#endif110110+111111+ if (TYPE_USERSPACE(type))112112+ return;113113+114114+ if (is_userspace_type(type))115115+ TYPE_USERSPACE(type) = 1;116116+}117117+118118+static void initialize(tree var)119119+{120120+ basic_block bb;121121+ gimple_stmt_iterator gsi;122122+ tree initializer;123123+ gimple init_stmt;124124+125125+ /* this is the original entry bb before the forced split */126126+ bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));127127+128128+ /* first check if variable is already initialized, warn otherwise */129129+ for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {130130+ gimple stmt = gsi_stmt(gsi);131131+ tree rhs1;132132+133133+ /* we're looking for an assignment of a single rhs... */134134+ if (!gimple_assign_single_p(stmt))135135+ continue;136136+ rhs1 = gimple_assign_rhs1(stmt);137137+#if BUILDING_GCC_VERSION >= 4007138138+ /* ... of a non-clobbering expression... */139139+ if (TREE_CLOBBER_P(rhs1))140140+ continue;141141+#endif142142+ /* ... to our variable... */143143+ if (gimple_get_lhs(stmt) != var)144144+ continue;145145+ /* if it's an initializer then we're good */146146+ if (TREE_CODE(rhs1) == CONSTRUCTOR)147147+ return;148148+ }149149+150150+ /* these aren't the 0days you're looking for */151151+ if (verbose)152152+ inform(DECL_SOURCE_LOCATION(var),153153+ "userspace variable will be forcibly initialized");154154+155155+ /* build the initializer expression */156156+ initializer = build_constructor(TREE_TYPE(var), NULL);157157+158158+ /* build the initializer stmt */159159+ init_stmt = gimple_build_assign(var, initializer);160160+ gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));161161+ gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);162162+ update_stmt(init_stmt);163163+}164164+165165+static unsigned int structleak_execute(void)166166+{167167+ basic_block bb;168168+ unsigned int ret = 0;169169+ tree var;170170+ unsigned int i;171171+172172+ /* split the first bb where we can put the forced initializers */173173+ gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));174174+ bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));175175+ if (!single_pred_p(bb)) {176176+ split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));177177+ gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));178178+ }179179+180180+ /* enumerate all local variables and forcibly initialize our targets */181181+ FOR_EACH_LOCAL_DECL(cfun, i, var) {182182+ tree type = TREE_TYPE(var);183183+184184+ gcc_assert(DECL_P(var));185185+ if (!auto_var_in_fn_p(var, current_function_decl))186186+ continue;187187+188188+ /* only care about structure types */189189+ if (TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)190190+ continue;191191+192192+ /* if the type is of interest, examine the variable */193193+ if (TYPE_USERSPACE(type))194194+ initialize(var);195195+ }196196+197197+ return ret;198198+}199199+200200+#define PASS_NAME structleak201201+#define NO_GATE202202+#define PROPERTIES_REQUIRED PROP_cfg203203+#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_flow204204+#include "gcc-generate-gimple-pass.h"205205+206206+__visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)207207+{208208+ int i;209209+ const char * const plugin_name = plugin_info->base_name;210210+ const int argc = plugin_info->argc;211211+ const struct plugin_argument * const argv = plugin_info->argv;212212+ bool enable = true;213213+214214+ PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);215215+216216+ if (!plugin_default_version_check(version, &gcc_version)) {217217+ error(G_("incompatible gcc/plugin versions"));218218+ return 1;219219+ }220220+221221+ if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {222222+ inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);223223+ enable = false;224224+ }225225+226226+ for (i = 0; i < argc; ++i) {227227+ if (!strcmp(argv[i].key, "disable")) {228228+ enable = false;229229+ continue;230230+ }231231+ if (!strcmp(argv[i].key, "verbose")) {232232+ verbose = true;233233+ continue;234234+ }235235+ error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);236236+ }237237+238238+ register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);239239+ if (enable) {240240+ register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);241241+ register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);242242+ }243243+ register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);244244+245245+ return 0;246246+}