1 // SPDX-License-Identifier: GPL-2.0
3 * This is for all the tests related to refcount bugs (e.g. overflow,
4 * underflow, reaching zero untested, etc).
7 #include <linux/refcount.h>
9 static void overflow_check(refcount_t
*ref
)
11 switch (refcount_read(ref
)) {
12 case REFCOUNT_SATURATED
:
13 pr_info("Overflow detected: saturated\n");
16 pr_warn("Overflow detected: unsafely reset to max\n");
19 pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref
));
24 * A refcount_inc() above the maximum value of the refcount implementation,
25 * should at least saturate, and at most also WARN.
27 static void lkdtm_REFCOUNT_INC_OVERFLOW(void)
29 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
- 1);
31 pr_info("attempting good refcount_inc() without overflow\n");
35 pr_info("attempting bad refcount_inc() overflow\n");
39 overflow_check(&over
);
42 /* refcount_add() should behave just like refcount_inc() above. */
43 static void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
45 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
- 1);
47 pr_info("attempting good refcount_add() without overflow\n");
52 refcount_add(4, &over
);
54 pr_info("attempting bad refcount_add() overflow\n");
55 refcount_add(4, &over
);
57 overflow_check(&over
);
60 /* refcount_inc_not_zero() should behave just like refcount_inc() above. */
61 static void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
63 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
);
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");
69 overflow_check(&over
);
72 /* refcount_add_not_zero() should behave just like refcount_inc() above. */
73 static void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
75 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
);
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");
81 overflow_check(&over
);
84 static void check_zero(refcount_t
*ref
)
86 switch (refcount_read(ref
)) {
87 case REFCOUNT_SATURATED
:
88 pr_info("Zero detected: saturated\n");
91 pr_warn("Zero detected: unsafely reset to max\n");
94 pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
97 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
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.
106 static void lkdtm_REFCOUNT_DEC_ZERO(void)
108 refcount_t zero
= REFCOUNT_INIT(2);
110 pr_info("attempting good refcount_dec()\n");
113 pr_info("attempting bad refcount_dec() to zero\n");
119 static void check_negative(refcount_t
*ref
, int start
)
122 * refcount_t 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.
126 if (refcount_read(ref
) == start
) {
127 pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
132 switch (refcount_read(ref
)) {
133 case REFCOUNT_SATURATED
:
134 pr_info("Negative detected: saturated\n");
137 pr_warn("Negative detected: unsafely reset to max\n");
140 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
144 /* A refcount_dec() going negative should saturate and may WARN. */
145 static void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
147 refcount_t neg
= REFCOUNT_INIT(0);
149 pr_info("attempting bad refcount_dec() below zero\n");
152 check_negative(&neg
, 0);
156 * A refcount_dec_and_test() should act like refcount_dec() above when
159 static void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
161 refcount_t neg
= REFCOUNT_INIT(0);
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");
167 check_negative(&neg
, 0);
171 * A refcount_sub_and_test() should act like refcount_dec_and_test()
172 * above when going negative.
174 static void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
176 refcount_t neg
= REFCOUNT_INIT(3);
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");
182 check_negative(&neg
, 3);
186 * A refcount_sub_and_test() by zero when the counter is at zero should act like
187 * refcount_sub_and_test() above when going negative.
189 static void lkdtm_REFCOUNT_SUB_AND_TEST_ZERO(void)
191 refcount_t neg
= REFCOUNT_INIT(0);
193 pr_info("attempting bad refcount_sub_and_test() at zero\n");
194 if (refcount_sub_and_test(0, &neg
))
195 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
197 check_negative(&neg
, 0);
200 static void check_from_zero(refcount_t
*ref
)
202 switch (refcount_read(ref
)) {
204 pr_info("Zero detected: stayed at zero\n");
206 case REFCOUNT_SATURATED
:
207 pr_info("Zero detected: saturated\n");
210 pr_warn("Zero detected: unsafely reset to max\n");
213 pr_info("Fail: zero not detected, incremented to %d\n",
219 * A refcount_inc() from zero should pin to zero or saturate and may WARN.
221 static void lkdtm_REFCOUNT_INC_ZERO(void)
223 refcount_t zero
= REFCOUNT_INIT(0);
225 pr_info("attempting safe refcount_inc_not_zero() from zero\n");
226 if (!refcount_inc_not_zero(&zero
)) {
227 pr_info("Good: zero detected\n");
228 if (refcount_read(&zero
) == 0)
229 pr_info("Correctly stayed at zero\n");
231 pr_err("Fail: refcount went past zero!\n");
233 pr_err("Fail: Zero not detected!?\n");
236 pr_info("attempting bad refcount_inc() from zero\n");
239 check_from_zero(&zero
);
243 * A refcount_add() should act like refcount_inc() above when starting
246 static void lkdtm_REFCOUNT_ADD_ZERO(void)
248 refcount_t zero
= REFCOUNT_INIT(0);
250 pr_info("attempting safe refcount_add_not_zero() from zero\n");
251 if (!refcount_add_not_zero(3, &zero
)) {
252 pr_info("Good: zero detected\n");
253 if (refcount_read(&zero
) == 0)
254 pr_info("Correctly stayed at zero\n");
256 pr_err("Fail: refcount went past zero\n");
258 pr_err("Fail: Zero not detected!?\n");
261 pr_info("attempting bad refcount_add() from zero\n");
262 refcount_add(3, &zero
);
264 check_from_zero(&zero
);
267 static void check_saturated(refcount_t
*ref
)
269 switch (refcount_read(ref
)) {
270 case REFCOUNT_SATURATED
:
271 pr_info("Saturation detected: still saturated\n");
274 pr_warn("Saturation detected: unsafely reset to max\n");
277 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
282 * A refcount_inc() from a saturated value should at most warn about
283 * being saturated already.
285 static void lkdtm_REFCOUNT_INC_SATURATED(void)
287 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
289 pr_info("attempting bad refcount_inc() from saturated\n");
292 check_saturated(&sat
);
295 /* Should act like refcount_inc() above from saturated. */
296 static void lkdtm_REFCOUNT_DEC_SATURATED(void)
298 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
300 pr_info("attempting bad refcount_dec() from saturated\n");
303 check_saturated(&sat
);
306 /* Should act like refcount_inc() above from saturated. */
307 static void lkdtm_REFCOUNT_ADD_SATURATED(void)
309 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
311 pr_info("attempting bad refcount_dec() from saturated\n");
312 refcount_add(8, &sat
);
314 check_saturated(&sat
);
317 /* Should act like refcount_inc() above from saturated. */
318 static void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
320 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
322 pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
323 if (!refcount_inc_not_zero(&sat
))
324 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
326 check_saturated(&sat
);
329 /* Should act like refcount_inc() above from saturated. */
330 static void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
332 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
334 pr_info("attempting bad refcount_add_not_zero() from saturated\n");
335 if (!refcount_add_not_zero(7, &sat
))
336 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
338 check_saturated(&sat
);
341 /* Should act like refcount_inc() above from saturated. */
342 static void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
344 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
346 pr_info("attempting bad refcount_dec_and_test() from saturated\n");
347 if (refcount_dec_and_test(&sat
))
348 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
350 check_saturated(&sat
);
353 /* Should act like refcount_inc() above from saturated. */
354 static void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
356 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
358 pr_info("attempting bad refcount_sub_and_test() from saturated\n");
359 if (refcount_sub_and_test(8, &sat
))
360 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
362 check_saturated(&sat
);
365 /* Used to time the existing atomic_t when used for reference counting */
366 static void lkdtm_ATOMIC_TIMING(void)
369 atomic_t count
= ATOMIC_INIT(1);
371 for (i
= 0; i
< INT_MAX
- 1; i
++)
374 for (i
= INT_MAX
; i
> 0; i
--)
375 if (atomic_dec_and_test(&count
))
379 pr_err("atomic timing: out of sync up/down cycle: %u\n", i
- 1);
381 pr_info("atomic timing: done\n");
385 * This can be compared to ATOMIC_TIMING when implementing fast refcount
386 * protections. Looking at the number of CPU cycles tells the real story
387 * about performance. For example:
388 * cd /sys/kernel/debug/provoke-crash
389 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
391 static void lkdtm_REFCOUNT_TIMING(void)
394 refcount_t count
= REFCOUNT_INIT(1);
396 for (i
= 0; i
< INT_MAX
- 1; i
++)
397 refcount_inc(&count
);
399 for (i
= INT_MAX
; i
> 0; i
--)
400 if (refcount_dec_and_test(&count
))
404 pr_err("refcount: out of sync up/down cycle: %u\n", i
- 1);
406 pr_info("refcount timing: done\n");
409 static struct crashtype crashtypes
[] = {
410 CRASHTYPE(REFCOUNT_INC_OVERFLOW
),
411 CRASHTYPE(REFCOUNT_ADD_OVERFLOW
),
412 CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW
),
413 CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW
),
414 CRASHTYPE(REFCOUNT_DEC_ZERO
),
415 CRASHTYPE(REFCOUNT_DEC_NEGATIVE
),
416 CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE
),
417 CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE
),
418 CRASHTYPE(REFCOUNT_SUB_AND_TEST_ZERO
),
419 CRASHTYPE(REFCOUNT_INC_ZERO
),
420 CRASHTYPE(REFCOUNT_ADD_ZERO
),
421 CRASHTYPE(REFCOUNT_INC_SATURATED
),
422 CRASHTYPE(REFCOUNT_DEC_SATURATED
),
423 CRASHTYPE(REFCOUNT_ADD_SATURATED
),
424 CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED
),
425 CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED
),
426 CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED
),
427 CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED
),
428 CRASHTYPE(ATOMIC_TIMING
),
429 CRASHTYPE(REFCOUNT_TIMING
),
432 struct crashtype_category refcount_crashtypes
= {
433 .crashtypes
= crashtypes
,
434 .len
= ARRAY_SIZE(crashtypes
),