test_stackinit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test cases for compiler-based stack variable zeroing via future
  4. * compiler flags or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/string.h>
  11. /* Exfiltration buffer. */
  12. #define MAX_VAR_SIZE 128
  13. static u8 check_buf[MAX_VAR_SIZE];
  14. /* Character array to trigger stack protector in all functions. */
  15. #define VAR_BUFFER 32
  16. /* Volatile mask to convince compiler to copy memory with 0xff. */
  17. static volatile u8 forced_mask = 0xff;
  18. /* Location and size tracking to validate fill and test are colocated. */
  19. static void *fill_start, *target_start;
  20. static size_t fill_size, target_size;
  21. static bool range_contains(char *haystack_start, size_t haystack_size,
  22. char *needle_start, size_t needle_size)
  23. {
  24. if (needle_start >= haystack_start &&
  25. needle_start + needle_size <= haystack_start + haystack_size)
  26. return true;
  27. return false;
  28. }
  29. #define DO_NOTHING_TYPE_SCALAR(var_type) var_type
  30. #define DO_NOTHING_TYPE_STRING(var_type) void
  31. #define DO_NOTHING_TYPE_STRUCT(var_type) void
  32. #define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
  33. #define DO_NOTHING_RETURN_STRING(ptr) /**/
  34. #define DO_NOTHING_RETURN_STRUCT(ptr) /**/
  35. #define DO_NOTHING_CALL_SCALAR(var, name) \
  36. (var) = do_nothing_ ## name(&(var))
  37. #define DO_NOTHING_CALL_STRING(var, name) \
  38. do_nothing_ ## name(var)
  39. #define DO_NOTHING_CALL_STRUCT(var, name) \
  40. do_nothing_ ## name(&(var))
  41. #define FETCH_ARG_SCALAR(var) &var
  42. #define FETCH_ARG_STRING(var) var
  43. #define FETCH_ARG_STRUCT(var) &var
  44. #define FILL_SIZE_STRING 16
  45. #define INIT_CLONE_SCALAR /**/
  46. #define INIT_CLONE_STRING [FILL_SIZE_STRING]
  47. #define INIT_CLONE_STRUCT /**/
  48. #define INIT_SCALAR_none /**/
  49. #define INIT_SCALAR_zero = 0
  50. #define INIT_STRING_none [FILL_SIZE_STRING] /**/
  51. #define INIT_STRING_zero [FILL_SIZE_STRING] = { }
  52. #define INIT_STRUCT_none /**/
  53. #define INIT_STRUCT_zero = { }
  54. #define INIT_STRUCT_static_partial = { .two = 0, }
  55. #define INIT_STRUCT_static_all = { .one = 0, \
  56. .two = 0, \
  57. .three = 0, \
  58. .four = 0, \
  59. }
  60. #define INIT_STRUCT_dynamic_partial = { .two = arg->two, }
  61. #define INIT_STRUCT_dynamic_all = { .one = arg->one, \
  62. .two = arg->two, \
  63. .three = arg->three, \
  64. .four = arg->four, \
  65. }
  66. #define INIT_STRUCT_runtime_partial ; \
  67. var.two = 0
  68. #define INIT_STRUCT_runtime_all ; \
  69. var.one = 0; \
  70. var.two = 0; \
  71. var.three = 0; \
  72. var.four = 0
  73. /*
  74. * @name: unique string name for the test
  75. * @var_type: type to be tested for zeroing initialization
  76. * @which: is this a SCALAR, STRING, or STRUCT type?
  77. * @init_level: what kind of initialization is performed
  78. * @xfail: is this test expected to fail?
  79. */
  80. #define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
  81. /* Returns 0 on success, 1 on failure. */ \
  82. static noinline __init int test_ ## name (void) \
  83. { \
  84. var_type zero INIT_CLONE_ ## which; \
  85. int ignored; \
  86. u8 sum = 0, i; \
  87. \
  88. /* Notice when a new test is larger than expected. */ \
  89. BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
  90. \
  91. /* Fill clone type with zero for per-field init. */ \
  92. memset(&zero, 0x00, sizeof(zero)); \
  93. /* Clear entire check buffer for 0xFF overlap test. */ \
  94. memset(check_buf, 0x00, sizeof(check_buf)); \
  95. /* Fill stack with 0xFF. */ \
  96. ignored = leaf_ ##name((unsigned long)&ignored, 1, \
  97. FETCH_ARG_ ## which(zero)); \
  98. /* Verify all bytes overwritten with 0xFF. */ \
  99. for (sum = 0, i = 0; i < target_size; i++) \
  100. sum += (check_buf[i] != 0xFF); \
  101. if (sum) { \
  102. pr_err(#name ": leaf fill was not 0xFF!?\n"); \
  103. return 1; \
  104. } \
  105. /* Clear entire check buffer for later bit tests. */ \
  106. memset(check_buf, 0x00, sizeof(check_buf)); \
  107. /* Extract stack-defined variable contents. */ \
  108. ignored = leaf_ ##name((unsigned long)&ignored, 0, \
  109. FETCH_ARG_ ## which(zero)); \
  110. \
  111. /* Validate that compiler lined up fill and target. */ \
  112. if (!range_contains(fill_start, fill_size, \
  113. target_start, target_size)) { \
  114. pr_err(#name ": stack fill missed target!?\n"); \
  115. pr_err(#name ": fill %zu wide\n", fill_size); \
  116. pr_err(#name ": target offset by %d\n", \
  117. (int)((ssize_t)(uintptr_t)fill_start - \
  118. (ssize_t)(uintptr_t)target_start)); \
  119. return 1; \
  120. } \
  121. \
  122. /* Look for any bytes still 0xFF in check region. */ \
  123. for (sum = 0, i = 0; i < target_size; i++) \
  124. sum += (check_buf[i] == 0xFF); \
  125. \
  126. if (sum == 0) { \
  127. pr_info(#name " ok\n"); \
  128. return 0; \
  129. } else { \
  130. pr_warn(#name " %sFAIL (uninit bytes: %d)\n", \
  131. (xfail) ? "X" : "", sum); \
  132. return (xfail) ? 0 : 1; \
  133. } \
  134. }
  135. #define DEFINE_TEST(name, var_type, which, init_level) \
  136. /* no-op to force compiler into ignoring "uninitialized" vars */\
  137. static noinline __init DO_NOTHING_TYPE_ ## which(var_type) \
  138. do_nothing_ ## name(var_type *ptr) \
  139. { \
  140. /* Will always be true, but compiler doesn't know. */ \
  141. if ((unsigned long)ptr > 0x2) \
  142. return DO_NOTHING_RETURN_ ## which(ptr); \
  143. else \
  144. return DO_NOTHING_RETURN_ ## which(ptr + 1); \
  145. } \
  146. static noinline __init int leaf_ ## name(unsigned long sp, \
  147. bool fill, \
  148. var_type *arg) \
  149. { \
  150. char buf[VAR_BUFFER]; \
  151. var_type var INIT_ ## which ## _ ## init_level; \
  152. \
  153. target_start = &var; \
  154. target_size = sizeof(var); \
  155. /* \
  156. * Keep this buffer around to make sure we've got a \
  157. * stack frame of SOME kind... \
  158. */ \
  159. memset(buf, (char)(sp & 0xff), sizeof(buf)); \
  160. /* Fill variable with 0xFF. */ \
  161. if (fill) { \
  162. fill_start = &var; \
  163. fill_size = sizeof(var); \
  164. memset(fill_start, \
  165. (char)((sp & 0xff) | forced_mask), \
  166. fill_size); \
  167. } \
  168. \
  169. /* Silence "never initialized" warnings. */ \
  170. DO_NOTHING_CALL_ ## which(var, name); \
  171. \
  172. /* Exfiltrate "var". */ \
  173. memcpy(check_buf, target_start, target_size); \
  174. \
  175. return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
  176. } \
  177. DEFINE_TEST_DRIVER(name, var_type, which, 0)
  178. /* Structure with no padding. */
  179. struct test_packed {
  180. unsigned long one;
  181. unsigned long two;
  182. unsigned long three;
  183. unsigned long four;
  184. };
  185. /* Simple structure with padding likely to be covered by compiler. */
  186. struct test_small_hole {
  187. size_t one;
  188. char two;
  189. /* 3 byte padding hole here. */
  190. int three;
  191. unsigned long four;
  192. };
  193. /* Trigger unhandled padding in a structure. */
  194. struct test_big_hole {
  195. u8 one;
  196. u8 two;
  197. u8 three;
  198. /* 61 byte padding hole here. */
  199. u8 four __aligned(64);
  200. } __aligned(64);
  201. struct test_trailing_hole {
  202. char *one;
  203. char *two;
  204. char *three;
  205. char four;
  206. /* "sizeof(unsigned long) - 1" byte padding hole here. */
  207. };
  208. /* Test if STRUCTLEAK is clearing structs with __user fields. */
  209. struct test_user {
  210. u8 one;
  211. unsigned long two;
  212. char __user *three;
  213. unsigned long four;
  214. };
  215. #define DEFINE_SCALAR_TEST(name, init) \
  216. DEFINE_TEST(name ## _ ## init, name, SCALAR, init)
  217. #define DEFINE_SCALAR_TESTS(init) \
  218. DEFINE_SCALAR_TEST(u8, init); \
  219. DEFINE_SCALAR_TEST(u16, init); \
  220. DEFINE_SCALAR_TEST(u32, init); \
  221. DEFINE_SCALAR_TEST(u64, init); \
  222. DEFINE_TEST(char_array_ ## init, unsigned char, STRING, init)
  223. #define DEFINE_STRUCT_TEST(name, init) \
  224. DEFINE_TEST(name ## _ ## init, \
  225. struct test_ ## name, STRUCT, init)
  226. #define DEFINE_STRUCT_TESTS(init) \
  227. DEFINE_STRUCT_TEST(small_hole, init); \
  228. DEFINE_STRUCT_TEST(big_hole, init); \
  229. DEFINE_STRUCT_TEST(trailing_hole, init); \
  230. DEFINE_STRUCT_TEST(packed, init)
  231. /* These should be fully initialized all the time! */
  232. DEFINE_SCALAR_TESTS(zero);
  233. DEFINE_STRUCT_TESTS(zero);
  234. /* Static initialization: padding may be left uninitialized. */
  235. DEFINE_STRUCT_TESTS(static_partial);
  236. DEFINE_STRUCT_TESTS(static_all);
  237. /* Dynamic initialization: padding may be left uninitialized. */
  238. DEFINE_STRUCT_TESTS(dynamic_partial);
  239. DEFINE_STRUCT_TESTS(dynamic_all);
  240. /* Runtime initialization: padding may be left uninitialized. */
  241. DEFINE_STRUCT_TESTS(runtime_partial);
  242. DEFINE_STRUCT_TESTS(runtime_all);
  243. /* No initialization without compiler instrumentation. */
  244. DEFINE_SCALAR_TESTS(none);
  245. DEFINE_STRUCT_TESTS(none);
  246. DEFINE_TEST(user, struct test_user, STRUCT, none);
  247. /*
  248. * Check two uses through a variable declaration outside either path,
  249. * which was noticed as a special case in porting earlier stack init
  250. * compiler logic.
  251. */
  252. static int noinline __leaf_switch_none(int path, bool fill)
  253. {
  254. switch (path) {
  255. uint64_t var;
  256. case 1:
  257. target_start = &var;
  258. target_size = sizeof(var);
  259. if (fill) {
  260. fill_start = &var;
  261. fill_size = sizeof(var);
  262. memset(fill_start, forced_mask | 0x55, fill_size);
  263. }
  264. memcpy(check_buf, target_start, target_size);
  265. break;
  266. case 2:
  267. target_start = &var;
  268. target_size = sizeof(var);
  269. if (fill) {
  270. fill_start = &var;
  271. fill_size = sizeof(var);
  272. memset(fill_start, forced_mask | 0xaa, fill_size);
  273. }
  274. memcpy(check_buf, target_start, target_size);
  275. break;
  276. default:
  277. var = 5;
  278. return var & forced_mask;
  279. }
  280. return 0;
  281. }
  282. static noinline __init int leaf_switch_1_none(unsigned long sp, bool fill,
  283. uint64_t *arg)
  284. {
  285. return __leaf_switch_none(1, fill);
  286. }
  287. static noinline __init int leaf_switch_2_none(unsigned long sp, bool fill,
  288. uint64_t *arg)
  289. {
  290. return __leaf_switch_none(2, fill);
  291. }
  292. /*
  293. * These are expected to fail for most configurations because neither
  294. * GCC nor Clang have a way to perform initialization of variables in
  295. * non-code areas (i.e. in a switch statement before the first "case").
  296. * https://bugs.llvm.org/show_bug.cgi?id=44916
  297. */
  298. DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, 1);
  299. DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, 1);
  300. static int __init test_stackinit_init(void)
  301. {
  302. unsigned int failures = 0;
  303. #define test_scalars(init) do { \
  304. failures += test_u8_ ## init (); \
  305. failures += test_u16_ ## init (); \
  306. failures += test_u32_ ## init (); \
  307. failures += test_u64_ ## init (); \
  308. failures += test_char_array_ ## init (); \
  309. } while (0)
  310. #define test_structs(init) do { \
  311. failures += test_small_hole_ ## init (); \
  312. failures += test_big_hole_ ## init (); \
  313. failures += test_trailing_hole_ ## init (); \
  314. failures += test_packed_ ## init (); \
  315. } while (0)
  316. /* These are explicitly initialized and should always pass. */
  317. test_scalars(zero);
  318. test_structs(zero);
  319. /* Padding here appears to be accidentally always initialized? */
  320. test_structs(dynamic_partial);
  321. /* Padding initialization depends on compiler behaviors. */
  322. test_structs(static_partial);
  323. test_structs(static_all);
  324. test_structs(dynamic_all);
  325. test_structs(runtime_partial);
  326. test_structs(runtime_all);
  327. /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
  328. test_scalars(none);
  329. failures += test_switch_1_none();
  330. failures += test_switch_2_none();
  331. /* STRUCTLEAK_BYREF should cover from here down. */
  332. test_structs(none);
  333. /* STRUCTLEAK will only cover this. */
  334. failures += test_user();
  335. if (failures == 0)
  336. pr_info("all tests passed!\n");
  337. else
  338. pr_err("failures: %u\n", failures);
  339. return failures ? -EINVAL : 0;
  340. }
  341. module_init(test_stackinit_init);
  342. static void __exit test_stackinit_exit(void)
  343. { }
  344. module_exit(test_stackinit_exit);
  345. MODULE_LICENSE("GPL");