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>
13 struct list_head node
;
17 * Make sure our attempts to over run the kernel stack doesn't trigger
18 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
19 * recurse past the end of THREAD_SIZE by default.
21 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
22 #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
24 #define REC_STACK_SIZE (THREAD_SIZE / 8)
26 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
28 static int recur_count
= REC_NUM_DEFAULT
;
30 static DEFINE_SPINLOCK(lock_me_up
);
32 static int recursive_loop(int remaining
)
34 char buf
[REC_STACK_SIZE
];
36 /* Make sure compiler does not optimize this away. */
37 memset(buf
, (remaining
& 0xff) | 0x1, REC_STACK_SIZE
);
41 return recursive_loop(remaining
- 1);
44 /* If the depth is negative, use the default, otherwise keep parameter. */
45 void __init
lkdtm_bugs_init(int *recur_param
)
48 *recur_param
= recur_count
;
50 recur_count
= *recur_param
;
53 void lkdtm_PANIC(void)
63 void lkdtm_WARNING(void)
68 void lkdtm_EXCEPTION(void)
79 void lkdtm_OVERFLOW(void)
81 (void) recursive_loop(recur_count
);
84 static noinline
void __lkdtm_CORRUPT_STACK(void *stack
)
86 memset(stack
, 'a', 64);
89 noinline
void lkdtm_CORRUPT_STACK(void)
91 /* Use default char array length that triggers stack protection. */
93 __lkdtm_CORRUPT_STACK(&data
);
95 pr_info("Corrupted stack with '%16s'...\n", data
);
98 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
100 static u8 data
[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
102 u32 val
= 0x12345678;
104 p
= (u32
*)(data
+ 1);
110 void lkdtm_SOFTLOCKUP(void)
117 void lkdtm_HARDLOCKUP(void)
124 void lkdtm_SPINLOCKUP(void)
126 /* Must be called twice to trigger. */
127 spin_lock(&lock_me_up
);
128 /* Let sparse know we intended to exit holding the lock. */
129 __release(&lock_me_up
);
132 void lkdtm_HUNG_TASK(void)
134 set_current_state(TASK_UNINTERRUPTIBLE
);
138 void lkdtm_REFCOUNT_SATURATE_INC(void)
140 refcount_t over
= REFCOUNT_INIT(UINT_MAX
- 1);
142 pr_info("attempting good refcount decrement\n");
146 pr_info("attempting bad refcount inc overflow\n");
149 if (refcount_read(&over
) == UINT_MAX
)
150 pr_err("Correctly stayed saturated, but no BUG?!\n");
152 pr_err("Fail: refcount wrapped\n");
155 void lkdtm_REFCOUNT_SATURATE_ADD(void)
157 refcount_t over
= REFCOUNT_INIT(UINT_MAX
- 1);
159 pr_info("attempting good refcount decrement\n");
163 pr_info("attempting bad refcount add overflow\n");
164 refcount_add(2, &over
);
165 if (refcount_read(&over
) == UINT_MAX
)
166 pr_err("Correctly stayed saturated, but no BUG?!\n");
168 pr_err("Fail: refcount wrapped\n");
171 void lkdtm_REFCOUNT_ZERO_DEC(void)
173 refcount_t zero
= REFCOUNT_INIT(1);
175 pr_info("attempting bad refcount decrement to zero\n");
177 if (refcount_read(&zero
) == 0)
178 pr_err("Stayed at zero, but no BUG?!\n");
180 pr_err("Fail: refcount went crazy\n");
183 void lkdtm_REFCOUNT_ZERO_SUB(void)
185 refcount_t zero
= REFCOUNT_INIT(1);
187 pr_info("attempting bad refcount subtract past zero\n");
188 if (!refcount_sub_and_test(2, &zero
))
189 pr_info("wrap attempt was noticed\n");
190 if (refcount_read(&zero
) == 1)
191 pr_err("Correctly stayed above 0, but no BUG?!\n");
193 pr_err("Fail: refcount wrapped\n");
196 void lkdtm_REFCOUNT_ZERO_INC(void)
198 refcount_t zero
= REFCOUNT_INIT(0);
200 pr_info("attempting bad refcount increment from zero\n");
202 if (refcount_read(&zero
) == 0)
203 pr_err("Stayed at zero, but no BUG?!\n");
205 pr_err("Fail: refcount went past zero\n");
208 void lkdtm_REFCOUNT_ZERO_ADD(void)
210 refcount_t zero
= REFCOUNT_INIT(0);
212 pr_info("attempting bad refcount addition from zero\n");
213 refcount_add(2, &zero
);
214 if (refcount_read(&zero
) == 0)
215 pr_err("Stayed at zero, but no BUG?!\n");
217 pr_err("Fail: refcount went past zero\n");
220 void lkdtm_CORRUPT_LIST_ADD(void)
223 * Initially, an empty list via LIST_HEAD:
224 * test_head.next = &test_head
225 * test_head.prev = &test_head
227 LIST_HEAD(test_head
);
228 struct lkdtm_list good
, bad
;
229 void *target
[2] = { };
230 void *redirection
= &target
;
232 pr_info("attempting good list addition\n");
235 * Adding to the list performs these actions:
236 * test_head.next->prev = &good.node
237 * good.node.next = test_head.next
238 * good.node.prev = test_head
239 * test_head.next = good.node
241 list_add(&good
.node
, &test_head
);
243 pr_info("attempting corrupted list addition\n");
245 * In simulating this "write what where" primitive, the "what" is
246 * the address of &bad.node, and the "where" is the address held
249 test_head
.next
= redirection
;
250 list_add(&bad
.node
, &test_head
);
252 if (target
[0] == NULL
&& target
[1] == NULL
)
253 pr_err("Overwrite did not happen, but no BUG?!\n");
255 pr_err("list_add() corruption not detected!\n");
258 void lkdtm_CORRUPT_LIST_DEL(void)
260 LIST_HEAD(test_head
);
261 struct lkdtm_list item
;
262 void *target
[2] = { };
263 void *redirection
= &target
;
265 list_add(&item
.node
, &test_head
);
267 pr_info("attempting good list removal\n");
268 list_del(&item
.node
);
270 pr_info("attempting corrupted list removal\n");
271 list_add(&item
.node
, &test_head
);
273 /* As with the list_add() test above, this corrupts "next". */
274 item
.node
.next
= redirection
;
275 list_del(&item
.node
);
277 if (target
[0] == NULL
&& target
[1] == NULL
)
278 pr_err("Overwrite did not happen, but no BUG?!\n");
280 pr_err("list_del() corruption not detected!\n");