lkdtm_bugs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This is for all the tests related to logic bugs (e.g. bad dereferences,
  3. * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and
  4. * lockups) along with other things that don't fit well into existing LKDTM
  5. * test source files.
  6. */
  7. #include "lkdtm.h"
  8. #include <linux/list.h>
  9. #include <linux/refcount.h>
  10. #include <linux/sched.h>
  11. struct lkdtm_list {
  12. struct list_head node;
  13. };
  14. /*
  15. * Make sure our attempts to over run the kernel stack doesn't trigger
  16. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  17. * recurse past the end of THREAD_SIZE by default.
  18. */
  19. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  20. #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
  21. #else
  22. #define REC_STACK_SIZE (THREAD_SIZE / 8)
  23. #endif
  24. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  25. static int recur_count = REC_NUM_DEFAULT;
  26. static DEFINE_SPINLOCK(lock_me_up);
  27. static int recursive_loop(int remaining)
  28. {
  29. char buf[REC_STACK_SIZE];
  30. /* Make sure compiler does not optimize this away. */
  31. memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
  32. if (!remaining)
  33. return 0;
  34. else
  35. return recursive_loop(remaining - 1);
  36. }
  37. /* If the depth is negative, use the default, otherwise keep parameter. */
  38. void __init lkdtm_bugs_init(int *recur_param)
  39. {
  40. if (*recur_param < 0)
  41. *recur_param = recur_count;
  42. else
  43. recur_count = *recur_param;
  44. }
  45. void lkdtm_PANIC(void)
  46. {
  47. panic("dumptest");
  48. }
  49. void lkdtm_BUG(void)
  50. {
  51. BUG();
  52. }
  53. void lkdtm_WARNING(void)
  54. {
  55. WARN_ON(1);
  56. }
  57. void lkdtm_EXCEPTION(void)
  58. {
  59. *((int *) 0) = 0;
  60. }
  61. void lkdtm_LOOP(void)
  62. {
  63. for (;;)
  64. ;
  65. }
  66. void lkdtm_OVERFLOW(void)
  67. {
  68. (void) recursive_loop(recur_count);
  69. }
  70. noinline void lkdtm_CORRUPT_STACK(void)
  71. {
  72. /* Use default char array length that triggers stack protection. */
  73. char data[8];
  74. memset((void *)data, 'a', 64);
  75. pr_info("Corrupted stack with '%16s'...\n", data);
  76. }
  77. void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
  78. {
  79. static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
  80. u32 *p;
  81. u32 val = 0x12345678;
  82. p = (u32 *)(data + 1);
  83. if (*p == 0)
  84. val = 0x87654321;
  85. *p = val;
  86. }
  87. void lkdtm_SOFTLOCKUP(void)
  88. {
  89. preempt_disable();
  90. for (;;)
  91. cpu_relax();
  92. }
  93. void lkdtm_HARDLOCKUP(void)
  94. {
  95. local_irq_disable();
  96. for (;;)
  97. cpu_relax();
  98. }
  99. void lkdtm_SPINLOCKUP(void)
  100. {
  101. /* Must be called twice to trigger. */
  102. spin_lock(&lock_me_up);
  103. /* Let sparse know we intended to exit holding the lock. */
  104. __release(&lock_me_up);
  105. }
  106. void lkdtm_HUNG_TASK(void)
  107. {
  108. set_current_state(TASK_UNINTERRUPTIBLE);
  109. schedule();
  110. }
  111. void lkdtm_REFCOUNT_SATURATE_INC(void)
  112. {
  113. refcount_t over = REFCOUNT_INIT(UINT_MAX - 1);
  114. pr_info("attempting good refcount decrement\n");
  115. refcount_dec(&over);
  116. refcount_inc(&over);
  117. pr_info("attempting bad refcount inc overflow\n");
  118. refcount_inc(&over);
  119. refcount_inc(&over);
  120. if (refcount_read(&over) == UINT_MAX)
  121. pr_err("Correctly stayed saturated, but no BUG?!\n");
  122. else
  123. pr_err("Fail: refcount wrapped\n");
  124. }
  125. void lkdtm_REFCOUNT_SATURATE_ADD(void)
  126. {
  127. refcount_t over = REFCOUNT_INIT(UINT_MAX - 1);
  128. pr_info("attempting good refcount decrement\n");
  129. refcount_dec(&over);
  130. refcount_inc(&over);
  131. pr_info("attempting bad refcount add overflow\n");
  132. refcount_add(2, &over);
  133. if (refcount_read(&over) == UINT_MAX)
  134. pr_err("Correctly stayed saturated, but no BUG?!\n");
  135. else
  136. pr_err("Fail: refcount wrapped\n");
  137. }
  138. void lkdtm_REFCOUNT_ZERO_DEC(void)
  139. {
  140. refcount_t zero = REFCOUNT_INIT(1);
  141. pr_info("attempting bad refcount decrement to zero\n");
  142. refcount_dec(&zero);
  143. if (refcount_read(&zero) == 0)
  144. pr_err("Stayed at zero, but no BUG?!\n");
  145. else
  146. pr_err("Fail: refcount went crazy\n");
  147. }
  148. void lkdtm_REFCOUNT_ZERO_SUB(void)
  149. {
  150. refcount_t zero = REFCOUNT_INIT(1);
  151. pr_info("attempting bad refcount subtract past zero\n");
  152. if (!refcount_sub_and_test(2, &zero))
  153. pr_info("wrap attempt was noticed\n");
  154. if (refcount_read(&zero) == 1)
  155. pr_err("Correctly stayed above 0, but no BUG?!\n");
  156. else
  157. pr_err("Fail: refcount wrapped\n");
  158. }
  159. void lkdtm_REFCOUNT_ZERO_INC(void)
  160. {
  161. refcount_t zero = REFCOUNT_INIT(0);
  162. pr_info("attempting bad refcount increment from zero\n");
  163. refcount_inc(&zero);
  164. if (refcount_read(&zero) == 0)
  165. pr_err("Stayed at zero, but no BUG?!\n");
  166. else
  167. pr_err("Fail: refcount went past zero\n");
  168. }
  169. void lkdtm_REFCOUNT_ZERO_ADD(void)
  170. {
  171. refcount_t zero = REFCOUNT_INIT(0);
  172. pr_info("attempting bad refcount addition from zero\n");
  173. refcount_add(2, &zero);
  174. if (refcount_read(&zero) == 0)
  175. pr_err("Stayed at zero, but no BUG?!\n");
  176. else
  177. pr_err("Fail: refcount went past zero\n");
  178. }
  179. void lkdtm_CORRUPT_LIST_ADD(void)
  180. {
  181. /*
  182. * Initially, an empty list via LIST_HEAD:
  183. * test_head.next = &test_head
  184. * test_head.prev = &test_head
  185. */
  186. LIST_HEAD(test_head);
  187. struct lkdtm_list good, bad;
  188. void *target[2] = { };
  189. void *redirection = &target;
  190. pr_info("attempting good list addition\n");
  191. /*
  192. * Adding to the list performs these actions:
  193. * test_head.next->prev = &good.node
  194. * good.node.next = test_head.next
  195. * good.node.prev = test_head
  196. * test_head.next = good.node
  197. */
  198. list_add(&good.node, &test_head);
  199. pr_info("attempting corrupted list addition\n");
  200. /*
  201. * In simulating this "write what where" primitive, the "what" is
  202. * the address of &bad.node, and the "where" is the address held
  203. * by "redirection".
  204. */
  205. test_head.next = redirection;
  206. list_add(&bad.node, &test_head);
  207. if (target[0] == NULL && target[1] == NULL)
  208. pr_err("Overwrite did not happen, but no BUG?!\n");
  209. else
  210. pr_err("list_add() corruption not detected!\n");
  211. }
  212. void lkdtm_CORRUPT_LIST_DEL(void)
  213. {
  214. LIST_HEAD(test_head);
  215. struct lkdtm_list item;
  216. void *target[2] = { };
  217. void *redirection = &target;
  218. list_add(&item.node, &test_head);
  219. pr_info("attempting good list removal\n");
  220. list_del(&item.node);
  221. pr_info("attempting corrupted list removal\n");
  222. list_add(&item.node, &test_head);
  223. /* As with the list_add() test above, this corrupts "next". */
  224. item.node.next = redirection;
  225. list_del(&item.node);
  226. if (target[0] == NULL && target[1] == NULL)
  227. pr_err("Overwrite did not happen, but no BUG?!\n");
  228. else
  229. pr_err("list_del() corruption not detected!\n");
  230. }