structleak_plugin.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2013-2017 by PaX Team <[email protected]>
  3. * Licensed under the GPL v2
  4. *
  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
  9. *
  10. * gcc plugin to forcibly initialize certain local variables that could
  11. * otherwise leak kernel stack to userland if they aren't properly initialized
  12. * by later code
  13. *
  14. * Homepage: https://pax.grsecurity.net/
  15. *
  16. * Options:
  17. * -fplugin-arg-structleak_plugin-disable
  18. * -fplugin-arg-structleak_plugin-verbose
  19. * -fplugin-arg-structleak_plugin-byref
  20. * -fplugin-arg-structleak_plugin-byref-all
  21. *
  22. * Usage:
  23. * $ # for 4.5/4.6/C based 4.7
  24. * $ 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+
  26. * $ 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.c -O2
  28. *
  29. * TODO: eliminate redundant initializers
  30. */
  31. #include "gcc-common.h"
  32. /* unused C type flag in all versions 4.5-6 */
  33. #define TYPE_USERSPACE(TYPE) TYPE_LANG_FLAG_5(TYPE)
  34. __visible int plugin_is_GPL_compatible;
  35. static struct plugin_info structleak_plugin_info = {
  36. .version = "20190125vanilla",
  37. .help = "disable\tdo not activate plugin\n"
  38. "byref\tinit structs passed by reference\n"
  39. "byref-all\tinit anything passed by reference\n"
  40. "verbose\tprint all initialized variables\n",
  41. };
  42. #define BYREF_STRUCT 1
  43. #define BYREF_ALL 2
  44. static bool verbose;
  45. static int byref;
  46. static tree handle_user_attribute(tree *node, tree name, tree args, int flags, bool *no_add_attrs)
  47. {
  48. *no_add_attrs = true;
  49. /* check for types? for now accept everything linux has to offer */
  50. if (TREE_CODE(*node) != FIELD_DECL)
  51. return NULL_TREE;
  52. *no_add_attrs = false;
  53. return NULL_TREE;
  54. }
  55. static struct attribute_spec user_attr = { };
  56. static void register_attributes(void *event_data, void *data)
  57. {
  58. user_attr.name = "user";
  59. user_attr.handler = handle_user_attribute;
  60. #if BUILDING_GCC_VERSION >= 4007
  61. user_attr.affects_type_identity = true;
  62. #endif
  63. register_attribute(&user_attr);
  64. }
  65. static tree get_field_type(tree field)
  66. {
  67. return strip_array_types(TREE_TYPE(field));
  68. }
  69. static bool is_userspace_type(tree type)
  70. {
  71. tree field;
  72. for (field = TYPE_FIELDS(type); field; field = TREE_CHAIN(field)) {
  73. tree fieldtype = get_field_type(field);
  74. enum tree_code code = TREE_CODE(fieldtype);
  75. if (code == RECORD_TYPE || code == UNION_TYPE)
  76. if (is_userspace_type(fieldtype))
  77. return true;
  78. if (lookup_attribute("user", DECL_ATTRIBUTES(field)))
  79. return true;
  80. }
  81. return false;
  82. }
  83. static void finish_type(void *event_data, void *data)
  84. {
  85. tree type = (tree)event_data;
  86. if (type == NULL_TREE || type == error_mark_node)
  87. return;
  88. #if BUILDING_GCC_VERSION >= 5000
  89. if (TREE_CODE(type) == ENUMERAL_TYPE)
  90. return;
  91. #endif
  92. if (TYPE_USERSPACE(type))
  93. return;
  94. if (is_userspace_type(type))
  95. TYPE_USERSPACE(type) = 1;
  96. }
  97. static void initialize(tree var)
  98. {
  99. basic_block bb;
  100. gimple_stmt_iterator gsi;
  101. tree initializer;
  102. gimple init_stmt;
  103. tree type;
  104. /* this is the original entry bb before the forced split */
  105. bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
  106. /* first check if variable is already initialized, warn otherwise */
  107. for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
  108. gimple stmt = gsi_stmt(gsi);
  109. tree rhs1;
  110. /* we're looking for an assignment of a single rhs... */
  111. if (!gimple_assign_single_p(stmt))
  112. continue;
  113. rhs1 = gimple_assign_rhs1(stmt);
  114. #if BUILDING_GCC_VERSION >= 4007
  115. /* ... of a non-clobbering expression... */
  116. if (TREE_CLOBBER_P(rhs1))
  117. continue;
  118. #endif
  119. /* ... to our variable... */
  120. if (gimple_get_lhs(stmt) != var)
  121. continue;
  122. /* if it's an initializer then we're good */
  123. if (TREE_CODE(rhs1) == CONSTRUCTOR)
  124. return;
  125. }
  126. /* these aren't the 0days you're looking for */
  127. if (verbose)
  128. inform(DECL_SOURCE_LOCATION(var),
  129. "%s variable will be forcibly initialized",
  130. (byref && TREE_ADDRESSABLE(var)) ? "byref"
  131. : "userspace");
  132. /* build the initializer expression */
  133. type = TREE_TYPE(var);
  134. if (AGGREGATE_TYPE_P(type))
  135. initializer = build_constructor(type, NULL);
  136. else
  137. initializer = fold_convert(type, integer_zero_node);
  138. /* build the initializer stmt */
  139. init_stmt = gimple_build_assign(var, initializer);
  140. gsi = gsi_after_labels(single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  141. gsi_insert_before(&gsi, init_stmt, GSI_NEW_STMT);
  142. update_stmt(init_stmt);
  143. }
  144. static unsigned int structleak_execute(void)
  145. {
  146. basic_block bb;
  147. unsigned int ret = 0;
  148. tree var;
  149. unsigned int i;
  150. /* split the first bb where we can put the forced initializers */
  151. gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  152. bb = single_succ(ENTRY_BLOCK_PTR_FOR_FN(cfun));
  153. if (!single_pred_p(bb)) {
  154. split_edge(single_succ_edge(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  155. gcc_assert(single_succ_p(ENTRY_BLOCK_PTR_FOR_FN(cfun)));
  156. }
  157. /* enumerate all local variables and forcibly initialize our targets */
  158. FOR_EACH_LOCAL_DECL(cfun, i, var) {
  159. tree type = TREE_TYPE(var);
  160. gcc_assert(DECL_P(var));
  161. if (!auto_var_in_fn_p(var, current_function_decl))
  162. continue;
  163. /* only care about structure types unless byref-all */
  164. if (byref != BYREF_ALL && TREE_CODE(type) != RECORD_TYPE && TREE_CODE(type) != UNION_TYPE)
  165. continue;
  166. /* if the type is of interest, examine the variable */
  167. if (TYPE_USERSPACE(type) ||
  168. (byref && TREE_ADDRESSABLE(var)))
  169. initialize(var);
  170. }
  171. return ret;
  172. }
  173. #define PASS_NAME structleak
  174. #define NO_GATE
  175. #define PROPERTIES_REQUIRED PROP_cfg
  176. #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
  177. #include "gcc-generate-gimple-pass.h"
  178. __visible int plugin_init(struct plugin_name_args *plugin_info, struct plugin_gcc_version *version)
  179. {
  180. int i;
  181. const char * const plugin_name = plugin_info->base_name;
  182. const int argc = plugin_info->argc;
  183. const struct plugin_argument * const argv = plugin_info->argv;
  184. bool enable = true;
  185. PASS_INFO(structleak, "early_optimizations", 1, PASS_POS_INSERT_BEFORE);
  186. if (!plugin_default_version_check(version, &gcc_version)) {
  187. error(G_("incompatible gcc/plugin versions"));
  188. return 1;
  189. }
  190. if (strncmp(lang_hooks.name, "GNU C", 5) && !strncmp(lang_hooks.name, "GNU C+", 6)) {
  191. inform(UNKNOWN_LOCATION, G_("%s supports C only, not %s"), plugin_name, lang_hooks.name);
  192. enable = false;
  193. }
  194. for (i = 0; i < argc; ++i) {
  195. if (!strcmp(argv[i].key, "disable")) {
  196. enable = false;
  197. continue;
  198. }
  199. if (!strcmp(argv[i].key, "verbose")) {
  200. verbose = true;
  201. continue;
  202. }
  203. if (!strcmp(argv[i].key, "byref")) {
  204. byref = BYREF_STRUCT;
  205. continue;
  206. }
  207. if (!strcmp(argv[i].key, "byref-all")) {
  208. byref = BYREF_ALL;
  209. continue;
  210. }
  211. error(G_("unknown option '-fplugin-arg-%s-%s'"), plugin_name, argv[i].key);
  212. }
  213. register_callback(plugin_name, PLUGIN_INFO, NULL, &structleak_plugin_info);
  214. if (enable) {
  215. register_callback(plugin_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &structleak_pass_info);
  216. register_callback(plugin_name, PLUGIN_FINISH_TYPE, finish_type, NULL);
  217. }
  218. register_callback(plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL);
  219. return 0;
  220. }