treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / lkdtm / bugs.c
blobde87693cf557d405ba86e8977c5aeab6ce15f2f6
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>
15 #ifdef CONFIG_X86_32
16 #include <asm/desc.h>
17 #endif
19 struct lkdtm_list {
20 struct list_head node;
24 * Make sure our attempts to over run the kernel stack doesn't trigger
25 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
26 * recurse past the end of THREAD_SIZE by default.
28 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
29 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
30 #else
31 #define REC_STACK_SIZE (THREAD_SIZE / 8)
32 #endif
33 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
35 static int recur_count = REC_NUM_DEFAULT;
37 static DEFINE_SPINLOCK(lock_me_up);
40 * Make sure compiler does not optimize this function or stack frame away:
41 * - function marked noinline
42 * - stack variables are marked volatile
43 * - stack variables are written (memset()) and read (pr_info())
44 * - function has external effects (pr_info())
45 * */
46 static int noinline recursive_loop(int remaining)
48 volatile char buf[REC_STACK_SIZE];
50 memset((void *)buf, remaining & 0xFF, sizeof(buf));
51 pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)],
52 recur_count);
53 if (!remaining)
54 return 0;
55 else
56 return recursive_loop(remaining - 1);
59 /* If the depth is negative, use the default, otherwise keep parameter. */
60 void __init lkdtm_bugs_init(int *recur_param)
62 if (*recur_param < 0)
63 *recur_param = recur_count;
64 else
65 recur_count = *recur_param;
68 void lkdtm_PANIC(void)
70 panic("dumptest");
73 void lkdtm_BUG(void)
75 BUG();
78 static int warn_counter;
80 void lkdtm_WARNING(void)
82 WARN_ON(++warn_counter);
85 void lkdtm_WARNING_MESSAGE(void)
87 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
90 void lkdtm_EXCEPTION(void)
92 *((volatile int *) 0) = 0;
95 void lkdtm_LOOP(void)
97 for (;;)
101 void lkdtm_EXHAUST_STACK(void)
103 pr_info("Calling function with %lu frame size to depth %d ...\n",
104 REC_STACK_SIZE, recur_count);
105 recursive_loop(recur_count);
106 pr_info("FAIL: survived without exhausting stack?!\n");
109 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
111 memset(stack, '\xff', 64);
114 /* This should trip the stack canary, not corrupt the return address. */
115 noinline void lkdtm_CORRUPT_STACK(void)
117 /* Use default char array length that triggers stack protection. */
118 char data[8] __aligned(sizeof(void *));
120 __lkdtm_CORRUPT_STACK(&data);
122 pr_info("Corrupted stack containing char array ...\n");
125 /* Same as above but will only get a canary with -fstack-protector-strong */
126 noinline void lkdtm_CORRUPT_STACK_STRONG(void)
128 union {
129 unsigned short shorts[4];
130 unsigned long *ptr;
131 } data __aligned(sizeof(void *));
133 __lkdtm_CORRUPT_STACK(&data);
135 pr_info("Corrupted stack containing union ...\n");
138 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
140 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
141 u32 *p;
142 u32 val = 0x12345678;
144 p = (u32 *)(data + 1);
145 if (*p == 0)
146 val = 0x87654321;
147 *p = val;
150 void lkdtm_SOFTLOCKUP(void)
152 preempt_disable();
153 for (;;)
154 cpu_relax();
157 void lkdtm_HARDLOCKUP(void)
159 local_irq_disable();
160 for (;;)
161 cpu_relax();
164 void lkdtm_SPINLOCKUP(void)
166 /* Must be called twice to trigger. */
167 spin_lock(&lock_me_up);
168 /* Let sparse know we intended to exit holding the lock. */
169 __release(&lock_me_up);
172 void lkdtm_HUNG_TASK(void)
174 set_current_state(TASK_UNINTERRUPTIBLE);
175 schedule();
178 void lkdtm_CORRUPT_LIST_ADD(void)
181 * Initially, an empty list via LIST_HEAD:
182 * test_head.next = &test_head
183 * test_head.prev = &test_head
185 LIST_HEAD(test_head);
186 struct lkdtm_list good, bad;
187 void *target[2] = { };
188 void *redirection = &target;
190 pr_info("attempting good list addition\n");
193 * Adding to the list performs these actions:
194 * test_head.next->prev = &good.node
195 * good.node.next = test_head.next
196 * good.node.prev = test_head
197 * test_head.next = good.node
199 list_add(&good.node, &test_head);
201 pr_info("attempting corrupted list addition\n");
203 * In simulating this "write what where" primitive, the "what" is
204 * the address of &bad.node, and the "where" is the address held
205 * by "redirection".
207 test_head.next = redirection;
208 list_add(&bad.node, &test_head);
210 if (target[0] == NULL && target[1] == NULL)
211 pr_err("Overwrite did not happen, but no BUG?!\n");
212 else
213 pr_err("list_add() corruption not detected!\n");
216 void lkdtm_CORRUPT_LIST_DEL(void)
218 LIST_HEAD(test_head);
219 struct lkdtm_list item;
220 void *target[2] = { };
221 void *redirection = &target;
223 list_add(&item.node, &test_head);
225 pr_info("attempting good list removal\n");
226 list_del(&item.node);
228 pr_info("attempting corrupted list removal\n");
229 list_add(&item.node, &test_head);
231 /* As with the list_add() test above, this corrupts "next". */
232 item.node.next = redirection;
233 list_del(&item.node);
235 if (target[0] == NULL && target[1] == NULL)
236 pr_err("Overwrite did not happen, but no BUG?!\n");
237 else
238 pr_err("list_del() corruption not detected!\n");
241 /* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
242 void lkdtm_CORRUPT_USER_DS(void)
244 pr_info("setting bad task size limit\n");
245 set_fs(KERNEL_DS);
247 /* Make sure we do not keep running with a KERNEL_DS! */
248 force_sig(SIGKILL);
251 /* Test that VMAP_STACK is actually allocating with a leading guard page */
252 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
254 const unsigned char *stack = task_stack_page(current);
255 const unsigned char *ptr = stack - 1;
256 volatile unsigned char byte;
258 pr_info("attempting bad read from page below current stack\n");
260 byte = *ptr;
262 pr_err("FAIL: accessed page before stack!\n");
265 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
266 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
268 const unsigned char *stack = task_stack_page(current);
269 const unsigned char *ptr = stack + THREAD_SIZE;
270 volatile unsigned char byte;
272 pr_info("attempting bad read from page above current stack\n");
274 byte = *ptr;
276 pr_err("FAIL: accessed page after stack!\n");
279 void lkdtm_UNSET_SMEP(void)
281 #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
282 #define MOV_CR4_DEPTH 64
283 void (*direct_write_cr4)(unsigned long val);
284 unsigned char *insn;
285 unsigned long cr4;
286 int i;
288 cr4 = native_read_cr4();
290 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
291 pr_err("FAIL: SMEP not in use\n");
292 return;
294 cr4 &= ~(X86_CR4_SMEP);
296 pr_info("trying to clear SMEP normally\n");
297 native_write_cr4(cr4);
298 if (cr4 == native_read_cr4()) {
299 pr_err("FAIL: pinning SMEP failed!\n");
300 cr4 |= X86_CR4_SMEP;
301 pr_info("restoring SMEP\n");
302 native_write_cr4(cr4);
303 return;
305 pr_info("ok: SMEP did not get cleared\n");
308 * To test the post-write pinning verification we need to call
309 * directly into the middle of native_write_cr4() where the
310 * cr4 write happens, skipping any pinning. This searches for
311 * the cr4 writing instruction.
313 insn = (unsigned char *)native_write_cr4;
314 for (i = 0; i < MOV_CR4_DEPTH; i++) {
315 /* mov %rdi, %cr4 */
316 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
317 break;
318 /* mov %rdi,%rax; mov %rax, %cr4 */
319 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
320 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
321 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
322 break;
324 if (i >= MOV_CR4_DEPTH) {
325 pr_info("ok: cannot locate cr4 writing call gadget\n");
326 return;
328 direct_write_cr4 = (void *)(insn + i);
330 pr_info("trying to clear SMEP with call gadget\n");
331 direct_write_cr4(cr4);
332 if (native_read_cr4() & X86_CR4_SMEP) {
333 pr_info("ok: SMEP removal was reverted\n");
334 } else {
335 pr_err("FAIL: cleared SMEP not detected!\n");
336 cr4 |= X86_CR4_SMEP;
337 pr_info("restoring SMEP\n");
338 native_write_cr4(cr4);
340 #else
341 pr_err("XFAIL: this test is x86_64-only\n");
342 #endif
345 void lkdtm_DOUBLE_FAULT(void)
347 #ifdef CONFIG_X86_32
349 * Trigger #DF by setting the stack limit to zero. This clobbers
350 * a GDT TLS slot, which is okay because the current task will die
351 * anyway due to the double fault.
353 struct desc_struct d = {
354 .type = 3, /* expand-up, writable, accessed data */
355 .p = 1, /* present */
356 .d = 1, /* 32-bit */
357 .g = 0, /* limit in bytes */
358 .s = 1, /* not system */
361 local_irq_disable();
362 write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
363 GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
366 * Put our zero-limit segment in SS and then trigger a fault. The
367 * 4-byte access to (%esp) will fault with #SS, and the attempt to
368 * deliver the fault will recursively cause #SS and result in #DF.
369 * This whole process happens while NMIs and MCEs are blocked by the
370 * MOV SS window. This is nice because an NMI with an invalid SS
371 * would also double-fault, resulting in the NMI or MCE being lost.
373 asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
374 "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
376 pr_err("FAIL: tried to double fault but didn't die\n");
377 #else
378 pr_err("XFAIL: this test is ia32-only\n");
379 #endif