blob: abf3b7c1f686c13d3c7c0f782cc175fa719001cc [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 refcount bugs (e.g. overflow,
4 * underflow, reaching zero untested, etc).
5 */
6#include "lkdtm.h"
7#include <linux/refcount.h>
8
9static void overflow_check(refcount_t *ref)
10{
11 switch (refcount_read(ref)) {
12 case REFCOUNT_SATURATED:
13 pr_info("Overflow detected: saturated\n");
14 break;
15 case REFCOUNT_MAX:
16 pr_warn("Overflow detected: unsafely reset to max\n");
17 break;
18 default:
19 pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
20 }
21}
22
23/*
24 * A refcount_inc() above the maximum value of the refcount implementation,
25 * should at least saturate, and at most also WARN.
26 */
27void lkdtm_REFCOUNT_INC_OVERFLOW(void)
28{
29 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
30
31 pr_info("attempting good refcount_inc() without overflow\n");
32 refcount_dec(&over);
33 refcount_inc(&over);
34
35 pr_info("attempting bad refcount_inc() overflow\n");
36 refcount_inc(&over);
37 refcount_inc(&over);
38
39 overflow_check(&over);
40}
41
42/* refcount_add() should behave just like refcount_inc() above. */
43void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
44{
45 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
46
47 pr_info("attempting good refcount_add() without overflow\n");
48 refcount_dec(&over);
49 refcount_dec(&over);
50 refcount_dec(&over);
51 refcount_dec(&over);
52 refcount_add(4, &over);
53
54 pr_info("attempting bad refcount_add() overflow\n");
55 refcount_add(4, &over);
56
57 overflow_check(&over);
58}
59
60/* refcount_inc_not_zero() should behave just like refcount_inc() above. */
61void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
62{
63 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
64
65 pr_info("attempting bad refcount_inc_not_zero() overflow\n");
66 if (!refcount_inc_not_zero(&over))
67 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
68
69 overflow_check(&over);
70}
71
72/* refcount_add_not_zero() should behave just like refcount_inc() above. */
73void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
74{
75 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
76
77 pr_info("attempting bad refcount_add_not_zero() overflow\n");
78 if (!refcount_add_not_zero(6, &over))
79 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
80
81 overflow_check(&over);
82}
83
84static void check_zero(refcount_t *ref)
85{
86 switch (refcount_read(ref)) {
87 case REFCOUNT_SATURATED:
88 pr_info("Zero detected: saturated\n");
89 break;
90 case REFCOUNT_MAX:
91 pr_warn("Zero detected: unsafely reset to max\n");
92 break;
93 case 0:
94 pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
95 break;
96 default:
97 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
98 }
99}
100
101/*
102 * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits
103 * zero it should either saturate (when inc-from-zero isn't protected)
104 * or stay at zero (when inc-from-zero is protected) and should WARN for both.
105 */
106void lkdtm_REFCOUNT_DEC_ZERO(void)
107{
108 refcount_t zero = REFCOUNT_INIT(2);
109
110 pr_info("attempting good refcount_dec()\n");
111 refcount_dec(&zero);
112
113 pr_info("attempting bad refcount_dec() to zero\n");
114 refcount_dec(&zero);
115
116 check_zero(&zero);
117}
118
119static void check_negative(refcount_t *ref, int start)
120{
121 /*
122 * CONFIG_REFCOUNT_FULL refuses to move a refcount at all on an
123 * over-sub, so we have to track our starting position instead of
124 * looking only at zero-pinning.
125 */
126 if (refcount_read(ref) == start) {
127 pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
128 start);
129 return;
130 }
131
132 switch (refcount_read(ref)) {
133 case REFCOUNT_SATURATED:
134 pr_info("Negative detected: saturated\n");
135 break;
136 case REFCOUNT_MAX:
137 pr_warn("Negative detected: unsafely reset to max\n");
138 break;
139 default:
140 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
141 }
142}
143
144/* A refcount_dec() going negative should saturate and may WARN. */
145void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
146{
147 refcount_t neg = REFCOUNT_INIT(0);
148
149 pr_info("attempting bad refcount_dec() below zero\n");
150 refcount_dec(&neg);
151
152 check_negative(&neg, 0);
153}
154
155/*
156 * A refcount_dec_and_test() should act like refcount_dec() above when
157 * going negative.
158 */
159void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
160{
161 refcount_t neg = REFCOUNT_INIT(0);
162
163 pr_info("attempting bad refcount_dec_and_test() below zero\n");
164 if (refcount_dec_and_test(&neg))
165 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
166
167 check_negative(&neg, 0);
168}
169
170/*
171 * A refcount_sub_and_test() should act like refcount_dec_and_test()
172 * above when going negative.
173 */
174void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
175{
176 refcount_t neg = REFCOUNT_INIT(3);
177
178 pr_info("attempting bad refcount_sub_and_test() below zero\n");
179 if (refcount_sub_and_test(5, &neg))
180 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
181
182 check_negative(&neg, 3);
183}
184
185static void check_from_zero(refcount_t *ref)
186{
187 switch (refcount_read(ref)) {
188 case 0:
189 pr_info("Zero detected: stayed at zero\n");
190 break;
191 case REFCOUNT_SATURATED:
192 pr_info("Zero detected: saturated\n");
193 break;
194 case REFCOUNT_MAX:
195 pr_warn("Zero detected: unsafely reset to max\n");
196 break;
197 default:
198 pr_info("Fail: zero not detected, incremented to %d\n",
199 refcount_read(ref));
200 }
201}
202
203/*
204 * A refcount_inc() from zero should pin to zero or saturate and may WARN.
205 * Only CONFIG_REFCOUNT_FULL provides this protection currently.
206 */
207void lkdtm_REFCOUNT_INC_ZERO(void)
208{
209 refcount_t zero = REFCOUNT_INIT(0);
210
211 pr_info("attempting safe refcount_inc_not_zero() from zero\n");
212 if (!refcount_inc_not_zero(&zero)) {
213 pr_info("Good: zero detected\n");
214 if (refcount_read(&zero) == 0)
215 pr_info("Correctly stayed at zero\n");
216 else
217 pr_err("Fail: refcount went past zero!\n");
218 } else {
219 pr_err("Fail: Zero not detected!?\n");
220 }
221
222 pr_info("attempting bad refcount_inc() from zero\n");
223 refcount_inc(&zero);
224
225 check_from_zero(&zero);
226}
227
228/*
229 * A refcount_add() should act like refcount_inc() above when starting
230 * at zero.
231 */
232void lkdtm_REFCOUNT_ADD_ZERO(void)
233{
234 refcount_t zero = REFCOUNT_INIT(0);
235
236 pr_info("attempting safe refcount_add_not_zero() from zero\n");
237 if (!refcount_add_not_zero(3, &zero)) {
238 pr_info("Good: zero detected\n");
239 if (refcount_read(&zero) == 0)
240 pr_info("Correctly stayed at zero\n");
241 else
242 pr_err("Fail: refcount went past zero\n");
243 } else {
244 pr_err("Fail: Zero not detected!?\n");
245 }
246
247 pr_info("attempting bad refcount_add() from zero\n");
248 refcount_add(3, &zero);
249
250 check_from_zero(&zero);
251}
252
253static void check_saturated(refcount_t *ref)
254{
255 switch (refcount_read(ref)) {
256 case REFCOUNT_SATURATED:
257 pr_info("Saturation detected: still saturated\n");
258 break;
259 case REFCOUNT_MAX:
260 pr_warn("Saturation detected: unsafely reset to max\n");
261 break;
262 default:
263 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
264 }
265}
266
267/*
268 * A refcount_inc() from a saturated value should at most warn about
269 * being saturated already.
270 */
271void lkdtm_REFCOUNT_INC_SATURATED(void)
272{
273 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
274
275 pr_info("attempting bad refcount_inc() from saturated\n");
276 refcount_inc(&sat);
277
278 check_saturated(&sat);
279}
280
281/* Should act like refcount_inc() above from saturated. */
282void lkdtm_REFCOUNT_DEC_SATURATED(void)
283{
284 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
285
286 pr_info("attempting bad refcount_dec() from saturated\n");
287 refcount_dec(&sat);
288
289 check_saturated(&sat);
290}
291
292/* Should act like refcount_inc() above from saturated. */
293void lkdtm_REFCOUNT_ADD_SATURATED(void)
294{
295 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
296
297 pr_info("attempting bad refcount_dec() from saturated\n");
298 refcount_add(8, &sat);
299
300 check_saturated(&sat);
301}
302
303/* Should act like refcount_inc() above from saturated. */
304void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
305{
306 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
307
308 pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
309 if (!refcount_inc_not_zero(&sat))
310 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
311
312 check_saturated(&sat);
313}
314
315/* Should act like refcount_inc() above from saturated. */
316void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
317{
318 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
319
320 pr_info("attempting bad refcount_add_not_zero() from saturated\n");
321 if (!refcount_add_not_zero(7, &sat))
322 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
323
324 check_saturated(&sat);
325}
326
327/* Should act like refcount_inc() above from saturated. */
328void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
329{
330 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
331
332 pr_info("attempting bad refcount_dec_and_test() from saturated\n");
333 if (refcount_dec_and_test(&sat))
334 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
335
336 check_saturated(&sat);
337}
338
339/* Should act like refcount_inc() above from saturated. */
340void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
341{
342 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
343
344 pr_info("attempting bad refcount_sub_and_test() from saturated\n");
345 if (refcount_sub_and_test(8, &sat))
346 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
347
348 check_saturated(&sat);
349}
350
351/* Used to time the existing atomic_t when used for reference counting */
352void lkdtm_ATOMIC_TIMING(void)
353{
354 unsigned int i;
355 atomic_t count = ATOMIC_INIT(1);
356
357 for (i = 0; i < INT_MAX - 1; i++)
358 atomic_inc(&count);
359
360 for (i = INT_MAX; i > 0; i--)
361 if (atomic_dec_and_test(&count))
362 break;
363
364 if (i != 1)
365 pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
366 else
367 pr_info("atomic timing: done\n");
368}
369
370/*
371 * This can be compared to ATOMIC_TIMING when implementing fast refcount
372 * protections. Looking at the number of CPU cycles tells the real story
373 * about performance. For example:
374 * cd /sys/kernel/debug/provoke-crash
375 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
376 */
377void lkdtm_REFCOUNT_TIMING(void)
378{
379 unsigned int i;
380 refcount_t count = REFCOUNT_INIT(1);
381
382 for (i = 0; i < INT_MAX - 1; i++)
383 refcount_inc(&count);
384
385 for (i = INT_MAX; i > 0; i--)
386 if (refcount_dec_and_test(&count))
387 break;
388
389 if (i != 1)
390 pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
391 else
392 pr_info("refcount timing: done\n");
393}