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>
16 struct list_head node;
20 * Make sure our attempts to over run the kernel stack doesn't trigger
21 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
22 * recurse past the end of THREAD_SIZE by default.
24 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
25 #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
27 #define REC_STACK_SIZE (THREAD_SIZE / 8)
29 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
31 static int recur_count = REC_NUM_DEFAULT;
33 static DEFINE_SPINLOCK(lock_me_up);
36 * Make sure compiler does not optimize this function or stack frame away:
37 * - function marked noinline
38 * - stack variables are marked volatile
39 * - stack variables are written (memset()) and read (pr_info())
40 * - function has external effects (pr_info())
42 static int noinline recursive_loop(int remaining)
44 volatile char buf[REC_STACK_SIZE];
46 memset((void *)buf, remaining & 0xFF, sizeof(buf));
47 pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)],
52 return recursive_loop(remaining - 1);
55 /* If the depth is negative, use the default, otherwise keep parameter. */
56 void __init lkdtm_bugs_init(int *recur_param)
59 *recur_param = recur_count;
61 recur_count = *recur_param;
64 void lkdtm_PANIC(void)
74 static int warn_counter;
76 void lkdtm_WARNING(void)
78 WARN_ON(++warn_counter);
81 void lkdtm_WARNING_MESSAGE(void)
83 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
86 void lkdtm_EXCEPTION(void)
88 *((volatile int *) 0) = 0;
97 void lkdtm_EXHAUST_STACK(void)
99 pr_info("Calling function with %lu frame size to depth %d ...\n",
100 REC_STACK_SIZE, recur_count);
101 recursive_loop(recur_count);
102 pr_info("FAIL: survived without exhausting stack?!\n");
105 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
107 memset(stack, '\xff', 64);
110 /* This should trip the stack canary, not corrupt the return address. */
111 noinline void lkdtm_CORRUPT_STACK(void)
113 /* Use default char array length that triggers stack protection. */
114 char data[8] __aligned(sizeof(void *));
116 __lkdtm_CORRUPT_STACK(&data);
118 pr_info("Corrupted stack containing char array ...\n");
121 /* Same as above but will only get a canary with -fstack-protector-strong */
122 noinline void lkdtm_CORRUPT_STACK_STRONG(void)
125 unsigned short shorts[4];
127 } data __aligned(sizeof(void *));
129 __lkdtm_CORRUPT_STACK(&data);
131 pr_info("Corrupted stack containing union ...\n");
134 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
136 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
138 u32 val = 0x12345678;
140 p = (u32 *)(data + 1);
146 void lkdtm_SOFTLOCKUP(void)
153 void lkdtm_HARDLOCKUP(void)
160 void lkdtm_SPINLOCKUP(void)
162 /* Must be called twice to trigger. */
163 spin_lock(&lock_me_up);
164 /* Let sparse know we intended to exit holding the lock. */
165 __release(&lock_me_up);
168 void lkdtm_HUNG_TASK(void)
170 set_current_state(TASK_UNINTERRUPTIBLE);
174 void lkdtm_CORRUPT_LIST_ADD(void)
177 * Initially, an empty list via LIST_HEAD:
178 * test_head.next = &test_head
179 * test_head.prev = &test_head
181 LIST_HEAD(test_head);
182 struct lkdtm_list good, bad;
183 void *target[2] = { };
184 void *redirection = ⌖
186 pr_info("attempting good list addition\n");
189 * Adding to the list performs these actions:
190 * test_head.next->prev = &good.node
191 * good.node.next = test_head.next
192 * good.node.prev = test_head
193 * test_head.next = good.node
195 list_add(&good.node, &test_head);
197 pr_info("attempting corrupted list addition\n");
199 * In simulating this "write what where" primitive, the "what" is
200 * the address of &bad.node, and the "where" is the address held
203 test_head.next = redirection;
204 list_add(&bad.node, &test_head);
206 if (target[0] == NULL && target[1] == NULL)
207 pr_err("Overwrite did not happen, but no BUG?!\n");
209 pr_err("list_add() corruption not detected!\n");
212 void lkdtm_CORRUPT_LIST_DEL(void)
214 LIST_HEAD(test_head);
215 struct lkdtm_list item;
216 void *target[2] = { };
217 void *redirection = ⌖
219 list_add(&item.node, &test_head);
221 pr_info("attempting good list removal\n");
222 list_del(&item.node);
224 pr_info("attempting corrupted list removal\n");
225 list_add(&item.node, &test_head);
227 /* As with the list_add() test above, this corrupts "next". */
228 item.node.next = redirection;
229 list_del(&item.node);
231 if (target[0] == NULL && target[1] == NULL)
232 pr_err("Overwrite did not happen, but no BUG?!\n");
234 pr_err("list_del() corruption not detected!\n");
237 /* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
238 void lkdtm_CORRUPT_USER_DS(void)
240 pr_info("setting bad task size limit\n");
243 /* Make sure we do not keep running with a KERNEL_DS! */
247 /* Test that VMAP_STACK is actually allocating with a leading guard page */
248 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
250 const unsigned char *stack = task_stack_page(current);
251 const unsigned char *ptr = stack - 1;
252 volatile unsigned char byte;
254 pr_info("attempting bad read from page below current stack\n");
258 pr_err("FAIL: accessed page before stack!\n");
261 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
262 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
264 const unsigned char *stack = task_stack_page(current);
265 const unsigned char *ptr = stack + THREAD_SIZE;
266 volatile unsigned char byte;
268 pr_info("attempting bad read from page above current stack\n");
272 pr_err("FAIL: accessed page after stack!\n");
275 void lkdtm_UNSET_SMEP(void)
278 #define MOV_CR4_DEPTH 64
279 void (*direct_write_cr4)(unsigned long val);
284 cr4 = native_read_cr4();
286 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
287 pr_err("FAIL: SMEP not in use\n");
290 cr4 &= ~(X86_CR4_SMEP);
292 pr_info("trying to clear SMEP normally\n");
293 native_write_cr4(cr4);
294 if (cr4 == native_read_cr4()) {
295 pr_err("FAIL: pinning SMEP failed!\n");
297 pr_info("restoring SMEP\n");
298 native_write_cr4(cr4);
301 pr_info("ok: SMEP did not get cleared\n");
304 * To test the post-write pinning verification we need to call
305 * directly into the middle of native_write_cr4() where the
306 * cr4 write happens, skipping any pinning. This searches for
307 * the cr4 writing instruction.
309 insn = (unsigned char *)native_write_cr4;
310 for (i = 0; i < MOV_CR4_DEPTH; i++) {
312 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
314 /* mov %rdi,%rax; mov %rax, %cr4 */
315 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
316 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
317 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
320 if (i >= MOV_CR4_DEPTH) {
321 pr_info("ok: cannot locate cr4 writing call gadget\n");
324 direct_write_cr4 = (void *)(insn + i);
326 pr_info("trying to clear SMEP with call gadget\n");
327 direct_write_cr4(cr4);
328 if (native_read_cr4() & X86_CR4_SMEP) {
329 pr_info("ok: SMEP removal was reverted\n");
331 pr_err("FAIL: cleared SMEP not detected!\n");
333 pr_info("restoring SMEP\n");
334 native_write_cr4(cr4);
337 pr_err("FAIL: this test is x86_64-only\n");