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
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>
16 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
21 struct list_head node;
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.
29 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
30 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
32 #define REC_STACK_SIZE (THREAD_SIZE / 8UL)
34 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
36 static int recur_count = REC_NUM_DEFAULT;
38 static DEFINE_SPINLOCK(lock_me_up);
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 (buf[..] passed as arg)
45 * - function may have external effects (memzero_explicit())
46 * - no tail recursion possible
48 static int noinline recursive_loop(int remaining)
50 volatile char buf[REC_STACK_SIZE];
53 memset((void *)buf, remaining & 0xFF, sizeof(buf));
57 ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
58 memzero_explicit((void *)buf, sizeof(buf));
62 /* If the depth is negative, use the default, otherwise keep parameter. */
63 void __init lkdtm_bugs_init(int *recur_param)
66 *recur_param = recur_count;
68 recur_count = *recur_param;
71 static void lkdtm_PANIC(void)
76 static void lkdtm_BUG(void)
81 static int warn_counter;
83 static void lkdtm_WARNING(void)
85 WARN_ON(++warn_counter);
88 static void lkdtm_WARNING_MESSAGE(void)
90 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
93 static void lkdtm_EXCEPTION(void)
95 *((volatile int *) 0) = 0;
98 static void lkdtm_LOOP(void)
104 static void lkdtm_EXHAUST_STACK(void)
106 pr_info("Calling function with %lu frame size to depth %d ...\n",
107 REC_STACK_SIZE, recur_count);
108 recursive_loop(recur_count);
109 pr_info("FAIL: survived without exhausting stack?!\n");
112 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
114 memset(stack, '\xff', 64);
117 /* This should trip the stack canary, not corrupt the return address. */
118 static noinline void lkdtm_CORRUPT_STACK(void)
120 /* Use default char array length that triggers stack protection. */
121 char data[8] __aligned(sizeof(void *));
123 pr_info("Corrupting stack containing char array ...\n");
124 __lkdtm_CORRUPT_STACK((void *)&data);
127 /* Same as above but will only get a canary with -fstack-protector-strong */
128 static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
131 unsigned short shorts[4];
133 } data __aligned(sizeof(void *));
135 pr_info("Corrupting stack containing union ...\n");
136 __lkdtm_CORRUPT_STACK((void *)&data);
139 static pid_t stack_pid;
140 static unsigned long stack_addr;
142 static void lkdtm_REPORT_STACK(void)
144 volatile uintptr_t magic;
145 pid_t pid = task_pid_nr(current);
147 if (pid != stack_pid) {
148 pr_info("Starting stack offset tracking for pid %d\n", pid);
150 stack_addr = (uintptr_t)&magic;
153 pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
156 static pid_t stack_canary_pid;
157 static unsigned long stack_canary;
158 static unsigned long stack_canary_offset;
160 static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
163 pid_t pid = task_pid_nr(current);
164 unsigned long *canary = (unsigned long *)stack;
165 unsigned long current_offset = 0, init_offset = 0;
167 /* Do our best to find the canary in a 16 word window ... */
168 for (i = 1; i < 16; i++) {
169 canary = (unsigned long *)stack + i;
170 #ifdef CONFIG_STACKPROTECTOR
171 if (*canary == current->stack_canary)
173 if (*canary == init_task.stack_canary)
178 if (current_offset == 0) {
180 * If the canary doesn't match what's in the task_struct,
181 * we're either using a global canary or the stack frame
184 if (init_offset != 0) {
185 pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
188 pr_warn("FAIL: did not correctly locate stack canary :(\n");
189 pr_expected_config(CONFIG_STACKPROTECTOR);
193 } else if (init_offset != 0) {
194 pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
197 canary = (unsigned long *)stack + current_offset;
198 if (stack_canary_pid == 0) {
199 stack_canary = *canary;
200 stack_canary_pid = pid;
201 stack_canary_offset = current_offset;
202 pr_info("Recorded stack canary for pid %d at offset %ld\n",
203 stack_canary_pid, stack_canary_offset);
204 } else if (pid == stack_canary_pid) {
205 pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
207 if (current_offset != stack_canary_offset) {
208 pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
209 stack_canary_offset, current_offset);
213 if (*canary == stack_canary) {
214 pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
215 stack_canary_pid, pid, current_offset);
217 pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
218 stack_canary_pid, pid, current_offset);
219 /* Reset the test. */
220 stack_canary_pid = 0;
225 static void lkdtm_REPORT_STACK_CANARY(void)
227 /* Use default char array length that triggers stack protection. */
228 char data[8] __aligned(sizeof(void *)) = { };
230 __lkdtm_REPORT_STACK_CANARY((void *)&data);
233 static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
235 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
237 u32 val = 0x12345678;
239 p = (u32 *)(data + 1);
244 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
245 pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
248 static void lkdtm_SOFTLOCKUP(void)
255 static void lkdtm_HARDLOCKUP(void)
262 static void lkdtm_SPINLOCKUP(void)
264 /* Must be called twice to trigger. */
265 spin_lock(&lock_me_up);
266 /* Let sparse know we intended to exit holding the lock. */
267 __release(&lock_me_up);
270 static void lkdtm_HUNG_TASK(void)
272 set_current_state(TASK_UNINTERRUPTIBLE);
276 static volatile unsigned int huge = INT_MAX - 2;
277 static volatile unsigned int ignored;
279 static void lkdtm_OVERFLOW_SIGNED(void)
284 pr_info("Normal signed addition ...\n");
288 pr_info("Overflowing signed addition ...\n");
294 static void lkdtm_OVERFLOW_UNSIGNED(void)
299 pr_info("Normal unsigned addition ...\n");
303 pr_info("Overflowing unsigned addition ...\n");
308 /* Intentionally using unannotated flex array definition. */
309 struct array_bounds_flex_array {
315 struct array_bounds {
322 static void lkdtm_ARRAY_BOUNDS(void)
324 struct array_bounds_flex_array *not_checked;
325 struct array_bounds *checked;
328 not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
329 checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
330 if (!not_checked || !checked) {
336 pr_info("Array access within bounds ...\n");
337 /* For both, touch all bytes in the actual member size. */
338 for (i = 0; i < sizeof(checked->data); i++)
339 checked->data[i] = 'A';
341 * For the uninstrumented flex array member, also touch 1 byte
342 * beyond to verify it is correctly uninstrumented.
344 for (i = 0; i < 2; i++)
345 not_checked->data[i] = 'A';
347 pr_info("Array access beyond bounds ...\n");
348 for (i = 0; i < sizeof(checked->data) + 1; i++)
349 checked->data[i] = 'B';
353 pr_err("FAIL: survived array bounds overflow!\n");
354 if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
355 pr_expected_config(CONFIG_UBSAN_TRAP);
357 pr_expected_config(CONFIG_UBSAN_BOUNDS);
360 struct lkdtm_annotated {
363 int array[] __counted_by(count);
366 static volatile int fam_count = 4;
368 static void lkdtm_FAM_BOUNDS(void)
370 struct lkdtm_annotated *inst;
372 inst = kzalloc(struct_size(inst, array, fam_count + 1), GFP_KERNEL);
374 pr_err("FAIL: could not allocate test struct!\n");
378 inst->count = fam_count;
379 pr_info("Array access within bounds ...\n");
380 inst->array[1] = fam_count;
381 ignored = inst->array[1];
383 pr_info("Array access beyond bounds ...\n");
384 inst->array[fam_count] = fam_count;
385 ignored = inst->array[fam_count];
389 pr_err("FAIL: survived access of invalid flexible array member index!\n");
391 if (!__has_attribute(__counted_by__))
392 pr_warn("This is expected since this %s was built a compiler supporting __counted_by\n",
394 else if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
395 pr_expected_config(CONFIG_UBSAN_TRAP);
397 pr_expected_config(CONFIG_UBSAN_BOUNDS);
400 static void lkdtm_CORRUPT_LIST_ADD(void)
403 * Initially, an empty list via LIST_HEAD:
404 * test_head.next = &test_head
405 * test_head.prev = &test_head
407 LIST_HEAD(test_head);
408 struct lkdtm_list good, bad;
409 void *target[2] = { };
410 void *redirection = ⌖
412 pr_info("attempting good list addition\n");
415 * Adding to the list performs these actions:
416 * test_head.next->prev = &good.node
417 * good.node.next = test_head.next
418 * good.node.prev = test_head
419 * test_head.next = good.node
421 list_add(&good.node, &test_head);
423 pr_info("attempting corrupted list addition\n");
425 * In simulating this "write what where" primitive, the "what" is
426 * the address of &bad.node, and the "where" is the address held
429 test_head.next = redirection;
430 list_add(&bad.node, &test_head);
432 if (target[0] == NULL && target[1] == NULL)
433 pr_err("Overwrite did not happen, but no BUG?!\n");
435 pr_err("list_add() corruption not detected!\n");
436 pr_expected_config(CONFIG_LIST_HARDENED);
440 static void lkdtm_CORRUPT_LIST_DEL(void)
442 LIST_HEAD(test_head);
443 struct lkdtm_list item;
444 void *target[2] = { };
445 void *redirection = ⌖
447 list_add(&item.node, &test_head);
449 pr_info("attempting good list removal\n");
450 list_del(&item.node);
452 pr_info("attempting corrupted list removal\n");
453 list_add(&item.node, &test_head);
455 /* As with the list_add() test above, this corrupts "next". */
456 item.node.next = redirection;
457 list_del(&item.node);
459 if (target[0] == NULL && target[1] == NULL)
460 pr_err("Overwrite did not happen, but no BUG?!\n");
462 pr_err("list_del() corruption not detected!\n");
463 pr_expected_config(CONFIG_LIST_HARDENED);
467 /* Test that VMAP_STACK is actually allocating with a leading guard page */
468 static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
470 const unsigned char *stack = task_stack_page(current);
471 const unsigned char *ptr = stack - 1;
472 volatile unsigned char byte;
474 pr_info("attempting bad read from page below current stack\n");
478 pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
481 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
482 static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
484 const unsigned char *stack = task_stack_page(current);
485 const unsigned char *ptr = stack + THREAD_SIZE;
486 volatile unsigned char byte;
488 pr_info("attempting bad read from page above current stack\n");
492 pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
495 static void lkdtm_UNSET_SMEP(void)
497 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
498 #define MOV_CR4_DEPTH 64
499 void (*direct_write_cr4)(unsigned long val);
504 cr4 = native_read_cr4();
506 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
507 pr_err("FAIL: SMEP not in use\n");
510 cr4 &= ~(X86_CR4_SMEP);
512 pr_info("trying to clear SMEP normally\n");
513 native_write_cr4(cr4);
514 if (cr4 == native_read_cr4()) {
515 pr_err("FAIL: pinning SMEP failed!\n");
517 pr_info("restoring SMEP\n");
518 native_write_cr4(cr4);
521 pr_info("ok: SMEP did not get cleared\n");
524 * To test the post-write pinning verification we need to call
525 * directly into the middle of native_write_cr4() where the
526 * cr4 write happens, skipping any pinning. This searches for
527 * the cr4 writing instruction.
529 insn = (unsigned char *)native_write_cr4;
530 OPTIMIZER_HIDE_VAR(insn);
531 for (i = 0; i < MOV_CR4_DEPTH; i++) {
533 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
535 /* mov %rdi,%rax; mov %rax, %cr4 */
536 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
537 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
538 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
541 if (i >= MOV_CR4_DEPTH) {
542 pr_info("ok: cannot locate cr4 writing call gadget\n");
545 direct_write_cr4 = (void *)(insn + i);
547 pr_info("trying to clear SMEP with call gadget\n");
548 direct_write_cr4(cr4);
549 if (native_read_cr4() & X86_CR4_SMEP) {
550 pr_info("ok: SMEP removal was reverted\n");
552 pr_err("FAIL: cleared SMEP not detected!\n");
554 pr_info("restoring SMEP\n");
555 native_write_cr4(cr4);
558 pr_err("XFAIL: this test is x86_64-only\n");
562 static void lkdtm_DOUBLE_FAULT(void)
564 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
566 * Trigger #DF by setting the stack limit to zero. This clobbers
567 * a GDT TLS slot, which is okay because the current task will die
568 * anyway due to the double fault.
570 struct desc_struct d = {
571 .type = 3, /* expand-up, writable, accessed data */
572 .p = 1, /* present */
574 .g = 0, /* limit in bytes */
575 .s = 1, /* not system */
579 write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
580 GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
583 * Put our zero-limit segment in SS and then trigger a fault. The
584 * 4-byte access to (%esp) will fault with #SS, and the attempt to
585 * deliver the fault will recursively cause #SS and result in #DF.
586 * This whole process happens while NMIs and MCEs are blocked by the
587 * MOV SS window. This is nice because an NMI with an invalid SS
588 * would also double-fault, resulting in the NMI or MCE being lost.
590 asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
591 "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
593 pr_err("FAIL: tried to double fault but didn't die\n");
595 pr_err("XFAIL: this test is ia32-only\n");
600 static noinline void change_pac_parameters(void)
602 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
603 /* Reset the keys of current task */
604 ptrauth_thread_init_kernel(current);
605 ptrauth_thread_switch_kernel(current);
610 static noinline void lkdtm_CORRUPT_PAC(void)
613 #define CORRUPT_PAC_ITERATE 10
616 if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
617 pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
619 if (!system_supports_address_auth()) {
620 pr_err("FAIL: CPU lacks pointer authentication feature\n");
624 pr_info("changing PAC parameters to force function return failure...\n");
626 * PAC is a hash value computed from input keys, return address and
627 * stack pointer. As pac has fewer bits so there is a chance of
628 * collision, so iterate few times to reduce the collision probability.
630 for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
631 change_pac_parameters();
633 pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
635 pr_err("XFAIL: this test is arm64-only\n");
639 static struct crashtype crashtypes[] = {
643 CRASHTYPE(WARNING_MESSAGE),
644 CRASHTYPE(EXCEPTION),
646 CRASHTYPE(EXHAUST_STACK),
647 CRASHTYPE(CORRUPT_STACK),
648 CRASHTYPE(CORRUPT_STACK_STRONG),
649 CRASHTYPE(REPORT_STACK),
650 CRASHTYPE(REPORT_STACK_CANARY),
651 CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
652 CRASHTYPE(SOFTLOCKUP),
653 CRASHTYPE(HARDLOCKUP),
654 CRASHTYPE(SPINLOCKUP),
655 CRASHTYPE(HUNG_TASK),
656 CRASHTYPE(OVERFLOW_SIGNED),
657 CRASHTYPE(OVERFLOW_UNSIGNED),
658 CRASHTYPE(ARRAY_BOUNDS),
659 CRASHTYPE(FAM_BOUNDS),
660 CRASHTYPE(CORRUPT_LIST_ADD),
661 CRASHTYPE(CORRUPT_LIST_DEL),
662 CRASHTYPE(STACK_GUARD_PAGE_LEADING),
663 CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
664 CRASHTYPE(UNSET_SMEP),
665 CRASHTYPE(DOUBLE_FAULT),
666 CRASHTYPE(CORRUPT_PAC),
669 struct crashtype_category bugs_crashtypes = {
670 .crashtypes = crashtypes,
671 .len = ARRAY_SIZE(crashtypes),