refcount.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. static void check_from_zero(refcount_t *ref)
  154. {
  155. switch (refcount_read(ref)) {
  156. case 0:
  157. pr_info("Zero detected: stayed at zero\n");
  158. break;
  159. case REFCOUNT_SATURATED:
  160. pr_info("Zero detected: saturated\n");
  161. break;
  162. case REFCOUNT_MAX:
  163. pr_warn("Zero detected: unsafely reset to max\n");
  164. break;
  165. default:
  166. pr_info("Fail: zero not detected, incremented to %d\n",
  167. refcount_read(ref));
  168. }
  169. }
  170. /*
  171. * A refcount_inc() from zero should pin to zero or saturate and may WARN.
  172. */
  173. static void lkdtm_REFCOUNT_INC_ZERO(void)
  174. {
  175. refcount_t zero = REFCOUNT_INIT(0);
  176. pr_info("attempting safe refcount_inc_not_zero() from zero\n");
  177. if (!refcount_inc_not_zero(&zero)) {
  178. pr_info("Good: zero detected\n");
  179. if (refcount_read(&zero) == 0)
  180. pr_info("Correctly stayed at zero\n");
  181. else
  182. pr_err("Fail: refcount went past zero!\n");
  183. } else {
  184. pr_err("Fail: Zero not detected!?\n");
  185. }
  186. pr_info("attempting bad refcount_inc() from zero\n");
  187. refcount_inc(&zero);
  188. check_from_zero(&zero);
  189. }
  190. /*
  191. * A refcount_add() should act like refcount_inc() above when starting
  192. * at zero.
  193. */
  194. static void lkdtm_REFCOUNT_ADD_ZERO(void)
  195. {
  196. refcount_t zero = REFCOUNT_INIT(0);
  197. pr_info("attempting safe refcount_add_not_zero() from zero\n");
  198. if (!refcount_add_not_zero(3, &zero)) {
  199. pr_info("Good: zero detected\n");
  200. if (refcount_read(&zero) == 0)
  201. pr_info("Correctly stayed at zero\n");
  202. else
  203. pr_err("Fail: refcount went past zero\n");
  204. } else {
  205. pr_err("Fail: Zero not detected!?\n");
  206. }
  207. pr_info("attempting bad refcount_add() from zero\n");
  208. refcount_add(3, &zero);
  209. check_from_zero(&zero);
  210. }
  211. static void check_saturated(refcount_t *ref)
  212. {
  213. switch (refcount_read(ref)) {
  214. case REFCOUNT_SATURATED:
  215. pr_info("Saturation detected: still saturated\n");
  216. break;
  217. case REFCOUNT_MAX:
  218. pr_warn("Saturation detected: unsafely reset to max\n");
  219. break;
  220. default:
  221. pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
  222. }
  223. }
  224. /*
  225. * A refcount_inc() from a saturated value should at most warn about
  226. * being saturated already.
  227. */
  228. static void lkdtm_REFCOUNT_INC_SATURATED(void)
  229. {
  230. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  231. pr_info("attempting bad refcount_inc() from saturated\n");
  232. refcount_inc(&sat);
  233. check_saturated(&sat);
  234. }
  235. /* Should act like refcount_inc() above from saturated. */
  236. static void lkdtm_REFCOUNT_DEC_SATURATED(void)
  237. {
  238. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  239. pr_info("attempting bad refcount_dec() from saturated\n");
  240. refcount_dec(&sat);
  241. check_saturated(&sat);
  242. }
  243. /* Should act like refcount_inc() above from saturated. */
  244. static void lkdtm_REFCOUNT_ADD_SATURATED(void)
  245. {
  246. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  247. pr_info("attempting bad refcount_dec() from saturated\n");
  248. refcount_add(8, &sat);
  249. check_saturated(&sat);
  250. }
  251. /* Should act like refcount_inc() above from saturated. */
  252. static void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
  253. {
  254. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  255. pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
  256. if (!refcount_inc_not_zero(&sat))
  257. pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
  258. check_saturated(&sat);
  259. }
  260. /* Should act like refcount_inc() above from saturated. */
  261. static void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
  262. {
  263. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  264. pr_info("attempting bad refcount_add_not_zero() from saturated\n");
  265. if (!refcount_add_not_zero(7, &sat))
  266. pr_warn("Weird: refcount_add_not_zero() reported zero\n");
  267. check_saturated(&sat);
  268. }
  269. /* Should act like refcount_inc() above from saturated. */
  270. static void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
  271. {
  272. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  273. pr_info("attempting bad refcount_dec_and_test() from saturated\n");
  274. if (refcount_dec_and_test(&sat))
  275. pr_warn("Weird: refcount_dec_and_test() reported zero\n");
  276. check_saturated(&sat);
  277. }
  278. /* Should act like refcount_inc() above from saturated. */
  279. static void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
  280. {
  281. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  282. pr_info("attempting bad refcount_sub_and_test() from saturated\n");
  283. if (refcount_sub_and_test(8, &sat))
  284. pr_warn("Weird: refcount_sub_and_test() reported zero\n");
  285. check_saturated(&sat);
  286. }
  287. /* Used to time the existing atomic_t when used for reference counting */
  288. static void lkdtm_ATOMIC_TIMING(void)
  289. {
  290. unsigned int i;
  291. atomic_t count = ATOMIC_INIT(1);
  292. for (i = 0; i < INT_MAX - 1; i++)
  293. atomic_inc(&count);
  294. for (i = INT_MAX; i > 0; i--)
  295. if (atomic_dec_and_test(&count))
  296. break;
  297. if (i != 1)
  298. pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
  299. else
  300. pr_info("atomic timing: done\n");
  301. }
  302. /*
  303. * This can be compared to ATOMIC_TIMING when implementing fast refcount
  304. * protections. Looking at the number of CPU cycles tells the real story
  305. * about performance. For example:
  306. * cd /sys/kernel/debug/provoke-crash
  307. * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
  308. */
  309. static void lkdtm_REFCOUNT_TIMING(void)
  310. {
  311. unsigned int i;
  312. refcount_t count = REFCOUNT_INIT(1);
  313. for (i = 0; i < INT_MAX - 1; i++)
  314. refcount_inc(&count);
  315. for (i = INT_MAX; i > 0; i--)
  316. if (refcount_dec_and_test(&count))
  317. break;
  318. if (i != 1)
  319. pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
  320. else
  321. pr_info("refcount timing: done\n");
  322. }
  323. static struct crashtype crashtypes[] = {
  324. CRASHTYPE(REFCOUNT_INC_OVERFLOW),
  325. CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
  326. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_OVERFLOW),
  327. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_OVERFLOW),
  328. CRASHTYPE(REFCOUNT_DEC_ZERO),
  329. CRASHTYPE(REFCOUNT_DEC_NEGATIVE),
  330. CRASHTYPE(REFCOUNT_DEC_AND_TEST_NEGATIVE),
  331. CRASHTYPE(REFCOUNT_SUB_AND_TEST_NEGATIVE),
  332. CRASHTYPE(REFCOUNT_INC_ZERO),
  333. CRASHTYPE(REFCOUNT_ADD_ZERO),
  334. CRASHTYPE(REFCOUNT_INC_SATURATED),
  335. CRASHTYPE(REFCOUNT_DEC_SATURATED),
  336. CRASHTYPE(REFCOUNT_ADD_SATURATED),
  337. CRASHTYPE(REFCOUNT_INC_NOT_ZERO_SATURATED),
  338. CRASHTYPE(REFCOUNT_ADD_NOT_ZERO_SATURATED),
  339. CRASHTYPE(REFCOUNT_DEC_AND_TEST_SATURATED),
  340. CRASHTYPE(REFCOUNT_SUB_AND_TEST_SATURATED),
  341. CRASHTYPE(ATOMIC_TIMING),
  342. CRASHTYPE(REFCOUNT_TIMING),
  343. };
  344. struct crashtype_category refcount_crashtypes = {
  345. .crashtypes = crashtypes,
  346. .len = ARRAY_SIZE(crashtypes),
  347. };