blob: 70bcc1e5112cfcfb40f911548c6cf71e7ce74576 [file] [log] [blame]
Kees Cook1e2cd302021-07-23 15:19:32 -07001// SPDX-License-Identifier: GPL-2.0-or-later
Kees Cook50ceaa95e2019-01-23 12:24:32 -07002/*
Kees Cook1e2cd302021-07-23 15:19:32 -07003 * Test cases for compiler-based stack variable zeroing via
4 * -ftrivial-auto-var-init={zero,pattern} or CONFIG_GCC_PLUGIN_STRUCTLEAK*.
5 *
6 * External build example:
7 * clang -O2 -Wall -ftrivial-auto-var-init=pattern \
8 * -o test_stackinit test_stackinit.c
Kees Cook50ceaa95e2019-01-23 12:24:32 -07009 */
Kees Cook1e2cd302021-07-23 15:19:32 -070010#ifdef __KERNEL__
Kees Cook50ceaa95e2019-01-23 12:24:32 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/string.h>
17
Kees Cook1e2cd302021-07-23 15:19:32 -070018#else
19
20/* Userspace headers. */
21#include <stdio.h>
22#include <stdint.h>
23#include <string.h>
24#include <stdbool.h>
25#include <errno.h>
26#include <sys/types.h>
27
28/* Linux kernel-ism stubs for stand-alone userspace build. */
29#define KBUILD_MODNAME "stackinit"
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31#define pr_err(fmt, ...) fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__)
32#define pr_warn(fmt, ...) fprintf(stderr, pr_fmt(fmt), ##__VA_ARGS__)
33#define pr_info(fmt, ...) fprintf(stdout, pr_fmt(fmt), ##__VA_ARGS__)
34#define __init /**/
35#define __exit /**/
36#define __user /**/
37#define noinline __attribute__((__noinline__))
38#define __aligned(x) __attribute__((__aligned__(x)))
39#ifdef __clang__
40# define __compiletime_error(message) /**/
41#else
42# define __compiletime_error(message) __attribute__((__error__(message)))
43#endif
44#define __compiletime_assert(condition, msg, prefix, suffix) \
45 do { \
46 extern void prefix ## suffix(void) __compiletime_error(msg); \
47 if (!(condition)) \
48 prefix ## suffix(); \
49 } while (0)
50#define _compiletime_assert(condition, msg, prefix, suffix) \
51 __compiletime_assert(condition, msg, prefix, suffix)
52#define compiletime_assert(condition, msg) \
53 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
54#define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
55#define BUILD_BUG_ON(condition) \
56 BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
57typedef uint8_t u8;
58typedef uint16_t u16;
59typedef uint32_t u32;
60typedef uint64_t u64;
61
62#define module_init(func) static int (*do_init)(void) = func
63#define module_exit(func) static void (*do_exit)(void) = func
64#define MODULE_LICENSE(str) int main(void) { \
65 int rc; \
66 /* License: str */ \
67 rc = do_init(); \
68 if (rc == 0) \
69 do_exit(); \
70 return rc; \
71 }
72
73#endif /* __KERNEL__ */
74
Kees Cook50ceaa95e2019-01-23 12:24:32 -070075/* Exfiltration buffer. */
76#define MAX_VAR_SIZE 128
Kees Cook8c30d322019-06-04 22:13:59 -070077static u8 check_buf[MAX_VAR_SIZE];
Kees Cook50ceaa95e2019-01-23 12:24:32 -070078
79/* Character array to trigger stack protector in all functions. */
80#define VAR_BUFFER 32
81
82/* Volatile mask to convince compiler to copy memory with 0xff. */
83static volatile u8 forced_mask = 0xff;
84
85/* Location and size tracking to validate fill and test are colocated. */
86static void *fill_start, *target_start;
87static size_t fill_size, target_size;
88
89static bool range_contains(char *haystack_start, size_t haystack_size,
90 char *needle_start, size_t needle_size)
91{
92 if (needle_start >= haystack_start &&
93 needle_start + needle_size <= haystack_start + haystack_size)
94 return true;
95 return false;
96}
97
98#define DO_NOTHING_TYPE_SCALAR(var_type) var_type
99#define DO_NOTHING_TYPE_STRING(var_type) void
100#define DO_NOTHING_TYPE_STRUCT(var_type) void
101
102#define DO_NOTHING_RETURN_SCALAR(ptr) *(ptr)
103#define DO_NOTHING_RETURN_STRING(ptr) /**/
104#define DO_NOTHING_RETURN_STRUCT(ptr) /**/
105
106#define DO_NOTHING_CALL_SCALAR(var, name) \
107 (var) = do_nothing_ ## name(&(var))
108#define DO_NOTHING_CALL_STRING(var, name) \
109 do_nothing_ ## name(var)
110#define DO_NOTHING_CALL_STRUCT(var, name) \
111 do_nothing_ ## name(&(var))
112
113#define FETCH_ARG_SCALAR(var) &var
114#define FETCH_ARG_STRING(var) var
115#define FETCH_ARG_STRUCT(var) &var
116
117#define FILL_SIZE_STRING 16
118
119#define INIT_CLONE_SCALAR /**/
120#define INIT_CLONE_STRING [FILL_SIZE_STRING]
121#define INIT_CLONE_STRUCT /**/
122
123#define INIT_SCALAR_none /**/
124#define INIT_SCALAR_zero = 0
125
126#define INIT_STRING_none [FILL_SIZE_STRING] /**/
127#define INIT_STRING_zero [FILL_SIZE_STRING] = { }
128
129#define INIT_STRUCT_none /**/
130#define INIT_STRUCT_zero = { }
131#define INIT_STRUCT_static_partial = { .two = 0, }
Kees Cookf9398f12021-07-23 15:19:31 -0700132#define INIT_STRUCT_static_all = { .one = 0, \
133 .two = 0, \
134 .three = 0, \
135 .four = 0, \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700136 }
137#define INIT_STRUCT_dynamic_partial = { .two = arg->two, }
138#define INIT_STRUCT_dynamic_all = { .one = arg->one, \
139 .two = arg->two, \
140 .three = arg->three, \
141 .four = arg->four, \
142 }
143#define INIT_STRUCT_runtime_partial ; \
144 var.two = 0
145#define INIT_STRUCT_runtime_all ; \
146 var.one = 0; \
147 var.two = 0; \
148 var.three = 0; \
Kees Cookf9398f12021-07-23 15:19:31 -0700149 var.four = 0
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700150
151/*
152 * @name: unique string name for the test
153 * @var_type: type to be tested for zeroing initialization
154 * @which: is this a SCALAR, STRING, or STRUCT type?
155 * @init_level: what kind of initialization is performed
Kees Cook9cf016e2020-04-06 20:10:12 -0700156 * @xfail: is this test expected to fail?
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700157 */
Kees Cook9cf016e2020-04-06 20:10:12 -0700158#define DEFINE_TEST_DRIVER(name, var_type, which, xfail) \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700159/* Returns 0 on success, 1 on failure. */ \
160static noinline __init int test_ ## name (void) \
161{ \
162 var_type zero INIT_CLONE_ ## which; \
163 int ignored; \
164 u8 sum = 0, i; \
165 \
166 /* Notice when a new test is larger than expected. */ \
167 BUILD_BUG_ON(sizeof(zero) > MAX_VAR_SIZE); \
168 \
169 /* Fill clone type with zero for per-field init. */ \
170 memset(&zero, 0x00, sizeof(zero)); \
Kees Cook8c30d322019-06-04 22:13:59 -0700171 /* Clear entire check buffer for 0xFF overlap test. */ \
172 memset(check_buf, 0x00, sizeof(check_buf)); \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700173 /* Fill stack with 0xFF. */ \
174 ignored = leaf_ ##name((unsigned long)&ignored, 1, \
175 FETCH_ARG_ ## which(zero)); \
Kees Cook8c30d322019-06-04 22:13:59 -0700176 /* Verify all bytes overwritten with 0xFF. */ \
177 for (sum = 0, i = 0; i < target_size; i++) \
178 sum += (check_buf[i] != 0xFF); \
179 if (sum) { \
180 pr_err(#name ": leaf fill was not 0xFF!?\n"); \
181 return 1; \
182 } \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700183 /* Clear entire check buffer for later bit tests. */ \
184 memset(check_buf, 0x00, sizeof(check_buf)); \
185 /* Extract stack-defined variable contents. */ \
186 ignored = leaf_ ##name((unsigned long)&ignored, 0, \
187 FETCH_ARG_ ## which(zero)); \
188 \
189 /* Validate that compiler lined up fill and target. */ \
190 if (!range_contains(fill_start, fill_size, \
191 target_start, target_size)) { \
192 pr_err(#name ": stack fill missed target!?\n"); \
193 pr_err(#name ": fill %zu wide\n", fill_size); \
194 pr_err(#name ": target offset by %d\n", \
195 (int)((ssize_t)(uintptr_t)fill_start - \
196 (ssize_t)(uintptr_t)target_start)); \
197 return 1; \
198 } \
199 \
Kees Cook8c30d322019-06-04 22:13:59 -0700200 /* Look for any bytes still 0xFF in check region. */ \
201 for (sum = 0, i = 0; i < target_size; i++) \
202 sum += (check_buf[i] == 0xFF); \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700203 \
Kees Cook9cf016e2020-04-06 20:10:12 -0700204 if (sum == 0) { \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700205 pr_info(#name " ok\n"); \
Kees Cook9cf016e2020-04-06 20:10:12 -0700206 return 0; \
207 } else { \
208 pr_warn(#name " %sFAIL (uninit bytes: %d)\n", \
209 (xfail) ? "X" : "", sum); \
210 return (xfail) ? 0 : 1; \
211 } \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700212}
213#define DEFINE_TEST(name, var_type, which, init_level) \
214/* no-op to force compiler into ignoring "uninitialized" vars */\
215static noinline __init DO_NOTHING_TYPE_ ## which(var_type) \
216do_nothing_ ## name(var_type *ptr) \
217{ \
218 /* Will always be true, but compiler doesn't know. */ \
219 if ((unsigned long)ptr > 0x2) \
220 return DO_NOTHING_RETURN_ ## which(ptr); \
221 else \
222 return DO_NOTHING_RETURN_ ## which(ptr + 1); \
223} \
224static noinline __init int leaf_ ## name(unsigned long sp, \
225 bool fill, \
226 var_type *arg) \
227{ \
228 char buf[VAR_BUFFER]; \
229 var_type var INIT_ ## which ## _ ## init_level; \
230 \
231 target_start = &var; \
232 target_size = sizeof(var); \
233 /* \
234 * Keep this buffer around to make sure we've got a \
235 * stack frame of SOME kind... \
236 */ \
Kees Cook8c30d322019-06-04 22:13:59 -0700237 memset(buf, (char)(sp & 0xff), sizeof(buf)); \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700238 /* Fill variable with 0xFF. */ \
239 if (fill) { \
240 fill_start = &var; \
241 fill_size = sizeof(var); \
242 memset(fill_start, \
Kees Cook8c30d322019-06-04 22:13:59 -0700243 (char)((sp & 0xff) | forced_mask), \
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700244 fill_size); \
245 } \
246 \
247 /* Silence "never initialized" warnings. */ \
248 DO_NOTHING_CALL_ ## which(var, name); \
249 \
250 /* Exfiltrate "var". */ \
251 memcpy(check_buf, target_start, target_size); \
252 \
253 return (int)buf[0] | (int)buf[sizeof(buf) - 1]; \
254} \
Kees Cook9cf016e2020-04-06 20:10:12 -0700255DEFINE_TEST_DRIVER(name, var_type, which, 0)
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700256
257/* Structure with no padding. */
258struct test_packed {
259 unsigned long one;
260 unsigned long two;
261 unsigned long three;
262 unsigned long four;
263};
264
265/* Simple structure with padding likely to be covered by compiler. */
266struct test_small_hole {
267 size_t one;
268 char two;
269 /* 3 byte padding hole here. */
270 int three;
271 unsigned long four;
272};
273
Kees Cookf9398f12021-07-23 15:19:31 -0700274/* Trigger unhandled padding in a structure. */
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700275struct test_big_hole {
276 u8 one;
277 u8 two;
278 u8 three;
279 /* 61 byte padding hole here. */
Kees Cookf9398f12021-07-23 15:19:31 -0700280 u8 four __aligned(64);
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700281} __aligned(64);
282
283struct test_trailing_hole {
284 char *one;
285 char *two;
286 char *three;
287 char four;
288 /* "sizeof(unsigned long) - 1" byte padding hole here. */
289};
290
291/* Test if STRUCTLEAK is clearing structs with __user fields. */
292struct test_user {
293 u8 one;
294 unsigned long two;
295 char __user *three;
296 unsigned long four;
297};
298
299#define DEFINE_SCALAR_TEST(name, init) \
300 DEFINE_TEST(name ## _ ## init, name, SCALAR, init)
301
302#define DEFINE_SCALAR_TESTS(init) \
303 DEFINE_SCALAR_TEST(u8, init); \
304 DEFINE_SCALAR_TEST(u16, init); \
305 DEFINE_SCALAR_TEST(u32, init); \
306 DEFINE_SCALAR_TEST(u64, init); \
307 DEFINE_TEST(char_array_ ## init, unsigned char, STRING, init)
308
309#define DEFINE_STRUCT_TEST(name, init) \
310 DEFINE_TEST(name ## _ ## init, \
311 struct test_ ## name, STRUCT, init)
312
313#define DEFINE_STRUCT_TESTS(init) \
314 DEFINE_STRUCT_TEST(small_hole, init); \
315 DEFINE_STRUCT_TEST(big_hole, init); \
316 DEFINE_STRUCT_TEST(trailing_hole, init); \
317 DEFINE_STRUCT_TEST(packed, init)
318
319/* These should be fully initialized all the time! */
320DEFINE_SCALAR_TESTS(zero);
321DEFINE_STRUCT_TESTS(zero);
322/* Static initialization: padding may be left uninitialized. */
323DEFINE_STRUCT_TESTS(static_partial);
324DEFINE_STRUCT_TESTS(static_all);
325/* Dynamic initialization: padding may be left uninitialized. */
326DEFINE_STRUCT_TESTS(dynamic_partial);
327DEFINE_STRUCT_TESTS(dynamic_all);
328/* Runtime initialization: padding may be left uninitialized. */
329DEFINE_STRUCT_TESTS(runtime_partial);
330DEFINE_STRUCT_TESTS(runtime_all);
331/* No initialization without compiler instrumentation. */
332DEFINE_SCALAR_TESTS(none);
333DEFINE_STRUCT_TESTS(none);
334DEFINE_TEST(user, struct test_user, STRUCT, none);
335
336/*
337 * Check two uses through a variable declaration outside either path,
338 * which was noticed as a special case in porting earlier stack init
339 * compiler logic.
340 */
341static int noinline __leaf_switch_none(int path, bool fill)
342{
343 switch (path) {
Kees Cook1e2cd302021-07-23 15:19:32 -0700344 /*
345 * This is intentionally unreachable. To silence the
346 * warning, build with -Wno-switch-unreachable
347 */
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700348 uint64_t var;
349
350 case 1:
351 target_start = &var;
352 target_size = sizeof(var);
353 if (fill) {
354 fill_start = &var;
355 fill_size = sizeof(var);
356
357 memset(fill_start, forced_mask | 0x55, fill_size);
358 }
359 memcpy(check_buf, target_start, target_size);
360 break;
361 case 2:
362 target_start = &var;
363 target_size = sizeof(var);
364 if (fill) {
365 fill_start = &var;
366 fill_size = sizeof(var);
367
368 memset(fill_start, forced_mask | 0xaa, fill_size);
369 }
370 memcpy(check_buf, target_start, target_size);
371 break;
372 default:
373 var = 5;
374 return var & forced_mask;
375 }
376 return 0;
377}
378
379static noinline __init int leaf_switch_1_none(unsigned long sp, bool fill,
380 uint64_t *arg)
381{
382 return __leaf_switch_none(1, fill);
383}
384
385static noinline __init int leaf_switch_2_none(unsigned long sp, bool fill,
386 uint64_t *arg)
387{
388 return __leaf_switch_none(2, fill);
389}
390
Kees Cook9cf016e2020-04-06 20:10:12 -0700391/*
392 * These are expected to fail for most configurations because neither
393 * GCC nor Clang have a way to perform initialization of variables in
394 * non-code areas (i.e. in a switch statement before the first "case").
395 * https://bugs.llvm.org/show_bug.cgi?id=44916
396 */
397DEFINE_TEST_DRIVER(switch_1_none, uint64_t, SCALAR, 1);
398DEFINE_TEST_DRIVER(switch_2_none, uint64_t, SCALAR, 1);
Kees Cook50ceaa95e2019-01-23 12:24:32 -0700399
400static int __init test_stackinit_init(void)
401{
402 unsigned int failures = 0;
403
404#define test_scalars(init) do { \
405 failures += test_u8_ ## init (); \
406 failures += test_u16_ ## init (); \
407 failures += test_u32_ ## init (); \
408 failures += test_u64_ ## init (); \
409 failures += test_char_array_ ## init (); \
410 } while (0)
411
412#define test_structs(init) do { \
413 failures += test_small_hole_ ## init (); \
414 failures += test_big_hole_ ## init (); \
415 failures += test_trailing_hole_ ## init (); \
416 failures += test_packed_ ## init (); \
417 } while (0)
418
419 /* These are explicitly initialized and should always pass. */
420 test_scalars(zero);
421 test_structs(zero);
422 /* Padding here appears to be accidentally always initialized? */
423 test_structs(dynamic_partial);
424 /* Padding initialization depends on compiler behaviors. */
425 test_structs(static_partial);
426 test_structs(static_all);
427 test_structs(dynamic_all);
428 test_structs(runtime_partial);
429 test_structs(runtime_all);
430
431 /* STRUCTLEAK_BYREF_ALL should cover everything from here down. */
432 test_scalars(none);
433 failures += test_switch_1_none();
434 failures += test_switch_2_none();
435
436 /* STRUCTLEAK_BYREF should cover from here down. */
437 test_structs(none);
438
439 /* STRUCTLEAK will only cover this. */
440 failures += test_user();
441
442 if (failures == 0)
443 pr_info("all tests passed!\n");
444 else
445 pr_err("failures: %u\n", failures);
446
447 return failures ? -EINVAL : 0;
448}
449module_init(test_stackinit_init);
450
451static void __exit test_stackinit_exit(void)
452{ }
453module_exit(test_stackinit_exit);
454
455MODULE_LICENSE("GPL");