lkdtm_bugs.c 6.3 KB

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