1 // SPDX-License-Identifier: GPL-2.0
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
10 #include <linux/list.h>
11 #include <linux/sched.h>
12 #include <linux/sched/signal.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/slab.h>
15 #include <linux/stop_machine.h>
16 #include <linux/uaccess.h>
18 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
23 struct list_head node;
27 * Make sure our attempts to over run the kernel stack doesn't trigger
28 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
29 * recurse past the end of THREAD_SIZE by default.
31 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
32 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
34 #define REC_STACK_SIZE (THREAD_SIZE / 8UL)
36 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
38 static int recur_count = REC_NUM_DEFAULT;
40 static DEFINE_SPINLOCK(lock_me_up);
43 * Make sure compiler does not optimize this function or stack frame away:
44 * - function marked noinline
45 * - stack variables are marked volatile
46 * - stack variables are written (memset()) and read (buf[..] passed as arg)
47 * - function may have external effects (memzero_explicit())
48 * - no tail recursion possible
50 static int noinline recursive_loop(int remaining)
52 volatile char buf[REC_STACK_SIZE];
55 memset((void *)buf, remaining & 0xFF, sizeof(buf));
59 ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
60 memzero_explicit((void *)buf, sizeof(buf));
64 /* If the depth is negative, use the default, otherwise keep parameter. */
65 void __init lkdtm_bugs_init(int *recur_param)
68 *recur_param = recur_count;
70 recur_count = *recur_param;
73 static void lkdtm_PANIC(void)
78 static int panic_stop_irqoff_fn(void *arg)
83 * As stop_machine() disables interrupts, all CPUs within this function
84 * have interrupts disabled and cannot take a regular IPI.
86 * The last CPU which enters here will trigger a panic, and as all CPUs
87 * cannot take a regular IPI, we'll only be able to stop secondaries if
88 * smp_send_stop() or crash_smp_send_stop() uses an NMI.
90 if (atomic_inc_return(v) == num_online_cpus())
91 panic("panic stop irqoff test");
97 static void lkdtm_PANIC_STOP_IRQOFF(void)
99 atomic_t v = ATOMIC_INIT(0);
100 stop_machine(panic_stop_irqoff_fn, &v, cpu_online_mask);
103 static void lkdtm_BUG(void)
108 static int warn_counter;
110 static void lkdtm_WARNING(void)
112 WARN_ON(++warn_counter);
115 static void lkdtm_WARNING_MESSAGE(void)
117 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
120 static void lkdtm_EXCEPTION(void)
122 *((volatile int *) 0) = 0;
125 static void lkdtm_LOOP(void)
131 static void lkdtm_EXHAUST_STACK(void)
133 pr_info("Calling function with %lu frame size to depth %d ...\n",
134 REC_STACK_SIZE, recur_count);
135 recursive_loop(recur_count);
136 pr_info("FAIL: survived without exhausting stack?!\n");
139 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
141 memset(stack, '\xff', 64);
144 /* This should trip the stack canary, not corrupt the return address. */
145 static noinline void lkdtm_CORRUPT_STACK(void)
147 /* Use default char array length that triggers stack protection. */
148 char data[8] __aligned(sizeof(void *));
150 pr_info("Corrupting stack containing char array ...\n");
151 __lkdtm_CORRUPT_STACK((void *)&data);
154 /* Same as above but will only get a canary with -fstack-protector-strong */
155 static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
158 unsigned short shorts[4];
160 } data __aligned(sizeof(void *));
162 pr_info("Corrupting stack containing union ...\n");
163 __lkdtm_CORRUPT_STACK((void *)&data);
166 static pid_t stack_pid;
167 static unsigned long stack_addr;
169 static void lkdtm_REPORT_STACK(void)
171 volatile uintptr_t magic;
172 pid_t pid = task_pid_nr(current);
174 if (pid != stack_pid) {
175 pr_info("Starting stack offset tracking for pid %d\n", pid);
177 stack_addr = (uintptr_t)&magic;
180 pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
183 static pid_t stack_canary_pid;
184 static unsigned long stack_canary;
185 static unsigned long stack_canary_offset;
187 static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
190 pid_t pid = task_pid_nr(current);
191 unsigned long *canary = (unsigned long *)stack;
192 unsigned long current_offset = 0, init_offset = 0;
194 /* Do our best to find the canary in a 16 word window ... */
195 for (i = 1; i < 16; i++) {
196 canary = (unsigned long *)stack + i;
197 #ifdef CONFIG_STACKPROTECTOR
198 if (*canary == current->stack_canary)
200 if (*canary == init_task.stack_canary)
205 if (current_offset == 0) {
207 * If the canary doesn't match what's in the task_struct,
208 * we're either using a global canary or the stack frame
211 if (init_offset != 0) {
212 pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
215 pr_warn("FAIL: did not correctly locate stack canary :(\n");
216 pr_expected_config(CONFIG_STACKPROTECTOR);
220 } else if (init_offset != 0) {
221 pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
224 canary = (unsigned long *)stack + current_offset;
225 if (stack_canary_pid == 0) {
226 stack_canary = *canary;
227 stack_canary_pid = pid;
228 stack_canary_offset = current_offset;
229 pr_info("Recorded stack canary for pid %d at offset %ld\n",
230 stack_canary_pid, stack_canary_offset);
231 } else if (pid == stack_canary_pid) {
232 pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
234 if (current_offset != stack_canary_offset) {
235 pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
236 stack_canary_offset, current_offset);
240 if (*canary == stack_canary) {
241 pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
242 stack_canary_pid, pid, current_offset);
244 pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
245 stack_canary_pid, pid, current_offset);
246 /* Reset the test. */
247 stack_canary_pid = 0;
252 static void lkdtm_REPORT_STACK_CANARY(void)
254 /* Use default char array length that triggers stack protection. */
255 char data[8] __aligned(sizeof(void *)) = { };
257 __lkdtm_REPORT_STACK_CANARY((void *)&data);
260 static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
262 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
264 u32 val = 0x12345678;
266 p = (u32 *)(data + 1);
271 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
272 pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
275 static void lkdtm_SOFTLOCKUP(void)
282 static void lkdtm_HARDLOCKUP(void)
289 static void lkdtm_SPINLOCKUP(void)
291 /* Must be called twice to trigger. */
292 spin_lock(&lock_me_up);
293 /* Let sparse know we intended to exit holding the lock. */
294 __release(&lock_me_up);
297 static void __noreturn lkdtm_HUNG_TASK(void)
299 set_current_state(TASK_UNINTERRUPTIBLE);
304 static volatile unsigned int huge = INT_MAX - 2;
305 static volatile unsigned int ignored;
307 static void lkdtm_OVERFLOW_SIGNED(void)
312 pr_info("Normal signed addition ...\n");
316 pr_info("Overflowing signed addition ...\n");
322 static void lkdtm_OVERFLOW_UNSIGNED(void)
327 pr_info("Normal unsigned addition ...\n");
331 pr_info("Overflowing unsigned addition ...\n");
336 /* Intentionally using unannotated flex array definition. */
337 struct array_bounds_flex_array {
343 struct array_bounds {
350 static void lkdtm_ARRAY_BOUNDS(void)
352 struct array_bounds_flex_array *not_checked;
353 struct array_bounds *checked;
356 not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
357 checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
358 if (!not_checked || !checked) {
364 pr_info("Array access within bounds ...\n");
365 /* For both, touch all bytes in the actual member size. */
366 for (i = 0; i < sizeof(checked->data); i++)
367 checked->data[i] = 'A';
369 * For the uninstrumented flex array member, also touch 1 byte
370 * beyond to verify it is correctly uninstrumented.
372 for (i = 0; i < 2; i++)
373 not_checked->data[i] = 'A';
375 pr_info("Array access beyond bounds ...\n");
376 for (i = 0; i < sizeof(checked->data) + 1; i++)
377 checked->data[i] = 'B';
381 pr_err("FAIL: survived array bounds overflow!\n");
382 if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
383 pr_expected_config(CONFIG_UBSAN_TRAP);
385 pr_expected_config(CONFIG_UBSAN_BOUNDS);
388 struct lkdtm_annotated {
391 int array[] __counted_by(count);
394 static volatile int fam_count = 4;
396 static void lkdtm_FAM_BOUNDS(void)
398 struct lkdtm_annotated *inst;
400 inst = kzalloc(struct_size(inst, array, fam_count + 1), GFP_KERNEL);
402 pr_err("FAIL: could not allocate test struct!\n");
406 inst->count = fam_count;
407 pr_info("Array access within bounds ...\n");
408 inst->array[1] = fam_count;
409 ignored = inst->array[1];
411 pr_info("Array access beyond bounds ...\n");
412 inst->array[fam_count] = fam_count;
413 ignored = inst->array[fam_count];
417 pr_err("FAIL: survived access of invalid flexible array member index!\n");
419 if (!__has_attribute(__counted_by__))
420 pr_warn("This is expected since this %s was built a compiler supporting __counted_by\n",
422 else if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
423 pr_expected_config(CONFIG_UBSAN_TRAP);
425 pr_expected_config(CONFIG_UBSAN_BOUNDS);
428 static void lkdtm_CORRUPT_LIST_ADD(void)
431 * Initially, an empty list via LIST_HEAD:
432 * test_head.next = &test_head
433 * test_head.prev = &test_head
435 LIST_HEAD(test_head);
436 struct lkdtm_list good, bad;
437 void *target[2] = { };
438 void *redirection = ⌖
440 pr_info("attempting good list addition\n");
443 * Adding to the list performs these actions:
444 * test_head.next->prev = &good.node
445 * good.node.next = test_head.next
446 * good.node.prev = test_head
447 * test_head.next = good.node
449 list_add(&good.node, &test_head);
451 pr_info("attempting corrupted list addition\n");
453 * In simulating this "write what where" primitive, the "what" is
454 * the address of &bad.node, and the "where" is the address held
457 test_head.next = redirection;
458 list_add(&bad.node, &test_head);
460 if (target[0] == NULL && target[1] == NULL)
461 pr_err("Overwrite did not happen, but no BUG?!\n");
463 pr_err("list_add() corruption not detected!\n");
464 pr_expected_config(CONFIG_LIST_HARDENED);
468 static void lkdtm_CORRUPT_LIST_DEL(void)
470 LIST_HEAD(test_head);
471 struct lkdtm_list item;
472 void *target[2] = { };
473 void *redirection = ⌖
475 list_add(&item.node, &test_head);
477 pr_info("attempting good list removal\n");
478 list_del(&item.node);
480 pr_info("attempting corrupted list removal\n");
481 list_add(&item.node, &test_head);
483 /* As with the list_add() test above, this corrupts "next". */
484 item.node.next = redirection;
485 list_del(&item.node);
487 if (target[0] == NULL && target[1] == NULL)
488 pr_err("Overwrite did not happen, but no BUG?!\n");
490 pr_err("list_del() corruption not detected!\n");
491 pr_expected_config(CONFIG_LIST_HARDENED);
495 /* Test that VMAP_STACK is actually allocating with a leading guard page */
496 static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
498 const unsigned char *stack = task_stack_page(current);
499 const unsigned char *ptr = stack - 1;
500 volatile unsigned char byte;
502 pr_info("attempting bad read from page below current stack\n");
506 pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
509 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
510 static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
512 const unsigned char *stack = task_stack_page(current);
513 const unsigned char *ptr = stack + THREAD_SIZE;
514 volatile unsigned char byte;
516 pr_info("attempting bad read from page above current stack\n");
520 pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
523 static void lkdtm_UNSET_SMEP(void)
525 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
526 #define MOV_CR4_DEPTH 64
527 void (*direct_write_cr4)(unsigned long val);
532 cr4 = native_read_cr4();
534 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
535 pr_err("FAIL: SMEP not in use\n");
538 cr4 &= ~(X86_CR4_SMEP);
540 pr_info("trying to clear SMEP normally\n");
541 native_write_cr4(cr4);
542 if (cr4 == native_read_cr4()) {
543 pr_err("FAIL: pinning SMEP failed!\n");
545 pr_info("restoring SMEP\n");
546 native_write_cr4(cr4);
549 pr_info("ok: SMEP did not get cleared\n");
552 * To test the post-write pinning verification we need to call
553 * directly into the middle of native_write_cr4() where the
554 * cr4 write happens, skipping any pinning. This searches for
555 * the cr4 writing instruction.
557 insn = (unsigned char *)native_write_cr4;
558 OPTIMIZER_HIDE_VAR(insn);
559 for (i = 0; i < MOV_CR4_DEPTH; i++) {
561 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
563 /* mov %rdi,%rax; mov %rax, %cr4 */
564 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
565 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
566 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
569 if (i >= MOV_CR4_DEPTH) {
570 pr_info("ok: cannot locate cr4 writing call gadget\n");
573 direct_write_cr4 = (void *)(insn + i);
575 pr_info("trying to clear SMEP with call gadget\n");
576 direct_write_cr4(cr4);
577 if (native_read_cr4() & X86_CR4_SMEP) {
578 pr_info("ok: SMEP removal was reverted\n");
580 pr_err("FAIL: cleared SMEP not detected!\n");
582 pr_info("restoring SMEP\n");
583 native_write_cr4(cr4);
586 pr_err("XFAIL: this test is x86_64-only\n");
590 static void lkdtm_DOUBLE_FAULT(void)
592 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
594 * Trigger #DF by setting the stack limit to zero. This clobbers
595 * a GDT TLS slot, which is okay because the current task will die
596 * anyway due to the double fault.
598 struct desc_struct d = {
599 .type = 3, /* expand-up, writable, accessed data */
600 .p = 1, /* present */
602 .g = 0, /* limit in bytes */
603 .s = 1, /* not system */
607 write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
608 GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
611 * Put our zero-limit segment in SS and then trigger a fault. The
612 * 4-byte access to (%esp) will fault with #SS, and the attempt to
613 * deliver the fault will recursively cause #SS and result in #DF.
614 * This whole process happens while NMIs and MCEs are blocked by the
615 * MOV SS window. This is nice because an NMI with an invalid SS
616 * would also double-fault, resulting in the NMI or MCE being lost.
618 asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
619 "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
621 pr_err("FAIL: tried to double fault but didn't die\n");
623 pr_err("XFAIL: this test is ia32-only\n");
628 static noinline void change_pac_parameters(void)
630 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
631 /* Reset the keys of current task */
632 ptrauth_thread_init_kernel(current);
633 ptrauth_thread_switch_kernel(current);
638 static noinline void lkdtm_CORRUPT_PAC(void)
641 #define CORRUPT_PAC_ITERATE 10
644 if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
645 pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
647 if (!system_supports_address_auth()) {
648 pr_err("FAIL: CPU lacks pointer authentication feature\n");
652 pr_info("changing PAC parameters to force function return failure...\n");
654 * PAC is a hash value computed from input keys, return address and
655 * stack pointer. As pac has fewer bits so there is a chance of
656 * collision, so iterate few times to reduce the collision probability.
658 for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
659 change_pac_parameters();
661 pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
663 pr_err("XFAIL: this test is arm64-only\n");
667 static struct crashtype crashtypes[] = {
669 CRASHTYPE(PANIC_STOP_IRQOFF),
672 CRASHTYPE(WARNING_MESSAGE),
673 CRASHTYPE(EXCEPTION),
675 CRASHTYPE(EXHAUST_STACK),
676 CRASHTYPE(CORRUPT_STACK),
677 CRASHTYPE(CORRUPT_STACK_STRONG),
678 CRASHTYPE(REPORT_STACK),
679 CRASHTYPE(REPORT_STACK_CANARY),
680 CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
681 CRASHTYPE(SOFTLOCKUP),
682 CRASHTYPE(HARDLOCKUP),
683 CRASHTYPE(SPINLOCKUP),
684 CRASHTYPE(HUNG_TASK),
685 CRASHTYPE(OVERFLOW_SIGNED),
686 CRASHTYPE(OVERFLOW_UNSIGNED),
687 CRASHTYPE(ARRAY_BOUNDS),
688 CRASHTYPE(FAM_BOUNDS),
689 CRASHTYPE(CORRUPT_LIST_ADD),
690 CRASHTYPE(CORRUPT_LIST_DEL),
691 CRASHTYPE(STACK_GUARD_PAGE_LEADING),
692 CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
693 CRASHTYPE(UNSET_SMEP),
694 CRASHTYPE(DOUBLE_FAULT),
695 CRASHTYPE(CORRUPT_PAC),
698 struct crashtype_category bugs_crashtypes = {
699 .crashtypes = crashtypes,
700 .len = ARRAY_SIZE(crashtypes),