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