• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This is for all the tests related to logic bugs (e.g. bad dereferences,
4  * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
5  * lockups) along with other things that don't fit well into existing LKDTM
6  * test source files.
7  */
8 #include "lkdtm.h"
9 #include <linux/list.h>
10 #include <linux/sched.h>
11 #include <linux/sched/signal.h>
12 #include <linux/sched/task_stack.h>
13 #include <linux/uaccess.h>
14 #include <linux/slab.h>
15 
16 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
17 #include <asm/desc.h>
18 #endif
19 
20 struct lkdtm_list {
21 	struct list_head node;
22 };
23 
24 /*
25  * Make sure our attempts to over run the kernel stack doesn't trigger
26  * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
27  * recurse past the end of THREAD_SIZE by default.
28  */
29 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
30 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
31 #else
32 #define REC_STACK_SIZE (THREAD_SIZE / 8)
33 #endif
34 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
35 
36 static int recur_count = REC_NUM_DEFAULT;
37 
38 static DEFINE_SPINLOCK(lock_me_up);
39 
40 /*
41  * Make sure compiler does not optimize this function or stack frame away:
42  * - function marked noinline
43  * - stack variables are marked volatile
44  * - stack variables are written (memset()) and read (pr_info())
45  * - function has external effects (pr_info())
46  * */
recursive_loop(int remaining)47 static int noinline recursive_loop(int remaining)
48 {
49 	volatile char buf[REC_STACK_SIZE];
50 
51 	memset((void *)buf, remaining & 0xFF, sizeof(buf));
52 	pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)],
53 		recur_count);
54 	if (!remaining)
55 		return 0;
56 	else
57 		return recursive_loop(remaining - 1);
58 }
59 
60 /* If the depth is negative, use the default, otherwise keep parameter. */
lkdtm_bugs_init(int * recur_param)61 void __init lkdtm_bugs_init(int *recur_param)
62 {
63 	if (*recur_param < 0)
64 		*recur_param = recur_count;
65 	else
66 		recur_count = *recur_param;
67 }
68 
lkdtm_PANIC(void)69 void lkdtm_PANIC(void)
70 {
71 	panic("dumptest");
72 }
73 
lkdtm_BUG(void)74 void lkdtm_BUG(void)
75 {
76 	BUG();
77 }
78 
79 static int warn_counter;
80 
lkdtm_WARNING(void)81 void lkdtm_WARNING(void)
82 {
83 	WARN_ON(++warn_counter);
84 }
85 
lkdtm_WARNING_MESSAGE(void)86 void lkdtm_WARNING_MESSAGE(void)
87 {
88 	WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
89 }
90 
lkdtm_EXCEPTION(void)91 void lkdtm_EXCEPTION(void)
92 {
93 	*((volatile int *) 0) = 0;
94 }
95 
lkdtm_LOOP(void)96 void lkdtm_LOOP(void)
97 {
98 	for (;;)
99 		;
100 }
101 
lkdtm_EXHAUST_STACK(void)102 void lkdtm_EXHAUST_STACK(void)
103 {
104 	pr_info("Calling function with %lu frame size to depth %d ...\n",
105 		REC_STACK_SIZE, recur_count);
106 	recursive_loop(recur_count);
107 	pr_info("FAIL: survived without exhausting stack?!\n");
108 }
109 
__lkdtm_CORRUPT_STACK(void * stack)110 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
111 {
112 	memset(stack, '\xff', 64);
113 }
114 
115 /* This should trip the stack canary, not corrupt the return address. */
lkdtm_CORRUPT_STACK(void)116 noinline void lkdtm_CORRUPT_STACK(void)
117 {
118 	/* Use default char array length that triggers stack protection. */
119 	char data[8] __aligned(sizeof(void *));
120 
121 	pr_info("Corrupting stack containing char array ...\n");
122 	__lkdtm_CORRUPT_STACK((void *)&data);
123 }
124 
125 /* Same as above but will only get a canary with -fstack-protector-strong */
lkdtm_CORRUPT_STACK_STRONG(void)126 noinline void lkdtm_CORRUPT_STACK_STRONG(void)
127 {
128 	union {
129 		unsigned short shorts[4];
130 		unsigned long *ptr;
131 	} data __aligned(sizeof(void *));
132 
133 	pr_info("Corrupting stack containing union ...\n");
134 	__lkdtm_CORRUPT_STACK((void *)&data);
135 }
136 
lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)137 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
138 {
139 	static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
140 	u32 *p;
141 	u32 val = 0x12345678;
142 
143 	p = (u32 *)(data + 1);
144 	if (*p == 0)
145 		val = 0x87654321;
146 	*p = val;
147 
148 	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
149 		pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
150 }
151 
lkdtm_SOFTLOCKUP(void)152 void lkdtm_SOFTLOCKUP(void)
153 {
154 	preempt_disable();
155 	for (;;)
156 		cpu_relax();
157 }
158 
lkdtm_HARDLOCKUP(void)159 void lkdtm_HARDLOCKUP(void)
160 {
161 	local_irq_disable();
162 	for (;;)
163 		cpu_relax();
164 }
165 
lkdtm_SPINLOCKUP(void)166 void lkdtm_SPINLOCKUP(void)
167 {
168 	/* Must be called twice to trigger. */
169 	spin_lock(&lock_me_up);
170 	/* Let sparse know we intended to exit holding the lock. */
171 	__release(&lock_me_up);
172 }
173 
lkdtm_HUNG_TASK(void)174 void lkdtm_HUNG_TASK(void)
175 {
176 	set_current_state(TASK_UNINTERRUPTIBLE);
177 	schedule();
178 }
179 
180 volatile unsigned int huge = INT_MAX - 2;
181 volatile unsigned int ignored;
182 
lkdtm_OVERFLOW_SIGNED(void)183 void lkdtm_OVERFLOW_SIGNED(void)
184 {
185 	int value;
186 
187 	value = huge;
188 	pr_info("Normal signed addition ...\n");
189 	value += 1;
190 	ignored = value;
191 
192 	pr_info("Overflowing signed addition ...\n");
193 	value += 4;
194 	ignored = value;
195 }
196 
197 
lkdtm_OVERFLOW_UNSIGNED(void)198 void lkdtm_OVERFLOW_UNSIGNED(void)
199 {
200 	unsigned int value;
201 
202 	value = huge;
203 	pr_info("Normal unsigned addition ...\n");
204 	value += 1;
205 	ignored = value;
206 
207 	pr_info("Overflowing unsigned addition ...\n");
208 	value += 4;
209 	ignored = value;
210 }
211 
212 /* Intentionally using old-style flex array definition of 1 byte. */
213 struct array_bounds_flex_array {
214 	int one;
215 	int two;
216 	char data[1];
217 };
218 
219 struct array_bounds {
220 	int one;
221 	int two;
222 	char data[8];
223 	int three;
224 };
225 
lkdtm_ARRAY_BOUNDS(void)226 void lkdtm_ARRAY_BOUNDS(void)
227 {
228 	struct array_bounds_flex_array *not_checked;
229 	struct array_bounds *checked;
230 	volatile int i;
231 
232 	not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
233 	checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
234 
235 	pr_info("Array access within bounds ...\n");
236 	/* For both, touch all bytes in the actual member size. */
237 	for (i = 0; i < sizeof(checked->data); i++)
238 		checked->data[i] = 'A';
239 	/*
240 	 * For the uninstrumented flex array member, also touch 1 byte
241 	 * beyond to verify it is correctly uninstrumented.
242 	 */
243 	for (i = 0; i < sizeof(not_checked->data) + 1; i++)
244 		not_checked->data[i] = 'A';
245 
246 	pr_info("Array access beyond bounds ...\n");
247 	for (i = 0; i < sizeof(checked->data) + 1; i++)
248 		checked->data[i] = 'B';
249 
250 	kfree(not_checked);
251 	kfree(checked);
252 	pr_err("FAIL: survived array bounds overflow!\n");
253 }
254 
lkdtm_CORRUPT_LIST_ADD(void)255 void lkdtm_CORRUPT_LIST_ADD(void)
256 {
257 	/*
258 	 * Initially, an empty list via LIST_HEAD:
259 	 *	test_head.next = &test_head
260 	 *	test_head.prev = &test_head
261 	 */
262 	LIST_HEAD(test_head);
263 	struct lkdtm_list good, bad;
264 	void *target[2] = { };
265 	void *redirection = &target;
266 
267 	pr_info("attempting good list addition\n");
268 
269 	/*
270 	 * Adding to the list performs these actions:
271 	 *	test_head.next->prev = &good.node
272 	 *	good.node.next = test_head.next
273 	 *	good.node.prev = test_head
274 	 *	test_head.next = good.node
275 	 */
276 	list_add(&good.node, &test_head);
277 
278 	pr_info("attempting corrupted list addition\n");
279 	/*
280 	 * In simulating this "write what where" primitive, the "what" is
281 	 * the address of &bad.node, and the "where" is the address held
282 	 * by "redirection".
283 	 */
284 	test_head.next = redirection;
285 	list_add(&bad.node, &test_head);
286 
287 	if (target[0] == NULL && target[1] == NULL)
288 		pr_err("Overwrite did not happen, but no BUG?!\n");
289 	else
290 		pr_err("list_add() corruption not detected!\n");
291 }
292 
lkdtm_CORRUPT_LIST_DEL(void)293 void lkdtm_CORRUPT_LIST_DEL(void)
294 {
295 	LIST_HEAD(test_head);
296 	struct lkdtm_list item;
297 	void *target[2] = { };
298 	void *redirection = &target;
299 
300 	list_add(&item.node, &test_head);
301 
302 	pr_info("attempting good list removal\n");
303 	list_del(&item.node);
304 
305 	pr_info("attempting corrupted list removal\n");
306 	list_add(&item.node, &test_head);
307 
308 	/* As with the list_add() test above, this corrupts "next". */
309 	item.node.next = redirection;
310 	list_del(&item.node);
311 
312 	if (target[0] == NULL && target[1] == NULL)
313 		pr_err("Overwrite did not happen, but no BUG?!\n");
314 	else
315 		pr_err("list_del() corruption not detected!\n");
316 }
317 
318 /* Test that VMAP_STACK is actually allocating with a leading guard page */
lkdtm_STACK_GUARD_PAGE_LEADING(void)319 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
320 {
321 	const unsigned char *stack = task_stack_page(current);
322 	const unsigned char *ptr = stack - 1;
323 	volatile unsigned char byte;
324 
325 	pr_info("attempting bad read from page below current stack\n");
326 
327 	byte = *ptr;
328 
329 	pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
330 }
331 
332 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
lkdtm_STACK_GUARD_PAGE_TRAILING(void)333 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
334 {
335 	const unsigned char *stack = task_stack_page(current);
336 	const unsigned char *ptr = stack + THREAD_SIZE;
337 	volatile unsigned char byte;
338 
339 	pr_info("attempting bad read from page above current stack\n");
340 
341 	byte = *ptr;
342 
343 	pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
344 }
345 
lkdtm_UNSET_SMEP(void)346 void lkdtm_UNSET_SMEP(void)
347 {
348 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
349 #define MOV_CR4_DEPTH	64
350 	void (*direct_write_cr4)(unsigned long val);
351 	unsigned char *insn;
352 	unsigned long cr4;
353 	int i;
354 
355 	cr4 = native_read_cr4();
356 
357 	if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
358 		pr_err("FAIL: SMEP not in use\n");
359 		return;
360 	}
361 	cr4 &= ~(X86_CR4_SMEP);
362 
363 	pr_info("trying to clear SMEP normally\n");
364 	native_write_cr4(cr4);
365 	if (cr4 == native_read_cr4()) {
366 		pr_err("FAIL: pinning SMEP failed!\n");
367 		cr4 |= X86_CR4_SMEP;
368 		pr_info("restoring SMEP\n");
369 		native_write_cr4(cr4);
370 		return;
371 	}
372 	pr_info("ok: SMEP did not get cleared\n");
373 
374 	/*
375 	 * To test the post-write pinning verification we need to call
376 	 * directly into the middle of native_write_cr4() where the
377 	 * cr4 write happens, skipping any pinning. This searches for
378 	 * the cr4 writing instruction.
379 	 */
380 	insn = (unsigned char *)native_write_cr4;
381 	for (i = 0; i < MOV_CR4_DEPTH; i++) {
382 		/* mov %rdi, %cr4 */
383 		if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
384 			break;
385 		/* mov %rdi,%rax; mov %rax, %cr4 */
386 		if (insn[i]   == 0x48 && insn[i+1] == 0x89 &&
387 		    insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
388 		    insn[i+4] == 0x22 && insn[i+5] == 0xe0)
389 			break;
390 	}
391 	if (i >= MOV_CR4_DEPTH) {
392 		pr_info("ok: cannot locate cr4 writing call gadget\n");
393 		return;
394 	}
395 	direct_write_cr4 = (void *)(insn + i);
396 
397 	pr_info("trying to clear SMEP with call gadget\n");
398 	direct_write_cr4(cr4);
399 	if (native_read_cr4() & X86_CR4_SMEP) {
400 		pr_info("ok: SMEP removal was reverted\n");
401 	} else {
402 		pr_err("FAIL: cleared SMEP not detected!\n");
403 		cr4 |= X86_CR4_SMEP;
404 		pr_info("restoring SMEP\n");
405 		native_write_cr4(cr4);
406 	}
407 #else
408 	pr_err("XFAIL: this test is x86_64-only\n");
409 #endif
410 }
411 
lkdtm_DOUBLE_FAULT(void)412 void lkdtm_DOUBLE_FAULT(void)
413 {
414 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
415 	/*
416 	 * Trigger #DF by setting the stack limit to zero.  This clobbers
417 	 * a GDT TLS slot, which is okay because the current task will die
418 	 * anyway due to the double fault.
419 	 */
420 	struct desc_struct d = {
421 		.type = 3,	/* expand-up, writable, accessed data */
422 		.p = 1,		/* present */
423 		.d = 1,		/* 32-bit */
424 		.g = 0,		/* limit in bytes */
425 		.s = 1,		/* not system */
426 	};
427 
428 	local_irq_disable();
429 	write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
430 			GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
431 
432 	/*
433 	 * Put our zero-limit segment in SS and then trigger a fault.  The
434 	 * 4-byte access to (%esp) will fault with #SS, and the attempt to
435 	 * deliver the fault will recursively cause #SS and result in #DF.
436 	 * This whole process happens while NMIs and MCEs are blocked by the
437 	 * MOV SS window.  This is nice because an NMI with an invalid SS
438 	 * would also double-fault, resulting in the NMI or MCE being lost.
439 	 */
440 	asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
441 		      "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
442 
443 	pr_err("FAIL: tried to double fault but didn't die\n");
444 #else
445 	pr_err("XFAIL: this test is ia32-only\n");
446 #endif
447 }
448 
449 #ifdef CONFIG_ARM64
change_pac_parameters(void)450 static noinline void change_pac_parameters(void)
451 {
452 	if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) {
453 		/* Reset the keys of current task */
454 		ptrauth_thread_init_kernel(current);
455 		ptrauth_thread_switch_kernel(current);
456 	}
457 }
458 #endif
459 
lkdtm_CORRUPT_PAC(void)460 noinline void lkdtm_CORRUPT_PAC(void)
461 {
462 #ifdef CONFIG_ARM64
463 #define CORRUPT_PAC_ITERATE	10
464 	int i;
465 
466 	if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH))
467 		pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH\n");
468 
469 	if (!system_supports_address_auth()) {
470 		pr_err("FAIL: CPU lacks pointer authentication feature\n");
471 		return;
472 	}
473 
474 	pr_info("changing PAC parameters to force function return failure...\n");
475 	/*
476 	 * PAC is a hash value computed from input keys, return address and
477 	 * stack pointer. As pac has fewer bits so there is a chance of
478 	 * collision, so iterate few times to reduce the collision probability.
479 	 */
480 	for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
481 		change_pac_parameters();
482 
483 	pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
484 #else
485 	pr_err("XFAIL: this test is arm64-only\n");
486 #endif
487 }
488