]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/misc/lkdtm_bugs.c
lkdtm: Provide timing tests for atomic_t vs refcount_t
[mirror_ubuntu-bionic-kernel.git] / drivers / misc / lkdtm_bugs.c
CommitLineData
00f496c4
KC
1/*
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 */
00f496c4 7#include "lkdtm.h"
6819d101 8#include <linux/list.h>
6d2e91a6 9#include <linux/sched.h>
e22aa9d7
KC
10#include <linux/sched/signal.h>
11#include <linux/uaccess.h>
00f496c4 12
6819d101
KC
13struct lkdtm_list {
14 struct list_head node;
15};
16
00f496c4
KC
17/*
18 * Make sure our attempts to over run the kernel stack doesn't trigger
19 * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
20 * recurse past the end of THREAD_SIZE by default.
21 */
22#if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
23#define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
24#else
25#define REC_STACK_SIZE (THREAD_SIZE / 8)
26#endif
27#define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
28
29static int recur_count = REC_NUM_DEFAULT;
30
31static DEFINE_SPINLOCK(lock_me_up);
32
33static int recursive_loop(int remaining)
34{
35 char buf[REC_STACK_SIZE];
36
37 /* Make sure compiler does not optimize this away. */
38 memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
39 if (!remaining)
40 return 0;
41 else
42 return recursive_loop(remaining - 1);
43}
44
45/* If the depth is negative, use the default, otherwise keep parameter. */
46void __init lkdtm_bugs_init(int *recur_param)
47{
48 if (*recur_param < 0)
49 *recur_param = recur_count;
50 else
51 recur_count = *recur_param;
52}
53
54void lkdtm_PANIC(void)
55{
56 panic("dumptest");
57}
58
59void lkdtm_BUG(void)
60{
61 BUG();
62}
63
64void lkdtm_WARNING(void)
65{
66 WARN_ON(1);
67}
68
69void lkdtm_EXCEPTION(void)
70{
9e18308a 71 *((volatile int *) 0) = 0;
00f496c4
KC
72}
73
74void lkdtm_LOOP(void)
75{
76 for (;;)
77 ;
78}
79
80void lkdtm_OVERFLOW(void)
81{
82 (void) recursive_loop(recur_count);
83}
84
7a11a1d1
AB
85static noinline void __lkdtm_CORRUPT_STACK(void *stack)
86{
87 memset(stack, 'a', 64);
88}
89
00f496c4
KC
90noinline void lkdtm_CORRUPT_STACK(void)
91{
92 /* Use default char array length that triggers stack protection. */
93 char data[8];
7a11a1d1 94 __lkdtm_CORRUPT_STACK(&data);
00f496c4 95
c55d2400 96 pr_info("Corrupted stack with '%16s'...\n", data);
00f496c4
KC
97}
98
99void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
100{
101 static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
102 u32 *p;
103 u32 val = 0x12345678;
104
105 p = (u32 *)(data + 1);
106 if (*p == 0)
107 val = 0x87654321;
108 *p = val;
109}
110
111void lkdtm_SOFTLOCKUP(void)
112{
113 preempt_disable();
114 for (;;)
115 cpu_relax();
116}
117
118void lkdtm_HARDLOCKUP(void)
119{
120 local_irq_disable();
121 for (;;)
122 cpu_relax();
123}
124
125void lkdtm_SPINLOCKUP(void)
126{
127 /* Must be called twice to trigger. */
128 spin_lock(&lock_me_up);
129 /* Let sparse know we intended to exit holding the lock. */
130 __release(&lock_me_up);
131}
132
133void lkdtm_HUNG_TASK(void)
134{
135 set_current_state(TASK_UNINTERRUPTIBLE);
136 schedule();
137}
138
6819d101
KC
139void lkdtm_CORRUPT_LIST_ADD(void)
140{
141 /*
142 * Initially, an empty list via LIST_HEAD:
143 * test_head.next = &test_head
144 * test_head.prev = &test_head
145 */
146 LIST_HEAD(test_head);
147 struct lkdtm_list good, bad;
148 void *target[2] = { };
149 void *redirection = &target;
150
151 pr_info("attempting good list addition\n");
152
153 /*
154 * Adding to the list performs these actions:
155 * test_head.next->prev = &good.node
156 * good.node.next = test_head.next
157 * good.node.prev = test_head
158 * test_head.next = good.node
159 */
160 list_add(&good.node, &test_head);
161
162 pr_info("attempting corrupted list addition\n");
163 /*
164 * In simulating this "write what where" primitive, the "what" is
165 * the address of &bad.node, and the "where" is the address held
166 * by "redirection".
167 */
168 test_head.next = redirection;
169 list_add(&bad.node, &test_head);
170
171 if (target[0] == NULL && target[1] == NULL)
172 pr_err("Overwrite did not happen, but no BUG?!\n");
173 else
174 pr_err("list_add() corruption not detected!\n");
175}
176
177void lkdtm_CORRUPT_LIST_DEL(void)
178{
179 LIST_HEAD(test_head);
180 struct lkdtm_list item;
181 void *target[2] = { };
182 void *redirection = &target;
183
184 list_add(&item.node, &test_head);
185
186 pr_info("attempting good list removal\n");
187 list_del(&item.node);
188
189 pr_info("attempting corrupted list removal\n");
190 list_add(&item.node, &test_head);
191
192 /* As with the list_add() test above, this corrupts "next". */
193 item.node.next = redirection;
194 list_del(&item.node);
195
196 if (target[0] == NULL && target[1] == NULL)
197 pr_err("Overwrite did not happen, but no BUG?!\n");
198 else
199 pr_err("list_del() corruption not detected!\n");
200}
e22aa9d7
KC
201
202void lkdtm_CORRUPT_USER_DS(void)
203{
204 pr_info("setting bad task size limit\n");
205 set_fs(KERNEL_DS);
206
207 /* Make sure we do not keep running with a KERNEL_DS! */
208 force_sig(SIGKILL, current);
209}