2 * This is for all the tests related to refcount bugs (e.g. overflow,
3 * underflow, reaching zero untested, etc).
6 #include <linux/refcount.h>
8 #ifdef CONFIG_REFCOUNT_FULL
9 #define REFCOUNT_MAX (UINT_MAX - 1)
10 #define REFCOUNT_SATURATED UINT_MAX
12 #define REFCOUNT_MAX INT_MAX
13 #define REFCOUNT_SATURATED (INT_MIN / 2)
16 static void overflow_check(refcount_t
*ref
)
18 switch (refcount_read(ref
)) {
19 case REFCOUNT_SATURATED
:
20 pr_info("Overflow detected: saturated\n");
23 pr_warn("Overflow detected: unsafely reset to max\n");
26 pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref
));
31 * A refcount_inc() above the maximum value of the refcount implementation,
32 * should at least saturate, and at most also WARN.
34 void lkdtm_REFCOUNT_INC_OVERFLOW(void)
36 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
- 1);
38 pr_info("attempting good refcount_inc() without overflow\n");
42 pr_info("attempting bad refcount_inc() overflow\n");
46 overflow_check(&over
);
49 /* refcount_add() should behave just like refcount_inc() above. */
50 void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
52 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
- 1);
54 pr_info("attempting good refcount_add() without overflow\n");
59 refcount_add(4, &over
);
61 pr_info("attempting bad refcount_add() overflow\n");
62 refcount_add(4, &over
);
64 overflow_check(&over
);
67 /* refcount_inc_not_zero() should behave just like refcount_inc() above. */
68 void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
70 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
);
72 pr_info("attempting bad refcount_inc_not_zero() overflow\n");
73 if (!refcount_inc_not_zero(&over
))
74 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
76 overflow_check(&over
);
79 /* refcount_add_not_zero() should behave just like refcount_inc() above. */
80 void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
82 refcount_t over
= REFCOUNT_INIT(REFCOUNT_MAX
);
84 pr_info("attempting bad refcount_add_not_zero() overflow\n");
85 if (!refcount_add_not_zero(6, &over
))
86 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
88 overflow_check(&over
);
91 static void check_zero(refcount_t
*ref
)
93 switch (refcount_read(ref
)) {
94 case REFCOUNT_SATURATED
:
95 pr_info("Zero detected: saturated\n");
98 pr_warn("Zero detected: unsafely reset to max\n");
101 pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
104 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
109 * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits
110 * zero it should either saturate (when inc-from-zero isn't protected)
111 * or stay at zero (when inc-from-zero is protected) and should WARN for both.
113 void lkdtm_REFCOUNT_DEC_ZERO(void)
115 refcount_t zero
= REFCOUNT_INIT(2);
117 pr_info("attempting good refcount_dec()\n");
120 pr_info("attempting bad refcount_dec() to zero\n");
126 static void check_negative(refcount_t
*ref
, int start
)
129 * CONFIG_REFCOUNT_FULL refuses to move a refcount at all on an
130 * over-sub, so we have to track our starting position instead of
131 * looking only at zero-pinning.
133 if (refcount_read(ref
) == start
) {
134 pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
139 switch (refcount_read(ref
)) {
140 case REFCOUNT_SATURATED
:
141 pr_info("Negative detected: saturated\n");
144 pr_warn("Negative detected: unsafely reset to max\n");
147 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
151 /* A refcount_dec() going negative should saturate and may WARN. */
152 void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
154 refcount_t neg
= REFCOUNT_INIT(0);
156 pr_info("attempting bad refcount_dec() below zero\n");
159 check_negative(&neg
, 0);
163 * A refcount_dec_and_test() should act like refcount_dec() above when
166 void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
168 refcount_t neg
= REFCOUNT_INIT(0);
170 pr_info("attempting bad refcount_dec_and_test() below zero\n");
171 if (refcount_dec_and_test(&neg
))
172 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
174 check_negative(&neg
, 0);
178 * A refcount_sub_and_test() should act like refcount_dec_and_test()
179 * above when going negative.
181 void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
183 refcount_t neg
= REFCOUNT_INIT(3);
185 pr_info("attempting bad refcount_sub_and_test() below zero\n");
186 if (refcount_sub_and_test(5, &neg
))
187 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
189 check_negative(&neg
, 3);
192 static void check_from_zero(refcount_t
*ref
)
194 switch (refcount_read(ref
)) {
196 pr_info("Zero detected: stayed at zero\n");
198 case REFCOUNT_SATURATED
:
199 pr_info("Zero detected: saturated\n");
202 pr_warn("Zero detected: unsafely reset to max\n");
205 pr_info("Fail: zero not detected, incremented to %d\n",
211 * A refcount_inc() from zero should pin to zero or saturate and may WARN.
212 * Only CONFIG_REFCOUNT_FULL provides this protection currently.
214 void lkdtm_REFCOUNT_INC_ZERO(void)
216 refcount_t zero
= REFCOUNT_INIT(0);
218 pr_info("attempting safe refcount_inc_not_zero() from zero\n");
219 if (!refcount_inc_not_zero(&zero
)) {
220 pr_info("Good: zero detected\n");
221 if (refcount_read(&zero
) == 0)
222 pr_info("Correctly stayed at zero\n");
224 pr_err("Fail: refcount went past zero!\n");
226 pr_err("Fail: Zero not detected!?\n");
229 pr_info("attempting bad refcount_inc() from zero\n");
232 check_from_zero(&zero
);
236 * A refcount_add() should act like refcount_inc() above when starting
239 void lkdtm_REFCOUNT_ADD_ZERO(void)
241 refcount_t zero
= REFCOUNT_INIT(0);
243 pr_info("attempting safe refcount_add_not_zero() from zero\n");
244 if (!refcount_add_not_zero(3, &zero
)) {
245 pr_info("Good: zero detected\n");
246 if (refcount_read(&zero
) == 0)
247 pr_info("Correctly stayed at zero\n");
249 pr_err("Fail: refcount went past zero\n");
251 pr_err("Fail: Zero not detected!?\n");
254 pr_info("attempting bad refcount_add() from zero\n");
255 refcount_add(3, &zero
);
257 check_from_zero(&zero
);
260 static void check_saturated(refcount_t
*ref
)
262 switch (refcount_read(ref
)) {
263 case REFCOUNT_SATURATED
:
264 pr_info("Saturation detected: still saturated\n");
267 pr_warn("Saturation detected: unsafely reset to max\n");
270 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref
));
275 * A refcount_inc() from a saturated value should at most warn about
276 * being saturated already.
278 void lkdtm_REFCOUNT_INC_SATURATED(void)
280 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
282 pr_info("attempting bad refcount_inc() from saturated\n");
285 check_saturated(&sat
);
288 /* Should act like refcount_inc() above from saturated. */
289 void lkdtm_REFCOUNT_DEC_SATURATED(void)
291 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
293 pr_info("attempting bad refcount_dec() from saturated\n");
296 check_saturated(&sat
);
299 /* Should act like refcount_inc() above from saturated. */
300 void lkdtm_REFCOUNT_ADD_SATURATED(void)
302 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
304 pr_info("attempting bad refcount_dec() from saturated\n");
305 refcount_add(8, &sat
);
307 check_saturated(&sat
);
310 /* Should act like refcount_inc() above from saturated. */
311 void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
313 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
315 pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
316 if (!refcount_inc_not_zero(&sat
))
317 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
319 check_saturated(&sat
);
322 /* Should act like refcount_inc() above from saturated. */
323 void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
325 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
327 pr_info("attempting bad refcount_add_not_zero() from saturated\n");
328 if (!refcount_add_not_zero(7, &sat
))
329 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
331 check_saturated(&sat
);
334 /* Should act like refcount_inc() above from saturated. */
335 void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
337 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
339 pr_info("attempting bad refcount_dec_and_test() from saturated\n");
340 if (refcount_dec_and_test(&sat
))
341 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
343 check_saturated(&sat
);
346 /* Should act like refcount_inc() above from saturated. */
347 void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
349 refcount_t sat
= REFCOUNT_INIT(REFCOUNT_SATURATED
);
351 pr_info("attempting bad refcount_sub_and_test() from saturated\n");
352 if (refcount_sub_and_test(8, &sat
))
353 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
355 check_saturated(&sat
);
358 /* Used to time the existing atomic_t when used for reference counting */
359 void lkdtm_ATOMIC_TIMING(void)
362 atomic_t count
= ATOMIC_INIT(1);
364 for (i
= 0; i
< INT_MAX
- 1; i
++)
367 for (i
= INT_MAX
; i
> 0; i
--)
368 if (atomic_dec_and_test(&count
))
372 pr_err("atomic timing: out of sync up/down cycle: %u\n", i
- 1);
374 pr_info("atomic timing: done\n");
378 * This can be compared to ATOMIC_TIMING when implementing fast refcount
379 * protections. Looking at the number of CPU cycles tells the real story
380 * about performance. For example:
381 * cd /sys/kernel/debug/provoke-crash
382 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
384 void lkdtm_REFCOUNT_TIMING(void)
387 refcount_t count
= REFCOUNT_INIT(1);
389 for (i
= 0; i
< INT_MAX
- 1; i
++)
390 refcount_inc(&count
);
392 for (i
= INT_MAX
; i
> 0; i
--)
393 if (refcount_dec_and_test(&count
))
397 pr_err("refcount: out of sync up/down cycle: %u\n", i
- 1);
399 pr_info("refcount timing: done\n");