test_stackinit.c 11 KB

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