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 / 8)
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 (pr_info())
45 * - function has external effects (pr_info())
47 static int noinline recursive_loop(int remaining)
49 volatile char buf[REC_STACK_SIZE];
51 memset((void *)buf, remaining & 0xFF, sizeof(buf));
52 pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)],
57 return recursive_loop(remaining - 1);
60 /* If the depth is negative, use the default, otherwise keep parameter. */
61 void __init lkdtm_bugs_init(int *recur_param)
64 *recur_param = recur_count;
66 recur_count = *recur_param;
69 void lkdtm_PANIC(void)
79 static int warn_counter;
81 void lkdtm_WARNING(void)
83 WARN_ON(++warn_counter);
86 void lkdtm_WARNING_MESSAGE(void)
88 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
91 void lkdtm_EXCEPTION(void)
93 *((volatile int *) 0) = 0;
102 void lkdtm_EXHAUST_STACK(void)
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");
110 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
112 memset(stack, '\xff', 64);
115 /* This should trip the stack canary, not corrupt the return address. */
116 noinline void lkdtm_CORRUPT_STACK(void)
118 /* Use default char array length that triggers stack protection. */
119 char data[8] __aligned(sizeof(void *));
121 pr_info("Corrupting stack containing char array ...\n");
122 __lkdtm_CORRUPT_STACK((void *)&data);
125 /* Same as above but will only get a canary with -fstack-protector-strong */
126 noinline void lkdtm_CORRUPT_STACK_STRONG(void)
129 unsigned short shorts[4];
131 } data __aligned(sizeof(void *));
133 pr_info("Corrupting stack containing union ...\n");
134 __lkdtm_CORRUPT_STACK((void *)&data);
137 static pid_t stack_pid;
138 static unsigned long stack_addr;
140 void lkdtm_REPORT_STACK(void)
142 volatile uintptr_t magic;
143 pid_t pid = task_pid_nr(current);
145 if (pid != stack_pid) {
146 pr_info("Starting stack offset tracking for pid %d\n", pid);
148 stack_addr = (uintptr_t)&magic;
151 pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
154 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
156 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
158 u32 val = 0x12345678;
160 p = (u32 *)(data + 1);
165 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
166 pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
169 void lkdtm_SOFTLOCKUP(void)
176 void lkdtm_HARDLOCKUP(void)
183 void lkdtm_SPINLOCKUP(void)
185 /* Must be called twice to trigger. */
186 spin_lock(&lock_me_up);
187 /* Let sparse know we intended to exit holding the lock. */
188 __release(&lock_me_up);
191 void lkdtm_HUNG_TASK(void)
193 set_current_state(TASK_UNINTERRUPTIBLE);
197 volatile unsigned int huge = INT_MAX - 2;
198 volatile unsigned int ignored;
200 void lkdtm_OVERFLOW_SIGNED(void)
205 pr_info("Normal signed addition ...\n");
209 pr_info("Overflowing signed addition ...\n");
215 void lkdtm_OVERFLOW_UNSIGNED(void)
220 pr_info("Normal unsigned addition ...\n");
224 pr_info("Overflowing unsigned addition ...\n");
229 /* Intentionally using old-style flex array definition of 1 byte. */
230 struct array_bounds_flex_array {
236 struct array_bounds {
243 void lkdtm_ARRAY_BOUNDS(void)
245 struct array_bounds_flex_array *not_checked;
246 struct array_bounds *checked;
249 not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
250 checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
252 pr_info("Array access within bounds ...\n");
253 /* For both, touch all bytes in the actual member size. */
254 for (i = 0; i < sizeof(checked->data); i++)
255 checked->data[i] = 'A';
257 * For the uninstrumented flex array member, also touch 1 byte
258 * beyond to verify it is correctly uninstrumented.
260 for (i = 0; i < sizeof(not_checked->data) + 1; i++)
261 not_checked->data[i] = 'A';
263 pr_info("Array access beyond bounds ...\n");
264 for (i = 0; i < sizeof(checked->data) + 1; i++)
265 checked->data[i] = 'B';
269 pr_err("FAIL: survived array bounds overflow!\n");
270 pr_expected_config(CONFIG_UBSAN_BOUNDS);
273 void lkdtm_CORRUPT_LIST_ADD(void)
276 * Initially, an empty list via LIST_HEAD:
277 * test_head.next = &test_head
278 * test_head.prev = &test_head
280 LIST_HEAD(test_head);
281 struct lkdtm_list good, bad;
282 void *target[2] = { };
283 void *redirection = ⌖
285 pr_info("attempting good list addition\n");
288 * Adding to the list performs these actions:
289 * test_head.next->prev = &good.node
290 * good.node.next = test_head.next
291 * good.node.prev = test_head
292 * test_head.next = good.node
294 list_add(&good.node, &test_head);
296 pr_info("attempting corrupted list addition\n");
298 * In simulating this "write what where" primitive, the "what" is
299 * the address of &bad.node, and the "where" is the address held
302 test_head.next = redirection;
303 list_add(&bad.node, &test_head);
305 if (target[0] == NULL && target[1] == NULL)
306 pr_err("Overwrite did not happen, but no BUG?!\n");
308 pr_err("list_add() corruption not detected!\n");
309 pr_expected_config(CONFIG_DEBUG_LIST);
313 void lkdtm_CORRUPT_LIST_DEL(void)
315 LIST_HEAD(test_head);
316 struct lkdtm_list item;
317 void *target[2] = { };
318 void *redirection = ⌖
320 list_add(&item.node, &test_head);
322 pr_info("attempting good list removal\n");
323 list_del(&item.node);
325 pr_info("attempting corrupted list removal\n");
326 list_add(&item.node, &test_head);
328 /* As with the list_add() test above, this corrupts "next". */
329 item.node.next = redirection;
330 list_del(&item.node);
332 if (target[0] == NULL && target[1] == NULL)
333 pr_err("Overwrite did not happen, but no BUG?!\n");
335 pr_err("list_del() corruption not detected!\n");
336 pr_expected_config(CONFIG_DEBUG_LIST);
340 /* Test that VMAP_STACK is actually allocating with a leading guard page */
341 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
343 const unsigned char *stack = task_stack_page(current);
344 const unsigned char *ptr = stack - 1;
345 volatile unsigned char byte;
347 pr_info("attempting bad read from page below current stack\n");
351 pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
354 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
355 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
357 const unsigned char *stack = task_stack_page(current);
358 const unsigned char *ptr = stack + THREAD_SIZE;
359 volatile unsigned char byte;
361 pr_info("attempting bad read from page above current stack\n");
365 pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
368 void lkdtm_UNSET_SMEP(void)
370 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
371 #define MOV_CR4_DEPTH 64
372 void (*direct_write_cr4)(unsigned long val);
377 cr4 = native_read_cr4();
379 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
380 pr_err("FAIL: SMEP not in use\n");
383 cr4 &= ~(X86_CR4_SMEP);
385 pr_info("trying to clear SMEP normally\n");
386 native_write_cr4(cr4);
387 if (cr4 == native_read_cr4()) {
388 pr_err("FAIL: pinning SMEP failed!\n");
390 pr_info("restoring SMEP\n");
391 native_write_cr4(cr4);
394 pr_info("ok: SMEP did not get cleared\n");
397 * To test the post-write pinning verification we need to call
398 * directly into the middle of native_write_cr4() where the
399 * cr4 write happens, skipping any pinning. This searches for
400 * the cr4 writing instruction.
402 insn = (unsigned char *)native_write_cr4;
403 for (i = 0; i < MOV_CR4_DEPTH; i++) {
405 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
407 /* mov %rdi,%rax; mov %rax, %cr4 */
408 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
409 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
410 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
413 if (i >= MOV_CR4_DEPTH) {
414 pr_info("ok: cannot locate cr4 writing call gadget\n");
417 direct_write_cr4 = (void *)(insn + i);
419 pr_info("trying to clear SMEP with call gadget\n");
420 direct_write_cr4(cr4);
421 if (native_read_cr4() & X86_CR4_SMEP) {
422 pr_info("ok: SMEP removal was reverted\n");
424 pr_err("FAIL: cleared SMEP not detected!\n");
426 pr_info("restoring SMEP\n");
427 native_write_cr4(cr4);
430 pr_err("XFAIL: this test is x86_64-only\n");
434 void lkdtm_DOUBLE_FAULT(void)
436 #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
438 * Trigger #DF by setting the stack limit to zero. This clobbers
439 * a GDT TLS slot, which is okay because the current task will die
440 * anyway due to the double fault.
442 struct desc_struct d = {
443 .type = 3, /* expand-up, writable, accessed data */
444 .p = 1, /* present */
446 .g = 0, /* limit in bytes */
447 .s = 1, /* not system */
451 write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
452 GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
455 * Put our zero-limit segment in SS and then trigger a fault. The
456 * 4-byte access to (%esp) will fault with #SS, and the attempt to
457 * deliver the fault will recursively cause #SS and result in #DF.
458 * This whole process happens while NMIs and MCEs are blocked by the
459 * MOV SS window. This is nice because an NMI with an invalid SS
460 * would also double-fault, resulting in the NMI or MCE being lost.
462 asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
463 "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
465 pr_err("FAIL: tried to double fault but didn't die\n");
467 pr_err("XFAIL: this test is ia32-only\n");
472 static noinline void change_pac_parameters(void)
474 if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
475 /* Reset the keys of current task */
476 ptrauth_thread_init_kernel(current);
477 ptrauth_thread_switch_kernel(current);
482 noinline void lkdtm_CORRUPT_PAC(void)
485 #define CORRUPT_PAC_ITERATE 10
488 if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
489 pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
491 if (!system_supports_address_auth()) {
492 pr_err("FAIL: CPU lacks pointer authentication feature\n");
496 pr_info("changing PAC parameters to force function return failure...\n");
498 * PAC is a hash value computed from input keys, return address and
499 * stack pointer. As pac has fewer bits so there is a chance of
500 * collision, so iterate few times to reduce the collision probability.
502 for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
503 change_pac_parameters();
505 pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
507 pr_err("XFAIL: this test is arm64-only\n");