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
);
35 static int recursive_loop(int remaining
)
37 char buf
[REC_STACK_SIZE
];
39 /* Make sure compiler does not optimize this away. */
40 memset(buf
, (remaining
& 0xff) | 0x1, REC_STACK_SIZE
);
44 return recursive_loop(remaining
- 1);
47 /* If the depth is negative, use the default, otherwise keep parameter. */
48 void __init
lkdtm_bugs_init(int *recur_param
)
51 *recur_param
= recur_count
;
53 recur_count
= *recur_param
;
56 void lkdtm_PANIC(void)
66 void lkdtm_WARNING(void)
71 void lkdtm_EXCEPTION(void)
73 *((volatile int *) 0) = 0;
82 void lkdtm_OVERFLOW(void)
84 (void) recursive_loop(recur_count
);
87 static noinline
void __lkdtm_CORRUPT_STACK(void *stack
)
89 memset(stack
, '\xff', 64);
92 /* This should trip the stack canary, not corrupt the return address. */
93 noinline
void lkdtm_CORRUPT_STACK(void)
95 /* Use default char array length that triggers stack protection. */
96 char data
[8] __aligned(sizeof(void *));
98 __lkdtm_CORRUPT_STACK(&data
);
100 pr_info("Corrupted stack containing char array ...\n");
103 /* Same as above but will only get a canary with -fstack-protector-strong */
104 noinline
void lkdtm_CORRUPT_STACK_STRONG(void)
107 unsigned short shorts
[4];
109 } data
__aligned(sizeof(void *));
111 __lkdtm_CORRUPT_STACK(&data
);
113 pr_info("Corrupted stack containing union ...\n");
116 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
118 static u8 data
[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
120 u32 val
= 0x12345678;
122 p
= (u32
*)(data
+ 1);
128 void lkdtm_SOFTLOCKUP(void)
135 void lkdtm_HARDLOCKUP(void)
142 void lkdtm_SPINLOCKUP(void)
144 /* Must be called twice to trigger. */
145 spin_lock(&lock_me_up
);
146 /* Let sparse know we intended to exit holding the lock. */
147 __release(&lock_me_up
);
150 void lkdtm_HUNG_TASK(void)
152 set_current_state(TASK_UNINTERRUPTIBLE
);
156 void lkdtm_CORRUPT_LIST_ADD(void)
159 * Initially, an empty list via LIST_HEAD:
160 * test_head.next = &test_head
161 * test_head.prev = &test_head
163 LIST_HEAD(test_head
);
164 struct lkdtm_list good
, bad
;
165 void *target
[2] = { };
166 void *redirection
= &target
;
168 pr_info("attempting good list addition\n");
171 * Adding to the list performs these actions:
172 * test_head.next->prev = &good.node
173 * good.node.next = test_head.next
174 * good.node.prev = test_head
175 * test_head.next = good.node
177 list_add(&good
.node
, &test_head
);
179 pr_info("attempting corrupted list addition\n");
181 * In simulating this "write what where" primitive, the "what" is
182 * the address of &bad.node, and the "where" is the address held
185 test_head
.next
= redirection
;
186 list_add(&bad
.node
, &test_head
);
188 if (target
[0] == NULL
&& target
[1] == NULL
)
189 pr_err("Overwrite did not happen, but no BUG?!\n");
191 pr_err("list_add() corruption not detected!\n");
194 void lkdtm_CORRUPT_LIST_DEL(void)
196 LIST_HEAD(test_head
);
197 struct lkdtm_list item
;
198 void *target
[2] = { };
199 void *redirection
= &target
;
201 list_add(&item
.node
, &test_head
);
203 pr_info("attempting good list removal\n");
204 list_del(&item
.node
);
206 pr_info("attempting corrupted list removal\n");
207 list_add(&item
.node
, &test_head
);
209 /* As with the list_add() test above, this corrupts "next". */
210 item
.node
.next
= redirection
;
211 list_del(&item
.node
);
213 if (target
[0] == NULL
&& target
[1] == NULL
)
214 pr_err("Overwrite did not happen, but no BUG?!\n");
216 pr_err("list_del() corruption not detected!\n");
219 /* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
220 void lkdtm_CORRUPT_USER_DS(void)
222 pr_info("setting bad task size limit\n");
225 /* Make sure we do not keep running with a KERNEL_DS! */
226 force_sig(SIGKILL
, current
);
229 /* Test that VMAP_STACK is actually allocating with a leading guard page */
230 void lkdtm_STACK_GUARD_PAGE_LEADING(void)
232 const unsigned char *stack
= task_stack_page(current
);
233 const unsigned char *ptr
= stack
- 1;
234 volatile unsigned char byte
;
236 pr_info("attempting bad read from page below current stack\n");
240 pr_err("FAIL: accessed page before stack!\n");
243 /* Test that VMAP_STACK is actually allocating with a trailing guard page */
244 void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
246 const unsigned char *stack
= task_stack_page(current
);
247 const unsigned char *ptr
= stack
+ THREAD_SIZE
;
248 volatile unsigned char byte
;
250 pr_info("attempting bad read from page above current stack\n");
254 pr_err("FAIL: accessed page after stack!\n");