2 * This is for all the tests related to logic bugs (e.g. bad dereferences,
3 * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
4 * lockups) along with other things that don't fit well into existing LKDTM
8 #include <linux/list.h>
9 #include <linux/refcount.h>
10 #include <linux/sched.h>
11 #include <linux/sched/signal.h>
12 #include <linux/uaccess.h>
15 struct list_head node
;
19 * Make sure our attempts to over run the kernel stack doesn't trigger
20 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
21 * recurse past the end of THREAD_SIZE by default.
23 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
24 #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
26 #define REC_STACK_SIZE (THREAD_SIZE / 8)
28 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
30 static int recur_count
= REC_NUM_DEFAULT
;
32 static DEFINE_SPINLOCK(lock_me_up
);
34 static int recursive_loop(int remaining
)
36 char buf
[REC_STACK_SIZE
];
38 /* Make sure compiler does not optimize this away. */
39 memset(buf
, (remaining
& 0xff) | 0x1, REC_STACK_SIZE
);
43 return recursive_loop(remaining
- 1);
46 /* If the depth is negative, use the default, otherwise keep parameter. */
47 void __init
lkdtm_bugs_init(int *recur_param
)
50 *recur_param
= recur_count
;
52 recur_count
= *recur_param
;
55 void lkdtm_PANIC(void)
65 void lkdtm_WARNING(void)
70 void lkdtm_EXCEPTION(void)
72 *((volatile int *) 0) = 0;
81 void lkdtm_OVERFLOW(void)
83 (void) recursive_loop(recur_count
);
86 static noinline
void __lkdtm_CORRUPT_STACK(void *stack
)
88 memset(stack
, 'a', 64);
91 noinline
void lkdtm_CORRUPT_STACK(void)
93 /* Use default char array length that triggers stack protection. */
95 __lkdtm_CORRUPT_STACK(&data
);
97 pr_info("Corrupted stack with '%16s'...\n", data
);
100 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
102 static u8 data
[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
104 u32 val
= 0x12345678;
106 p
= (u32
*)(data
+ 1);
112 void lkdtm_SOFTLOCKUP(void)
119 void lkdtm_HARDLOCKUP(void)
126 void lkdtm_SPINLOCKUP(void)
128 /* Must be called twice to trigger. */
129 spin_lock(&lock_me_up
);
130 /* Let sparse know we intended to exit holding the lock. */
131 __release(&lock_me_up
);
134 void lkdtm_HUNG_TASK(void)
136 set_current_state(TASK_UNINTERRUPTIBLE
);
140 void lkdtm_REFCOUNT_SATURATE_INC(void)
142 refcount_t over
= REFCOUNT_INIT(UINT_MAX
- 1);
144 pr_info("attempting good refcount decrement\n");
148 pr_info("attempting bad refcount inc overflow\n");
151 if (refcount_read(&over
) == UINT_MAX
)
152 pr_err("Correctly stayed saturated, but no BUG?!\n");
154 pr_err("Fail: refcount wrapped\n");
157 void lkdtm_REFCOUNT_SATURATE_ADD(void)
159 refcount_t over
= REFCOUNT_INIT(UINT_MAX
- 1);
161 pr_info("attempting good refcount decrement\n");
165 pr_info("attempting bad refcount add overflow\n");
166 refcount_add(2, &over
);
167 if (refcount_read(&over
) == UINT_MAX
)
168 pr_err("Correctly stayed saturated, but no BUG?!\n");
170 pr_err("Fail: refcount wrapped\n");
173 void lkdtm_REFCOUNT_ZERO_DEC(void)
175 refcount_t zero
= REFCOUNT_INIT(1);
177 pr_info("attempting bad refcount decrement to zero\n");
179 if (refcount_read(&zero
) == 0)
180 pr_err("Stayed at zero, but no BUG?!\n");
182 pr_err("Fail: refcount went crazy\n");
185 void lkdtm_REFCOUNT_ZERO_SUB(void)
187 refcount_t zero
= REFCOUNT_INIT(1);
189 pr_info("attempting bad refcount subtract past zero\n");
190 if (!refcount_sub_and_test(2, &zero
))
191 pr_info("wrap attempt was noticed\n");
192 if (refcount_read(&zero
) == 1)
193 pr_err("Correctly stayed above 0, but no BUG?!\n");
195 pr_err("Fail: refcount wrapped\n");
198 void lkdtm_REFCOUNT_ZERO_INC(void)
200 refcount_t zero
= REFCOUNT_INIT(0);
202 pr_info("attempting bad refcount increment from zero\n");
204 if (refcount_read(&zero
) == 0)
205 pr_err("Stayed at zero, but no BUG?!\n");
207 pr_err("Fail: refcount went past zero\n");
210 void lkdtm_REFCOUNT_ZERO_ADD(void)
212 refcount_t zero
= REFCOUNT_INIT(0);
214 pr_info("attempting bad refcount addition from zero\n");
215 refcount_add(2, &zero
);
216 if (refcount_read(&zero
) == 0)
217 pr_err("Stayed at zero, but no BUG?!\n");
219 pr_err("Fail: refcount went past zero\n");
222 void lkdtm_CORRUPT_LIST_ADD(void)
225 * Initially, an empty list via LIST_HEAD:
226 * test_head.next = &test_head
227 * test_head.prev = &test_head
229 LIST_HEAD(test_head
);
230 struct lkdtm_list good
, bad
;
231 void *target
[2] = { };
232 void *redirection
= &target
;
234 pr_info("attempting good list addition\n");
237 * Adding to the list performs these actions:
238 * test_head.next->prev = &good.node
239 * good.node.next = test_head.next
240 * good.node.prev = test_head
241 * test_head.next = good.node
243 list_add(&good
.node
, &test_head
);
245 pr_info("attempting corrupted list addition\n");
247 * In simulating this "write what where" primitive, the "what" is
248 * the address of &bad.node, and the "where" is the address held
251 test_head
.next
= redirection
;
252 list_add(&bad
.node
, &test_head
);
254 if (target
[0] == NULL
&& target
[1] == NULL
)
255 pr_err("Overwrite did not happen, but no BUG?!\n");
257 pr_err("list_add() corruption not detected!\n");
260 void lkdtm_CORRUPT_LIST_DEL(void)
262 LIST_HEAD(test_head
);
263 struct lkdtm_list item
;
264 void *target
[2] = { };
265 void *redirection
= &target
;
267 list_add(&item
.node
, &test_head
);
269 pr_info("attempting good list removal\n");
270 list_del(&item
.node
);
272 pr_info("attempting corrupted list removal\n");
273 list_add(&item
.node
, &test_head
);
275 /* As with the list_add() test above, this corrupts "next". */
276 item
.node
.next
= redirection
;
277 list_del(&item
.node
);
279 if (target
[0] == NULL
&& target
[1] == NULL
)
280 pr_err("Overwrite did not happen, but no BUG?!\n");
282 pr_err("list_del() corruption not detected!\n");
285 void lkdtm_CORRUPT_USER_DS(void)
287 pr_info("setting bad task size limit\n");
290 /* Make sure we do not keep running with a KERNEL_DS! */
291 force_sig(SIGKILL
, current
);