stackinit_kunit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Test cases for compiler-based stack variable zeroing via
  4. * -ftrivial-auto-var-init={zero,pattern} or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
  5. * For example, see:
  6. * "Running tests with kunit_tool" at Documentation/dev-tools/kunit/start.rst
  7. * ./tools/testing/kunit/kunit.py run stackinit [--raw_output] \
  8. * --make_option LLVM=1 \
  9. * --kconfig_add CONFIG_INIT_STACK_ALL_ZERO=y
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <kunit/test.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. /* Exfiltration buffer. */
  19. #define MAX_VAR_SIZE 128
  20. static u8 check_buf[MAX_VAR_SIZE];
  21. /* Character array to trigger stack protector in all functions. */
  22. #define VAR_BUFFER 32
  23. /* Volatile mask to convince compiler to copy memory with 0xff. */
  24. static volatile u8 forced_mask = 0xff;
  25. /* Location and size tracking to validate fill and test are colocated. */
  26. static void *fill_start, *target_start;
  27. static size_t fill_size, target_size;
  28. static bool range_contains(char *haystack_start, size_t haystack_size,
  29. char *needle_start, size_t needle_size)
  30. {
  31. if (needle_start >= haystack_start &&
  32. needle_start + needle_size <= haystack_start + haystack_size)
  33. return true;
  34. return false;
  35. }
  36. /* Whether the test is expected to fail. */
  37. #define WANT_SUCCESS 0
  38. #define XFAIL 1
  39. #define DO_NOTHING_TYPE_SCALAR(var_type) var_type
  40. #define DO_NOTHING_TYPE_STRING(var_type) void
  41. #define DO_NOTHING_TYPE_STRUCT(var_type) void
  42. #define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
  43. #define DO_NOTHING_RETURN_STRING(ptr) /**/
  44. #define DO_NOTHING_RETURN_STRUCT(ptr) /**/
  45. #define DO_NOTHING_CALL_SCALAR(var, name) \
  46. (var) = do_nothing_ ## name(&(var))
  47. #define DO_NOTHING_CALL_STRING(var, name) \
  48. do_nothing_ ## name(var)
  49. #define DO_NOTHING_CALL_STRUCT(var, name) \
  50. do_nothing_ ## name(&(var))
  51. #define FETCH_ARG_SCALAR(var) &var
  52. #define FETCH_ARG_STRING(var) var
  53. #define FETCH_ARG_STRUCT(var) &var
  54. #define FILL_SIZE_STRING 16
  55. #define INIT_CLONE_SCALAR /**/
  56. #define INIT_CLONE_STRING [FILL_SIZE_STRING]
  57. #define INIT_CLONE_STRUCT /**/
  58. #define ZERO_CLONE_SCALAR(zero) memset(&(zero), 0x00, sizeof(zero))
  59. #define ZERO_CLONE_STRING(zero) memset(&(zero), 0x00, sizeof(zero))
  60. /*
  61. * For the struct, intentionally poison padding to see if it gets
  62. * copied out in direct assignments.
  63. * */
  64. #define ZERO_CLONE_STRUCT(zero) \
  65. do { \
  66. memset(&(zero), 0xFF, sizeof(zero)); \
  67. zero.one = 0; \
  68. zero.two = 0; \
  69. zero.three = 0; \
  70. zero.four = 0; \
  71. } while (0)
  72. #define INIT_SCALAR_none(var_type) /**/
  73. #define INIT_SCALAR_zero(var_type) = 0
  74. #define INIT_STRING_none(var_type) [FILL_SIZE_STRING] /**/
  75. #define INIT_STRING_zero(var_type) [FILL_SIZE_STRING] = { }
  76. #define INIT_STRUCT_none(var_type) /**/
  77. #define INIT_STRUCT_zero(var_type) = { }
  78. #define __static_partial { .two = 0, }
  79. #define __static_all { .one = 0, \
  80. .two = 0, \
  81. .three = 0, \
  82. .four = 0, \
  83. }
  84. #define __dynamic_partial { .two = arg->two, }
  85. #define __dynamic_all { .one = arg->one, \
  86. .two = arg->two, \
  87. .three = arg->three, \
  88. .four = arg->four, \
  89. }
  90. #define __runtime_partial var.two = 0
  91. #define __runtime_all var.one = 0; \
  92. var.two = 0; \
  93. var.three = 0; \
  94. var.four = 0
  95. #define INIT_STRUCT_static_partial(var_type) \
  96. = __static_partial
  97. #define INIT_STRUCT_static_all(var_type) \
  98. = __static_all
  99. #define INIT_STRUCT_dynamic_partial(var_type) \
  100. = __dynamic_partial
  101. #define INIT_STRUCT_dynamic_all(var_type) \
  102. = __dynamic_all
  103. #define INIT_STRUCT_runtime_partial(var_type) \
  104. ; __runtime_partial
  105. #define INIT_STRUCT_runtime_all(var_type) \
  106. ; __runtime_all
  107. #define INIT_STRUCT_assigned_static_partial(var_type) \
  108. ; var = (var_type)__static_partial
  109. #define INIT_STRUCT_assigned_static_all(var_type) \
  110. ; var = (var_type)__static_all
  111. #define INIT_STRUCT_assigned_dynamic_partial(var_type) \
  112. ; var = (var_type)__dynamic_partial
  113. #define INIT_STRUCT_assigned_dynamic_all(var_type) \
  114. ; var = (var_type)__dynamic_all
  115. #define INIT_STRUCT_assigned_copy(var_type) \
  116. ; var = *(arg)
  117. /*
  118. * @name: unique string name for the test
  119. * @var_type: type to be tested for zeroing initialization
  120. * @which: is this a SCALAR, STRING, or STRUCT type?
  121. * @init_level: what kind of initialization is performed
  122. * @xfail: is this test expected to fail?
  123. */
  124. #define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
  125. /* Returns 0 on success, 1 on failure. */ \
  126. static noinline void test_ ## name (struct kunit *test) \
  127. { \
  128. var_type zero INIT_CLONE_ ## which; \
  129. int ignored; \
  130. u8 sum = 0, i; \
  131. \
  132. /* Notice when a new test is larger than expected. */ \
  133. BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
  134. \
  135. /* Fill clone type with zero for per-field init. */ \
  136. ZERO_CLONE_ ## which(zero); \
  137. /* Clear entire check buffer for 0xFF overlap test. */ \
  138. memset(check_buf, 0x00, sizeof(check_buf)); \
  139. /* Fill stack with 0xFF. */ \
  140. ignored = leaf_ ##name((unsigned long)&ignored, 1, \
  141. FETCH_ARG_ ## which(zero)); \
  142. /* Verify all bytes overwritten with 0xFF. */ \
  143. for (sum = 0, i = 0; i < target_size; i++) \
  144. sum += (check_buf[i] != 0xFF); \
  145. KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
  146. "leaf fill was not 0xFF!?\n"); \
  147. /* Clear entire check buffer for later bit tests. */ \
  148. memset(check_buf, 0x00, sizeof(check_buf)); \
  149. /* Extract stack-defined variable contents. */ \
  150. ignored = leaf_ ##name((unsigned long)&ignored, 0, \
  151. FETCH_ARG_ ## which(zero)); \
  152. \
  153. /* Validate that compiler lined up fill and target. */ \
  154. KUNIT_ASSERT_TRUE_MSG(test, \
  155. range_contains(fill_start, fill_size, \
  156. target_start, target_size), \
  157. "stack fill missed target!? " \
  158. "(fill %zu wide, target offset by %d)\n", \
  159. fill_size, \
  160. (int)((ssize_t)(uintptr_t)fill_start - \
  161. (ssize_t)(uintptr_t)target_start)); \
  162. \
  163. /* Look for any bytes still 0xFF in check region. */ \
  164. for (sum = 0, i = 0; i < target_size; i++) \
  165. sum += (check_buf[i] == 0xFF); \
  166. \
  167. if (sum != 0 && xfail) \
  168. kunit_skip(test, \
  169. "XFAIL uninit bytes: %d\n", \
  170. sum); \
  171. KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
  172. "uninit bytes: %d\n", sum); \
  173. }
  174. #define DEFINE_TEST(name, var_type, which, init_level, xfail) \
  175. /* no-op to force compiler into ignoring "uninitialized" vars */\
  176. static noinline DO_NOTHING_TYPE_ ## which(var_type) \
  177. do_nothing_ ## name(var_type *ptr) \
  178. { \
  179. /* Will always be true, but compiler doesn't know. */ \
  180. if ((unsigned long)ptr > 0x2) \
  181. return DO_NOTHING_RETURN_ ## which(ptr); \
  182. else \
  183. return DO_NOTHING_RETURN_ ## which(ptr + 1); \
  184. } \
  185. static noinline int leaf_ ## name(unsigned long sp, bool fill, \
  186. var_type *arg) \
  187. { \
  188. char buf[VAR_BUFFER]; \
  189. var_type var \
  190. INIT_ ## which ## _ ## init_level(var_type); \
  191. \
  192. target_start = &var; \
  193. target_size = sizeof(var); \
  194. /* \
  195. * Keep this buffer around to make sure we've got a \
  196. * stack frame of SOME kind... \
  197. */ \
  198. memset(buf, (char)(sp & 0xff), sizeof(buf)); \
  199. /* Fill variable with 0xFF. */ \
  200. if (fill) { \
  201. fill_start = &var; \
  202. fill_size = sizeof(var); \
  203. memset(fill_start, \
  204. (char)((sp & 0xff) | forced_mask), \
  205. fill_size); \
  206. } \
  207. \
  208. /* Silence "never initialized" warnings. */ \
  209. DO_NOTHING_CALL_ ## which(var, name); \
  210. \
  211. /* Exfiltrate "var". */ \
  212. memcpy(check_buf, target_start, target_size); \
  213. \
  214. return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
  215. } \
  216. DEFINE_TEST_DRIVER(name, var_type, which, xfail)
  217. /* Structure with no padding. */
  218. struct test_packed {
  219. unsigned long one;
  220. unsigned long two;
  221. unsigned long three;
  222. unsigned long four;
  223. };
  224. /* Simple structure with padding likely to be covered by compiler. */
  225. struct test_small_hole {
  226. size_t one;
  227. char two;
  228. /* 3 byte padding hole here. */
  229. int three;
  230. unsigned long four;
  231. };
  232. /* Trigger unhandled padding in a structure. */
  233. struct test_big_hole {
  234. u8 one;
  235. u8 two;
  236. u8 three;
  237. /* 61 byte padding hole here. */
  238. u8 four __aligned(64);
  239. } __aligned(64);
  240. struct test_trailing_hole {
  241. char *one;
  242. char *two;
  243. char *three;
  244. char four;
  245. /* "sizeof(unsigned long) - 1" byte padding hole here. */
  246. };
  247. /* Test if STRUCTLEAK is clearing structs with __user fields. */
  248. struct test_user {
  249. u8 one;
  250. unsigned long two;
  251. char __user *three;
  252. unsigned long four;
  253. };
  254. #define ALWAYS_PASS WANT_SUCCESS
  255. #define ALWAYS_FAIL XFAIL
  256. #ifdef CONFIG_INIT_STACK_NONE
  257. # define USER_PASS XFAIL
  258. # define BYREF_PASS XFAIL
  259. # define STRONG_PASS XFAIL
  260. #elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER)
  261. # define USER_PASS WANT_SUCCESS
  262. # define BYREF_PASS XFAIL
  263. # define STRONG_PASS XFAIL
  264. #elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF)
  265. # define USER_PASS WANT_SUCCESS
  266. # define BYREF_PASS WANT_SUCCESS
  267. # define STRONG_PASS XFAIL
  268. #else
  269. # define USER_PASS WANT_SUCCESS
  270. # define BYREF_PASS WANT_SUCCESS
  271. # define STRONG_PASS WANT_SUCCESS
  272. #endif
  273. #define DEFINE_SCALAR_TEST(name, init, xfail) \
  274. DEFINE_TEST(name ## _ ## init, name, SCALAR, \
  275. init, xfail)
  276. #define DEFINE_SCALAR_TESTS(init, xfail) \
  277. DEFINE_SCALAR_TEST(u8, init, xfail); \
  278. DEFINE_SCALAR_TEST(u16, init, xfail); \
  279. DEFINE_SCALAR_TEST(u32, init, xfail); \
  280. DEFINE_SCALAR_TEST(u64, init, xfail); \
  281. DEFINE_TEST(char_array_ ## init, unsigned char, \
  282. STRING, init, xfail)
  283. #define DEFINE_STRUCT_TEST(name, init, xfail) \
  284. DEFINE_TEST(name ## _ ## init, \
  285. struct test_ ## name, STRUCT, init, \
  286. xfail)
  287. #define DEFINE_STRUCT_TESTS(init, xfail) \
  288. DEFINE_STRUCT_TEST(small_hole, init, xfail); \
  289. DEFINE_STRUCT_TEST(big_hole, init, xfail); \
  290. DEFINE_STRUCT_TEST(trailing_hole, init, xfail); \
  291. DEFINE_STRUCT_TEST(packed, init, xfail)
  292. #define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail) \
  293. DEFINE_STRUCT_TESTS(base ## _ ## partial, \
  294. xfail); \
  295. DEFINE_STRUCT_TESTS(base ## _ ## all, xfail)
  296. /* These should be fully initialized all the time! */
  297. DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS);
  298. DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS);
  299. /* Struct initializers: padding may be left uninitialized. */
  300. DEFINE_STRUCT_INITIALIZER_TESTS(static, STRONG_PASS);
  301. DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, STRONG_PASS);
  302. DEFINE_STRUCT_INITIALIZER_TESTS(runtime, STRONG_PASS);
  303. DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, STRONG_PASS);
  304. DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS);
  305. DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL);
  306. /* No initialization without compiler instrumentation. */
  307. DEFINE_SCALAR_TESTS(none, STRONG_PASS);
  308. DEFINE_STRUCT_TESTS(none, BYREF_PASS);
  309. /* Initialization of members with __user attribute. */
  310. DEFINE_TEST(user, struct test_user, STRUCT, none, USER_PASS);
  311. /*
  312. * Check two uses through a variable declaration outside either path,
  313. * which was noticed as a special case in porting earlier stack init
  314. * compiler logic.
  315. */
  316. static int noinline __leaf_switch_none(int path, bool fill)
  317. {
  318. switch (path) {
  319. /*
  320. * This is intentionally unreachable. To silence the
  321. * warning, build with -Wno-switch-unreachable
  322. */
  323. uint64_t var[10];
  324. case 1:
  325. target_start = &var;
  326. target_size = sizeof(var);
  327. if (fill) {
  328. fill_start = &var;
  329. fill_size = sizeof(var);
  330. memset(fill_start, forced_mask | 0x55, fill_size);
  331. }
  332. memcpy(check_buf, target_start, target_size);
  333. break;
  334. case 2:
  335. target_start = &var;
  336. target_size = sizeof(var);
  337. if (fill) {
  338. fill_start = &var;
  339. fill_size = sizeof(var);
  340. memset(fill_start, forced_mask | 0xaa, fill_size);
  341. }
  342. memcpy(check_buf, target_start, target_size);
  343. break;
  344. default:
  345. var[1] = 5;
  346. return var[1] & forced_mask;
  347. }
  348. return 0;
  349. }
  350. static noinline int leaf_switch_1_none(unsigned long sp, bool fill,
  351. uint64_t *arg)
  352. {
  353. return __leaf_switch_none(1, fill);
  354. }
  355. static noinline int leaf_switch_2_none(unsigned long sp, bool fill,
  356. uint64_t *arg)
  357. {
  358. return __leaf_switch_none(2, fill);
  359. }
  360. /*
  361. * These are expected to fail for most configurations because neither
  362. * GCC nor Clang have a way to perform initialization of variables in
  363. * non-code areas (i.e. in a switch statement before the first "case").
  364. * https://bugs.llvm.org/show_bug.cgi?id=44916
  365. */
  366. DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, ALWAYS_FAIL);
  367. DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, ALWAYS_FAIL);
  368. #define KUNIT_test_scalars(init) \
  369. KUNIT_CASE(test_u8_ ## init), \
  370. KUNIT_CASE(test_u16_ ## init), \
  371. KUNIT_CASE(test_u32_ ## init), \
  372. KUNIT_CASE(test_u64_ ## init), \
  373. KUNIT_CASE(test_char_array_ ## init)
  374. #define KUNIT_test_structs(init) \
  375. KUNIT_CASE(test_small_hole_ ## init), \
  376. KUNIT_CASE(test_big_hole_ ## init), \
  377. KUNIT_CASE(test_trailing_hole_ ## init),\
  378. KUNIT_CASE(test_packed_ ## init) \
  379. static struct kunit_case stackinit_test_cases[] = {
  380. /* These are explicitly initialized and should always pass. */
  381. KUNIT_test_scalars(zero),
  382. KUNIT_test_structs(zero),
  383. /* Padding here appears to be accidentally always initialized? */
  384. KUNIT_test_structs(dynamic_partial),
  385. KUNIT_test_structs(assigned_dynamic_partial),
  386. /* Padding initialization depends on compiler behaviors. */
  387. KUNIT_test_structs(static_partial),
  388. KUNIT_test_structs(static_all),
  389. KUNIT_test_structs(dynamic_all),
  390. KUNIT_test_structs(runtime_partial),
  391. KUNIT_test_structs(runtime_all),
  392. KUNIT_test_structs(assigned_static_partial),
  393. KUNIT_test_structs(assigned_static_all),
  394. KUNIT_test_structs(assigned_dynamic_all),
  395. /* Everything fails this since it effectively performs a memcpy(). */
  396. KUNIT_test_structs(assigned_copy),
  397. /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
  398. KUNIT_test_scalars(none),
  399. KUNIT_CASE(test_switch_1_none),
  400. KUNIT_CASE(test_switch_2_none),
  401. /* STRUCTLEAK_BYREF should cover from here down. */
  402. KUNIT_test_structs(none),
  403. /* STRUCTLEAK will only cover this. */
  404. KUNIT_CASE(test_user),
  405. {}
  406. };
  407. static struct kunit_suite stackinit_test_suite = {
  408. .name = "stackinit",
  409. .test_cases = stackinit_test_cases,
  410. };
  411. kunit_test_suites(&stackinit_test_suite);
  412. MODULE_LICENSE("GPL");