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