1 // SPDX-Licenses: GPLv2
3 * Test cases for compiler-based stack variable zeroing via future
4 * compiler flags or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/string.h>
13 /* Exfiltration buffer. */
14 #define MAX_VAR_SIZE 128
15 static char check_buf[MAX_VAR_SIZE];
17 /* Character array to trigger stack protector in all functions. */
20 /* Volatile mask to convince compiler to copy memory with 0xff. */
21 static volatile u8 forced_mask = 0xff;
23 /* Location and size tracking to validate fill and test are colocated. */
24 static void *fill_start, *target_start;
25 static size_t fill_size, target_size;
27 static bool range_contains(char *haystack_start, size_t haystack_size,
28 char *needle_start, size_t needle_size)
30 if (needle_start >= haystack_start &&
31 needle_start + needle_size <= haystack_start + haystack_size)
36 #define DO_NOTHING_TYPE_SCALAR(var_type) var_type
37 #define DO_NOTHING_TYPE_STRING(var_type) void
38 #define DO_NOTHING_TYPE_STRUCT(var_type) void
40 #define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
41 #define DO_NOTHING_RETURN_STRING(ptr) /**/
42 #define DO_NOTHING_RETURN_STRUCT(ptr) /**/
44 #define DO_NOTHING_CALL_SCALAR(var, name) \
45 (var) = do_nothing_ ## name(&(var))
46 #define DO_NOTHING_CALL_STRING(var, name) \
47 do_nothing_ ## name(var)
48 #define DO_NOTHING_CALL_STRUCT(var, name) \
49 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
55 #define FILL_SIZE_STRING 16
57 #define INIT_CLONE_SCALAR /**/
58 #define INIT_CLONE_STRING [FILL_SIZE_STRING]
59 #define INIT_CLONE_STRUCT /**/
61 #define INIT_SCALAR_none /**/
62 #define INIT_SCALAR_zero = 0
64 #define INIT_STRING_none [FILL_SIZE_STRING] /**/
65 #define INIT_STRING_zero [FILL_SIZE_STRING] = { }
67 #define INIT_STRUCT_none /**/
68 #define INIT_STRUCT_zero = { }
69 #define INIT_STRUCT_static_partial = { .two = 0, }
70 #define INIT_STRUCT_static_all = { .one = arg->one, \
72 .three = arg->three, \
75 #define INIT_STRUCT_dynamic_partial = { .two = arg->two, }
76 #define INIT_STRUCT_dynamic_all = { .one = arg->one, \
78 .three = arg->three, \
81 #define INIT_STRUCT_runtime_partial ; \
83 #define INIT_STRUCT_runtime_all ; \
87 memset(&var.four, 0, \
91 * @name: unique string name for the test
92 * @var_type: type to be tested for zeroing initialization
93 * @which: is this a SCALAR, STRING, or STRUCT type?
94 * @init_level: what kind of initialization is performed
96 #define DEFINE_TEST_DRIVER(name, var_type, which) \
97 /* Returns 0 on success, 1 on failure. */ \
98 static noinline __init int test_ ## name (void) \
100 var_type zero INIT_CLONE_ ## which; \
104 /* Notice when a new test is larger than expected. */ \
105 BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
107 /* Fill clone type with zero for per-field init. */ \
108 memset(&zero, 0x00, sizeof(zero)); \
109 /* Fill stack with 0xFF. */ \
110 ignored = leaf_ ##name((unsigned long)&ignored, 1, \
111 FETCH_ARG_ ## which(zero)); \
112 /* Clear entire check buffer for later bit tests. */ \
113 memset(check_buf, 0x00, sizeof(check_buf)); \
114 /* Extract stack-defined variable contents. */ \
115 ignored = leaf_ ##name((unsigned long)&ignored, 0, \
116 FETCH_ARG_ ## which(zero)); \
118 /* Validate that compiler lined up fill and target. */ \
119 if (!range_contains(fill_start, fill_size, \
120 target_start, target_size)) { \
121 pr_err(#name ": stack fill missed target!?\n"); \
122 pr_err(#name ": fill %zu wide\n", fill_size); \
123 pr_err(#name ": target offset by %d\n", \
124 (int)((ssize_t)(uintptr_t)fill_start - \
125 (ssize_t)(uintptr_t)target_start)); \
129 /* Look for any set bits in the check region. */ \
130 for (i = 0; i < sizeof(check_buf); i++) \
131 sum += (check_buf[i] != 0); \
134 pr_info(#name " ok\n"); \
136 pr_warn(#name " FAIL (uninit bytes: %d)\n", \
141 #define DEFINE_TEST(name, var_type, which, init_level) \
142 /* no-op to force compiler into ignoring "uninitialized" vars */\
143 static noinline __init DO_NOTHING_TYPE_ ## which(var_type) \
144 do_nothing_ ## name(var_type *ptr) \
146 /* Will always be true, but compiler doesn't know. */ \
147 if ((unsigned long)ptr > 0x2) \
148 return DO_NOTHING_RETURN_ ## which(ptr); \
150 return DO_NOTHING_RETURN_ ## which(ptr + 1); \
152 static noinline __init int leaf_ ## name(unsigned long sp, \
156 char buf[VAR_BUFFER]; \
157 var_type var INIT_ ## which ## _ ## init_level; \
159 target_start = &var; \
160 target_size = sizeof(var); \
162 * Keep this buffer around to make sure we've got a \
163 * stack frame of SOME kind... \
165 memset(buf, (char)(sp && 0xff), sizeof(buf)); \
166 /* Fill variable with 0xFF. */ \
169 fill_size = sizeof(var); \
171 (char)((sp && 0xff) | forced_mask), \
175 /* Silence "never initialized" warnings. */ \
176 DO_NOTHING_CALL_ ## which(var, name); \
178 /* Exfiltrate "var". */ \
179 memcpy(check_buf, target_start, target_size); \
181 return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
183 DEFINE_TEST_DRIVER(name, var_type, which)
185 /* Structure with no padding. */
193 /* Simple structure with padding likely to be covered by compiler. */
194 struct test_small_hole {
197 /* 3 byte padding hole here. */
202 /* Try to trigger unhandled padding in a structure. */
203 struct test_aligned {
208 struct test_big_hole {
212 /* 61 byte padding hole here. */
213 struct test_aligned four;
216 struct test_trailing_hole {
221 /* "sizeof(unsigned long) - 1" byte padding hole here. */
224 /* Test if STRUCTLEAK is clearing structs with __user fields. */
232 #define DEFINE_SCALAR_TEST(name, init) \
233 DEFINE_TEST(name ## _ ## init, name, SCALAR, init)
235 #define DEFINE_SCALAR_TESTS(init) \
236 DEFINE_SCALAR_TEST(u8, init); \
237 DEFINE_SCALAR_TEST(u16, init); \
238 DEFINE_SCALAR_TEST(u32, init); \
239 DEFINE_SCALAR_TEST(u64, init); \
240 DEFINE_TEST(char_array_ ## init, unsigned char, STRING, init)
242 #define DEFINE_STRUCT_TEST(name, init) \
243 DEFINE_TEST(name ## _ ## init, \
244 struct test_ ## name, STRUCT, init)
246 #define DEFINE_STRUCT_TESTS(init) \
247 DEFINE_STRUCT_TEST(small_hole, init); \
248 DEFINE_STRUCT_TEST(big_hole, init); \
249 DEFINE_STRUCT_TEST(trailing_hole, init); \
250 DEFINE_STRUCT_TEST(packed, init)
252 /* These should be fully initialized all the time! */
253 DEFINE_SCALAR_TESTS(zero);
254 DEFINE_STRUCT_TESTS(zero);
255 /* Static initialization: padding may be left uninitialized. */
256 DEFINE_STRUCT_TESTS(static_partial);
257 DEFINE_STRUCT_TESTS(static_all);
258 /* Dynamic initialization: padding may be left uninitialized. */
259 DEFINE_STRUCT_TESTS(dynamic_partial);
260 DEFINE_STRUCT_TESTS(dynamic_all);
261 /* Runtime initialization: padding may be left uninitialized. */
262 DEFINE_STRUCT_TESTS(runtime_partial);
263 DEFINE_STRUCT_TESTS(runtime_all);
264 /* No initialization without compiler instrumentation. */
265 DEFINE_SCALAR_TESTS(none);
266 DEFINE_STRUCT_TESTS(none);
267 DEFINE_TEST(user, struct test_user, STRUCT, none);
270 * Check two uses through a variable declaration outside either path,
271 * which was noticed as a special case in porting earlier stack init
274 static int noinline __leaf_switch_none(int path, bool fill)
281 target_size = sizeof(var);
284 fill_size = sizeof(var);
286 memset(fill_start, forced_mask | 0x55, fill_size);
288 memcpy(check_buf, target_start, target_size);
292 target_size = sizeof(var);
295 fill_size = sizeof(var);
297 memset(fill_start, forced_mask | 0xaa, fill_size);
299 memcpy(check_buf, target_start, target_size);
303 return var & forced_mask;
308 static noinline __init int leaf_switch_1_none(unsigned long sp, bool fill,
311 return __leaf_switch_none(1, fill);
314 static noinline __init int leaf_switch_2_none(unsigned long sp, bool fill,
317 return __leaf_switch_none(2, fill);
320 DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR);
321 DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR);
323 static int __init test_stackinit_init(void)
325 unsigned int failures = 0;
327 #define test_scalars(init) do { \
328 failures += test_u8_ ## init (); \
329 failures += test_u16_ ## init (); \
330 failures += test_u32_ ## init (); \
331 failures += test_u64_ ## init (); \
332 failures += test_char_array_ ## init (); \
335 #define test_structs(init) do { \
336 failures += test_small_hole_ ## init (); \
337 failures += test_big_hole_ ## init (); \
338 failures += test_trailing_hole_ ## init (); \
339 failures += test_packed_ ## init (); \
342 /* These are explicitly initialized and should always pass. */
345 /* Padding here appears to be accidentally always initialized? */
346 test_structs(dynamic_partial);
347 /* Padding initialization depends on compiler behaviors. */
348 test_structs(static_partial);
349 test_structs(static_all);
350 test_structs(dynamic_all);
351 test_structs(runtime_partial);
352 test_structs(runtime_all);
354 /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
356 failures += test_switch_1_none();
357 failures += test_switch_2_none();
359 /* STRUCTLEAK_BYREF should cover from here down. */
362 /* STRUCTLEAK will only cover this. */
363 failures += test_user();
366 pr_info("all tests passed!\n");
368 pr_err("failures: %u\n", failures);
370 return failures ? -EINVAL : 0;
372 module_init(test_stackinit_init);
374 static void __exit test_stackinit_exit(void)
376 module_exit(test_stackinit_exit);
378 MODULE_LICENSE("GPL");