xref: /openbmc/linux/drivers/misc/lkdtm/bugs.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
1039a1c42SKees Cook // SPDX-License-Identifier: GPL-2.0
2039a1c42SKees Cook /*
3039a1c42SKees Cook  * This is for all the tests related to logic bugs (e.g. bad dereferences,
4039a1c42SKees Cook  * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
5039a1c42SKees Cook  * lockups) along with other things that don't fit well into existing LKDTM
6039a1c42SKees Cook  * test source files.
7039a1c42SKees Cook  */
8039a1c42SKees Cook #include "lkdtm.h"
9039a1c42SKees Cook #include <linux/list.h>
10039a1c42SKees Cook #include <linux/sched.h>
11039a1c42SKees Cook #include <linux/sched/signal.h>
12039a1c42SKees Cook #include <linux/sched/task_stack.h>
13039a1c42SKees Cook #include <linux/uaccess.h>
14ae2e1aadSKees Cook #include <linux/slab.h>
15039a1c42SKees Cook 
16ae56942cSKees Cook #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
17b09511c2SAndy Lutomirski #include <asm/desc.h>
18b09511c2SAndy Lutomirski #endif
19b09511c2SAndy Lutomirski 
20039a1c42SKees Cook struct lkdtm_list {
21039a1c42SKees Cook 	struct list_head node;
22039a1c42SKees Cook };
23039a1c42SKees Cook 
24039a1c42SKees Cook /*
25039a1c42SKees Cook  * Make sure our attempts to over run the kernel stack doesn't trigger
26039a1c42SKees Cook  * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
27039a1c42SKees Cook  * recurse past the end of THREAD_SIZE by default.
28039a1c42SKees Cook  */
29039a1c42SKees Cook #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
30b9bc7b8bSRaul E Rangel #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
31039a1c42SKees Cook #else
32b4909252SJustin Stitt #define REC_STACK_SIZE (THREAD_SIZE / 8UL)
33039a1c42SKees Cook #endif
34039a1c42SKees Cook #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
35039a1c42SKees Cook 
36039a1c42SKees Cook static int recur_count = REC_NUM_DEFAULT;
37039a1c42SKees Cook 
38039a1c42SKees Cook static DEFINE_SPINLOCK(lock_me_up);
39039a1c42SKees Cook 
4024cccab4SKees Cook /*
4124cccab4SKees Cook  * Make sure compiler does not optimize this function or stack frame away:
4224cccab4SKees Cook  * - function marked noinline
4324cccab4SKees Cook  * - stack variables are marked volatile
44026c6fa1SArd Biesheuvel  * - stack variables are written (memset()) and read (buf[..] passed as arg)
45026c6fa1SArd Biesheuvel  * - function may have external effects (memzero_explicit())
46026c6fa1SArd Biesheuvel  * - no tail recursion possible
47026c6fa1SArd Biesheuvel  */
recursive_loop(int remaining)4824cccab4SKees Cook static int noinline recursive_loop(int remaining)
49039a1c42SKees Cook {
5024cccab4SKees Cook 	volatile char buf[REC_STACK_SIZE];
51026c6fa1SArd Biesheuvel 	volatile int ret;
52039a1c42SKees Cook 
5324cccab4SKees Cook 	memset((void *)buf, remaining & 0xFF, sizeof(buf));
54039a1c42SKees Cook 	if (!remaining)
55026c6fa1SArd Biesheuvel 		ret = 0;
56039a1c42SKees Cook 	else
57026c6fa1SArd Biesheuvel 		ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
58026c6fa1SArd Biesheuvel 	memzero_explicit((void *)buf, sizeof(buf));
59026c6fa1SArd Biesheuvel 	return ret;
60039a1c42SKees Cook }
61039a1c42SKees Cook 
62039a1c42SKees Cook /* If the depth is negative, use the default, otherwise keep parameter. */
lkdtm_bugs_init(int * recur_param)63039a1c42SKees Cook void __init lkdtm_bugs_init(int *recur_param)
64039a1c42SKees Cook {
65039a1c42SKees Cook 	if (*recur_param < 0)
66039a1c42SKees Cook 		*recur_param = recur_count;
67039a1c42SKees Cook 	else
68039a1c42SKees Cook 		recur_count = *recur_param;
69039a1c42SKees Cook }
70039a1c42SKees Cook 
lkdtm_PANIC(void)7173f62e60SKees Cook static void lkdtm_PANIC(void)
72039a1c42SKees Cook {
73039a1c42SKees Cook 	panic("dumptest");
74039a1c42SKees Cook }
75039a1c42SKees Cook 
lkdtm_BUG(void)7673f62e60SKees Cook static void lkdtm_BUG(void)
77039a1c42SKees Cook {
78039a1c42SKees Cook 	BUG();
79039a1c42SKees Cook }
80039a1c42SKees Cook 
81039a1c42SKees Cook static int warn_counter;
82039a1c42SKees Cook 
lkdtm_WARNING(void)8373f62e60SKees Cook static void lkdtm_WARNING(void)
84039a1c42SKees Cook {
851ee170eaSKees Cook 	WARN_ON(++warn_counter);
861ee170eaSKees Cook }
871ee170eaSKees Cook 
lkdtm_WARNING_MESSAGE(void)8873f62e60SKees Cook static void lkdtm_WARNING_MESSAGE(void)
891ee170eaSKees Cook {
901ee170eaSKees Cook 	WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
91039a1c42SKees Cook }
92039a1c42SKees Cook 
lkdtm_EXCEPTION(void)9373f62e60SKees Cook static void lkdtm_EXCEPTION(void)
94039a1c42SKees Cook {
95039a1c42SKees Cook 	*((volatile int *) 0) = 0;
96039a1c42SKees Cook }
97039a1c42SKees Cook 
lkdtm_LOOP(void)9873f62e60SKees Cook static void lkdtm_LOOP(void)
99039a1c42SKees Cook {
100039a1c42SKees Cook 	for (;;)
101039a1c42SKees Cook 		;
102039a1c42SKees Cook }
103039a1c42SKees Cook 
lkdtm_EXHAUST_STACK(void)10473f62e60SKees Cook static void lkdtm_EXHAUST_STACK(void)
105039a1c42SKees Cook {
106b9bc7b8bSRaul E Rangel 	pr_info("Calling function with %lu frame size to depth %d ...\n",
10724cccab4SKees Cook 		REC_STACK_SIZE, recur_count);
10824cccab4SKees Cook 	recursive_loop(recur_count);
10924cccab4SKees Cook 	pr_info("FAIL: survived without exhausting stack?!\n");
110039a1c42SKees Cook }
111039a1c42SKees Cook 
__lkdtm_CORRUPT_STACK(void * stack)112039a1c42SKees Cook static noinline void __lkdtm_CORRUPT_STACK(void *stack)
113039a1c42SKees Cook {
114039a1c42SKees Cook 	memset(stack, '\xff', 64);
115039a1c42SKees Cook }
116039a1c42SKees Cook 
117039a1c42SKees Cook /* This should trip the stack canary, not corrupt the return address. */
lkdtm_CORRUPT_STACK(void)11873f62e60SKees Cook static noinline void lkdtm_CORRUPT_STACK(void)
119039a1c42SKees Cook {
120039a1c42SKees Cook 	/* Use default char array length that triggers stack protection. */
121039a1c42SKees Cook 	char data[8] __aligned(sizeof(void *));
122039a1c42SKees Cook 
123464e86b4SKees Cook 	pr_info("Corrupting stack containing char array ...\n");
124464e86b4SKees Cook 	__lkdtm_CORRUPT_STACK((void *)&data);
125039a1c42SKees Cook }
126039a1c42SKees Cook 
127039a1c42SKees Cook /* Same as above but will only get a canary with -fstack-protector-strong */
lkdtm_CORRUPT_STACK_STRONG(void)12873f62e60SKees Cook static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
129039a1c42SKees Cook {
130039a1c42SKees Cook 	union {
131039a1c42SKees Cook 		unsigned short shorts[4];
132039a1c42SKees Cook 		unsigned long *ptr;
133039a1c42SKees Cook 	} data __aligned(sizeof(void *));
134039a1c42SKees Cook 
135464e86b4SKees Cook 	pr_info("Corrupting stack containing union ...\n");
136464e86b4SKees Cook 	__lkdtm_CORRUPT_STACK((void *)&data);
137039a1c42SKees Cook }
138039a1c42SKees Cook 
13968ef8735SKees Cook static pid_t stack_pid;
14068ef8735SKees Cook static unsigned long stack_addr;
14168ef8735SKees Cook 
lkdtm_REPORT_STACK(void)14273f62e60SKees Cook static void lkdtm_REPORT_STACK(void)
14368ef8735SKees Cook {
14468ef8735SKees Cook 	volatile uintptr_t magic;
14568ef8735SKees Cook 	pid_t pid = task_pid_nr(current);
14668ef8735SKees Cook 
14768ef8735SKees Cook 	if (pid != stack_pid) {
14868ef8735SKees Cook 		pr_info("Starting stack offset tracking for pid %d\n", pid);
14968ef8735SKees Cook 		stack_pid = pid;
15068ef8735SKees Cook 		stack_addr = (uintptr_t)&magic;
15168ef8735SKees Cook 	}
15268ef8735SKees Cook 
15368ef8735SKees Cook 	pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
15468ef8735SKees Cook }
15568ef8735SKees Cook 
156d46e58efSKees Cook static pid_t stack_canary_pid;
157d46e58efSKees Cook static unsigned long stack_canary;
158d46e58efSKees Cook static unsigned long stack_canary_offset;
159d46e58efSKees Cook 
__lkdtm_REPORT_STACK_CANARY(void * stack)160d46e58efSKees Cook static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
161d46e58efSKees Cook {
162d46e58efSKees Cook 	int i = 0;
163d46e58efSKees Cook 	pid_t pid = task_pid_nr(current);
164d46e58efSKees Cook 	unsigned long *canary = (unsigned long *)stack;
165d46e58efSKees Cook 	unsigned long current_offset = 0, init_offset = 0;
166d46e58efSKees Cook 
167d46e58efSKees Cook 	/* Do our best to find the canary in a 16 word window ... */
168d46e58efSKees Cook 	for (i = 1; i < 16; i++) {
169d46e58efSKees Cook 		canary = (unsigned long *)stack + i;
170d46e58efSKees Cook #ifdef CONFIG_STACKPROTECTOR
171d46e58efSKees Cook 		if (*canary == current->stack_canary)
172d46e58efSKees Cook 			current_offset = i;
173d46e58efSKees Cook 		if (*canary == init_task.stack_canary)
174d46e58efSKees Cook 			init_offset = i;
175d46e58efSKees Cook #endif
176d46e58efSKees Cook 	}
177d46e58efSKees Cook 
178d46e58efSKees Cook 	if (current_offset == 0) {
179d46e58efSKees Cook 		/*
180d46e58efSKees Cook 		 * If the canary doesn't match what's in the task_struct,
181d46e58efSKees Cook 		 * we're either using a global canary or the stack frame
182d46e58efSKees Cook 		 * layout changed.
183d46e58efSKees Cook 		 */
184d46e58efSKees Cook 		if (init_offset != 0) {
185d46e58efSKees Cook 			pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
186d46e58efSKees Cook 			       init_offset, pid);
187d46e58efSKees Cook 		} else {
188d46e58efSKees Cook 			pr_warn("FAIL: did not correctly locate stack canary :(\n");
189d46e58efSKees Cook 			pr_expected_config(CONFIG_STACKPROTECTOR);
190d46e58efSKees Cook 		}
191d46e58efSKees Cook 
192d46e58efSKees Cook 		return;
193d46e58efSKees Cook 	} else if (init_offset != 0) {
194d46e58efSKees Cook 		pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
195d46e58efSKees Cook 	}
196d46e58efSKees Cook 
197d46e58efSKees Cook 	canary = (unsigned long *)stack + current_offset;
198d46e58efSKees Cook 	if (stack_canary_pid == 0) {
199d46e58efSKees Cook 		stack_canary = *canary;
200d46e58efSKees Cook 		stack_canary_pid = pid;
201d46e58efSKees Cook 		stack_canary_offset = current_offset;
202d46e58efSKees Cook 		pr_info("Recorded stack canary for pid %d at offset %ld\n",
203d46e58efSKees Cook 			stack_canary_pid, stack_canary_offset);
204d46e58efSKees Cook 	} else if (pid == stack_canary_pid) {
205d46e58efSKees Cook 		pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
206d46e58efSKees Cook 	} else {
207d46e58efSKees Cook 		if (current_offset != stack_canary_offset) {
208d46e58efSKees Cook 			pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
209d46e58efSKees Cook 				stack_canary_offset, current_offset);
210d46e58efSKees Cook 			return;
211d46e58efSKees Cook 		}
212d46e58efSKees Cook 
213d46e58efSKees Cook 		if (*canary == stack_canary) {
214d46e58efSKees Cook 			pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
215d46e58efSKees Cook 				stack_canary_pid, pid, current_offset);
216d46e58efSKees Cook 		} else {
217d46e58efSKees Cook 			pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
218d46e58efSKees Cook 				stack_canary_pid, pid, current_offset);
219d46e58efSKees Cook 			/* Reset the test. */
220d46e58efSKees Cook 			stack_canary_pid = 0;
221d46e58efSKees Cook 		}
222d46e58efSKees Cook 	}
223d46e58efSKees Cook }
224d46e58efSKees Cook 
lkdtm_REPORT_STACK_CANARY(void)22573f62e60SKees Cook static void lkdtm_REPORT_STACK_CANARY(void)
226d46e58efSKees Cook {
227d46e58efSKees Cook 	/* Use default char array length that triggers stack protection. */
228d46e58efSKees Cook 	char data[8] __aligned(sizeof(void *)) = { };
229d46e58efSKees Cook 
230d46e58efSKees Cook 	__lkdtm_REPORT_STACK_CANARY((void *)&data);
231d46e58efSKees Cook }
232d46e58efSKees Cook 
lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)23373f62e60SKees Cook static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
234039a1c42SKees Cook {
235039a1c42SKees Cook 	static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
236039a1c42SKees Cook 	u32 *p;
237039a1c42SKees Cook 	u32 val = 0x12345678;
238039a1c42SKees Cook 
239039a1c42SKees Cook 	p = (u32 *)(data + 1);
240039a1c42SKees Cook 	if (*p == 0)
241039a1c42SKees Cook 		val = 0x87654321;
242039a1c42SKees Cook 	*p = val;
243a15676acSKees Cook 
244a15676acSKees Cook 	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
245a15676acSKees Cook 		pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
246039a1c42SKees Cook }
247039a1c42SKees Cook 
lkdtm_SOFTLOCKUP(void)24873f62e60SKees Cook static void lkdtm_SOFTLOCKUP(void)
249039a1c42SKees Cook {
250039a1c42SKees Cook 	preempt_disable();
251039a1c42SKees Cook 	for (;;)
252039a1c42SKees Cook 		cpu_relax();
253039a1c42SKees Cook }
254039a1c42SKees Cook 
lkdtm_HARDLOCKUP(void)25573f62e60SKees Cook static void lkdtm_HARDLOCKUP(void)
256039a1c42SKees Cook {
257039a1c42SKees Cook 	local_irq_disable();
258039a1c42SKees Cook 	for (;;)
259039a1c42SKees Cook 		cpu_relax();
260039a1c42SKees Cook }
261039a1c42SKees Cook 
lkdtm_SPINLOCKUP(void)26273f62e60SKees Cook static void lkdtm_SPINLOCKUP(void)
263039a1c42SKees Cook {
264039a1c42SKees Cook 	/* Must be called twice to trigger. */
265039a1c42SKees Cook 	spin_lock(&lock_me_up);
266039a1c42SKees Cook 	/* Let sparse know we intended to exit holding the lock. */
267039a1c42SKees Cook 	__release(&lock_me_up);
268039a1c42SKees Cook }
269039a1c42SKees Cook 
lkdtm_HUNG_TASK(void)27073f62e60SKees Cook static void lkdtm_HUNG_TASK(void)
271039a1c42SKees Cook {
272039a1c42SKees Cook 	set_current_state(TASK_UNINTERRUPTIBLE);
273039a1c42SKees Cook 	schedule();
274039a1c42SKees Cook }
275039a1c42SKees Cook 
2765d207e83SKees Cook static volatile unsigned int huge = INT_MAX - 2;
2775d207e83SKees Cook static volatile unsigned int ignored;
278ae2e1aadSKees Cook 
lkdtm_OVERFLOW_SIGNED(void)27973f62e60SKees Cook static void lkdtm_OVERFLOW_SIGNED(void)
280ae2e1aadSKees Cook {
281ae2e1aadSKees Cook 	int value;
282ae2e1aadSKees Cook 
283ae2e1aadSKees Cook 	value = huge;
284ae2e1aadSKees Cook 	pr_info("Normal signed addition ...\n");
285ae2e1aadSKees Cook 	value += 1;
286ae2e1aadSKees Cook 	ignored = value;
287ae2e1aadSKees Cook 
288ae2e1aadSKees Cook 	pr_info("Overflowing signed addition ...\n");
289ae2e1aadSKees Cook 	value += 4;
290ae2e1aadSKees Cook 	ignored = value;
291ae2e1aadSKees Cook }
292ae2e1aadSKees Cook 
293ae2e1aadSKees Cook 
lkdtm_OVERFLOW_UNSIGNED(void)29473f62e60SKees Cook static void lkdtm_OVERFLOW_UNSIGNED(void)
295ae2e1aadSKees Cook {
296ae2e1aadSKees Cook 	unsigned int value;
297ae2e1aadSKees Cook 
298ae2e1aadSKees Cook 	value = huge;
299ae2e1aadSKees Cook 	pr_info("Normal unsigned addition ...\n");
300ae2e1aadSKees Cook 	value += 1;
301ae2e1aadSKees Cook 	ignored = value;
302ae2e1aadSKees Cook 
303ae2e1aadSKees Cook 	pr_info("Overflowing unsigned addition ...\n");
304ae2e1aadSKees Cook 	value += 4;
305ae2e1aadSKees Cook 	ignored = value;
306ae2e1aadSKees Cook }
307ae2e1aadSKees Cook 
3085d207e83SKees Cook /* Intentionally using unannotated flex array definition. */
309ae2e1aadSKees Cook struct array_bounds_flex_array {
310ae2e1aadSKees Cook 	int one;
311ae2e1aadSKees Cook 	int two;
312b45861edSKees Cook 	char data[];
313ae2e1aadSKees Cook };
314ae2e1aadSKees Cook 
315ae2e1aadSKees Cook struct array_bounds {
316ae2e1aadSKees Cook 	int one;
317ae2e1aadSKees Cook 	int two;
318ae2e1aadSKees Cook 	char data[8];
319ae2e1aadSKees Cook 	int three;
320ae2e1aadSKees Cook };
321ae2e1aadSKees Cook 
lkdtm_ARRAY_BOUNDS(void)32273f62e60SKees Cook static void lkdtm_ARRAY_BOUNDS(void)
323ae2e1aadSKees Cook {
324ae2e1aadSKees Cook 	struct array_bounds_flex_array *not_checked;
325ae2e1aadSKees Cook 	struct array_bounds *checked;
326ae2e1aadSKees Cook 	volatile int i;
327ae2e1aadSKees Cook 
328ae2e1aadSKees Cook 	not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
329ae2e1aadSKees Cook 	checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
3304a9800c8SJiasheng Jiang 	if (!not_checked || !checked) {
3314a9800c8SJiasheng Jiang 		kfree(not_checked);
3324a9800c8SJiasheng Jiang 		kfree(checked);
3334a9800c8SJiasheng Jiang 		return;
3344a9800c8SJiasheng Jiang 	}
335ae2e1aadSKees Cook 
336ae2e1aadSKees Cook 	pr_info("Array access within bounds ...\n");
337ae2e1aadSKees Cook 	/* For both, touch all bytes in the actual member size. */
338ae2e1aadSKees Cook 	for (i = 0; i < sizeof(checked->data); i++)
339ae2e1aadSKees Cook 		checked->data[i] = 'A';
340ae2e1aadSKees Cook 	/*
341ae2e1aadSKees Cook 	 * For the uninstrumented flex array member, also touch 1 byte
342ae2e1aadSKees Cook 	 * beyond to verify it is correctly uninstrumented.
343ae2e1aadSKees Cook 	 */
344b45861edSKees Cook 	for (i = 0; i < 2; i++)
345ae2e1aadSKees Cook 		not_checked->data[i] = 'A';
346ae2e1aadSKees Cook 
347ae2e1aadSKees Cook 	pr_info("Array access beyond bounds ...\n");
348ae2e1aadSKees Cook 	for (i = 0; i < sizeof(checked->data) + 1; i++)
349ae2e1aadSKees Cook 		checked->data[i] = 'B';
350ae2e1aadSKees Cook 
351ae2e1aadSKees Cook 	kfree(not_checked);
352ae2e1aadSKees Cook 	kfree(checked);
353464e86b4SKees Cook 	pr_err("FAIL: survived array bounds overflow!\n");
3548bfdbdddSChristophe Leroy 	if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
3558bfdbdddSChristophe Leroy 		pr_expected_config(CONFIG_UBSAN_TRAP);
3568bfdbdddSChristophe Leroy 	else
357c75be56eSKees Cook 		pr_expected_config(CONFIG_UBSAN_BOUNDS);
358ae2e1aadSKees Cook }
359ae2e1aadSKees Cook 
3605d207e83SKees Cook struct lkdtm_annotated {
3615d207e83SKees Cook 	unsigned long flags;
3625d207e83SKees Cook 	int count;
3635d207e83SKees Cook 	int array[] __counted_by(count);
3645d207e83SKees Cook };
3655d207e83SKees Cook 
3665d207e83SKees Cook static volatile int fam_count = 4;
3675d207e83SKees Cook 
lkdtm_FAM_BOUNDS(void)3685d207e83SKees Cook static void lkdtm_FAM_BOUNDS(void)
3695d207e83SKees Cook {
3705d207e83SKees Cook 	struct lkdtm_annotated *inst;
3715d207e83SKees Cook 
3725d207e83SKees Cook 	inst = kzalloc(struct_size(inst, array, fam_count + 1), GFP_KERNEL);
3735d207e83SKees Cook 	if (!inst) {
3745d207e83SKees Cook 		pr_err("FAIL: could not allocate test struct!\n");
3755d207e83SKees Cook 		return;
3765d207e83SKees Cook 	}
3775d207e83SKees Cook 
3785d207e83SKees Cook 	inst->count = fam_count;
3795d207e83SKees Cook 	pr_info("Array access within bounds ...\n");
3805d207e83SKees Cook 	inst->array[1] = fam_count;
3815d207e83SKees Cook 	ignored = inst->array[1];
3825d207e83SKees Cook 
3835d207e83SKees Cook 	pr_info("Array access beyond bounds ...\n");
3845d207e83SKees Cook 	inst->array[fam_count] = fam_count;
3855d207e83SKees Cook 	ignored = inst->array[fam_count];
3865d207e83SKees Cook 
3875d207e83SKees Cook 	kfree(inst);
3885d207e83SKees Cook 
3895d207e83SKees Cook 	pr_err("FAIL: survived access of invalid flexible array member index!\n");
3905d207e83SKees Cook 
391*5540869aSJan Hendrik Farr 	if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
392*5540869aSJan Hendrik Farr 		pr_warn("This is expected since this %s was built with a compiler that does not support __counted_by\n",
3935d207e83SKees Cook 			lkdtm_kernel_info);
3945d207e83SKees Cook 	else if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
3955d207e83SKees Cook 		pr_expected_config(CONFIG_UBSAN_TRAP);
3965d207e83SKees Cook 	else
3975d207e83SKees Cook 		pr_expected_config(CONFIG_UBSAN_BOUNDS);
3985d207e83SKees Cook }
3995d207e83SKees Cook 
lkdtm_CORRUPT_LIST_ADD(void)40073f62e60SKees Cook static void lkdtm_CORRUPT_LIST_ADD(void)
401039a1c42SKees Cook {
402039a1c42SKees Cook 	/*
403039a1c42SKees Cook 	 * Initially, an empty list via LIST_HEAD:
404039a1c42SKees Cook 	 *	test_head.next = &test_head
405039a1c42SKees Cook 	 *	test_head.prev = &test_head
406039a1c42SKees Cook 	 */
407039a1c42SKees Cook 	LIST_HEAD(test_head);
408039a1c42SKees Cook 	struct lkdtm_list good, bad;
409039a1c42SKees Cook 	void *target[2] = { };
410039a1c42SKees Cook 	void *redirection = &target;
411039a1c42SKees Cook 
412039a1c42SKees Cook 	pr_info("attempting good list addition\n");
413039a1c42SKees Cook 
414039a1c42SKees Cook 	/*
415039a1c42SKees Cook 	 * Adding to the list performs these actions:
416039a1c42SKees Cook 	 *	test_head.next->prev = &good.node
417039a1c42SKees Cook 	 *	good.node.next = test_head.next
418039a1c42SKees Cook 	 *	good.node.prev = test_head
419039a1c42SKees Cook 	 *	test_head.next = good.node
420039a1c42SKees Cook 	 */
421039a1c42SKees Cook 	list_add(&good.node, &test_head);
422039a1c42SKees Cook 
423039a1c42SKees Cook 	pr_info("attempting corrupted list addition\n");
424039a1c42SKees Cook 	/*
425039a1c42SKees Cook 	 * In simulating this "write what where" primitive, the "what" is
426039a1c42SKees Cook 	 * the address of &bad.node, and the "where" is the address held
427039a1c42SKees Cook 	 * by "redirection".
428039a1c42SKees Cook 	 */
429039a1c42SKees Cook 	test_head.next = redirection;
430039a1c42SKees Cook 	list_add(&bad.node, &test_head);
431039a1c42SKees Cook 
432039a1c42SKees Cook 	if (target[0] == NULL && target[1] == NULL)
433039a1c42SKees Cook 		pr_err("Overwrite did not happen, but no BUG?!\n");
4345b777131SKees Cook 	else {
435039a1c42SKees Cook 		pr_err("list_add() corruption not detected!\n");
436aebc7b0dSMarco Elver 		pr_expected_config(CONFIG_LIST_HARDENED);
4375b777131SKees Cook 	}
438039a1c42SKees Cook }
439039a1c42SKees Cook 
lkdtm_CORRUPT_LIST_DEL(void)44073f62e60SKees Cook static void lkdtm_CORRUPT_LIST_DEL(void)
441039a1c42SKees Cook {
442039a1c42SKees Cook 	LIST_HEAD(test_head);
443039a1c42SKees Cook 	struct lkdtm_list item;
444039a1c42SKees Cook 	void *target[2] = { };
445039a1c42SKees Cook 	void *redirection = &target;
446039a1c42SKees Cook 
447039a1c42SKees Cook 	list_add(&item.node, &test_head);
448039a1c42SKees Cook 
449039a1c42SKees Cook 	pr_info("attempting good list removal\n");
450039a1c42SKees Cook 	list_del(&item.node);
451039a1c42SKees Cook 
452039a1c42SKees Cook 	pr_info("attempting corrupted list removal\n");
453039a1c42SKees Cook 	list_add(&item.node, &test_head);
454039a1c42SKees Cook 
455039a1c42SKees Cook 	/* As with the list_add() test above, this corrupts "next". */
456039a1c42SKees Cook 	item.node.next = redirection;
457039a1c42SKees Cook 	list_del(&item.node);
458039a1c42SKees Cook 
459039a1c42SKees Cook 	if (target[0] == NULL && target[1] == NULL)
460039a1c42SKees Cook 		pr_err("Overwrite did not happen, but no BUG?!\n");
4615b777131SKees Cook 	else {
462039a1c42SKees Cook 		pr_err("list_del() corruption not detected!\n");
463aebc7b0dSMarco Elver 		pr_expected_config(CONFIG_LIST_HARDENED);
4645b777131SKees Cook 	}
465039a1c42SKees Cook }
466039a1c42SKees Cook 
467039a1c42SKees Cook /* Test that VMAP_STACK is actually allocating with a leading guard page */
lkdtm_STACK_GUARD_PAGE_LEADING(void)46873f62e60SKees Cook static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
469039a1c42SKees Cook {
470039a1c42SKees Cook 	const unsigned char *stack = task_stack_page(current);
471039a1c42SKees Cook 	const unsigned char *ptr = stack - 1;
472039a1c42SKees Cook 	volatile unsigned char byte;
473039a1c42SKees Cook 
474039a1c42SKees Cook 	pr_info("attempting bad read from page below current stack\n");
475039a1c42SKees Cook 
476039a1c42SKees Cook 	byte = *ptr;
477039a1c42SKees Cook 
478f049c545SLee Jones 	pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
479039a1c42SKees Cook }
480039a1c42SKees Cook 
481039a1c42SKees Cook /* Test that VMAP_STACK is actually allocating with a trailing guard page */
lkdtm_STACK_GUARD_PAGE_TRAILING(void)48273f62e60SKees Cook static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
483039a1c42SKees Cook {
484039a1c42SKees Cook 	const unsigned char *stack = task_stack_page(current);
485039a1c42SKees Cook 	const unsigned char *ptr = stack + THREAD_SIZE;
486039a1c42SKees Cook 	volatile unsigned char byte;
487039a1c42SKees Cook 
488039a1c42SKees Cook 	pr_info("attempting bad read from page above current stack\n");
489039a1c42SKees Cook 
490039a1c42SKees Cook 	byte = *ptr;
491039a1c42SKees Cook 
492f049c545SLee Jones 	pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
493039a1c42SKees Cook }
49406b32fdbSKees Cook 
lkdtm_UNSET_SMEP(void)49573f62e60SKees Cook static void lkdtm_UNSET_SMEP(void)
49606b32fdbSKees Cook {
4970e31e357SBrendan Higgins #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
49806b32fdbSKees Cook #define MOV_CR4_DEPTH	64
49906b32fdbSKees Cook 	void (*direct_write_cr4)(unsigned long val);
50006b32fdbSKees Cook 	unsigned char *insn;
50106b32fdbSKees Cook 	unsigned long cr4;
50206b32fdbSKees Cook 	int i;
50306b32fdbSKees Cook 
50406b32fdbSKees Cook 	cr4 = native_read_cr4();
50506b32fdbSKees Cook 
50606b32fdbSKees Cook 	if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
50706b32fdbSKees Cook 		pr_err("FAIL: SMEP not in use\n");
50806b32fdbSKees Cook 		return;
50906b32fdbSKees Cook 	}
51006b32fdbSKees Cook 	cr4 &= ~(X86_CR4_SMEP);
51106b32fdbSKees Cook 
51206b32fdbSKees Cook 	pr_info("trying to clear SMEP normally\n");
51306b32fdbSKees Cook 	native_write_cr4(cr4);
51406b32fdbSKees Cook 	if (cr4 == native_read_cr4()) {
51506b32fdbSKees Cook 		pr_err("FAIL: pinning SMEP failed!\n");
51606b32fdbSKees Cook 		cr4 |= X86_CR4_SMEP;
51706b32fdbSKees Cook 		pr_info("restoring SMEP\n");
51806b32fdbSKees Cook 		native_write_cr4(cr4);
51906b32fdbSKees Cook 		return;
52006b32fdbSKees Cook 	}
52106b32fdbSKees Cook 	pr_info("ok: SMEP did not get cleared\n");
52206b32fdbSKees Cook 
52306b32fdbSKees Cook 	/*
52406b32fdbSKees Cook 	 * To test the post-write pinning verification we need to call
52506b32fdbSKees Cook 	 * directly into the middle of native_write_cr4() where the
52606b32fdbSKees Cook 	 * cr4 write happens, skipping any pinning. This searches for
52706b32fdbSKees Cook 	 * the cr4 writing instruction.
52806b32fdbSKees Cook 	 */
52906b32fdbSKees Cook 	insn = (unsigned char *)native_write_cr4;
5304a03aa34SPeter Zijlstra 	OPTIMIZER_HIDE_VAR(insn);
53106b32fdbSKees Cook 	for (i = 0; i < MOV_CR4_DEPTH; i++) {
53206b32fdbSKees Cook 		/* mov %rdi, %cr4 */
53306b32fdbSKees Cook 		if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
53406b32fdbSKees Cook 			break;
53506b32fdbSKees Cook 		/* mov %rdi,%rax; mov %rax, %cr4 */
53606b32fdbSKees Cook 		if (insn[i]   == 0x48 && insn[i+1] == 0x89 &&
53706b32fdbSKees Cook 		    insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
53806b32fdbSKees Cook 		    insn[i+4] == 0x22 && insn[i+5] == 0xe0)
53906b32fdbSKees Cook 			break;
54006b32fdbSKees Cook 	}
54106b32fdbSKees Cook 	if (i >= MOV_CR4_DEPTH) {
54206b32fdbSKees Cook 		pr_info("ok: cannot locate cr4 writing call gadget\n");
54306b32fdbSKees Cook 		return;
54406b32fdbSKees Cook 	}
54506b32fdbSKees Cook 	direct_write_cr4 = (void *)(insn + i);
54606b32fdbSKees Cook 
54706b32fdbSKees Cook 	pr_info("trying to clear SMEP with call gadget\n");
54806b32fdbSKees Cook 	direct_write_cr4(cr4);
54906b32fdbSKees Cook 	if (native_read_cr4() & X86_CR4_SMEP) {
55006b32fdbSKees Cook 		pr_info("ok: SMEP removal was reverted\n");
55106b32fdbSKees Cook 	} else {
55206b32fdbSKees Cook 		pr_err("FAIL: cleared SMEP not detected!\n");
55306b32fdbSKees Cook 		cr4 |= X86_CR4_SMEP;
55406b32fdbSKees Cook 		pr_info("restoring SMEP\n");
55506b32fdbSKees Cook 		native_write_cr4(cr4);
55606b32fdbSKees Cook 	}
55706b32fdbSKees Cook #else
558cea23efbSKees Cook 	pr_err("XFAIL: this test is x86_64-only\n");
55906b32fdbSKees Cook #endif
56006b32fdbSKees Cook }
561b09511c2SAndy Lutomirski 
lkdtm_DOUBLE_FAULT(void)56273f62e60SKees Cook static void lkdtm_DOUBLE_FAULT(void)
563b09511c2SAndy Lutomirski {
564ae56942cSKees Cook #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
565b09511c2SAndy Lutomirski 	/*
566b09511c2SAndy Lutomirski 	 * Trigger #DF by setting the stack limit to zero.  This clobbers
567b09511c2SAndy Lutomirski 	 * a GDT TLS slot, which is okay because the current task will die
568b09511c2SAndy Lutomirski 	 * anyway due to the double fault.
569b09511c2SAndy Lutomirski 	 */
570b09511c2SAndy Lutomirski 	struct desc_struct d = {
571b09511c2SAndy Lutomirski 		.type = 3,	/* expand-up, writable, accessed data */
572b09511c2SAndy Lutomirski 		.p = 1,		/* present */
573b09511c2SAndy Lutomirski 		.d = 1,		/* 32-bit */
574b09511c2SAndy Lutomirski 		.g = 0,		/* limit in bytes */
575b09511c2SAndy Lutomirski 		.s = 1,		/* not system */
576b09511c2SAndy Lutomirski 	};
577b09511c2SAndy Lutomirski 
578b09511c2SAndy Lutomirski 	local_irq_disable();
579b09511c2SAndy Lutomirski 	write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
580b09511c2SAndy Lutomirski 			GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
581b09511c2SAndy Lutomirski 
582b09511c2SAndy Lutomirski 	/*
583b09511c2SAndy Lutomirski 	 * Put our zero-limit segment in SS and then trigger a fault.  The
584b09511c2SAndy Lutomirski 	 * 4-byte access to (%esp) will fault with #SS, and the attempt to
585b09511c2SAndy Lutomirski 	 * deliver the fault will recursively cause #SS and result in #DF.
586b09511c2SAndy Lutomirski 	 * This whole process happens while NMIs and MCEs are blocked by the
587b09511c2SAndy Lutomirski 	 * MOV SS window.  This is nice because an NMI with an invalid SS
588b09511c2SAndy Lutomirski 	 * would also double-fault, resulting in the NMI or MCE being lost.
589b09511c2SAndy Lutomirski 	 */
590b09511c2SAndy Lutomirski 	asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
591b09511c2SAndy Lutomirski 		      "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
592b09511c2SAndy Lutomirski 
593cea23efbSKees Cook 	pr_err("FAIL: tried to double fault but didn't die\n");
594cea23efbSKees Cook #else
595cea23efbSKees Cook 	pr_err("XFAIL: this test is ia32-only\n");
596b09511c2SAndy Lutomirski #endif
597cea23efbSKees Cook }
5986cb6982fSAmit Daniel Kachhap 
599ae56942cSKees Cook #ifdef CONFIG_ARM64
change_pac_parameters(void)6006cb6982fSAmit Daniel Kachhap static noinline void change_pac_parameters(void)
6016cb6982fSAmit Daniel Kachhap {
602b27a9f41SDaniel Kiss 	if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
6036cb6982fSAmit Daniel Kachhap 		/* Reset the keys of current task */
6046cb6982fSAmit Daniel Kachhap 		ptrauth_thread_init_kernel(current);
6056cb6982fSAmit Daniel Kachhap 		ptrauth_thread_switch_kernel(current);
6066cb6982fSAmit Daniel Kachhap 	}
607ae56942cSKees Cook }
608ae56942cSKees Cook #endif
6096cb6982fSAmit Daniel Kachhap 
lkdtm_CORRUPT_PAC(void)61073f62e60SKees Cook static noinline void lkdtm_CORRUPT_PAC(void)
6116cb6982fSAmit Daniel Kachhap {
612ae56942cSKees Cook #ifdef CONFIG_ARM64
613ae56942cSKees Cook #define CORRUPT_PAC_ITERATE	10
6146cb6982fSAmit Daniel Kachhap 	int i;
6156cb6982fSAmit Daniel Kachhap 
616b27a9f41SDaniel Kiss 	if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
617b27a9f41SDaniel Kiss 		pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
618ae56942cSKees Cook 
6196cb6982fSAmit Daniel Kachhap 	if (!system_supports_address_auth()) {
620ae56942cSKees Cook 		pr_err("FAIL: CPU lacks pointer authentication feature\n");
6216cb6982fSAmit Daniel Kachhap 		return;
6226cb6982fSAmit Daniel Kachhap 	}
6236cb6982fSAmit Daniel Kachhap 
624ae56942cSKees Cook 	pr_info("changing PAC parameters to force function return failure...\n");
6256cb6982fSAmit Daniel Kachhap 	/*
626ae56942cSKees Cook 	 * PAC is a hash value computed from input keys, return address and
6276cb6982fSAmit Daniel Kachhap 	 * stack pointer. As pac has fewer bits so there is a chance of
6286cb6982fSAmit Daniel Kachhap 	 * collision, so iterate few times to reduce the collision probability.
6296cb6982fSAmit Daniel Kachhap 	 */
6306cb6982fSAmit Daniel Kachhap 	for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
6316cb6982fSAmit Daniel Kachhap 		change_pac_parameters();
6326cb6982fSAmit Daniel Kachhap 
633ae56942cSKees Cook 	pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
634ae56942cSKees Cook #else
635ae56942cSKees Cook 	pr_err("XFAIL: this test is arm64-only\n");
6366cb6982fSAmit Daniel Kachhap #endif
637ae56942cSKees Cook }
63873f62e60SKees Cook 
63973f62e60SKees Cook static struct crashtype crashtypes[] = {
64073f62e60SKees Cook 	CRASHTYPE(PANIC),
64173f62e60SKees Cook 	CRASHTYPE(BUG),
64273f62e60SKees Cook 	CRASHTYPE(WARNING),
64373f62e60SKees Cook 	CRASHTYPE(WARNING_MESSAGE),
64473f62e60SKees Cook 	CRASHTYPE(EXCEPTION),
64573f62e60SKees Cook 	CRASHTYPE(LOOP),
64673f62e60SKees Cook 	CRASHTYPE(EXHAUST_STACK),
64773f62e60SKees Cook 	CRASHTYPE(CORRUPT_STACK),
64873f62e60SKees Cook 	CRASHTYPE(CORRUPT_STACK_STRONG),
64973f62e60SKees Cook 	CRASHTYPE(REPORT_STACK),
65073f62e60SKees Cook 	CRASHTYPE(REPORT_STACK_CANARY),
65173f62e60SKees Cook 	CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
65273f62e60SKees Cook 	CRASHTYPE(SOFTLOCKUP),
65373f62e60SKees Cook 	CRASHTYPE(HARDLOCKUP),
65473f62e60SKees Cook 	CRASHTYPE(SPINLOCKUP),
65573f62e60SKees Cook 	CRASHTYPE(HUNG_TASK),
65673f62e60SKees Cook 	CRASHTYPE(OVERFLOW_SIGNED),
65773f62e60SKees Cook 	CRASHTYPE(OVERFLOW_UNSIGNED),
65873f62e60SKees Cook 	CRASHTYPE(ARRAY_BOUNDS),
6595d207e83SKees Cook 	CRASHTYPE(FAM_BOUNDS),
66073f62e60SKees Cook 	CRASHTYPE(CORRUPT_LIST_ADD),
66173f62e60SKees Cook 	CRASHTYPE(CORRUPT_LIST_DEL),
66273f62e60SKees Cook 	CRASHTYPE(STACK_GUARD_PAGE_LEADING),
66373f62e60SKees Cook 	CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
66473f62e60SKees Cook 	CRASHTYPE(UNSET_SMEP),
66573f62e60SKees Cook 	CRASHTYPE(DOUBLE_FAULT),
66673f62e60SKees Cook 	CRASHTYPE(CORRUPT_PAC),
66773f62e60SKees Cook };
66873f62e60SKees Cook 
66973f62e60SKees Cook struct crashtype_category bugs_crashtypes = {
67073f62e60SKees Cook 	.crashtypes = crashtypes,
67173f62e60SKees Cook 	.len	    = ARRAY_SIZE(crashtypes),
67273f62e60SKees Cook };
673