blob: bb3bb8ef5f4401d13e5e6b35e8797cb48dc8f081 [file] [log] [blame]
Kees Cook00f496c2016-06-26 22:17:25 -07001/*
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
5 * test source files.
6 */
Kees Cook00f496c2016-06-26 22:17:25 -07007#include "lkdtm.h"
Kees Cookbef1d722016-08-17 14:42:12 -07008#include <linux/list.h>
Kees Cook6d2e91a2016-07-15 16:04:39 -07009#include <linux/sched.h>
Kees Cook00f496c2016-06-26 22:17:25 -070010
Kees Cookbef1d722016-08-17 14:42:12 -070011struct lkdtm_list {
12 struct list_head node;
13};
14
Kees Cook00f496c2016-06-26 22:17:25 -070015/*
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.
19 */
20#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
21#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
22#else
23#define REC_STACK_SIZE (THREAD_SIZE / 8)
24#endif
25#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
26
27static int recur_count = REC_NUM_DEFAULT;
28
29static DEFINE_SPINLOCK(lock_me_up);
30
31static int recursive_loop(int remaining)
32{
33 char buf[REC_STACK_SIZE];
34
35 /* Make sure compiler does not optimize this away. */
36 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
37 if (!remaining)
38 return 0;
39 else
40 return recursive_loop(remaining - 1);
41}
42
43/* If the depth is negative, use the default, otherwise keep parameter. */
44void __init lkdtm_bugs_init(int *recur_param)
45{
46 if (*recur_param < 0)
47 *recur_param = recur_count;
48 else
49 recur_count = *recur_param;
50}
51
52void lkdtm_PANIC(void)
53{
54 panic("dumptest");
55}
56
57void lkdtm_BUG(void)
58{
59 BUG();
60}
61
62void lkdtm_WARNING(void)
63{
64 WARN_ON(1);
65}
66
67void lkdtm_EXCEPTION(void)
68{
69 *((int *) 0) = 0;
70}
71
72void lkdtm_LOOP(void)
73{
74 for (;;)
75 ;
76}
77
78void lkdtm_OVERFLOW(void)
79{
80 (void) recursive_loop(recur_count);
81}
82
Arnd Bergmanndf3d6382017-01-11 15:56:44 +010083static noinline void __lkdtm_CORRUPT_STACK(void *stack)
84{
85 memset(stack, 'a', 64);
86}
87
Kees Cook00f496c2016-06-26 22:17:25 -070088noinline void lkdtm_CORRUPT_STACK(void)
89{
90 /* Use default char array length that triggers stack protection. */
91 char data[8];
Arnd Bergmanndf3d6382017-01-11 15:56:44 +010092 __lkdtm_CORRUPT_STACK(&data);
Kees Cook00f496c2016-06-26 22:17:25 -070093
Michael Ellerman5a12d922016-11-15 18:02:32 +110094 pr_info("Corrupted stack with '%16s'...\n", data);
Kees Cook00f496c2016-06-26 22:17:25 -070095}
96
97void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
98{
99 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
100 u32 *p;
101 u32 val = 0x12345678;
102
103 p = (u32 *)(data + 1);
104 if (*p == 0)
105 val = 0x87654321;
106 *p = val;
107}
108
109void lkdtm_SOFTLOCKUP(void)
110{
111 preempt_disable();
112 for (;;)
113 cpu_relax();
114}
115
116void lkdtm_HARDLOCKUP(void)
117{
118 local_irq_disable();
119 for (;;)
120 cpu_relax();
121}
122
123void lkdtm_SPINLOCKUP(void)
124{
125 /* Must be called twice to trigger. */
126 spin_lock(&lock_me_up);
127 /* Let sparse know we intended to exit holding the lock. */
128 __release(&lock_me_up);
129}
130
131void lkdtm_HUNG_TASK(void)
132{
133 set_current_state(TASK_UNINTERRUPTIBLE);
134 schedule();
135}
136
137void lkdtm_ATOMIC_UNDERFLOW(void)
138{
139 atomic_t under = ATOMIC_INIT(INT_MIN);
140
141 pr_info("attempting good atomic increment\n");
142 atomic_inc(&under);
143 atomic_dec(&under);
144
145 pr_info("attempting bad atomic underflow\n");
146 atomic_dec(&under);
147}
148
149void lkdtm_ATOMIC_OVERFLOW(void)
150{
151 atomic_t over = ATOMIC_INIT(INT_MAX);
152
153 pr_info("attempting good atomic decrement\n");
154 atomic_dec(&over);
155 atomic_inc(&over);
156
157 pr_info("attempting bad atomic overflow\n");
158 atomic_inc(&over);
159}
Kees Cookbef1d722016-08-17 14:42:12 -0700160
161void lkdtm_CORRUPT_LIST_ADD(void)
162{
163 /*
164 * Initially, an empty list via LIST_HEAD:
165 * test_head.next = &test_head
166 * test_head.prev = &test_head
167 */
168 LIST_HEAD(test_head);
169 struct lkdtm_list good, bad;
170 void *target[2] = { };
171 void *redirection = &target;
172
173 pr_info("attempting good list addition\n");
174
175 /*
176 * Adding to the list performs these actions:
177 * test_head.next->prev = &good.node
178 * good.node.next = test_head.next
179 * good.node.prev = test_head
180 * test_head.next = good.node
181 */
182 list_add(&good.node, &test_head);
183
184 pr_info("attempting corrupted list addition\n");
185 /*
186 * In simulating this "write what where" primitive, the "what" is
187 * the address of &bad.node, and the "where" is the address held
188 * by "redirection".
189 */
190 test_head.next = redirection;
191 list_add(&bad.node, &test_head);
192
193 if (target[0] == NULL && target[1] == NULL)
194 pr_err("Overwrite did not happen, but no BUG?!\n");
195 else
196 pr_err("list_add() corruption not detected!\n");
197}
198
199void lkdtm_CORRUPT_LIST_DEL(void)
200{
201 LIST_HEAD(test_head);
202 struct lkdtm_list item;
203 void *target[2] = { };
204 void *redirection = &target;
205
206 list_add(&item.node, &test_head);
207
208 pr_info("attempting good list removal\n");
209 list_del(&item.node);
210
211 pr_info("attempting corrupted list removal\n");
212 list_add(&item.node, &test_head);
213
214 /* As with the list_add() test above, this corrupts "next". */
215 item.node.next = redirection;
216 list_del(&item.node);
217
218 if (target[0] == NULL && target[1] == NULL)
219 pr_err("Overwrite did not happen, but no BUG?!\n");
220 else
221 pr_err("list_del() corruption not detected!\n");
222}