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/sched.h>
12 struct list_head node;
16 * Make sure our attempts to over run the kernel stack doesn't trigger
17 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
18 * recurse past the end of THREAD_SIZE by default.
20 #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
21 #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
23 #define REC_STACK_SIZE (THREAD_SIZE / 8)
25 #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
27 static int recur_count = REC_NUM_DEFAULT;
29 static DEFINE_SPINLOCK(lock_me_up);
31 static int recursive_loop(int remaining)
33 char buf[REC_STACK_SIZE];
35 /* Make sure compiler does not optimize this away. */
36 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
40 return recursive_loop(remaining - 1);
43 /* If the depth is negative, use the default, otherwise keep parameter. */
44 void __init lkdtm_bugs_init(int *recur_param)
47 *recur_param = recur_count;
49 recur_count = *recur_param;
52 void lkdtm_PANIC(void)
62 void lkdtm_WARNING(void)
67 void lkdtm_EXCEPTION(void)
78 void lkdtm_OVERFLOW(void)
80 (void) recursive_loop(recur_count);
83 noinline void lkdtm_CORRUPT_STACK(void)
85 /* Use default char array length that triggers stack protection. */
88 memset((void *)data, 0, 64);
91 void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
93 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
97 p = (u32 *)(data + 1);
103 void lkdtm_SOFTLOCKUP(void)
110 void lkdtm_HARDLOCKUP(void)
117 void lkdtm_SPINLOCKUP(void)
119 /* Must be called twice to trigger. */
120 spin_lock(&lock_me_up);
121 /* Let sparse know we intended to exit holding the lock. */
122 __release(&lock_me_up);
125 void lkdtm_HUNG_TASK(void)
127 set_current_state(TASK_UNINTERRUPTIBLE);
131 void lkdtm_ATOMIC_UNDERFLOW(void)
133 atomic_t under = ATOMIC_INIT(INT_MIN);
135 pr_info("attempting good atomic increment\n");
139 pr_info("attempting bad atomic underflow\n");
143 void lkdtm_ATOMIC_OVERFLOW(void)
145 atomic_t over = ATOMIC_INIT(INT_MAX);
147 pr_info("attempting good atomic decrement\n");
151 pr_info("attempting bad atomic overflow\n");
155 void lkdtm_CORRUPT_LIST_ADD(void)
158 * Initially, an empty list via LIST_HEAD:
159 * test_head.next = &test_head
160 * test_head.prev = &test_head
162 LIST_HEAD(test_head);
163 struct lkdtm_list good, bad;
164 void *target[2] = { };
165 void *redirection = ⌖
167 pr_info("attempting good list addition\n");
170 * Adding to the list performs these actions:
171 * test_head.next->prev = &good.node
172 * good.node.next = test_head.next
173 * good.node.prev = test_head
174 * test_head.next = good.node
176 list_add(&good.node, &test_head);
178 pr_info("attempting corrupted list addition\n");
180 * In simulating this "write what where" primitive, the "what" is
181 * the address of &bad.node, and the "where" is the address held
184 test_head.next = redirection;
185 list_add(&bad.node, &test_head);
187 if (target[0] == NULL && target[1] == NULL)
188 pr_err("Overwrite did not happen, but no BUG?!\n");
190 pr_err("list_add() corruption not detected!\n");
193 void lkdtm_CORRUPT_LIST_DEL(void)
195 LIST_HEAD(test_head);
196 struct lkdtm_list item;
197 void *target[2] = { };
198 void *redirection = ⌖
200 list_add(&item.node, &test_head);
202 pr_info("attempting good list removal\n");
203 list_del(&item.node);
205 pr_info("attempting corrupted list removal\n");
206 list_add(&item.node, &test_head);
208 /* As with the list_add() test above, this corrupts "next". */
209 item.node.next = redirection;
210 list_del(&item.node);
212 if (target[0] == NULL && target[1] == NULL)
213 pr_err("Overwrite did not happen, but no BUG?!\n");
215 pr_err("list_del() corruption not detected!\n");