lkdtm_refcount.c 9.9 KB

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