lkdtm_bugs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #define pr_fmt(fmt) "lkdtm: " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include "lkdtm.h"
  11. /*
  12. * Make sure our attempts to over run the kernel stack doesn't trigger
  13. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  14. * recurse past the end of THREAD_SIZE by default.
  15. */
  16. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  17. #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2)
  18. #else
  19. #define REC_STACK_SIZE (THREAD_SIZE / 8)
  20. #endif
  21. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  22. static int recur_count = REC_NUM_DEFAULT;
  23. static DEFINE_SPINLOCK(lock_me_up);
  24. static int recursive_loop(int remaining)
  25. {
  26. char buf[REC_STACK_SIZE];
  27. /* Make sure compiler does not optimize this away. */
  28. memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE);
  29. if (!remaining)
  30. return 0;
  31. else
  32. return recursive_loop(remaining - 1);
  33. }
  34. /* If the depth is negative, use the default, otherwise keep parameter. */
  35. void __init lkdtm_bugs_init(int *recur_param)
  36. {
  37. if (*recur_param < 0)
  38. *recur_param = recur_count;
  39. else
  40. recur_count = *recur_param;
  41. }
  42. void lkdtm_PANIC(void)
  43. {
  44. panic("dumptest");
  45. }
  46. void lkdtm_BUG(void)
  47. {
  48. BUG();
  49. }
  50. void lkdtm_WARNING(void)
  51. {
  52. WARN_ON(1);
  53. }
  54. void lkdtm_EXCEPTION(void)
  55. {
  56. *((int *) 0) = 0;
  57. }
  58. void lkdtm_LOOP(void)
  59. {
  60. for (;;)
  61. ;
  62. }
  63. void lkdtm_OVERFLOW(void)
  64. {
  65. (void) recursive_loop(recur_count);
  66. }
  67. noinline void lkdtm_CORRUPT_STACK(void)
  68. {
  69. /* Use default char array length that triggers stack protection. */
  70. char data[8];
  71. memset((void *)data, 0, 64);
  72. }
  73. void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
  74. {
  75. static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
  76. u32 *p;
  77. u32 val = 0x12345678;
  78. p = (u32 *)(data + 1);
  79. if (*p == 0)
  80. val = 0x87654321;
  81. *p = val;
  82. }
  83. void lkdtm_SOFTLOCKUP(void)
  84. {
  85. preempt_disable();
  86. for (;;)
  87. cpu_relax();
  88. }
  89. void lkdtm_HARDLOCKUP(void)
  90. {
  91. local_irq_disable();
  92. for (;;)
  93. cpu_relax();
  94. }
  95. void lkdtm_SPINLOCKUP(void)
  96. {
  97. /* Must be called twice to trigger. */
  98. spin_lock(&lock_me_up);
  99. /* Let sparse know we intended to exit holding the lock. */
  100. __release(&lock_me_up);
  101. }
  102. void lkdtm_HUNG_TASK(void)
  103. {
  104. set_current_state(TASK_UNINTERRUPTIBLE);
  105. schedule();
  106. }
  107. void lkdtm_ATOMIC_UNDERFLOW(void)
  108. {
  109. atomic_t under = ATOMIC_INIT(INT_MIN);
  110. pr_info("attempting good atomic increment\n");
  111. atomic_inc(&under);
  112. atomic_dec(&under);
  113. pr_info("attempting bad atomic underflow\n");
  114. atomic_dec(&under);
  115. }
  116. void lkdtm_ATOMIC_OVERFLOW(void)
  117. {
  118. atomic_t over = ATOMIC_INIT(INT_MAX);
  119. pr_info("attempting good atomic decrement\n");
  120. atomic_dec(&over);
  121. atomic_inc(&over);
  122. pr_info("attempting bad atomic overflow\n");
  123. atomic_inc(&over);
  124. }