lkdtm_bugs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. static noinline void __lkdtm_CORRUPT_STACK(void *stack)
  71. {
  72. memset(stack, 'a', 64);
  73. }
  74. noinline void lkdtm_CORRUPT_STACK(void)
  75. {
  76. /* Use default char array length that triggers stack protection. */
  77. char data[8];
  78. __lkdtm_CORRUPT_STACK(&data);
  79. pr_info("Corrupted stack with '%16s'...\n", data);
  80. }
  81. void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
  82. {
  83. static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
  84. u32 *p;
  85. u32 val = 0x12345678;
  86. p = (u32 *)(data + 1);
  87. if (*p == 0)
  88. val = 0x87654321;
  89. *p = val;
  90. }
  91. void lkdtm_SOFTLOCKUP(void)
  92. {
  93. preempt_disable();
  94. for (;;)
  95. cpu_relax();
  96. }
  97. void lkdtm_HARDLOCKUP(void)
  98. {
  99. local_irq_disable();
  100. for (;;)
  101. cpu_relax();
  102. }
  103. void lkdtm_SPINLOCKUP(void)
  104. {
  105. /* Must be called twice to trigger. */
  106. spin_lock(&lock_me_up);
  107. /* Let sparse know we intended to exit holding the lock. */
  108. __release(&lock_me_up);
  109. }
  110. void lkdtm_HUNG_TASK(void)
  111. {
  112. set_current_state(TASK_UNINTERRUPTIBLE);
  113. schedule();
  114. }
  115. void lkdtm_REFCOUNT_SATURATE_INC(void)
  116. {
  117. refcount_t over = REFCOUNT_INIT(UINT_MAX - 1);
  118. pr_info("attempting good refcount decrement\n");
  119. refcount_dec(&over);
  120. refcount_inc(&over);
  121. pr_info("attempting bad refcount inc overflow\n");
  122. refcount_inc(&over);
  123. refcount_inc(&over);
  124. if (refcount_read(&over) == UINT_MAX)
  125. pr_err("Correctly stayed saturated, but no BUG?!\n");
  126. else
  127. pr_err("Fail: refcount wrapped\n");
  128. }
  129. void lkdtm_REFCOUNT_SATURATE_ADD(void)
  130. {
  131. refcount_t over = REFCOUNT_INIT(UINT_MAX - 1);
  132. pr_info("attempting good refcount decrement\n");
  133. refcount_dec(&over);
  134. refcount_inc(&over);
  135. pr_info("attempting bad refcount add overflow\n");
  136. refcount_add(2, &over);
  137. if (refcount_read(&over) == UINT_MAX)
  138. pr_err("Correctly stayed saturated, but no BUG?!\n");
  139. else
  140. pr_err("Fail: refcount wrapped\n");
  141. }
  142. void lkdtm_REFCOUNT_ZERO_DEC(void)
  143. {
  144. refcount_t zero = REFCOUNT_INIT(1);
  145. pr_info("attempting bad refcount decrement to zero\n");
  146. refcount_dec(&zero);
  147. if (refcount_read(&zero) == 0)
  148. pr_err("Stayed at zero, but no BUG?!\n");
  149. else
  150. pr_err("Fail: refcount went crazy\n");
  151. }
  152. void lkdtm_REFCOUNT_ZERO_SUB(void)
  153. {
  154. refcount_t zero = REFCOUNT_INIT(1);
  155. pr_info("attempting bad refcount subtract past zero\n");
  156. if (!refcount_sub_and_test(2, &zero))
  157. pr_info("wrap attempt was noticed\n");
  158. if (refcount_read(&zero) == 1)
  159. pr_err("Correctly stayed above 0, but no BUG?!\n");
  160. else
  161. pr_err("Fail: refcount wrapped\n");
  162. }
  163. void lkdtm_REFCOUNT_ZERO_INC(void)
  164. {
  165. refcount_t zero = REFCOUNT_INIT(0);
  166. pr_info("attempting bad refcount increment from zero\n");
  167. refcount_inc(&zero);
  168. if (refcount_read(&zero) == 0)
  169. pr_err("Stayed at zero, but no BUG?!\n");
  170. else
  171. pr_err("Fail: refcount went past zero\n");
  172. }
  173. void lkdtm_REFCOUNT_ZERO_ADD(void)
  174. {
  175. refcount_t zero = REFCOUNT_INIT(0);
  176. pr_info("attempting bad refcount addition from zero\n");
  177. refcount_add(2, &zero);
  178. if (refcount_read(&zero) == 0)
  179. pr_err("Stayed at zero, but no BUG?!\n");
  180. else
  181. pr_err("Fail: refcount went past zero\n");
  182. }
  183. void lkdtm_CORRUPT_LIST_ADD(void)
  184. {
  185. /*
  186. * Initially, an empty list via LIST_HEAD:
  187. * test_head.next = &test_head
  188. * test_head.prev = &test_head
  189. */
  190. LIST_HEAD(test_head);
  191. struct lkdtm_list good, bad;
  192. void *target[2] = { };
  193. void *redirection = &target;
  194. pr_info("attempting good list addition\n");
  195. /*
  196. * Adding to the list performs these actions:
  197. * test_head.next->prev = &good.node
  198. * good.node.next = test_head.next
  199. * good.node.prev = test_head
  200. * test_head.next = good.node
  201. */
  202. list_add(&good.node, &test_head);
  203. pr_info("attempting corrupted list addition\n");
  204. /*
  205. * In simulating this "write what where" primitive, the "what" is
  206. * the address of &bad.node, and the "where" is the address held
  207. * by "redirection".
  208. */
  209. test_head.next = redirection;
  210. list_add(&bad.node, &test_head);
  211. if (target[0] == NULL && target[1] == NULL)
  212. pr_err("Overwrite did not happen, but no BUG?!\n");
  213. else
  214. pr_err("list_add() corruption not detected!\n");
  215. }
  216. void lkdtm_CORRUPT_LIST_DEL(void)
  217. {
  218. LIST_HEAD(test_head);
  219. struct lkdtm_list item;
  220. void *target[2] = { };
  221. void *redirection = &target;
  222. list_add(&item.node, &test_head);
  223. pr_info("attempting good list removal\n");
  224. list_del(&item.node);
  225. pr_info("attempting corrupted list removal\n");
  226. list_add(&item.node, &test_head);
  227. /* As with the list_add() test above, this corrupts "next". */
  228. item.node.next = redirection;
  229. list_del(&item.node);
  230. if (target[0] == NULL && target[1] == NULL)
  231. pr_err("Overwrite did not happen, but no BUG?!\n");
  232. else
  233. pr_err("list_del() corruption not detected!\n");
  234. }