blob: da1cf47e554afe9979523348293d3b70a25389a0 [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 Cookd8ee5d32016-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 Cookd8ee5d32016-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{
70 *((int *) 0) = 0;
71}
72
73void lkdtm_LOOP(void)
74{
75 for (;;)
76 ;
77}
78
79void lkdtm_OVERFLOW(void)
80{
81 (void) recursive_loop(recur_count);
82}
83
84noinline void lkdtm_CORRUPT_STACK(void)
85{
86 /* Use default char array length that triggers stack protection. */
87 char data[8];
88
89 memset((void *)data, 0, 64);
90}
91
92void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
93{
94 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
95 u32 *p;
96 u32 val = 0x12345678;
97
98 p = (u32 *)(data + 1);
99 if (*p == 0)
100 val = 0x87654321;
101 *p = val;
102}
103
104void lkdtm_SOFTLOCKUP(void)
105{
106 preempt_disable();
107 for (;;)
108 cpu_relax();
109}
110
111void lkdtm_HARDLOCKUP(void)
112{
113 local_irq_disable();
114 for (;;)
115 cpu_relax();
116}
117
118void lkdtm_SPINLOCKUP(void)
119{
120 /* Must be called twice to trigger. */
121 spin_lock(&lock_me_up);
122 /* Let sparse know we intended to exit holding the lock. */
123 __release(&lock_me_up);
124}
125
126void lkdtm_HUNG_TASK(void)
127{
128 set_current_state(TASK_UNINTERRUPTIBLE);
129 schedule();
130}
131
132void lkdtm_ATOMIC_UNDERFLOW(void)
133{
134 atomic_t under = ATOMIC_INIT(INT_MIN);
135
136 pr_info("attempting good atomic increment\n");
137 atomic_inc(&under);
138 atomic_dec(&under);
139
140 pr_info("attempting bad atomic underflow\n");
141 atomic_dec(&under);
142}
143
144void lkdtm_ATOMIC_OVERFLOW(void)
145{
146 atomic_t over = ATOMIC_INIT(INT_MAX);
147
148 pr_info("attempting good atomic decrement\n");
149 atomic_dec(&over);
150 atomic_inc(&over);
151
152 pr_info("attempting bad atomic overflow\n");
153 atomic_inc(&over);
154}
Kees Cookd8ee5d32016-08-17 14:42:12 -0700155
156void lkdtm_CORRUPT_LIST_ADD(void)
157{
158 /*
159 * Initially, an empty list via LIST_HEAD:
160 * test_head.next = &test_head
161 * test_head.prev = &test_head
162 */
163 LIST_HEAD(test_head);
164 struct lkdtm_list good, bad;
165 void *target[2] = { };
166 void *redirection = &target;
167
168 pr_info("attempting good list addition\n");
169
170 /*
171 * Adding to the list performs these actions:
172 * test_head.next->prev = &good.node
173 * good.node.next = test_head.next
174 * good.node.prev = test_head
175 * test_head.next = good.node
176 */
177 list_add(&good.node, &test_head);
178
179 pr_info("attempting corrupted list addition\n");
180 /*
181 * In simulating this "write what where" primitive, the "what" is
182 * the address of &bad.node, and the "where" is the address held
183 * by "redirection".
184 */
185 test_head.next = redirection;
186 list_add(&bad.node, &test_head);
187
188 if (target[0] == NULL && target[1] == NULL)
189 pr_err("Overwrite did not happen, but no BUG?!\n");
190 else
191 pr_err("list_add() corruption not detected!\n");
192}
193
194void lkdtm_CORRUPT_LIST_DEL(void)
195{
196 LIST_HEAD(test_head);
197 struct lkdtm_list item;
198 void *target[2] = { };
199 void *redirection = &target;
200
201 list_add(&item.node, &test_head);
202
203 pr_info("attempting good list removal\n");
204 list_del(&item.node);
205
206 pr_info("attempting corrupted list removal\n");
207 list_add(&item.node, &test_head);
208
209 /* As with the list_add() test above, this corrupts "next". */
210 item.node.next = redirection;
211 list_del(&item.node);
212
213 if (target[0] == NULL && target[1] == NULL)
214 pr_err("Overwrite did not happen, but no BUG?!\n");
215 else
216 pr_err("list_del() corruption not detected!\n");
217}
Kees Cookba9f901c2017-03-24 10:51:25 -0700218
219void lkdtm_CORRUPT_USER_DS(void)
220{
221 pr_info("setting bad task size limit\n");
222 set_fs(KERNEL_DS);
223
224 /* Make sure we do not keep running with a KERNEL_DS! */
225 force_sig(SIGKILL, current);
226}