blob: 048a688ee0e4cdeb1c91abcd8e009d6d16a26ce4 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * This is for all the tests related to logic bugs (e.g. bad dereferences,
4 * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
5 * lockups) along with other things that don't fit well into existing LKDTM
6 * test source files.
7 */
8#include "lkdtm.h"
9#include <linux/list.h>
10#include <linux/sched.h>
11#include <linux/sched/signal.h>
12#include <linux/sched/task_stack.h>
13#include <linux/uaccess.h>
14#include <linux/slab.h>
15
16struct lkdtm_list {
17 struct list_head node;
18};
19
20/*
21 * Make sure our attempts to over run the kernel stack doesn't trigger
22 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
23 * recurse past the end of THREAD_SIZE by default.
24 */
25#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
26#define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
27#else
28#define REC_STACK_SIZE (THREAD_SIZE / 8)
29#endif
30#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
31
32static int recur_count = REC_NUM_DEFAULT;
33
34static DEFINE_SPINLOCK(lock_me_up);
35
36/*
37 * Make sure compiler does not optimize this function or stack frame away:
38 * - function marked noinline
39 * - stack variables are marked volatile
40 * - stack variables are written (memset()) and read (pr_info())
41 * - function has external effects (pr_info())
42 * */
43static int noinline recursive_loop(int remaining)
44{
45 volatile char buf[REC_STACK_SIZE];
46
47 memset((void *)buf, remaining & 0xFF, sizeof(buf));
48 pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)],
49 recur_count);
50 if (!remaining)
51 return 0;
52 else
53 return recursive_loop(remaining - 1);
54}
55
56/* If the depth is negative, use the default, otherwise keep parameter. */
57void __init lkdtm_bugs_init(int *recur_param)
58{
59 if (*recur_param < 0)
60 *recur_param = recur_count;
61 else
62 recur_count = *recur_param;
63}
64
65void lkdtm_PANIC(void)
66{
67 panic("dumptest");
68}
69
70void lkdtm_BUG(void)
71{
72 BUG();
73}
74
75static int warn_counter;
76
77void lkdtm_WARNING(void)
78{
79 WARN_ON(++warn_counter);
80}
81
82void lkdtm_WARNING_MESSAGE(void)
83{
84 WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
85}
86
87void lkdtm_EXCEPTION(void)
88{
89 *((volatile int *) 0) = 0;
90}
91
92void lkdtm_LOOP(void)
93{
94 for (;;)
95 ;
96}
97
98void lkdtm_EXHAUST_STACK(void)
99{
100 pr_info("Calling function with %lu frame size to depth %d ...\n",
101 REC_STACK_SIZE, recur_count);
102 recursive_loop(recur_count);
103 pr_info("FAIL: survived without exhausting stack?!\n");
104}
105
106static noinline void __lkdtm_CORRUPT_STACK(void *stack)
107{
108 memset(stack, '\xff', 64);
109}
110
111/* This should trip the stack canary, not corrupt the return address. */
112noinline void lkdtm_CORRUPT_STACK(void)
113{
114 /* Use default char array length that triggers stack protection. */
115 char data[8] __aligned(sizeof(void *));
116
117 pr_info("Corrupting stack containing char array ...\n");
118 __lkdtm_CORRUPT_STACK((void *)&data);
119}
120
121/* Same as above but will only get a canary with -fstack-protector-strong */
122noinline void lkdtm_CORRUPT_STACK_STRONG(void)
123{
124 union {
125 unsigned short shorts[4];
126 unsigned long *ptr;
127 } data __aligned(sizeof(void *));
128
129 pr_info("Corrupting stack containing union ...\n");
130 __lkdtm_CORRUPT_STACK((void *)&data);
131}
132
133void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
134{
135 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
136 u32 *p;
137 u32 val = 0x12345678;
138
139 p = (u32 *)(data + 1);
140 if (*p == 0)
141 val = 0x87654321;
142 *p = val;
143}
144
145void lkdtm_SOFTLOCKUP(void)
146{
147 preempt_disable();
148 for (;;)
149 cpu_relax();
150}
151
152void lkdtm_HARDLOCKUP(void)
153{
154 local_irq_disable();
155 for (;;)
156 cpu_relax();
157}
158
159void lkdtm_SPINLOCKUP(void)
160{
161 /* Must be called twice to trigger. */
162 spin_lock(&lock_me_up);
163 /* Let sparse know we intended to exit holding the lock. */
164 __release(&lock_me_up);
165}
166
167void lkdtm_HUNG_TASK(void)
168{
169 set_current_state(TASK_UNINTERRUPTIBLE);
170 schedule();
171}
172
173volatile unsigned int huge = INT_MAX - 2;
174volatile unsigned int ignored;
175
176void lkdtm_OVERFLOW_SIGNED(void)
177{
178 int value;
179
180 value = huge;
181 pr_info("Normal signed addition ...\n");
182 value += 1;
183 ignored = value;
184
185 pr_info("Overflowing signed addition ...\n");
186 value += 4;
187 ignored = value;
188}
189
190
191void lkdtm_OVERFLOW_UNSIGNED(void)
192{
193 unsigned int value;
194
195 value = huge;
196 pr_info("Normal unsigned addition ...\n");
197 value += 1;
198 ignored = value;
199
200 pr_info("Overflowing unsigned addition ...\n");
201 value += 4;
202 ignored = value;
203}
204
205/* Intentially using old-style flex array definition of 1 byte. */
206struct array_bounds_flex_array {
207 int one;
208 int two;
209 char data[1];
210};
211
212struct array_bounds {
213 int one;
214 int two;
215 char data[8];
216 int three;
217};
218
219void lkdtm_ARRAY_BOUNDS(void)
220{
221 struct array_bounds_flex_array *not_checked;
222 struct array_bounds *checked;
223 volatile int i;
224
225 not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
226 checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
227
228 pr_info("Array access within bounds ...\n");
229 /* For both, touch all bytes in the actual member size. */
230 for (i = 0; i < sizeof(checked->data); i++)
231 checked->data[i] = 'A';
232 /*
233 * For the uninstrumented flex array member, also touch 1 byte
234 * beyond to verify it is correctly uninstrumented.
235 */
236 for (i = 0; i < sizeof(not_checked->data) + 1; i++)
237 not_checked->data[i] = 'A';
238
239 pr_info("Array access beyond bounds ...\n");
240 for (i = 0; i < sizeof(checked->data) + 1; i++)
241 checked->data[i] = 'B';
242
243 kfree(not_checked);
244 kfree(checked);
245}
246
247void lkdtm_CORRUPT_LIST_ADD(void)
248{
249 /*
250 * Initially, an empty list via LIST_HEAD:
251 * test_head.next = &test_head
252 * test_head.prev = &test_head
253 */
254 LIST_HEAD(test_head);
255 struct lkdtm_list good, bad;
256 void *target[2] = { };
257 void *redirection = &target;
258
259 pr_info("attempting good list addition\n");
260
261 /*
262 * Adding to the list performs these actions:
263 * test_head.next->prev = &good.node
264 * good.node.next = test_head.next
265 * good.node.prev = test_head
266 * test_head.next = good.node
267 */
268 list_add(&good.node, &test_head);
269
270 pr_info("attempting corrupted list addition\n");
271 /*
272 * In simulating this "write what where" primitive, the "what" is
273 * the address of &bad.node, and the "where" is the address held
274 * by "redirection".
275 */
276 test_head.next = redirection;
277 list_add(&bad.node, &test_head);
278
279 if (target[0] == NULL && target[1] == NULL)
280 pr_err("Overwrite did not happen, but no BUG?!\n");
281 else
282 pr_err("list_add() corruption not detected!\n");
283}
284
285void lkdtm_CORRUPT_LIST_DEL(void)
286{
287 LIST_HEAD(test_head);
288 struct lkdtm_list item;
289 void *target[2] = { };
290 void *redirection = &target;
291
292 list_add(&item.node, &test_head);
293
294 pr_info("attempting good list removal\n");
295 list_del(&item.node);
296
297 pr_info("attempting corrupted list removal\n");
298 list_add(&item.node, &test_head);
299
300 /* As with the list_add() test above, this corrupts "next". */
301 item.node.next = redirection;
302 list_del(&item.node);
303
304 if (target[0] == NULL && target[1] == NULL)
305 pr_err("Overwrite did not happen, but no BUG?!\n");
306 else
307 pr_err("list_del() corruption not detected!\n");
308}
309
310/* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */
311void lkdtm_CORRUPT_USER_DS(void)
312{
313 pr_info("setting bad task size limit\n");
314 set_fs(KERNEL_DS);
315
316 /* Make sure we do not keep running with a KERNEL_DS! */
317 force_sig(SIGKILL);
318}
319
320/* Test that VMAP_STACK is actually allocating with a leading guard page */
321void lkdtm_STACK_GUARD_PAGE_LEADING(void)
322{
323 const unsigned char *stack = task_stack_page(current);
324 const unsigned char *ptr = stack - 1;
325 volatile unsigned char byte;
326
327 pr_info("attempting bad read from page below current stack\n");
328
329 byte = *ptr;
330
331 pr_err("FAIL: accessed page before stack!\n");
332}
333
334/* Test that VMAP_STACK is actually allocating with a trailing guard page */
335void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
336{
337 const unsigned char *stack = task_stack_page(current);
338 const unsigned char *ptr = stack + THREAD_SIZE;
339 volatile unsigned char byte;
340
341 pr_info("attempting bad read from page above current stack\n");
342
343 byte = *ptr;
344
345 pr_err("FAIL: accessed page after stack!\n");
346}
347
348void lkdtm_UNSET_SMEP(void)
349{
350#if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
351#define MOV_CR4_DEPTH 64
352 void (*direct_write_cr4)(unsigned long val);
353 unsigned char *insn;
354 unsigned long cr4;
355 int i;
356
357 cr4 = native_read_cr4();
358
359 if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
360 pr_err("FAIL: SMEP not in use\n");
361 return;
362 }
363 cr4 &= ~(X86_CR4_SMEP);
364
365 pr_info("trying to clear SMEP normally\n");
366 native_write_cr4(cr4);
367 if (cr4 == native_read_cr4()) {
368 pr_err("FAIL: pinning SMEP failed!\n");
369 cr4 |= X86_CR4_SMEP;
370 pr_info("restoring SMEP\n");
371 native_write_cr4(cr4);
372 return;
373 }
374 pr_info("ok: SMEP did not get cleared\n");
375
376 /*
377 * To test the post-write pinning verification we need to call
378 * directly into the middle of native_write_cr4() where the
379 * cr4 write happens, skipping any pinning. This searches for
380 * the cr4 writing instruction.
381 */
382 insn = (unsigned char *)native_write_cr4;
383 for (i = 0; i < MOV_CR4_DEPTH; i++) {
384 /* mov %rdi, %cr4 */
385 if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
386 break;
387 /* mov %rdi,%rax; mov %rax, %cr4 */
388 if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
389 insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
390 insn[i+4] == 0x22 && insn[i+5] == 0xe0)
391 break;
392 }
393 if (i >= MOV_CR4_DEPTH) {
394 pr_info("ok: cannot locate cr4 writing call gadget\n");
395 return;
396 }
397 direct_write_cr4 = (void *)(insn + i);
398
399 pr_info("trying to clear SMEP with call gadget\n");
400 direct_write_cr4(cr4);
401 if (native_read_cr4() & X86_CR4_SMEP) {
402 pr_info("ok: SMEP removal was reverted\n");
403 } else {
404 pr_err("FAIL: cleared SMEP not detected!\n");
405 cr4 |= X86_CR4_SMEP;
406 pr_info("restoring SMEP\n");
407 native_write_cr4(cr4);
408 }
409#else
410 pr_err("FAIL: this test is x86_64-only\n");
411#endif
412}