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/cpu.h>
10 #include <linux/list.h>
11 #include <linux/hrtimer.h>
12 #include <linux/sched.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sched/task_stack.h>
15 #include <linux/slab.h>
16 #include <linux/stop_machine.h>
17 #include <linux/uaccess.h>
18
19 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
20 #include <asm/desc.h>
21 #endif
22
23 struct lkdtm_list {
24 struct list_head node;
25 };
26
27 /*
28 * Make sure our attempts to over run the kernel stack doesn't trigger
29 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
30 * recurse past the end of THREAD_SIZE by default.
31 */
32 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
33 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
34 #else
35 #define REC_STACK_SIZE (THREAD_SIZE / 8UL)
36 #endif
37 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
38
39 static int recur_count = REC_NUM_DEFAULT;
40
41 static DEFINE_SPINLOCK(lock_me_up);
42
43 /*
44 * Make sure compiler does not optimize this function or stack frame away:
45 * - function marked noinline
46 * - stack variables are marked volatile
47 * - stack variables are written (memset()) and read (buf[..] passed as arg)
48 * - function may have external effects (memzero_explicit())
49 * - no tail recursion possible
50 */
recursive_loop(int remaining)51 static int noinline recursive_loop(int remaining)
52 {
53 volatile char buf[REC_STACK_SIZE];
54 volatile int ret;
55
56 memset((void *)buf, remaining & 0xFF, sizeof(buf));
57 if (!remaining)
58 ret = 0;
59 else
60 ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
61 memzero_explicit((void *)buf, sizeof(buf));
62 return ret;
63 }
64
65 /* If the depth is negative, use the default, otherwise keep parameter. */
lkdtm_bugs_init(int * recur_param)66 void __init lkdtm_bugs_init(int *recur_param)
67 {
68 if (*recur_param < 0)
69 *recur_param = recur_count;
70 else
71 recur_count = *recur_param;
72 }
73
lkdtm_PANIC(void)74 static void lkdtm_PANIC(void)
75 {
76 panic("dumptest");
77 }
78
panic_stop_irqoff_fn(void * arg)79 static int panic_stop_irqoff_fn(void *arg)
80 {
81 atomic_t *v = arg;
82
83 /*
84 * As stop_machine() disables interrupts, all CPUs within this function
85 * have interrupts disabled and cannot take a regular IPI.
86 *
87 * The last CPU which enters here will trigger a panic, and as all CPUs
88 * cannot take a regular IPI, we'll only be able to stop secondaries if
89 * smp_send_stop() or crash_smp_send_stop() uses an NMI.
90 */
91 if (atomic_inc_return(v) == num_online_cpus())
92 panic("panic stop irqoff test");
93
94 for (;;)
95 cpu_relax();
96 }
97
lkdtm_PANIC_STOP_IRQOFF(void)98 static void lkdtm_PANIC_STOP_IRQOFF(void)
99 {
100 atomic_t v = ATOMIC_INIT(0);
101 stop_machine(panic_stop_irqoff_fn, &v, cpu_online_mask);
102 }
103
104 static bool wait_for_panic;
105
panic_in_hardirq(struct hrtimer * timer)106 static enum hrtimer_restart panic_in_hardirq(struct hrtimer *timer)
107 {
108 panic("from hard IRQ context");
109
110 wait_for_panic = false;
111 return HRTIMER_NORESTART;
112 }
113
lkdtm_PANIC_IN_HARDIRQ(void)114 static void lkdtm_PANIC_IN_HARDIRQ(void)
115 {
116 struct hrtimer timer;
117
118 wait_for_panic = true;
119 hrtimer_setup_on_stack(&timer, panic_in_hardirq,
120 CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
121 hrtimer_start(&timer, us_to_ktime(100), HRTIMER_MODE_REL_HARD);
122
123 while (READ_ONCE(wait_for_panic))
124 cpu_relax();
125
126 hrtimer_cancel(&timer);
127 }
128
lkdtm_BUG(void)129 static void lkdtm_BUG(void)
130 {
131 BUG();
132 }
133
134 static bool wait_for_bug;
135
bug_in_hardirq(struct hrtimer * timer)136 static enum hrtimer_restart bug_in_hardirq(struct hrtimer *timer)
137 {
138 BUG();
139
140 wait_for_bug = false;
141 return HRTIMER_NORESTART;
142 }
143
lkdtm_BUG_IN_HARDIRQ(void)144 static void lkdtm_BUG_IN_HARDIRQ(void)
145 {
146 struct hrtimer timer;
147
148 wait_for_bug = true;
149 hrtimer_setup_on_stack(&timer, bug_in_hardirq,
150 CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
151 hrtimer_start(&timer, us_to_ktime(100), HRTIMER_MODE_REL_HARD);
152
153 while (READ_ONCE(wait_for_bug))
154 cpu_relax();
155
156 hrtimer_cancel(&timer);
157 }
158
159 static int warn_counter;
160
lkdtm_WARNING(void)161 static void lkdtm_WARNING(void)
162 {
163 WARN_ON(++warn_counter);
164 }
165
lkdtm_WARNING_MESSAGE(void)166 static void lkdtm_WARNING_MESSAGE(void)
167 {
168 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
169 }
170
lkdtm_EXCEPTION(void)171 static void lkdtm_EXCEPTION(void)
172 {
173 *((volatile int *) 0) = 0;
174 }
175
lkdtm_LOOP(void)176 static void lkdtm_LOOP(void)
177 {
178 for (;;)
179 ;
180 }
181
lkdtm_EXHAUST_STACK(void)182 static void lkdtm_EXHAUST_STACK(void)
183 {
184 pr_info("Calling function with %lu frame size to depth %d ...\n",
185 REC_STACK_SIZE, recur_count);
186 recursive_loop(recur_count);
187 pr_info("FAIL: survived without exhausting stack?!\n");
188 }
189
__lkdtm_CORRUPT_STACK(void * stack)190 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
191 {
192 memset(stack, '\xff', 64);
193 }
194
195 /* This should trip the stack canary, not corrupt the return address. */
lkdtm_CORRUPT_STACK(void)196 static noinline void lkdtm_CORRUPT_STACK(void)
197 {
198 /* Use default char array length that triggers stack protection. */
199 char data[8] __aligned(sizeof(void *));
200
201 pr_info("Corrupting stack containing char array ...\n");
202 __lkdtm_CORRUPT_STACK((void *)&data);
203 }
204
205 /* Same as above but will only get a canary with -fstack-protector-strong */
lkdtm_CORRUPT_STACK_STRONG(void)206 static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
207 {
208 union {
209 unsigned short shorts[4];
210 unsigned long *ptr;
211 } data __aligned(sizeof(void *));
212
213 pr_info("Corrupting stack containing union ...\n");
214 __lkdtm_CORRUPT_STACK((void *)&data);
215 }
216
217 static pid_t stack_pid;
218 static unsigned long stack_addr;
219
lkdtm_REPORT_STACK(void)220 static void lkdtm_REPORT_STACK(void)
221 {
222 volatile uintptr_t magic;
223 pid_t pid = task_pid_nr(current);
224
225 if (pid != stack_pid) {
226 pr_info("Starting stack offset tracking for pid %d\n", pid);
227 stack_pid = pid;
228 stack_addr = (uintptr_t)&magic;
229 }
230
231 pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
232 }
233
234 static pid_t stack_canary_pid;
235 static unsigned long stack_canary;
236 static unsigned long stack_canary_offset;
237
__lkdtm_REPORT_STACK_CANARY(void * stack)238 static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
239 {
240 int i = 0;
241 pid_t pid = task_pid_nr(current);
242 unsigned long *canary = (unsigned long *)stack;
243 unsigned long current_offset = 0, init_offset = 0;
244
245 /* Do our best to find the canary in a 16 word window ... */
246 for (i = 1; i < 16; i++) {
247 canary = (unsigned long *)stack + i;
248 #ifdef CONFIG_STACKPROTECTOR
249 if (*canary == current->stack_canary)
250 current_offset = i;
251 if (*canary == init_task.stack_canary)
252 init_offset = i;
253 #endif
254 }
255
256 if (current_offset == 0) {
257 /*
258 * If the canary doesn't match what's in the task_struct,
259 * we're either using a global canary or the stack frame
260 * layout changed.
261 */
262 if (init_offset != 0) {
263 pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
264 init_offset, pid);
265 } else {
266 pr_warn("FAIL: did not correctly locate stack canary :(\n");
267 pr_expected_config(CONFIG_STACKPROTECTOR);
268 }
269
270 return;
271 } else if (init_offset != 0) {
272 pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
273 }
274
275 canary = (unsigned long *)stack + current_offset;
276 if (stack_canary_pid == 0) {
277 stack_canary = *canary;
278 stack_canary_pid = pid;
279 stack_canary_offset = current_offset;
280 pr_info("Recorded stack canary for pid %d at offset %ld\n",
281 stack_canary_pid, stack_canary_offset);
282 } else if (pid == stack_canary_pid) {
283 pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
284 } else {
285 if (current_offset != stack_canary_offset) {
286 pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
287 stack_canary_offset, current_offset);
288 return;
289 }
290
291 if (*canary == stack_canary) {
292 pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
293 stack_canary_pid, pid, current_offset);
294 } else {
295 pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
296 stack_canary_pid, pid, current_offset);
297 /* Reset the test. */
298 stack_canary_pid = 0;
299 }
300 }
301 }
302
lkdtm_REPORT_STACK_CANARY(void)303 static void lkdtm_REPORT_STACK_CANARY(void)
304 {
305 /* Use default char array length that triggers stack protection. */
306 char data[8] __aligned(sizeof(void *)) = { };
307
308 __lkdtm_REPORT_STACK_CANARY((void *)&data);
309 }
310
lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)311 static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
312 {
313 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
314 u32 *p;
315 u32 val = 0x12345678;
316
317 p = (u32 *)(data + 1);
318 if (*p == 0)
319 val = 0x87654321;
320 *p = val;
321
322 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
323 pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
324 }
325
lkdtm_SOFTLOCKUP(void)326 static void lkdtm_SOFTLOCKUP(void)
327 {
328 preempt_disable();
329 for (;;)
330 cpu_relax();
331 }
332
lkdtm_HARDLOCKUP(void)333 static void lkdtm_HARDLOCKUP(void)
334 {
335 local_irq_disable();
336 for (;;)
337 cpu_relax();
338 }
339
__lkdtm_SMP_CALL_LOCKUP(void * unused)340 static void __lkdtm_SMP_CALL_LOCKUP(void *unused)
341 {
342 for (;;)
343 cpu_relax();
344 }
345
lkdtm_SMP_CALL_LOCKUP(void)346 static void lkdtm_SMP_CALL_LOCKUP(void)
347 {
348 unsigned int cpu, target;
349
350 cpus_read_lock();
351
352 cpu = get_cpu();
353 target = cpumask_any_but(cpu_online_mask, cpu);
354
355 if (target >= nr_cpu_ids) {
356 pr_err("FAIL: no other online CPUs\n");
357 goto out_put_cpus;
358 }
359
360 smp_call_function_single(target, __lkdtm_SMP_CALL_LOCKUP, NULL, 1);
361
362 pr_err("FAIL: did not hang\n");
363
364 out_put_cpus:
365 put_cpu();
366 cpus_read_unlock();
367 }
368
lkdtm_SPINLOCKUP(void)369 static void lkdtm_SPINLOCKUP(void)
370 {
371 /* Must be called twice to trigger. */
372 spin_lock(&lock_me_up);
373 /* Let sparse know we intended to exit holding the lock. */
374 __release(&lock_me_up);
375 }
376
lkdtm_HUNG_TASK(void)377 static void __noreturn lkdtm_HUNG_TASK(void)
378 {
379 set_current_state(TASK_UNINTERRUPTIBLE);
380 schedule();
381 BUG();
382 }
383
384 static volatile unsigned int huge = INT_MAX - 2;
385 static volatile unsigned int ignored;
386
lkdtm_OVERFLOW_SIGNED(void)387 static void lkdtm_OVERFLOW_SIGNED(void)
388 {
389 int value;
390
391 value = huge;
392 pr_info("Normal signed addition ...\n");
393 value += 1;
394 ignored = value;
395
396 pr_info("Overflowing signed addition ...\n");
397 value += 4;
398 ignored = value;
399 }
400
401
lkdtm_OVERFLOW_UNSIGNED(void)402 static void lkdtm_OVERFLOW_UNSIGNED(void)
403 {
404 unsigned int value;
405
406 value = huge;
407 pr_info("Normal unsigned addition ...\n");
408 value += 1;
409 ignored = value;
410
411 pr_info("Overflowing unsigned addition ...\n");
412 value += 4;
413 ignored = value;
414 }
415
416 /* Intentionally using unannotated flex array definition. */
417 struct array_bounds_flex_array {
418 int one;
419 int two;
420 char data[];
421 };
422
423 struct array_bounds {
424 int one;
425 int two;
426 char data[8];
427 int three;
428 };
429
lkdtm_ARRAY_BOUNDS(void)430 static void lkdtm_ARRAY_BOUNDS(void)
431 {
432 struct array_bounds_flex_array *not_checked;
433 struct array_bounds *checked;
434 volatile int i;
435
436 not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
437 checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
438 if (!not_checked || !checked) {
439 kfree(not_checked);
440 kfree(checked);
441 return;
442 }
443
444 pr_info("Array access within bounds ...\n");
445 /* For both, touch all bytes in the actual member size. */
446 for (i = 0; i < sizeof(checked->data); i++)
447 checked->data[i] = 'A';
448 /*
449 * For the uninstrumented flex array member, also touch 1 byte
450 * beyond to verify it is correctly uninstrumented.
451 */
452 for (i = 0; i < 2; i++)
453 not_checked->data[i] = 'A';
454
455 pr_info("Array access beyond bounds ...\n");
456 for (i = 0; i < sizeof(checked->data) + 1; i++)
457 checked->data[i] = 'B';
458
459 kfree(not_checked);
460 kfree(checked);
461 pr_err("FAIL: survived array bounds overflow!\n");
462 if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
463 pr_expected_config(CONFIG_UBSAN_TRAP);
464 else
465 pr_expected_config(CONFIG_UBSAN_BOUNDS);
466 }
467
468 struct lkdtm_annotated {
469 unsigned long flags;
470 int count;
471 int array[] __counted_by(count);
472 };
473
474 static volatile int fam_count = 4;
475
lkdtm_FAM_BOUNDS(void)476 static void lkdtm_FAM_BOUNDS(void)
477 {
478 struct lkdtm_annotated *inst;
479
480 inst = kzalloc(struct_size(inst, array, fam_count + 1), GFP_KERNEL);
481 if (!inst) {
482 pr_err("FAIL: could not allocate test struct!\n");
483 return;
484 }
485
486 inst->count = fam_count;
487 pr_info("Array access within bounds ...\n");
488 inst->array[1] = fam_count;
489 ignored = inst->array[1];
490
491 pr_info("Array access beyond bounds ...\n");
492 inst->array[fam_count] = fam_count;
493 ignored = inst->array[fam_count];
494
495 kfree(inst);
496
497 pr_err("FAIL: survived access of invalid flexible array member index!\n");
498
499 if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
500 pr_warn("This is expected since this %s was built with a compiler that does not support __counted_by\n",
501 lkdtm_kernel_info);
502 else if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
503 pr_expected_config(CONFIG_UBSAN_TRAP);
504 else
505 pr_expected_config(CONFIG_UBSAN_BOUNDS);
506 }
507
lkdtm_CORRUPT_LIST_ADD(void)508 static void lkdtm_CORRUPT_LIST_ADD(void)
509 {
510 /*
511 * Initially, an empty list via LIST_HEAD:
512 * test_head.next = &test_head
513 * test_head.prev = &test_head
514 */
515 LIST_HEAD(test_head);
516 struct lkdtm_list good, bad;
517 void *target[2] = { };
518 void *redirection = ⌖
519
520 pr_info("attempting good list addition\n");
521
522 /*
523 * Adding to the list performs these actions:
524 * test_head.next->prev = &good.node
525 * good.node.next = test_head.next
526 * good.node.prev = test_head
527 * test_head.next = good.node
528 */
529 list_add(&good.node, &test_head);
530
531 pr_info("attempting corrupted list addition\n");
532 /*
533 * In simulating this "write what where" primitive, the "what" is
534 * the address of &bad.node, and the "where" is the address held
535 * by "redirection".
536 */
537 test_head.next = redirection;
538 list_add(&bad.node, &test_head);
539
540 if (target[0] == NULL && target[1] == NULL)
541 pr_err("Overwrite did not happen, but no BUG?!\n");
542 else {
543 pr_err("list_add() corruption not detected!\n");
544 pr_expected_config(CONFIG_LIST_HARDENED);
545 }
546 }
547
lkdtm_CORRUPT_LIST_DEL(void)548 static void lkdtm_CORRUPT_LIST_DEL(void)
549 {
550 LIST_HEAD(test_head);
551 struct lkdtm_list item;
552 void *target[2] = { };
553 void *redirection = ⌖
554
555 list_add(&item.node, &test_head);
556
557 pr_info("attempting good list removal\n");
558 list_del(&item.node);
559
560 pr_info("attempting corrupted list removal\n");
561 list_add(&item.node, &test_head);
562
563 /* As with the list_add() test above, this corrupts "next". */
564 item.node.next = redirection;
565 list_del(&item.node);
566
567 if (target[0] == NULL && target[1] == NULL)
568 pr_err("Overwrite did not happen, but no BUG?!\n");
569 else {
570 pr_err("list_del() corruption not detected!\n");
571 pr_expected_config(CONFIG_LIST_HARDENED);
572 }
573 }
574
575 /* Test that VMAP_STACK is actually allocating with a leading guard page */
lkdtm_STACK_GUARD_PAGE_LEADING(void)576 static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
577 {
578 const unsigned char *stack = task_stack_page(current);
579 const unsigned char *ptr = stack - 1;
580 volatile unsigned char byte;
581
582 pr_info("attempting bad read from page below current stack\n");
583
584 byte = *ptr;
585
586 pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
587 }
588
589 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
lkdtm_STACK_GUARD_PAGE_TRAILING(void)590 static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
591 {
592 const unsigned char *stack = task_stack_page(current);
593 const unsigned char *ptr = stack + THREAD_SIZE;
594 volatile unsigned char byte;
595
596 pr_info("attempting bad read from page above current stack\n");
597
598 byte = *ptr;
599
600 pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
601 }
602
lkdtm_UNSET_SMEP(void)603 static void lkdtm_UNSET_SMEP(void)
604 {
605 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
606 #define MOV_CR4_DEPTH 64
607 void (*direct_write_cr4)(unsigned long val);
608 unsigned char *insn;
609 unsigned long cr4;
610 int i;
611
612 cr4 = native_read_cr4();
613
614 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
615 pr_err("FAIL: SMEP not in use\n");
616 return;
617 }
618 cr4 &= ~(X86_CR4_SMEP);
619
620 pr_info("trying to clear SMEP normally\n");
621 native_write_cr4(cr4);
622 if (cr4 == native_read_cr4()) {
623 pr_err("FAIL: pinning SMEP failed!\n");
624 cr4 |= X86_CR4_SMEP;
625 pr_info("restoring SMEP\n");
626 native_write_cr4(cr4);
627 return;
628 }
629 pr_info("ok: SMEP did not get cleared\n");
630
631 /*
632 * To test the post-write pinning verification we need to call
633 * directly into the middle of native_write_cr4() where the
634 * cr4 write happens, skipping any pinning. This searches for
635 * the cr4 writing instruction.
636 */
637 insn = (unsigned char *)native_write_cr4;
638 OPTIMIZER_HIDE_VAR(insn);
639 for (i = 0; i < MOV_CR4_DEPTH; i++) {
640 /* mov %rdi, %cr4 */
641 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
642 break;
643 /* mov %rdi,%rax; mov %rax, %cr4 */
644 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
645 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
646 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
647 break;
648 }
649 if (i >= MOV_CR4_DEPTH) {
650 pr_info("ok: cannot locate cr4 writing call gadget\n");
651 return;
652 }
653 direct_write_cr4 = (void *)(insn + i);
654
655 pr_info("trying to clear SMEP with call gadget\n");
656 direct_write_cr4(cr4);
657 if (native_read_cr4() & X86_CR4_SMEP) {
658 pr_info("ok: SMEP removal was reverted\n");
659 } else {
660 pr_err("FAIL: cleared SMEP not detected!\n");
661 cr4 |= X86_CR4_SMEP;
662 pr_info("restoring SMEP\n");
663 native_write_cr4(cr4);
664 }
665 #else
666 pr_err("XFAIL: this test is x86_64-only\n");
667 #endif
668 }
669
lkdtm_DOUBLE_FAULT(void)670 static void lkdtm_DOUBLE_FAULT(void)
671 {
672 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
673 /*
674 * Trigger #DF by setting the stack limit to zero. This clobbers
675 * a GDT TLS slot, which is okay because the current task will die
676 * anyway due to the double fault.
677 */
678 struct desc_struct d = {
679 .type = 3, /* expand-up, writable, accessed data */
680 .p = 1, /* present */
681 .d = 1, /* 32-bit */
682 .g = 0, /* limit in bytes */
683 .s = 1, /* not system */
684 };
685
686 local_irq_disable();
687 write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
688 GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
689
690 /*
691 * Put our zero-limit segment in SS and then trigger a fault. The
692 * 4-byte access to (%esp) will fault with #SS, and the attempt to
693 * deliver the fault will recursively cause #SS and result in #DF.
694 * This whole process happens while NMIs and MCEs are blocked by the
695 * MOV SS window. This is nice because an NMI with an invalid SS
696 * would also double-fault, resulting in the NMI or MCE being lost.
697 */
698 asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
699 "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
700
701 pr_err("FAIL: tried to double fault but didn't die\n");
702 #else
703 pr_err("XFAIL: this test is ia32-only\n");
704 #endif
705 }
706
707 #ifdef CONFIG_ARM64
change_pac_parameters(void)708 static noinline void change_pac_parameters(void)
709 {
710 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
711 /* Reset the keys of current task */
712 ptrauth_thread_init_kernel(current);
713 ptrauth_thread_switch_kernel(current);
714 }
715 }
716 #endif
717
lkdtm_CORRUPT_PAC(void)718 static noinline void lkdtm_CORRUPT_PAC(void)
719 {
720 #ifdef CONFIG_ARM64
721 #define CORRUPT_PAC_ITERATE 10
722 int i;
723
724 if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
725 pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
726
727 if (!system_supports_address_auth()) {
728 pr_err("FAIL: CPU lacks pointer authentication feature\n");
729 return;
730 }
731
732 pr_info("changing PAC parameters to force function return failure...\n");
733 /*
734 * PAC is a hash value computed from input keys, return address and
735 * stack pointer. As pac has fewer bits so there is a chance of
736 * collision, so iterate few times to reduce the collision probability.
737 */
738 for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
739 change_pac_parameters();
740
741 pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
742 #else
743 pr_err("XFAIL: this test is arm64-only\n");
744 #endif
745 }
746
747 static struct crashtype crashtypes[] = {
748 CRASHTYPE(PANIC),
749 CRASHTYPE(PANIC_STOP_IRQOFF),
750 CRASHTYPE(PANIC_IN_HARDIRQ),
751 CRASHTYPE(BUG),
752 CRASHTYPE(BUG_IN_HARDIRQ),
753 CRASHTYPE(WARNING),
754 CRASHTYPE(WARNING_MESSAGE),
755 CRASHTYPE(EXCEPTION),
756 CRASHTYPE(LOOP),
757 CRASHTYPE(EXHAUST_STACK),
758 CRASHTYPE(CORRUPT_STACK),
759 CRASHTYPE(CORRUPT_STACK_STRONG),
760 CRASHTYPE(REPORT_STACK),
761 CRASHTYPE(REPORT_STACK_CANARY),
762 CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
763 CRASHTYPE(SOFTLOCKUP),
764 CRASHTYPE(HARDLOCKUP),
765 CRASHTYPE(SMP_CALL_LOCKUP),
766 CRASHTYPE(SPINLOCKUP),
767 CRASHTYPE(HUNG_TASK),
768 CRASHTYPE(OVERFLOW_SIGNED),
769 CRASHTYPE(OVERFLOW_UNSIGNED),
770 CRASHTYPE(ARRAY_BOUNDS),
771 CRASHTYPE(FAM_BOUNDS),
772 CRASHTYPE(CORRUPT_LIST_ADD),
773 CRASHTYPE(CORRUPT_LIST_DEL),
774 CRASHTYPE(STACK_GUARD_PAGE_LEADING),
775 CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
776 CRASHTYPE(UNSET_SMEP),
777 CRASHTYPE(DOUBLE_FAULT),
778 CRASHTYPE(CORRUPT_PAC),
779 };
780
781 struct crashtype_category bugs_crashtypes = {
782 .crashtypes = crashtypes,
783 .len = ARRAY_SIZE(crashtypes),
784 };
785