Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 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 | */ |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 7 | #include "lkdtm.h" |
Kees Cook | bef1d72 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 8 | #include <linux/list.h> |
Kees Cook | 6d2e91a | 2016-07-15 16:04:39 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 10 | |
Kees Cook | bef1d72 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 11 | struct lkdtm_list { |
| 12 | struct list_head node; |
| 13 | }; |
| 14 | |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 15 | /* |
| 16 | * Make sure our attempts to over run the kernel stack doesn't trigger |
| 17 | * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we |
| 18 | * recurse past the end of THREAD_SIZE by default. |
| 19 | */ |
| 20 | #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0) |
| 21 | #define REC_STACK_SIZE (CONFIG_FRAME_WARN / 2) |
| 22 | #else |
| 23 | #define REC_STACK_SIZE (THREAD_SIZE / 8) |
| 24 | #endif |
| 25 | #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2) |
| 26 | |
| 27 | static int recur_count = REC_NUM_DEFAULT; |
| 28 | |
| 29 | static DEFINE_SPINLOCK(lock_me_up); |
| 30 | |
| 31 | static int recursive_loop(int remaining) |
| 32 | { |
| 33 | char buf[REC_STACK_SIZE]; |
| 34 | |
| 35 | /* Make sure compiler does not optimize this away. */ |
| 36 | memset(buf, (remaining & 0xff) | 0x1, REC_STACK_SIZE); |
| 37 | if (!remaining) |
| 38 | return 0; |
| 39 | else |
| 40 | return recursive_loop(remaining - 1); |
| 41 | } |
| 42 | |
| 43 | /* If the depth is negative, use the default, otherwise keep parameter. */ |
| 44 | void __init lkdtm_bugs_init(int *recur_param) |
| 45 | { |
| 46 | if (*recur_param < 0) |
| 47 | *recur_param = recur_count; |
| 48 | else |
| 49 | recur_count = *recur_param; |
| 50 | } |
| 51 | |
| 52 | void lkdtm_PANIC(void) |
| 53 | { |
| 54 | panic("dumptest"); |
| 55 | } |
| 56 | |
| 57 | void lkdtm_BUG(void) |
| 58 | { |
| 59 | BUG(); |
| 60 | } |
| 61 | |
| 62 | void lkdtm_WARNING(void) |
| 63 | { |
| 64 | WARN_ON(1); |
| 65 | } |
| 66 | |
| 67 | void lkdtm_EXCEPTION(void) |
| 68 | { |
| 69 | *((int *) 0) = 0; |
| 70 | } |
| 71 | |
| 72 | void lkdtm_LOOP(void) |
| 73 | { |
| 74 | for (;;) |
| 75 | ; |
| 76 | } |
| 77 | |
| 78 | void lkdtm_OVERFLOW(void) |
| 79 | { |
| 80 | (void) recursive_loop(recur_count); |
| 81 | } |
| 82 | |
Arnd Bergmann | df3d638 | 2017-01-11 15:56:44 +0100 | [diff] [blame] | 83 | static noinline void __lkdtm_CORRUPT_STACK(void *stack) |
| 84 | { |
| 85 | memset(stack, 'a', 64); |
| 86 | } |
| 87 | |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 88 | noinline void lkdtm_CORRUPT_STACK(void) |
| 89 | { |
| 90 | /* Use default char array length that triggers stack protection. */ |
| 91 | char data[8]; |
Arnd Bergmann | df3d638 | 2017-01-11 15:56:44 +0100 | [diff] [blame] | 92 | __lkdtm_CORRUPT_STACK(&data); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 93 | |
Michael Ellerman | 5a12d92 | 2016-11-15 18:02:32 +1100 | [diff] [blame] | 94 | pr_info("Corrupted stack with '%16s'...\n", data); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void) |
| 98 | { |
| 99 | static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5}; |
| 100 | u32 *p; |
| 101 | u32 val = 0x12345678; |
| 102 | |
| 103 | p = (u32 *)(data + 1); |
| 104 | if (*p == 0) |
| 105 | val = 0x87654321; |
| 106 | *p = val; |
| 107 | } |
| 108 | |
| 109 | void lkdtm_SOFTLOCKUP(void) |
| 110 | { |
| 111 | preempt_disable(); |
| 112 | for (;;) |
| 113 | cpu_relax(); |
| 114 | } |
| 115 | |
| 116 | void lkdtm_HARDLOCKUP(void) |
| 117 | { |
| 118 | local_irq_disable(); |
| 119 | for (;;) |
| 120 | cpu_relax(); |
| 121 | } |
| 122 | |
| 123 | void lkdtm_SPINLOCKUP(void) |
| 124 | { |
| 125 | /* Must be called twice to trigger. */ |
| 126 | spin_lock(&lock_me_up); |
| 127 | /* Let sparse know we intended to exit holding the lock. */ |
| 128 | __release(&lock_me_up); |
| 129 | } |
| 130 | |
| 131 | void lkdtm_HUNG_TASK(void) |
| 132 | { |
| 133 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 134 | schedule(); |
| 135 | } |
| 136 | |
| 137 | void lkdtm_ATOMIC_UNDERFLOW(void) |
| 138 | { |
| 139 | atomic_t under = ATOMIC_INIT(INT_MIN); |
| 140 | |
| 141 | pr_info("attempting good atomic increment\n"); |
| 142 | atomic_inc(&under); |
| 143 | atomic_dec(&under); |
| 144 | |
| 145 | pr_info("attempting bad atomic underflow\n"); |
| 146 | atomic_dec(&under); |
| 147 | } |
| 148 | |
| 149 | void lkdtm_ATOMIC_OVERFLOW(void) |
| 150 | { |
| 151 | atomic_t over = ATOMIC_INIT(INT_MAX); |
| 152 | |
| 153 | pr_info("attempting good atomic decrement\n"); |
| 154 | atomic_dec(&over); |
| 155 | atomic_inc(&over); |
| 156 | |
| 157 | pr_info("attempting bad atomic overflow\n"); |
| 158 | atomic_inc(&over); |
| 159 | } |
Kees Cook | bef1d72 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 160 | |
| 161 | void lkdtm_CORRUPT_LIST_ADD(void) |
| 162 | { |
| 163 | /* |
| 164 | * Initially, an empty list via LIST_HEAD: |
| 165 | * test_head.next = &test_head |
| 166 | * test_head.prev = &test_head |
| 167 | */ |
| 168 | LIST_HEAD(test_head); |
| 169 | struct lkdtm_list good, bad; |
| 170 | void *target[2] = { }; |
| 171 | void *redirection = ⌖ |
| 172 | |
| 173 | pr_info("attempting good list addition\n"); |
| 174 | |
| 175 | /* |
| 176 | * Adding to the list performs these actions: |
| 177 | * test_head.next->prev = &good.node |
| 178 | * good.node.next = test_head.next |
| 179 | * good.node.prev = test_head |
| 180 | * test_head.next = good.node |
| 181 | */ |
| 182 | list_add(&good.node, &test_head); |
| 183 | |
| 184 | pr_info("attempting corrupted list addition\n"); |
| 185 | /* |
| 186 | * In simulating this "write what where" primitive, the "what" is |
| 187 | * the address of &bad.node, and the "where" is the address held |
| 188 | * by "redirection". |
| 189 | */ |
| 190 | test_head.next = redirection; |
| 191 | list_add(&bad.node, &test_head); |
| 192 | |
| 193 | if (target[0] == NULL && target[1] == NULL) |
| 194 | pr_err("Overwrite did not happen, but no BUG?!\n"); |
| 195 | else |
| 196 | pr_err("list_add() corruption not detected!\n"); |
| 197 | } |
| 198 | |
| 199 | void lkdtm_CORRUPT_LIST_DEL(void) |
| 200 | { |
| 201 | LIST_HEAD(test_head); |
| 202 | struct lkdtm_list item; |
| 203 | void *target[2] = { }; |
| 204 | void *redirection = ⌖ |
| 205 | |
| 206 | list_add(&item.node, &test_head); |
| 207 | |
| 208 | pr_info("attempting good list removal\n"); |
| 209 | list_del(&item.node); |
| 210 | |
| 211 | pr_info("attempting corrupted list removal\n"); |
| 212 | list_add(&item.node, &test_head); |
| 213 | |
| 214 | /* As with the list_add() test above, this corrupts "next". */ |
| 215 | item.node.next = redirection; |
| 216 | list_del(&item.node); |
| 217 | |
| 218 | if (target[0] == NULL && target[1] == NULL) |
| 219 | pr_err("Overwrite did not happen, but no BUG?!\n"); |
| 220 | else |
| 221 | pr_err("list_del() corruption not detected!\n"); |
| 222 | } |