refcount.c 11 KB

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