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 (CONFIG_FRAME_WARN / 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(1, "Warning message trigger count: %d\n", warn_counter++);
81 void lkdtm_EXCEPTION(void)
83 *((volatile int *) 0) = 0;
92 void lkdtm_EXHAUST_STACK(void)
94 pr_info("Calling function with %d frame size to depth %d ...\n",
95 REC_STACK_SIZE, recur_count);
96 recursive_loop(recur_count);
97 pr_info("FAIL: survived without exhausting stack?!\n");
100 static noinline void __lkdtm_CORRUPT_STACK(void *stack)
102 memset(stack, '\xff', 64);
105 /* This should trip the stack canary, not corrupt the return address. */
106 noinline void lkdtm_CORRUPT_STACK(void)
108 /* Use default char array length that triggers stack protection. */
109 char data[8] __aligned(sizeof(void *));
111 __lkdtm_CORRUPT_STACK(&data);
113 pr_info("Corrupted stack containing char array ...\n");
116 /* Same as above but will only get a canary with -fstack-protector-strong */
117 noinline void lkdtm_CORRUPT_STACK_STRONG(void)
120 unsigned short shorts[4];
122 } data __aligned(sizeof(void *));
124 __lkdtm_CORRUPT_STACK(&data);
126 pr_info("Corrupted stack containing union ...\n");
129 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
131 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
133 u32 val = 0x12345678;
135 p = (u32 *)(data + 1);
141 void lkdtm_SOFTLOCKUP(void)
148 void lkdtm_HARDLOCKUP(void)
155 void lkdtm_SPINLOCKUP(void)
157 /* Must be called twice to trigger. */
158 spin_lock(&lock_me_up);
159 /* Let sparse know we intended to exit holding the lock. */
160 __release(&lock_me_up);
163 void lkdtm_HUNG_TASK(void)
165 set_current_state(TASK_UNINTERRUPTIBLE);
169 void lkdtm_CORRUPT_LIST_ADD(void)
172 * Initially, an empty list via LIST_HEAD:
173 * test_head.next = &test_head
174 * test_head.prev = &test_head
176 LIST_HEAD(test_head);
177 struct lkdtm_list good, bad;
178 void *target[2] = { };
179 void *redirection = ⌖
181 pr_info("attempting good list addition\n");
184 * Adding to the list performs these actions:
185 * test_head.next->prev = &good.node
186 * good.node.next = test_head.next
187 * good.node.prev = test_head
188 * test_head.next = good.node
190 list_add(&good.node, &test_head);
192 pr_info("attempting corrupted list addition\n");
194 * In simulating this "write what where" primitive, the "what" is
195 * the address of &bad.node, and the "where" is the address held
198 test_head.next = redirection;
199 list_add(&bad.node, &test_head);
201 if (target[0] == NULL && target[1] == NULL)
202 pr_err("Overwrite did not happen, but no BUG?!\n");
204 pr_err("list_add() corruption not detected!\n");
207 void lkdtm_CORRUPT_LIST_DEL(void)
209 LIST_HEAD(test_head);
210 struct lkdtm_list item;
211 void *target[2] = { };
212 void *redirection = ⌖
214 list_add(&item.node, &test_head);
216 pr_info("attempting good list removal\n");
217 list_del(&item.node);
219 pr_info("attempting corrupted list removal\n");
220 list_add(&item.node, &test_head);
222 /* As with the list_add() test above, this corrupts "next". */
223 item.node.next = redirection;
224 list_del(&item.node);
226 if (target[0] == NULL && target[1] == NULL)
227 pr_err("Overwrite did not happen, but no BUG?!\n");
229 pr_err("list_del() corruption not detected!\n");
232 /* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
233 void lkdtm_CORRUPT_USER_DS(void)
235 pr_info("setting bad task size limit\n");
238 /* Make sure we do not keep running with a KERNEL_DS! */
242 /* Test that VMAP_STACK is actually allocating with a leading guard page */
243 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
245 const unsigned char *stack = task_stack_page(current);
246 const unsigned char *ptr = stack - 1;
247 volatile unsigned char byte;
249 pr_info("attempting bad read from page below current stack\n");
253 pr_err("FAIL: accessed page before stack!\n");
256 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
257 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
259 const unsigned char *stack = task_stack_page(current);
260 const unsigned char *ptr = stack + THREAD_SIZE;
261 volatile unsigned char byte;
263 pr_info("attempting bad read from page above current stack\n");
267 pr_err("FAIL: accessed page after stack!\n");
270 void lkdtm_UNSET_SMEP(void)
273 #define MOV_CR4_DEPTH 64
274 void (*direct_write_cr4)(unsigned long val);
279 cr4 = native_read_cr4();
281 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
282 pr_err("FAIL: SMEP not in use\n");
285 cr4 &= ~(X86_CR4_SMEP);
287 pr_info("trying to clear SMEP normally\n");
288 native_write_cr4(cr4);
289 if (cr4 == native_read_cr4()) {
290 pr_err("FAIL: pinning SMEP failed!\n");
292 pr_info("restoring SMEP\n");
293 native_write_cr4(cr4);
296 pr_info("ok: SMEP did not get cleared\n");
299 * To test the post-write pinning verification we need to call
300 * directly into the middle of native_write_cr4() where the
301 * cr4 write happens, skipping any pinning. This searches for
302 * the cr4 writing instruction.
304 insn = (unsigned char *)native_write_cr4;
305 for (i = 0; i < MOV_CR4_DEPTH; i++) {
307 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
309 /* mov %rdi,%rax; mov %rax, %cr4 */
310 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
311 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
312 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
315 if (i >= MOV_CR4_DEPTH) {
316 pr_info("ok: cannot locate cr4 writing call gadget\n");
319 direct_write_cr4 = (void *)(insn + i);
321 pr_info("trying to clear SMEP with call gadget\n");
322 direct_write_cr4(cr4);
323 if (native_read_cr4() & X86_CR4_SMEP) {
324 pr_info("ok: SMEP removal was reverted\n");
326 pr_err("FAIL: cleared SMEP not detected!\n");
328 pr_info("restoring SMEP\n");
329 native_write_cr4(cr4);
332 pr_err("FAIL: this test is x86_64-only\n");