Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * This is for all the tests related to logic bugs (e.g. bad dereferences, |
| 4 | * bad alignment, bad loops, bad locking, bad scheduling, deep stacks, and |
| 5 | * lockups) along with other things that don't fit well into existing LKDTM |
| 6 | * test source files. |
| 7 | */ |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 8 | #include "lkdtm.h" |
Kees Cook | 6819d10 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 9 | #include <linux/list.h> |
Kees Cook | 6d2e91a | 2016-07-15 16:04:39 -0700 | [diff] [blame] | 10 | #include <linux/sched.h> |
Kees Cook | e22aa9d | 2017-03-24 10:51:25 -0700 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Kees Cook | 7b25a85 | 2017-08-04 13:04:21 -0700 | [diff] [blame] | 12 | #include <linux/sched/task_stack.h> |
Kees Cook | e22aa9d | 2017-03-24 10:51:25 -0700 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Kees Cook | ae2e1aa | 2020-04-06 20:12:34 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 15 | |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 16 | #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML) |
Andy Lutomirski | b09511c | 2019-11-24 21:18:04 -0800 | [diff] [blame] | 17 | #include <asm/desc.h> |
| 18 | #endif |
| 19 | |
Kees Cook | 6819d10 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 20 | struct lkdtm_list { |
| 21 | struct list_head node; |
| 22 | }; |
| 23 | |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 24 | /* |
| 25 | * Make sure our attempts to over run the kernel stack doesn't trigger |
| 26 | * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we |
| 27 | * recurse past the end of THREAD_SIZE by default. |
| 28 | */ |
| 29 | #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0) |
Raul E Rangel | b9bc7b8 | 2019-08-27 11:36:19 -0600 | [diff] [blame] | 30 | #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2) |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 31 | #else |
| 32 | #define REC_STACK_SIZE (THREAD_SIZE / 8) |
| 33 | #endif |
| 34 | #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2) |
| 35 | |
| 36 | static int recur_count = REC_NUM_DEFAULT; |
| 37 | |
| 38 | static DEFINE_SPINLOCK(lock_me_up); |
| 39 | |
Kees Cook | 24cccab | 2019-04-06 08:59:19 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Make sure compiler does not optimize this function or stack frame away: |
| 42 | * - function marked noinline |
| 43 | * - stack variables are marked volatile |
| 44 | * - stack variables are written (memset()) and read (pr_info()) |
| 45 | * - function has external effects (pr_info()) |
| 46 | * */ |
| 47 | static int noinline recursive_loop(int remaining) |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 48 | { |
Kees Cook | 24cccab | 2019-04-06 08:59:19 -0700 | [diff] [blame] | 49 | volatile char buf[REC_STACK_SIZE]; |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 50 | |
Kees Cook | 24cccab | 2019-04-06 08:59:19 -0700 | [diff] [blame] | 51 | memset((void *)buf, remaining & 0xFF, sizeof(buf)); |
| 52 | pr_info("loop %d/%d ...\n", (int)buf[remaining % sizeof(buf)], |
| 53 | recur_count); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 54 | if (!remaining) |
| 55 | return 0; |
| 56 | else |
| 57 | return recursive_loop(remaining - 1); |
| 58 | } |
| 59 | |
| 60 | /* If the depth is negative, use the default, otherwise keep parameter. */ |
| 61 | void __init lkdtm_bugs_init(int *recur_param) |
| 62 | { |
| 63 | if (*recur_param < 0) |
| 64 | *recur_param = recur_count; |
| 65 | else |
| 66 | recur_count = *recur_param; |
| 67 | } |
| 68 | |
| 69 | void lkdtm_PANIC(void) |
| 70 | { |
| 71 | panic("dumptest"); |
| 72 | } |
| 73 | |
| 74 | void lkdtm_BUG(void) |
| 75 | { |
| 76 | BUG(); |
| 77 | } |
| 78 | |
Kees Cook | d32f11b | 2017-11-17 15:27:17 -0800 | [diff] [blame] | 79 | static int warn_counter; |
| 80 | |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 81 | void lkdtm_WARNING(void) |
| 82 | { |
Kees Cook | 1ee170e | 2019-08-19 10:24:52 -0700 | [diff] [blame] | 83 | WARN_ON(++warn_counter); |
| 84 | } |
| 85 | |
| 86 | void lkdtm_WARNING_MESSAGE(void) |
| 87 | { |
| 88 | WARN(1, "Warning message trigger count: %d\n", ++warn_counter); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void lkdtm_EXCEPTION(void) |
| 92 | { |
Michael Davidson | 9e18308 | 2017-04-14 14:15:09 -0700 | [diff] [blame] | 93 | *((volatile int *) 0) = 0; |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void lkdtm_LOOP(void) |
| 97 | { |
| 98 | for (;;) |
| 99 | ; |
| 100 | } |
| 101 | |
Kees Cook | 24cccab | 2019-04-06 08:59:19 -0700 | [diff] [blame] | 102 | void lkdtm_EXHAUST_STACK(void) |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 103 | { |
Raul E Rangel | b9bc7b8 | 2019-08-27 11:36:19 -0600 | [diff] [blame] | 104 | pr_info("Calling function with %lu frame size to depth %d ...\n", |
Kees Cook | 24cccab | 2019-04-06 08:59:19 -0700 | [diff] [blame] | 105 | REC_STACK_SIZE, recur_count); |
| 106 | recursive_loop(recur_count); |
| 107 | pr_info("FAIL: survived without exhausting stack?!\n"); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Arnd Bergmann | 7a11a1d | 2017-01-11 15:56:44 +0100 | [diff] [blame] | 110 | static noinline void __lkdtm_CORRUPT_STACK(void *stack) |
| 111 | { |
Kees Cook | 93e78c6 | 2017-08-04 14:34:40 -0700 | [diff] [blame] | 112 | memset(stack, '\xff', 64); |
Arnd Bergmann | 7a11a1d | 2017-01-11 15:56:44 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Kees Cook | 93e78c6 | 2017-08-04 14:34:40 -0700 | [diff] [blame] | 115 | /* This should trip the stack canary, not corrupt the return address. */ |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 116 | noinline void lkdtm_CORRUPT_STACK(void) |
| 117 | { |
| 118 | /* Use default char array length that triggers stack protection. */ |
Kees Cook | 93e78c6 | 2017-08-04 14:34:40 -0700 | [diff] [blame] | 119 | char data[8] __aligned(sizeof(void *)); |
| 120 | |
Kees Cook | 464e86b | 2020-06-25 13:37:01 -0700 | [diff] [blame] | 121 | pr_info("Corrupting stack containing char array ...\n"); |
| 122 | __lkdtm_CORRUPT_STACK((void *)&data); |
Kees Cook | 93e78c6 | 2017-08-04 14:34:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Same as above but will only get a canary with -fstack-protector-strong */ |
| 126 | noinline void lkdtm_CORRUPT_STACK_STRONG(void) |
| 127 | { |
| 128 | union { |
| 129 | unsigned short shorts[4]; |
| 130 | unsigned long *ptr; |
| 131 | } data __aligned(sizeof(void *)); |
| 132 | |
Kees Cook | 464e86b | 2020-06-25 13:37:01 -0700 | [diff] [blame] | 133 | pr_info("Corrupting stack containing union ...\n"); |
| 134 | __lkdtm_CORRUPT_STACK((void *)&data); |
Kees Cook | 00f496c | 2016-06-26 22:17:25 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void) |
| 138 | { |
| 139 | static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5}; |
| 140 | u32 *p; |
| 141 | u32 val = 0x12345678; |
| 142 | |
| 143 | p = (u32 *)(data + 1); |
| 144 | if (*p == 0) |
| 145 | val = 0x87654321; |
| 146 | *p = val; |
| 147 | } |
| 148 | |
| 149 | void lkdtm_SOFTLOCKUP(void) |
| 150 | { |
| 151 | preempt_disable(); |
| 152 | for (;;) |
| 153 | cpu_relax(); |
| 154 | } |
| 155 | |
| 156 | void lkdtm_HARDLOCKUP(void) |
| 157 | { |
| 158 | local_irq_disable(); |
| 159 | for (;;) |
| 160 | cpu_relax(); |
| 161 | } |
| 162 | |
| 163 | void lkdtm_SPINLOCKUP(void) |
| 164 | { |
| 165 | /* Must be called twice to trigger. */ |
| 166 | spin_lock(&lock_me_up); |
| 167 | /* Let sparse know we intended to exit holding the lock. */ |
| 168 | __release(&lock_me_up); |
| 169 | } |
| 170 | |
| 171 | void lkdtm_HUNG_TASK(void) |
| 172 | { |
| 173 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 174 | schedule(); |
| 175 | } |
| 176 | |
Kees Cook | ae2e1aa | 2020-04-06 20:12:34 -0700 | [diff] [blame] | 177 | volatile unsigned int huge = INT_MAX - 2; |
| 178 | volatile unsigned int ignored; |
| 179 | |
| 180 | void lkdtm_OVERFLOW_SIGNED(void) |
| 181 | { |
| 182 | int value; |
| 183 | |
| 184 | value = huge; |
| 185 | pr_info("Normal signed addition ...\n"); |
| 186 | value += 1; |
| 187 | ignored = value; |
| 188 | |
| 189 | pr_info("Overflowing signed addition ...\n"); |
| 190 | value += 4; |
| 191 | ignored = value; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void lkdtm_OVERFLOW_UNSIGNED(void) |
| 196 | { |
| 197 | unsigned int value; |
| 198 | |
| 199 | value = huge; |
| 200 | pr_info("Normal unsigned addition ...\n"); |
| 201 | value += 1; |
| 202 | ignored = value; |
| 203 | |
| 204 | pr_info("Overflowing unsigned addition ...\n"); |
| 205 | value += 4; |
| 206 | ignored = value; |
| 207 | } |
| 208 | |
Gustavo A. R. Silva | c568515 | 2020-04-01 13:28:55 -0500 | [diff] [blame] | 209 | /* Intentionally using old-style flex array definition of 1 byte. */ |
Kees Cook | ae2e1aa | 2020-04-06 20:12:34 -0700 | [diff] [blame] | 210 | struct array_bounds_flex_array { |
| 211 | int one; |
| 212 | int two; |
| 213 | char data[1]; |
| 214 | }; |
| 215 | |
| 216 | struct array_bounds { |
| 217 | int one; |
| 218 | int two; |
| 219 | char data[8]; |
| 220 | int three; |
| 221 | }; |
| 222 | |
| 223 | void lkdtm_ARRAY_BOUNDS(void) |
| 224 | { |
| 225 | struct array_bounds_flex_array *not_checked; |
| 226 | struct array_bounds *checked; |
| 227 | volatile int i; |
| 228 | |
| 229 | not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL); |
| 230 | checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL); |
| 231 | |
| 232 | pr_info("Array access within bounds ...\n"); |
| 233 | /* For both, touch all bytes in the actual member size. */ |
| 234 | for (i = 0; i < sizeof(checked->data); i++) |
| 235 | checked->data[i] = 'A'; |
| 236 | /* |
| 237 | * For the uninstrumented flex array member, also touch 1 byte |
| 238 | * beyond to verify it is correctly uninstrumented. |
| 239 | */ |
| 240 | for (i = 0; i < sizeof(not_checked->data) + 1; i++) |
| 241 | not_checked->data[i] = 'A'; |
| 242 | |
| 243 | pr_info("Array access beyond bounds ...\n"); |
| 244 | for (i = 0; i < sizeof(checked->data) + 1; i++) |
| 245 | checked->data[i] = 'B'; |
| 246 | |
| 247 | kfree(not_checked); |
| 248 | kfree(checked); |
Kees Cook | 464e86b | 2020-06-25 13:37:01 -0700 | [diff] [blame] | 249 | pr_err("FAIL: survived array bounds overflow!\n"); |
Kees Cook | ae2e1aa | 2020-04-06 20:12:34 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Kees Cook | 6819d10 | 2016-08-17 14:42:12 -0700 | [diff] [blame] | 252 | void lkdtm_CORRUPT_LIST_ADD(void) |
| 253 | { |
| 254 | /* |
| 255 | * Initially, an empty list via LIST_HEAD: |
| 256 | * test_head.next = &test_head |
| 257 | * test_head.prev = &test_head |
| 258 | */ |
| 259 | LIST_HEAD(test_head); |
| 260 | struct lkdtm_list good, bad; |
| 261 | void *target[2] = { }; |
| 262 | void *redirection = ⌖ |
| 263 | |
| 264 | pr_info("attempting good list addition\n"); |
| 265 | |
| 266 | /* |
| 267 | * Adding to the list performs these actions: |
| 268 | * test_head.next->prev = &good.node |
| 269 | * good.node.next = test_head.next |
| 270 | * good.node.prev = test_head |
| 271 | * test_head.next = good.node |
| 272 | */ |
| 273 | list_add(&good.node, &test_head); |
| 274 | |
| 275 | pr_info("attempting corrupted list addition\n"); |
| 276 | /* |
| 277 | * In simulating this "write what where" primitive, the "what" is |
| 278 | * the address of &bad.node, and the "where" is the address held |
| 279 | * by "redirection". |
| 280 | */ |
| 281 | test_head.next = redirection; |
| 282 | list_add(&bad.node, &test_head); |
| 283 | |
| 284 | if (target[0] == NULL && target[1] == NULL) |
| 285 | pr_err("Overwrite did not happen, but no BUG?!\n"); |
| 286 | else |
| 287 | pr_err("list_add() corruption not detected!\n"); |
| 288 | } |
| 289 | |
| 290 | void lkdtm_CORRUPT_LIST_DEL(void) |
| 291 | { |
| 292 | LIST_HEAD(test_head); |
| 293 | struct lkdtm_list item; |
| 294 | void *target[2] = { }; |
| 295 | void *redirection = ⌖ |
| 296 | |
| 297 | list_add(&item.node, &test_head); |
| 298 | |
| 299 | pr_info("attempting good list removal\n"); |
| 300 | list_del(&item.node); |
| 301 | |
| 302 | pr_info("attempting corrupted list removal\n"); |
| 303 | list_add(&item.node, &test_head); |
| 304 | |
| 305 | /* As with the list_add() test above, this corrupts "next". */ |
| 306 | item.node.next = redirection; |
| 307 | list_del(&item.node); |
| 308 | |
| 309 | if (target[0] == NULL && target[1] == NULL) |
| 310 | pr_err("Overwrite did not happen, but no BUG?!\n"); |
| 311 | else |
| 312 | pr_err("list_del() corruption not detected!\n"); |
| 313 | } |
Kees Cook | e22aa9d | 2017-03-24 10:51:25 -0700 | [diff] [blame] | 314 | |
Kees Cook | 7b25a85 | 2017-08-04 13:04:21 -0700 | [diff] [blame] | 315 | /* Test if unbalanced set_fs(KERNEL_DS)/set_fs(USER_DS) check exists. */ |
Kees Cook | e22aa9d | 2017-03-24 10:51:25 -0700 | [diff] [blame] | 316 | void lkdtm_CORRUPT_USER_DS(void) |
| 317 | { |
| 318 | pr_info("setting bad task size limit\n"); |
| 319 | set_fs(KERNEL_DS); |
| 320 | |
| 321 | /* Make sure we do not keep running with a KERNEL_DS! */ |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 322 | force_sig(SIGKILL); |
Kees Cook | e22aa9d | 2017-03-24 10:51:25 -0700 | [diff] [blame] | 323 | } |
Kees Cook | 7b25a85 | 2017-08-04 13:04:21 -0700 | [diff] [blame] | 324 | |
| 325 | /* Test that VMAP_STACK is actually allocating with a leading guard page */ |
| 326 | void lkdtm_STACK_GUARD_PAGE_LEADING(void) |
| 327 | { |
| 328 | const unsigned char *stack = task_stack_page(current); |
| 329 | const unsigned char *ptr = stack - 1; |
| 330 | volatile unsigned char byte; |
| 331 | |
| 332 | pr_info("attempting bad read from page below current stack\n"); |
| 333 | |
| 334 | byte = *ptr; |
| 335 | |
Lee Jones | f049c54 | 2020-06-26 14:05:19 +0100 | [diff] [blame] | 336 | pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte); |
Kees Cook | 7b25a85 | 2017-08-04 13:04:21 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* Test that VMAP_STACK is actually allocating with a trailing guard page */ |
| 340 | void lkdtm_STACK_GUARD_PAGE_TRAILING(void) |
| 341 | { |
| 342 | const unsigned char *stack = task_stack_page(current); |
| 343 | const unsigned char *ptr = stack + THREAD_SIZE; |
| 344 | volatile unsigned char byte; |
| 345 | |
| 346 | pr_info("attempting bad read from page above current stack\n"); |
| 347 | |
| 348 | byte = *ptr; |
| 349 | |
Lee Jones | f049c54 | 2020-06-26 14:05:19 +0100 | [diff] [blame] | 350 | pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte); |
Kees Cook | 7b25a85 | 2017-08-04 13:04:21 -0700 | [diff] [blame] | 351 | } |
Kees Cook | 06b32fd | 2019-06-22 13:18:23 -0700 | [diff] [blame] | 352 | |
| 353 | void lkdtm_UNSET_SMEP(void) |
| 354 | { |
Brendan Higgins | 0e31e35 | 2019-12-12 16:35:22 -0800 | [diff] [blame] | 355 | #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML) |
Kees Cook | 06b32fd | 2019-06-22 13:18:23 -0700 | [diff] [blame] | 356 | #define MOV_CR4_DEPTH 64 |
| 357 | void (*direct_write_cr4)(unsigned long val); |
| 358 | unsigned char *insn; |
| 359 | unsigned long cr4; |
| 360 | int i; |
| 361 | |
| 362 | cr4 = native_read_cr4(); |
| 363 | |
| 364 | if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) { |
| 365 | pr_err("FAIL: SMEP not in use\n"); |
| 366 | return; |
| 367 | } |
| 368 | cr4 &= ~(X86_CR4_SMEP); |
| 369 | |
| 370 | pr_info("trying to clear SMEP normally\n"); |
| 371 | native_write_cr4(cr4); |
| 372 | if (cr4 == native_read_cr4()) { |
| 373 | pr_err("FAIL: pinning SMEP failed!\n"); |
| 374 | cr4 |= X86_CR4_SMEP; |
| 375 | pr_info("restoring SMEP\n"); |
| 376 | native_write_cr4(cr4); |
| 377 | return; |
| 378 | } |
| 379 | pr_info("ok: SMEP did not get cleared\n"); |
| 380 | |
| 381 | /* |
| 382 | * To test the post-write pinning verification we need to call |
| 383 | * directly into the middle of native_write_cr4() where the |
| 384 | * cr4 write happens, skipping any pinning. This searches for |
| 385 | * the cr4 writing instruction. |
| 386 | */ |
| 387 | insn = (unsigned char *)native_write_cr4; |
| 388 | for (i = 0; i < MOV_CR4_DEPTH; i++) { |
| 389 | /* mov %rdi, %cr4 */ |
| 390 | if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7) |
| 391 | break; |
| 392 | /* mov %rdi,%rax; mov %rax, %cr4 */ |
| 393 | if (insn[i] == 0x48 && insn[i+1] == 0x89 && |
| 394 | insn[i+2] == 0xf8 && insn[i+3] == 0x0f && |
| 395 | insn[i+4] == 0x22 && insn[i+5] == 0xe0) |
| 396 | break; |
| 397 | } |
| 398 | if (i >= MOV_CR4_DEPTH) { |
| 399 | pr_info("ok: cannot locate cr4 writing call gadget\n"); |
| 400 | return; |
| 401 | } |
| 402 | direct_write_cr4 = (void *)(insn + i); |
| 403 | |
| 404 | pr_info("trying to clear SMEP with call gadget\n"); |
| 405 | direct_write_cr4(cr4); |
| 406 | if (native_read_cr4() & X86_CR4_SMEP) { |
| 407 | pr_info("ok: SMEP removal was reverted\n"); |
| 408 | } else { |
| 409 | pr_err("FAIL: cleared SMEP not detected!\n"); |
| 410 | cr4 |= X86_CR4_SMEP; |
| 411 | pr_info("restoring SMEP\n"); |
| 412 | native_write_cr4(cr4); |
| 413 | } |
| 414 | #else |
Kees Cook | cea23ef | 2020-01-02 12:29:17 -0800 | [diff] [blame] | 415 | pr_err("XFAIL: this test is x86_64-only\n"); |
Kees Cook | 06b32fd | 2019-06-22 13:18:23 -0700 | [diff] [blame] | 416 | #endif |
| 417 | } |
Andy Lutomirski | b09511c | 2019-11-24 21:18:04 -0800 | [diff] [blame] | 418 | |
Andy Lutomirski | b09511c | 2019-11-24 21:18:04 -0800 | [diff] [blame] | 419 | void lkdtm_DOUBLE_FAULT(void) |
| 420 | { |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 421 | #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML) |
Andy Lutomirski | b09511c | 2019-11-24 21:18:04 -0800 | [diff] [blame] | 422 | /* |
| 423 | * Trigger #DF by setting the stack limit to zero. This clobbers |
| 424 | * a GDT TLS slot, which is okay because the current task will die |
| 425 | * anyway due to the double fault. |
| 426 | */ |
| 427 | struct desc_struct d = { |
| 428 | .type = 3, /* expand-up, writable, accessed data */ |
| 429 | .p = 1, /* present */ |
| 430 | .d = 1, /* 32-bit */ |
| 431 | .g = 0, /* limit in bytes */ |
| 432 | .s = 1, /* not system */ |
| 433 | }; |
| 434 | |
| 435 | local_irq_disable(); |
| 436 | write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()), |
| 437 | GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S); |
| 438 | |
| 439 | /* |
| 440 | * Put our zero-limit segment in SS and then trigger a fault. The |
| 441 | * 4-byte access to (%esp) will fault with #SS, and the attempt to |
| 442 | * deliver the fault will recursively cause #SS and result in #DF. |
| 443 | * This whole process happens while NMIs and MCEs are blocked by the |
| 444 | * MOV SS window. This is nice because an NMI with an invalid SS |
| 445 | * would also double-fault, resulting in the NMI or MCE being lost. |
| 446 | */ |
| 447 | asm volatile ("movw %0, %%ss; addl $0, (%%esp)" :: |
| 448 | "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3))); |
| 449 | |
Kees Cook | cea23ef | 2020-01-02 12:29:17 -0800 | [diff] [blame] | 450 | pr_err("FAIL: tried to double fault but didn't die\n"); |
| 451 | #else |
| 452 | pr_err("XFAIL: this test is ia32-only\n"); |
Andy Lutomirski | b09511c | 2019-11-24 21:18:04 -0800 | [diff] [blame] | 453 | #endif |
Kees Cook | cea23ef | 2020-01-02 12:29:17 -0800 | [diff] [blame] | 454 | } |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 455 | |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 456 | #ifdef CONFIG_ARM64 |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 457 | static noinline void change_pac_parameters(void) |
| 458 | { |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 459 | if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) { |
| 460 | /* Reset the keys of current task */ |
| 461 | ptrauth_thread_init_kernel(current); |
| 462 | ptrauth_thread_switch_kernel(current); |
| 463 | } |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 464 | } |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 465 | #endif |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 466 | |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 467 | noinline void lkdtm_CORRUPT_PAC(void) |
| 468 | { |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 469 | #ifdef CONFIG_ARM64 |
| 470 | #define CORRUPT_PAC_ITERATE 10 |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 471 | int i; |
| 472 | |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 473 | if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH)) |
| 474 | pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH\n"); |
| 475 | |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 476 | if (!system_supports_address_auth()) { |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 477 | pr_err("FAIL: CPU lacks pointer authentication feature\n"); |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 481 | pr_info("changing PAC parameters to force function return failure...\n"); |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 482 | /* |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 483 | * PAC is a hash value computed from input keys, return address and |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 484 | * stack pointer. As pac has fewer bits so there is a chance of |
| 485 | * collision, so iterate few times to reduce the collision probability. |
| 486 | */ |
| 487 | for (i = 0; i < CORRUPT_PAC_ITERATE; i++) |
| 488 | change_pac_parameters(); |
| 489 | |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 490 | pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n"); |
| 491 | #else |
| 492 | pr_err("XFAIL: this test is arm64-only\n"); |
Amit Daniel Kachhap | 6cb6982 | 2020-03-13 14:35:04 +0530 | [diff] [blame] | 493 | #endif |
Kees Cook | ae56942 | 2020-06-25 13:37:04 -0700 | [diff] [blame] | 494 | } |