Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
14#include <kunit/test.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/string.h>
19
20/* Exfiltration buffer. */
21#define MAX_VAR_SIZE 128
22static u8 check_buf[MAX_VAR_SIZE];
23
24/* Character array to trigger stack protector in all functions. */
25#define VAR_BUFFER 32
26
27/* Volatile mask to convince compiler to copy memory with 0xff. */
28static volatile u8 forced_mask = 0xff;
29
30/* Location and size tracking to validate fill and test are colocated. */
31static void *fill_start, *target_start;
32static size_t fill_size, target_size;
33
34static bool stackinit_range_contains(char *haystack_start, size_t haystack_size,
35 char *needle_start, size_t needle_size)
36{
37 if (needle_start >= haystack_start &&
38 needle_start + needle_size <= haystack_start + haystack_size)
39 return true;
40 return false;
41}
42
43/* Whether the test is expected to fail. */
44#define WANT_SUCCESS 0
45#define XFAIL 1
46
47#define DO_NOTHING_TYPE_SCALAR(var_type) var_type
48#define DO_NOTHING_TYPE_STRING(var_type) void
49#define DO_NOTHING_TYPE_STRUCT(var_type) void
50#define DO_NOTHING_TYPE_UNION(var_type) void
51
52#define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
53#define DO_NOTHING_RETURN_STRING(ptr) /**/
54#define DO_NOTHING_RETURN_STRUCT(ptr) /**/
55#define DO_NOTHING_RETURN_UNION(ptr) /**/
56
57#define DO_NOTHING_CALL_SCALAR(var, name) \
58 (var) = do_nothing_ ## name(&(var))
59#define DO_NOTHING_CALL_STRING(var, name) \
60 do_nothing_ ## name(var)
61#define DO_NOTHING_CALL_STRUCT(var, name) \
62 do_nothing_ ## name(&(var))
63#define DO_NOTHING_CALL_UNION(var, name) \
64 do_nothing_ ## name(&(var))
65
66#define FETCH_ARG_SCALAR(var) &var
67#define FETCH_ARG_STRING(var) var
68#define FETCH_ARG_STRUCT(var) &var
69#define FETCH_ARG_UNION(var) &var
70
71/*
72 * On m68k, if the leaf function test variable is longer than 8 bytes,
73 * the start of the stack frame moves. 8 is sufficiently large to
74 * test m68k char arrays, but leave it at 16 for other architectures.
75 */
76#ifdef CONFIG_M68K
77#define FILL_SIZE_STRING 8
78#define FILL_SIZE_ARRAY 2
79#else
80#define FILL_SIZE_STRING 16
81#define FILL_SIZE_ARRAY 8
82#endif
83
84#define INIT_CLONE_SCALAR /**/
85#define INIT_CLONE_STRING [FILL_SIZE_STRING]
86#define INIT_CLONE_STRUCT /**/
87#define INIT_CLONE_UNION /**/
88
89#define ZERO_CLONE_SCALAR(zero) memset(&(zero), 0x00, sizeof(zero))
90#define ZERO_CLONE_STRING(zero) memset(&(zero), 0x00, sizeof(zero))
91/*
92 * For the struct, intentionally poison padding to see if it gets
93 * copied out in direct assignments.
94 * */
95#define ZERO_CLONE_STRUCT(zero) \
96 do { \
97 memset(&(zero), 0xFF, sizeof(zero)); \
98 zero.one = 0; \
99 zero.two = 0; \
100 zero.three = 0; \
101 zero.four = 0; \
102 } while (0)
103#define ZERO_CLONE_UNION(zero) ZERO_CLONE_STRUCT(zero)
104
105#define INIT_SCALAR_none(var_type) /**/
106#define INIT_SCALAR_zero(var_type) = 0
107
108#define INIT_STRING_none(var_type) [FILL_SIZE_STRING] /**/
109#define INIT_STRING_zero(var_type) [FILL_SIZE_STRING] = { }
110
111#define INIT_STRUCT_none(var_type) /**/
112#define INIT_STRUCT_zero(var_type) = { }
113#define INIT_STRUCT_old_zero(var_type) = { 0 }
114
115
116#define __static_partial { .two = 0, }
117#define __static_all { .one = 0, \
118 .two = 0, \
119 .three = 0, \
120 .four = 0, \
121 }
122#define __dynamic_partial { .two = arg->two, }
123#define __dynamic_all { .one = arg->one, \
124 .two = arg->two, \
125 .three = arg->three, \
126 .four = arg->four, \
127 }
128#define __runtime_partial var.two = 0
129#define __runtime_all var.one = 0; \
130 var.two = 0; \
131 var.three = 0; \
132 var.four = 0
133
134#define INIT_STRUCT_static_partial(var_type) \
135 = __static_partial
136#define INIT_STRUCT_static_all(var_type) \
137 = __static_all
138#define INIT_STRUCT_dynamic_partial(var_type) \
139 = __dynamic_partial
140#define INIT_STRUCT_dynamic_all(var_type) \
141 = __dynamic_all
142#define INIT_STRUCT_runtime_partial(var_type) \
143 ; __runtime_partial
144#define INIT_STRUCT_runtime_all(var_type) \
145 ; __runtime_all
146
147#define INIT_STRUCT_assigned_static_partial(var_type) \
148 ; var = (var_type)__static_partial
149#define INIT_STRUCT_assigned_static_all(var_type) \
150 ; var = (var_type)__static_all
151#define INIT_STRUCT_assigned_dynamic_partial(var_type) \
152 ; var = (var_type)__dynamic_partial
153#define INIT_STRUCT_assigned_dynamic_all(var_type) \
154 ; var = (var_type)__dynamic_all
155
156#define INIT_STRUCT_assigned_copy(var_type) \
157 ; var = *(arg)
158
159/* Union initialization is the same as structs. */
160#define INIT_UNION_none(var_type) INIT_STRUCT_none(var_type)
161#define INIT_UNION_zero(var_type) INIT_STRUCT_zero(var_type)
162#define INIT_UNION_old_zero(var_type) INIT_STRUCT_old_zero(var_type)
163
164#define INIT_UNION_static_partial(var_type) \
165 INIT_STRUCT_static_partial(var_type)
166#define INIT_UNION_static_all(var_type) \
167 INIT_STRUCT_static_all(var_type)
168#define INIT_UNION_dynamic_partial(var_type) \
169 INIT_STRUCT_dynamic_partial(var_type)
170#define INIT_UNION_dynamic_all(var_type) \
171 INIT_STRUCT_dynamic_all(var_type)
172#define INIT_UNION_runtime_partial(var_type) \
173 INIT_STRUCT_runtime_partial(var_type)
174#define INIT_UNION_runtime_all(var_type) \
175 INIT_STRUCT_runtime_all(var_type)
176#define INIT_UNION_assigned_static_partial(var_type) \
177 INIT_STRUCT_assigned_static_partial(var_type)
178#define INIT_UNION_assigned_static_all(var_type) \
179 INIT_STRUCT_assigned_static_all(var_type)
180#define INIT_UNION_assigned_dynamic_partial(var_type) \
181 INIT_STRUCT_assigned_dynamic_partial(var_type)
182#define INIT_UNION_assigned_dynamic_all(var_type) \
183 INIT_STRUCT_assigned_dynamic_all(var_type)
184#define INIT_UNION_assigned_copy(var_type) \
185 INIT_STRUCT_assigned_copy(var_type)
186
187/*
188 * @name: unique string name for the test
189 * @var_type: type to be tested for zeroing initialization
190 * @which: is this a SCALAR, STRING, or STRUCT type?
191 * @init_level: what kind of initialization is performed
192 * @xfail: is this test expected to fail?
193 */
194#define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
195/* Returns 0 on success, 1 on failure. */ \
196static noinline void test_ ## name (struct kunit *test) \
197{ \
198 var_type zero INIT_CLONE_ ## which; \
199 int ignored; \
200 u8 sum = 0, i; \
201 \
202 /* Notice when a new test is larger than expected. */ \
203 BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
204 \
205 /* Fill clone type with zero for per-field init. */ \
206 ZERO_CLONE_ ## which(zero); \
207 /* Clear entire check buffer for 0xFF overlap test. */ \
208 memset(check_buf, 0x00, sizeof(check_buf)); \
209 /* Fill stack with 0xFF. */ \
210 ignored = leaf_ ##name((unsigned long)&ignored, 1, \
211 FETCH_ARG_ ## which(zero)); \
212 /* Verify all bytes overwritten with 0xFF. */ \
213 for (sum = 0, i = 0; i < target_size; i++) \
214 sum += (check_buf[i] != 0xFF); \
215 /* Clear entire check buffer for later bit tests. */ \
216 memset(check_buf, 0x00, sizeof(check_buf)); \
217 /* Extract stack-defined variable contents. */ \
218 ignored = leaf_ ##name((unsigned long)&ignored, 0, \
219 FETCH_ARG_ ## which(zero)); \
220 /* \
221 * Delay the sum test to here to do as little as \
222 * possible between the two leaf function calls. \
223 */ \
224 KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
225 "leaf fill was not 0xFF!?\n"); \
226 \
227 /* Validate that compiler lined up fill and target. */ \
228 KUNIT_ASSERT_TRUE_MSG(test, \
229 stackinit_range_contains(fill_start, fill_size, \
230 target_start, target_size), \
231 "stackframe was not the same between calls!? " \
232 "(fill %zu wide, target offset by %d)\n", \
233 fill_size, \
234 (int)((ssize_t)(uintptr_t)fill_start - \
235 (ssize_t)(uintptr_t)target_start)); \
236 \
237 /* Look for any bytes still 0xFF in check region. */ \
238 for (sum = 0, i = 0; i < target_size; i++) \
239 sum += (check_buf[i] == 0xFF); \
240 \
241 if (sum != 0 && xfail) \
242 kunit_skip(test, \
243 "XFAIL uninit bytes: %d\n", \
244 sum); \
245 KUNIT_ASSERT_EQ_MSG(test, sum, 0, \
246 "uninit bytes: %d\n", sum); \
247}
248#define DEFINE_TEST(name, var_type, which, init_level, xfail) \
249/* no-op to force compiler into ignoring "uninitialized" vars */\
250static noinline DO_NOTHING_TYPE_ ## which(var_type) \
251do_nothing_ ## name(var_type *ptr) \
252{ \
253 OPTIMIZER_HIDE_VAR(ptr); \
254 /* Will always be true, but compiler doesn't know. */ \
255 if ((unsigned long)ptr > 0x2) \
256 return DO_NOTHING_RETURN_ ## which(ptr); \
257 else \
258 return DO_NOTHING_RETURN_ ## which(ptr + 1); \
259} \
260static noinline int leaf_ ## name(unsigned long sp, bool fill, \
261 var_type *arg) \
262{ \
263 char buf[VAR_BUFFER]; \
264 var_type var \
265 INIT_ ## which ## _ ## init_level(var_type); \
266 \
267 target_start = &var; \
268 target_size = sizeof(var); \
269 /* \
270 * Keep this buffer around to make sure we've got a \
271 * stack frame of SOME kind... \
272 */ \
273 memset(buf, (char)(sp & 0xff), sizeof(buf)); \
274 /* Fill variable with 0xFF. */ \
275 if (fill) { \
276 fill_start = &var; \
277 fill_size = sizeof(var); \
278 memset(fill_start, \
279 (char)((sp & 0xff) | forced_mask), \
280 fill_size); \
281 } \
282 \
283 /* Silence "never initialized" warnings. */ \
284 DO_NOTHING_CALL_ ## which(var, name); \
285 \
286 /* Exfiltrate "var". */ \
287 memcpy(check_buf, target_start, target_size); \
288 \
289 return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
290} \
291DEFINE_TEST_DRIVER(name, var_type, which, xfail)
292
293/* Structure with no padding. */
294struct test_packed {
295 unsigned long one;
296 unsigned long two;
297 unsigned long three;
298 unsigned long four;
299};
300
301/* Simple structure with padding likely to be covered by compiler. */
302struct test_small_hole {
303 size_t one;
304 char two;
305 /* 3 byte padding hole here. */
306 int three;
307 unsigned long four;
308};
309
310/* Trigger unhandled padding in a structure. */
311struct test_big_hole {
312 u8 one;
313 u8 two;
314 u8 three;
315 /* 61 byte padding hole here. */
316 u8 four __aligned(64);
317} __aligned(64);
318
319struct test_trailing_hole {
320 char *one;
321 char *two;
322 char *three;
323 char four;
324 /* "sizeof(unsigned long) - 1" byte padding hole here. */
325};
326
327/* Test if STRUCTLEAK is clearing structs with __user fields. */
328struct test_user {
329 u8 one;
330 unsigned long two;
331 char __user *three;
332 unsigned long four;
333};
334
335/* No padding: all members are the same size. */
336union test_same_sizes {
337 unsigned long one;
338 unsigned long two;
339 unsigned long three;
340 unsigned long four;
341};
342
343/* Mismatched sizes, with one and two being small */
344union test_small_start {
345 char one:1;
346 char two;
347 short three;
348 unsigned long four;
349 struct big_struct {
350 unsigned long array[FILL_SIZE_ARRAY];
351 } big;
352};
353
354/* Mismatched sizes, with three and four being small */
355union test_small_end {
356 short one;
357 unsigned long two;
358 char three:1;
359 char four;
360};
361
362#define ALWAYS_PASS WANT_SUCCESS
363#define ALWAYS_FAIL XFAIL
364
365#ifdef CONFIG_INIT_STACK_NONE
366# define USER_PASS XFAIL
367# define BYREF_PASS XFAIL
368# define STRONG_PASS XFAIL
369#elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_USER)
370# define USER_PASS WANT_SUCCESS
371# define BYREF_PASS XFAIL
372# define STRONG_PASS XFAIL
373#elif defined(CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF)
374# define USER_PASS WANT_SUCCESS
375# define BYREF_PASS WANT_SUCCESS
376# define STRONG_PASS XFAIL
377#else
378# define USER_PASS WANT_SUCCESS
379# define BYREF_PASS WANT_SUCCESS
380# define STRONG_PASS WANT_SUCCESS
381#endif
382
383#define DEFINE_SCALAR_TEST(name, init, xfail) \
384 DEFINE_TEST(name ## _ ## init, name, SCALAR, \
385 init, xfail)
386
387#define DEFINE_SCALAR_TESTS(init, xfail) \
388 DEFINE_SCALAR_TEST(u8, init, xfail); \
389 DEFINE_SCALAR_TEST(u16, init, xfail); \
390 DEFINE_SCALAR_TEST(u32, init, xfail); \
391 DEFINE_SCALAR_TEST(u64, init, xfail); \
392 DEFINE_TEST(char_array_ ## init, unsigned char, \
393 STRING, init, xfail)
394
395#define DEFINE_STRUCT_TEST(name, init, xfail) \
396 DEFINE_TEST(name ## _ ## init, \
397 struct test_ ## name, STRUCT, init, \
398 xfail)
399
400#define DEFINE_UNION_TEST(name, init, xfail) \
401 DEFINE_TEST(name ## _ ## init, \
402 union test_ ## name, STRUCT, init, \
403 xfail)
404
405#define DEFINE_STRUCT_TESTS(init, xfail) \
406 DEFINE_STRUCT_TEST(small_hole, init, xfail); \
407 DEFINE_STRUCT_TEST(big_hole, init, xfail); \
408 DEFINE_STRUCT_TEST(trailing_hole, init, xfail); \
409 DEFINE_STRUCT_TEST(packed, init, xfail)
410
411#define DEFINE_STRUCT_INITIALIZER_TESTS(base, xfail) \
412 DEFINE_STRUCT_TESTS(base ## _ ## partial, \
413 xfail); \
414 DEFINE_STRUCT_TESTS(base ## _ ## all, xfail)
415
416#define DEFINE_UNION_INITIALIZER_TESTS(base, xfail) \
417 DEFINE_UNION_TESTS(base ## _ ## partial, \
418 xfail); \
419 DEFINE_UNION_TESTS(base ## _ ## all, xfail)
420
421#define DEFINE_UNION_TESTS(init, xfail) \
422 DEFINE_UNION_TEST(same_sizes, init, xfail); \
423 DEFINE_UNION_TEST(small_start, init, xfail); \
424 DEFINE_UNION_TEST(small_end, init, xfail);
425
426/* These should be fully initialized all the time! */
427DEFINE_SCALAR_TESTS(zero, ALWAYS_PASS);
428DEFINE_STRUCT_TESTS(zero, ALWAYS_PASS);
429DEFINE_STRUCT_TESTS(old_zero, ALWAYS_PASS);
430DEFINE_UNION_TESTS(zero, ALWAYS_PASS);
431DEFINE_UNION_TESTS(old_zero, ALWAYS_PASS);
432/* Struct initializers: padding may be left uninitialized. */
433DEFINE_STRUCT_INITIALIZER_TESTS(static, STRONG_PASS);
434DEFINE_STRUCT_INITIALIZER_TESTS(dynamic, STRONG_PASS);
435DEFINE_STRUCT_INITIALIZER_TESTS(runtime, STRONG_PASS);
436DEFINE_STRUCT_INITIALIZER_TESTS(assigned_static, STRONG_PASS);
437DEFINE_STRUCT_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS);
438DEFINE_STRUCT_TESTS(assigned_copy, ALWAYS_FAIL);
439DEFINE_UNION_INITIALIZER_TESTS(static, STRONG_PASS);
440DEFINE_UNION_INITIALIZER_TESTS(dynamic, STRONG_PASS);
441DEFINE_UNION_INITIALIZER_TESTS(runtime, STRONG_PASS);
442DEFINE_UNION_INITIALIZER_TESTS(assigned_static, STRONG_PASS);
443DEFINE_UNION_INITIALIZER_TESTS(assigned_dynamic, STRONG_PASS);
444DEFINE_UNION_TESTS(assigned_copy, ALWAYS_FAIL);
445/* No initialization without compiler instrumentation. */
446DEFINE_SCALAR_TESTS(none, STRONG_PASS);
447DEFINE_STRUCT_TESTS(none, BYREF_PASS);
448/* Initialization of members with __user attribute. */
449DEFINE_TEST(user, struct test_user, STRUCT, none, USER_PASS);
450
451/*
452 * Check two uses through a variable declaration outside either path,
453 * which was noticed as a special case in porting earlier stack init
454 * compiler logic.
455 */
456static int noinline __leaf_switch_none(int path, bool fill)
457{
458 switch (path) {
459 /*
460 * This is intentionally unreachable. To silence the
461 * warning, build with -Wno-switch-unreachable
462 */
463 uint64_t var[10];
464
465 case 1:
466 target_start = &var;
467 target_size = sizeof(var);
468 if (fill) {
469 fill_start = &var;
470 fill_size = sizeof(var);
471
472 memset(fill_start, forced_mask | 0x55, fill_size);
473 }
474 memcpy(check_buf, target_start, target_size);
475 break;
476 case 2:
477 target_start = &var;
478 target_size = sizeof(var);
479 if (fill) {
480 fill_start = &var;
481 fill_size = sizeof(var);
482
483 memset(fill_start, forced_mask | 0xaa, fill_size);
484 }
485 memcpy(check_buf, target_start, target_size);
486 break;
487 default:
488 var[1] = 5;
489 return var[1] & forced_mask;
490 }
491 return 0;
492}
493
494static noinline int leaf_switch_1_none(unsigned long sp, bool fill,
495 uint64_t *arg)
496{
497 return __leaf_switch_none(1, fill);
498}
499
500static noinline int leaf_switch_2_none(unsigned long sp, bool fill,
501 uint64_t *arg)
502{
503 return __leaf_switch_none(2, fill);
504}
505
506/*
507 * These are expected to fail for most configurations because neither
508 * GCC nor Clang have a way to perform initialization of variables in
509 * non-code areas (i.e. in a switch statement before the first "case").
510 * https://llvm.org/pr44916
511 */
512DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, ALWAYS_FAIL);
513DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, ALWAYS_FAIL);
514
515#define KUNIT_test_scalars(init) \
516 KUNIT_CASE(test_u8_ ## init), \
517 KUNIT_CASE(test_u16_ ## init), \
518 KUNIT_CASE(test_u32_ ## init), \
519 KUNIT_CASE(test_u64_ ## init), \
520 KUNIT_CASE(test_char_array_ ## init)
521
522#define KUNIT_test_structs(init) \
523 KUNIT_CASE(test_small_hole_ ## init), \
524 KUNIT_CASE(test_big_hole_ ## init), \
525 KUNIT_CASE(test_trailing_hole_ ## init),\
526 KUNIT_CASE(test_packed_ ## init) \
527
528#define KUNIT_test_unions(init) \
529 KUNIT_CASE(test_same_sizes_ ## init), \
530 KUNIT_CASE(test_small_start_ ## init), \
531 KUNIT_CASE(test_small_end_ ## init) \
532
533static struct kunit_case stackinit_test_cases[] = {
534 /* These are explicitly initialized and should always pass. */
535 KUNIT_test_scalars(zero),
536 KUNIT_test_structs(zero),
537 KUNIT_test_structs(old_zero),
538 KUNIT_test_unions(zero),
539 KUNIT_test_unions(old_zero),
540 /* Padding here appears to be accidentally always initialized? */
541 KUNIT_test_structs(dynamic_partial),
542 KUNIT_test_structs(assigned_dynamic_partial),
543 KUNIT_test_unions(dynamic_partial),
544 KUNIT_test_unions(assigned_dynamic_partial),
545 /* Padding initialization depends on compiler behaviors. */
546 KUNIT_test_structs(static_partial),
547 KUNIT_test_structs(static_all),
548 KUNIT_test_structs(dynamic_all),
549 KUNIT_test_structs(runtime_partial),
550 KUNIT_test_structs(runtime_all),
551 KUNIT_test_structs(assigned_static_partial),
552 KUNIT_test_structs(assigned_static_all),
553 KUNIT_test_structs(assigned_dynamic_all),
554 KUNIT_test_unions(static_partial),
555 KUNIT_test_unions(static_all),
556 KUNIT_test_unions(dynamic_all),
557 KUNIT_test_unions(runtime_partial),
558 KUNIT_test_unions(runtime_all),
559 KUNIT_test_unions(assigned_static_partial),
560 KUNIT_test_unions(assigned_static_all),
561 KUNIT_test_unions(assigned_dynamic_all),
562 /* Everything fails this since it effectively performs a memcpy(). */
563 KUNIT_test_structs(assigned_copy),
564 KUNIT_test_unions(assigned_copy),
565 /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
566 KUNIT_test_scalars(none),
567 KUNIT_CASE(test_switch_1_none),
568 KUNIT_CASE(test_switch_2_none),
569 /* STRUCTLEAK_BYREF should cover from here down. */
570 KUNIT_test_structs(none),
571 /* STRUCTLEAK will only cover this. */
572 KUNIT_CASE(test_user),
573 {}
574};
575
576static struct kunit_suite stackinit_test_suite = {
577 .name = "stackinit",
578 .test_cases = stackinit_test_cases,
579};
580
581kunit_test_suites(&stackinit_test_suite);
582
583MODULE_DESCRIPTION("Test cases for compiler-based stack variable zeroing");
584MODULE_LICENSE("GPL");