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