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>
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)
31 #define REC_STACK_SIZE (THREAD_SIZE / 8)
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())
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
)],
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
)
63 *recur_param
= recur_count
;
65 recur_count
= *recur_param
;
68 void lkdtm_PANIC(void)
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;
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)
129 unsigned short shorts
[4];
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};
142 u32 val
= 0x12345678;
144 p
= (u32
*)(data
+ 1);
150 void lkdtm_SOFTLOCKUP(void)
157 void lkdtm_HARDLOCKUP(void)
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
);
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
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");
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");
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");
247 /* Make sure we do not keep running with a KERNEL_DS! */
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");
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");
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
);
288 cr4
= native_read_cr4();
290 if ((cr4
& X86_CR4_SMEP
) != X86_CR4_SMEP
) {
291 pr_err("FAIL: SMEP not in use\n");
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");
301 pr_info("restoring SMEP\n");
302 native_write_cr4(cr4
);
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
++) {
316 if (insn
[i
] == 0x0f && insn
[i
+1] == 0x22 && insn
[i
+2] == 0xe7)
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)
324 if (i
>= MOV_CR4_DEPTH
) {
325 pr_info("ok: cannot locate cr4 writing call gadget\n");
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");
335 pr_err("FAIL: cleared SMEP not detected!\n");
337 pr_info("restoring SMEP\n");
338 native_write_cr4(cr4
);
341 pr_err("XFAIL: this test is x86_64-only\n");
345 void lkdtm_DOUBLE_FAULT(void)
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 */
357 .g
= 0, /* limit in bytes */
358 .s
= 1, /* not system */
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");
378 pr_err("XFAIL: this test is ia32-only\n");