bugs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: GPL-2.0
  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. */
  8. #include "lkdtm.h"
  9. #include <linux/cpu.h>
  10. #include <linux/list.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <linux/slab.h>
  15. #include <linux/stop_machine.h>
  16. #include <linux/uaccess.h>
  17. #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
  18. #include <asm/desc.h>
  19. #endif
  20. struct lkdtm_list {
  21. struct list_head node;
  22. };
  23. /*
  24. * Make sure our attempts to over run the kernel stack doesn't trigger
  25. * a compiler warning when CONFIG_FRAME_WARN is set. Then make sure we
  26. * recurse past the end of THREAD_SIZE by default.
  27. */
  28. #if defined(CONFIG_FRAME_WARN) && (CONFIG_FRAME_WARN > 0)
  29. #define REC_STACK_SIZE (_AC(CONFIG_FRAME_WARN, UL) / 2)
  30. #else
  31. #define REC_STACK_SIZE (THREAD_SIZE / 8UL)
  32. #endif
  33. #define REC_NUM_DEFAULT ((THREAD_SIZE / REC_STACK_SIZE) * 2)
  34. static int recur_count = REC_NUM_DEFAULT;
  35. static DEFINE_SPINLOCK(lock_me_up);
  36. /*
  37. * Make sure compiler does not optimize this function or stack frame away:
  38. * - function marked noinline
  39. * - stack variables are marked volatile
  40. * - stack variables are written (memset()) and read (buf[..] passed as arg)
  41. * - function may have external effects (memzero_explicit())
  42. * - no tail recursion possible
  43. */
  44. static int noinline recursive_loop(int remaining)
  45. {
  46. volatile char buf[REC_STACK_SIZE];
  47. volatile int ret;
  48. memset((void *)buf, remaining & 0xFF, sizeof(buf));
  49. if (!remaining)
  50. ret = 0;
  51. else
  52. ret = recursive_loop((int)buf[remaining % sizeof(buf)] - 1);
  53. memzero_explicit((void *)buf, sizeof(buf));
  54. return ret;
  55. }
  56. /* If the depth is negative, use the default, otherwise keep parameter. */
  57. void __init lkdtm_bugs_init(int *recur_param)
  58. {
  59. if (*recur_param < 0)
  60. *recur_param = recur_count;
  61. else
  62. recur_count = *recur_param;
  63. }
  64. static void lkdtm_PANIC(void)
  65. {
  66. panic("dumptest");
  67. }
  68. static int panic_stop_irqoff_fn(void *arg)
  69. {
  70. atomic_t *v = arg;
  71. /*
  72. * As stop_machine() disables interrupts, all CPUs within this function
  73. * have interrupts disabled and cannot take a regular IPI.
  74. *
  75. * The last CPU which enters here will trigger a panic, and as all CPUs
  76. * cannot take a regular IPI, we'll only be able to stop secondaries if
  77. * smp_send_stop() or crash_smp_send_stop() uses an NMI.
  78. */
  79. if (atomic_inc_return(v) == num_online_cpus())
  80. panic("panic stop irqoff test");
  81. for (;;)
  82. cpu_relax();
  83. }
  84. static void lkdtm_PANIC_STOP_IRQOFF(void)
  85. {
  86. atomic_t v = ATOMIC_INIT(0);
  87. stop_machine(panic_stop_irqoff_fn, &v, cpu_online_mask);
  88. }
  89. static void lkdtm_BUG(void)
  90. {
  91. BUG();
  92. }
  93. static int warn_counter;
  94. static void lkdtm_WARNING(void)
  95. {
  96. WARN_ON(++warn_counter);
  97. }
  98. static void lkdtm_WARNING_MESSAGE(void)
  99. {
  100. WARN(1, "Warning message trigger count: %d\n", ++warn_counter);
  101. }
  102. static void lkdtm_EXCEPTION(void)
  103. {
  104. *((volatile int *) 0) = 0;
  105. }
  106. static void lkdtm_LOOP(void)
  107. {
  108. for (;;)
  109. ;
  110. }
  111. static void lkdtm_EXHAUST_STACK(void)
  112. {
  113. pr_info("Calling function with %lu frame size to depth %d ...\n",
  114. REC_STACK_SIZE, recur_count);
  115. recursive_loop(recur_count);
  116. pr_info("FAIL: survived without exhausting stack?!\n");
  117. }
  118. static noinline void __lkdtm_CORRUPT_STACK(void *stack)
  119. {
  120. memset(stack, '\xff', 64);
  121. }
  122. /* This should trip the stack canary, not corrupt the return address. */
  123. static noinline void lkdtm_CORRUPT_STACK(void)
  124. {
  125. /* Use default char array length that triggers stack protection. */
  126. char data[8] __aligned(sizeof(void *));
  127. pr_info("Corrupting stack containing char array ...\n");
  128. __lkdtm_CORRUPT_STACK((void *)&data);
  129. }
  130. /* Same as above but will only get a canary with -fstack-protector-strong */
  131. static noinline void lkdtm_CORRUPT_STACK_STRONG(void)
  132. {
  133. union {
  134. unsigned short shorts[4];
  135. unsigned long *ptr;
  136. } data __aligned(sizeof(void *));
  137. pr_info("Corrupting stack containing union ...\n");
  138. __lkdtm_CORRUPT_STACK((void *)&data);
  139. }
  140. static pid_t stack_pid;
  141. static unsigned long stack_addr;
  142. static void lkdtm_REPORT_STACK(void)
  143. {
  144. volatile uintptr_t magic;
  145. pid_t pid = task_pid_nr(current);
  146. if (pid != stack_pid) {
  147. pr_info("Starting stack offset tracking for pid %d\n", pid);
  148. stack_pid = pid;
  149. stack_addr = (uintptr_t)&magic;
  150. }
  151. pr_info("Stack offset: %d\n", (int)(stack_addr - (uintptr_t)&magic));
  152. }
  153. static pid_t stack_canary_pid;
  154. static unsigned long stack_canary;
  155. static unsigned long stack_canary_offset;
  156. static noinline void __lkdtm_REPORT_STACK_CANARY(void *stack)
  157. {
  158. int i = 0;
  159. pid_t pid = task_pid_nr(current);
  160. unsigned long *canary = (unsigned long *)stack;
  161. unsigned long current_offset = 0, init_offset = 0;
  162. /* Do our best to find the canary in a 16 word window ... */
  163. for (i = 1; i < 16; i++) {
  164. canary = (unsigned long *)stack + i;
  165. #ifdef CONFIG_STACKPROTECTOR
  166. if (*canary == current->stack_canary)
  167. current_offset = i;
  168. if (*canary == init_task.stack_canary)
  169. init_offset = i;
  170. #endif
  171. }
  172. if (current_offset == 0) {
  173. /*
  174. * If the canary doesn't match what's in the task_struct,
  175. * we're either using a global canary or the stack frame
  176. * layout changed.
  177. */
  178. if (init_offset != 0) {
  179. pr_err("FAIL: global stack canary found at offset %ld (canary for pid %d matches init_task's)!\n",
  180. init_offset, pid);
  181. } else {
  182. pr_warn("FAIL: did not correctly locate stack canary :(\n");
  183. pr_expected_config(CONFIG_STACKPROTECTOR);
  184. }
  185. return;
  186. } else if (init_offset != 0) {
  187. pr_warn("WARNING: found both current and init_task canaries nearby?!\n");
  188. }
  189. canary = (unsigned long *)stack + current_offset;
  190. if (stack_canary_pid == 0) {
  191. stack_canary = *canary;
  192. stack_canary_pid = pid;
  193. stack_canary_offset = current_offset;
  194. pr_info("Recorded stack canary for pid %d at offset %ld\n",
  195. stack_canary_pid, stack_canary_offset);
  196. } else if (pid == stack_canary_pid) {
  197. pr_warn("ERROR: saw pid %d again -- please use a new pid\n", pid);
  198. } else {
  199. if (current_offset != stack_canary_offset) {
  200. pr_warn("ERROR: canary offset changed from %ld to %ld!?\n",
  201. stack_canary_offset, current_offset);
  202. return;
  203. }
  204. if (*canary == stack_canary) {
  205. pr_warn("FAIL: canary identical for pid %d and pid %d at offset %ld!\n",
  206. stack_canary_pid, pid, current_offset);
  207. } else {
  208. pr_info("ok: stack canaries differ between pid %d and pid %d at offset %ld.\n",
  209. stack_canary_pid, pid, current_offset);
  210. /* Reset the test. */
  211. stack_canary_pid = 0;
  212. }
  213. }
  214. }
  215. static void lkdtm_REPORT_STACK_CANARY(void)
  216. {
  217. /* Use default char array length that triggers stack protection. */
  218. char data[8] __aligned(sizeof(void *)) = { };
  219. __lkdtm_REPORT_STACK_CANARY((void *)&data);
  220. }
  221. static void lkdtm_UNALIGNED_LOAD_STORE_WRITE(void)
  222. {
  223. static u8 data[5] __attribute__((aligned(4))) = {1, 2, 3, 4, 5};
  224. u32 *p;
  225. u32 val = 0x12345678;
  226. p = (u32 *)(data + 1);
  227. if (*p == 0)
  228. val = 0x87654321;
  229. *p = val;
  230. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
  231. pr_err("XFAIL: arch has CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS\n");
  232. }
  233. static void lkdtm_SOFTLOCKUP(void)
  234. {
  235. preempt_disable();
  236. for (;;)
  237. cpu_relax();
  238. }
  239. static void lkdtm_HARDLOCKUP(void)
  240. {
  241. local_irq_disable();
  242. for (;;)
  243. cpu_relax();
  244. }
  245. static void __lkdtm_SMP_CALL_LOCKUP(void *unused)
  246. {
  247. for (;;)
  248. cpu_relax();
  249. }
  250. static void lkdtm_SMP_CALL_LOCKUP(void)
  251. {
  252. unsigned int cpu, target;
  253. cpus_read_lock();
  254. cpu = get_cpu();
  255. target = cpumask_any_but(cpu_online_mask, cpu);
  256. if (target >= nr_cpu_ids) {
  257. pr_err("FAIL: no other online CPUs\n");
  258. goto out_put_cpus;
  259. }
  260. smp_call_function_single(target, __lkdtm_SMP_CALL_LOCKUP, NULL, 1);
  261. pr_err("FAIL: did not hang\n");
  262. out_put_cpus:
  263. put_cpu();
  264. cpus_read_unlock();
  265. }
  266. static void lkdtm_SPINLOCKUP(void)
  267. {
  268. /* Must be called twice to trigger. */
  269. spin_lock(&lock_me_up);
  270. /* Let sparse know we intended to exit holding the lock. */
  271. __release(&lock_me_up);
  272. }
  273. static void __noreturn lkdtm_HUNG_TASK(void)
  274. {
  275. set_current_state(TASK_UNINTERRUPTIBLE);
  276. schedule();
  277. BUG();
  278. }
  279. static volatile unsigned int huge = INT_MAX - 2;
  280. static volatile unsigned int ignored;
  281. static void lkdtm_OVERFLOW_SIGNED(void)
  282. {
  283. int value;
  284. value = huge;
  285. pr_info("Normal signed addition ...\n");
  286. value += 1;
  287. ignored = value;
  288. pr_info("Overflowing signed addition ...\n");
  289. value += 4;
  290. ignored = value;
  291. }
  292. static void lkdtm_OVERFLOW_UNSIGNED(void)
  293. {
  294. unsigned int value;
  295. value = huge;
  296. pr_info("Normal unsigned addition ...\n");
  297. value += 1;
  298. ignored = value;
  299. pr_info("Overflowing unsigned addition ...\n");
  300. value += 4;
  301. ignored = value;
  302. }
  303. /* Intentionally using unannotated flex array definition. */
  304. struct array_bounds_flex_array {
  305. int one;
  306. int two;
  307. char data[];
  308. };
  309. struct array_bounds {
  310. int one;
  311. int two;
  312. char data[8];
  313. int three;
  314. };
  315. static void lkdtm_ARRAY_BOUNDS(void)
  316. {
  317. struct array_bounds_flex_array *not_checked;
  318. struct array_bounds *checked;
  319. volatile int i;
  320. not_checked = kmalloc(sizeof(*not_checked) * 2, GFP_KERNEL);
  321. checked = kmalloc(sizeof(*checked) * 2, GFP_KERNEL);
  322. if (!not_checked || !checked) {
  323. kfree(not_checked);
  324. kfree(checked);
  325. return;
  326. }
  327. pr_info("Array access within bounds ...\n");
  328. /* For both, touch all bytes in the actual member size. */
  329. for (i = 0; i < sizeof(checked->data); i++)
  330. checked->data[i] = 'A';
  331. /*
  332. * For the uninstrumented flex array member, also touch 1 byte
  333. * beyond to verify it is correctly uninstrumented.
  334. */
  335. for (i = 0; i < 2; i++)
  336. not_checked->data[i] = 'A';
  337. pr_info("Array access beyond bounds ...\n");
  338. for (i = 0; i < sizeof(checked->data) + 1; i++)
  339. checked->data[i] = 'B';
  340. kfree(not_checked);
  341. kfree(checked);
  342. pr_err("FAIL: survived array bounds overflow!\n");
  343. if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
  344. pr_expected_config(CONFIG_UBSAN_TRAP);
  345. else
  346. pr_expected_config(CONFIG_UBSAN_BOUNDS);
  347. }
  348. struct lkdtm_annotated {
  349. unsigned long flags;
  350. int count;
  351. int array[] __counted_by(count);
  352. };
  353. static volatile int fam_count = 4;
  354. static void lkdtm_FAM_BOUNDS(void)
  355. {
  356. struct lkdtm_annotated *inst;
  357. inst = kzalloc(struct_size(inst, array, fam_count + 1), GFP_KERNEL);
  358. if (!inst) {
  359. pr_err("FAIL: could not allocate test struct!\n");
  360. return;
  361. }
  362. inst->count = fam_count;
  363. pr_info("Array access within bounds ...\n");
  364. inst->array[1] = fam_count;
  365. ignored = inst->array[1];
  366. pr_info("Array access beyond bounds ...\n");
  367. inst->array[fam_count] = fam_count;
  368. ignored = inst->array[fam_count];
  369. kfree(inst);
  370. pr_err("FAIL: survived access of invalid flexible array member index!\n");
  371. if (!IS_ENABLED(CONFIG_CC_HAS_COUNTED_BY))
  372. pr_warn("This is expected since this %s was built with a compiler that does not support __counted_by\n",
  373. lkdtm_kernel_info);
  374. else if (IS_ENABLED(CONFIG_UBSAN_BOUNDS))
  375. pr_expected_config(CONFIG_UBSAN_TRAP);
  376. else
  377. pr_expected_config(CONFIG_UBSAN_BOUNDS);
  378. }
  379. static void lkdtm_CORRUPT_LIST_ADD(void)
  380. {
  381. /*
  382. * Initially, an empty list via LIST_HEAD:
  383. * test_head.next = &test_head
  384. * test_head.prev = &test_head
  385. */
  386. LIST_HEAD(test_head);
  387. struct lkdtm_list good, bad;
  388. void *target[2] = { };
  389. void *redirection = &target;
  390. pr_info("attempting good list addition\n");
  391. /*
  392. * Adding to the list performs these actions:
  393. * test_head.next->prev = &good.node
  394. * good.node.next = test_head.next
  395. * good.node.prev = test_head
  396. * test_head.next = good.node
  397. */
  398. list_add(&good.node, &test_head);
  399. pr_info("attempting corrupted list addition\n");
  400. /*
  401. * In simulating this "write what where" primitive, the "what" is
  402. * the address of &bad.node, and the "where" is the address held
  403. * by "redirection".
  404. */
  405. test_head.next = redirection;
  406. list_add(&bad.node, &test_head);
  407. if (target[0] == NULL && target[1] == NULL)
  408. pr_err("Overwrite did not happen, but no BUG?!\n");
  409. else {
  410. pr_err("list_add() corruption not detected!\n");
  411. pr_expected_config(CONFIG_LIST_HARDENED);
  412. }
  413. }
  414. static void lkdtm_CORRUPT_LIST_DEL(void)
  415. {
  416. LIST_HEAD(test_head);
  417. struct lkdtm_list item;
  418. void *target[2] = { };
  419. void *redirection = &target;
  420. list_add(&item.node, &test_head);
  421. pr_info("attempting good list removal\n");
  422. list_del(&item.node);
  423. pr_info("attempting corrupted list removal\n");
  424. list_add(&item.node, &test_head);
  425. /* As with the list_add() test above, this corrupts "next". */
  426. item.node.next = redirection;
  427. list_del(&item.node);
  428. if (target[0] == NULL && target[1] == NULL)
  429. pr_err("Overwrite did not happen, but no BUG?!\n");
  430. else {
  431. pr_err("list_del() corruption not detected!\n");
  432. pr_expected_config(CONFIG_LIST_HARDENED);
  433. }
  434. }
  435. /* Test that VMAP_STACK is actually allocating with a leading guard page */
  436. static void lkdtm_STACK_GUARD_PAGE_LEADING(void)
  437. {
  438. const unsigned char *stack = task_stack_page(current);
  439. const unsigned char *ptr = stack - 1;
  440. volatile unsigned char byte;
  441. pr_info("attempting bad read from page below current stack\n");
  442. byte = *ptr;
  443. pr_err("FAIL: accessed page before stack! (byte: %x)\n", byte);
  444. }
  445. /* Test that VMAP_STACK is actually allocating with a trailing guard page */
  446. static void lkdtm_STACK_GUARD_PAGE_TRAILING(void)
  447. {
  448. const unsigned char *stack = task_stack_page(current);
  449. const unsigned char *ptr = stack + THREAD_SIZE;
  450. volatile unsigned char byte;
  451. pr_info("attempting bad read from page above current stack\n");
  452. byte = *ptr;
  453. pr_err("FAIL: accessed page after stack! (byte: %x)\n", byte);
  454. }
  455. static void lkdtm_UNSET_SMEP(void)
  456. {
  457. #if IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_UML)
  458. #define MOV_CR4_DEPTH 64
  459. void (*direct_write_cr4)(unsigned long val);
  460. unsigned char *insn;
  461. unsigned long cr4;
  462. int i;
  463. cr4 = native_read_cr4();
  464. if ((cr4 & X86_CR4_SMEP) != X86_CR4_SMEP) {
  465. pr_err("FAIL: SMEP not in use\n");
  466. return;
  467. }
  468. cr4 &= ~(X86_CR4_SMEP);
  469. pr_info("trying to clear SMEP normally\n");
  470. native_write_cr4(cr4);
  471. if (cr4 == native_read_cr4()) {
  472. pr_err("FAIL: pinning SMEP failed!\n");
  473. cr4 |= X86_CR4_SMEP;
  474. pr_info("restoring SMEP\n");
  475. native_write_cr4(cr4);
  476. return;
  477. }
  478. pr_info("ok: SMEP did not get cleared\n");
  479. /*
  480. * To test the post-write pinning verification we need to call
  481. * directly into the middle of native_write_cr4() where the
  482. * cr4 write happens, skipping any pinning. This searches for
  483. * the cr4 writing instruction.
  484. */
  485. insn = (unsigned char *)native_write_cr4;
  486. OPTIMIZER_HIDE_VAR(insn);
  487. for (i = 0; i < MOV_CR4_DEPTH; i++) {
  488. /* mov %rdi, %cr4 */
  489. if (insn[i] == 0x0f && insn[i+1] == 0x22 && insn[i+2] == 0xe7)
  490. break;
  491. /* mov %rdi,%rax; mov %rax, %cr4 */
  492. if (insn[i] == 0x48 && insn[i+1] == 0x89 &&
  493. insn[i+2] == 0xf8 && insn[i+3] == 0x0f &&
  494. insn[i+4] == 0x22 && insn[i+5] == 0xe0)
  495. break;
  496. }
  497. if (i >= MOV_CR4_DEPTH) {
  498. pr_info("ok: cannot locate cr4 writing call gadget\n");
  499. return;
  500. }
  501. direct_write_cr4 = (void *)(insn + i);
  502. pr_info("trying to clear SMEP with call gadget\n");
  503. direct_write_cr4(cr4);
  504. if (native_read_cr4() & X86_CR4_SMEP) {
  505. pr_info("ok: SMEP removal was reverted\n");
  506. } else {
  507. pr_err("FAIL: cleared SMEP not detected!\n");
  508. cr4 |= X86_CR4_SMEP;
  509. pr_info("restoring SMEP\n");
  510. native_write_cr4(cr4);
  511. }
  512. #else
  513. pr_err("XFAIL: this test is x86_64-only\n");
  514. #endif
  515. }
  516. static void lkdtm_DOUBLE_FAULT(void)
  517. {
  518. #if IS_ENABLED(CONFIG_X86_32) && !IS_ENABLED(CONFIG_UML)
  519. /*
  520. * Trigger #DF by setting the stack limit to zero. This clobbers
  521. * a GDT TLS slot, which is okay because the current task will die
  522. * anyway due to the double fault.
  523. */
  524. struct desc_struct d = {
  525. .type = 3, /* expand-up, writable, accessed data */
  526. .p = 1, /* present */
  527. .d = 1, /* 32-bit */
  528. .g = 0, /* limit in bytes */
  529. .s = 1, /* not system */
  530. };
  531. local_irq_disable();
  532. write_gdt_entry(get_cpu_gdt_rw(smp_processor_id()),
  533. GDT_ENTRY_TLS_MIN, &d, DESCTYPE_S);
  534. /*
  535. * Put our zero-limit segment in SS and then trigger a fault. The
  536. * 4-byte access to (%esp) will fault with #SS, and the attempt to
  537. * deliver the fault will recursively cause #SS and result in #DF.
  538. * This whole process happens while NMIs and MCEs are blocked by the
  539. * MOV SS window. This is nice because an NMI with an invalid SS
  540. * would also double-fault, resulting in the NMI or MCE being lost.
  541. */
  542. asm volatile ("movw %0, %%ss; addl $0, (%%esp)" ::
  543. "r" ((unsigned short)(GDT_ENTRY_TLS_MIN << 3)));
  544. pr_err("FAIL: tried to double fault but didn't die\n");
  545. #else
  546. pr_err("XFAIL: this test is ia32-only\n");
  547. #endif
  548. }
  549. #ifdef CONFIG_ARM64
  550. static noinline void change_pac_parameters(void)
  551. {
  552. if (IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL)) {
  553. /* Reset the keys of current task */
  554. ptrauth_thread_init_kernel(current);
  555. ptrauth_thread_switch_kernel(current);
  556. }
  557. }
  558. #endif
  559. static noinline void lkdtm_CORRUPT_PAC(void)
  560. {
  561. #ifdef CONFIG_ARM64
  562. #define CORRUPT_PAC_ITERATE 10
  563. int i;
  564. if (!IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL))
  565. pr_err("FAIL: kernel not built with CONFIG_ARM64_PTR_AUTH_KERNEL\n");
  566. if (!system_supports_address_auth()) {
  567. pr_err("FAIL: CPU lacks pointer authentication feature\n");
  568. return;
  569. }
  570. pr_info("changing PAC parameters to force function return failure...\n");
  571. /*
  572. * PAC is a hash value computed from input keys, return address and
  573. * stack pointer. As pac has fewer bits so there is a chance of
  574. * collision, so iterate few times to reduce the collision probability.
  575. */
  576. for (i = 0; i < CORRUPT_PAC_ITERATE; i++)
  577. change_pac_parameters();
  578. pr_err("FAIL: survived PAC changes! Kernel may be unstable from here\n");
  579. #else
  580. pr_err("XFAIL: this test is arm64-only\n");
  581. #endif
  582. }
  583. static struct crashtype crashtypes[] = {
  584. CRASHTYPE(PANIC),
  585. CRASHTYPE(PANIC_STOP_IRQOFF),
  586. CRASHTYPE(BUG),
  587. CRASHTYPE(WARNING),
  588. CRASHTYPE(WARNING_MESSAGE),
  589. CRASHTYPE(EXCEPTION),
  590. CRASHTYPE(LOOP),
  591. CRASHTYPE(EXHAUST_STACK),
  592. CRASHTYPE(CORRUPT_STACK),
  593. CRASHTYPE(CORRUPT_STACK_STRONG),
  594. CRASHTYPE(REPORT_STACK),
  595. CRASHTYPE(REPORT_STACK_CANARY),
  596. CRASHTYPE(UNALIGNED_LOAD_STORE_WRITE),
  597. CRASHTYPE(SOFTLOCKUP),
  598. CRASHTYPE(HARDLOCKUP),
  599. CRASHTYPE(SMP_CALL_LOCKUP),
  600. CRASHTYPE(SPINLOCKUP),
  601. CRASHTYPE(HUNG_TASK),
  602. CRASHTYPE(OVERFLOW_SIGNED),
  603. CRASHTYPE(OVERFLOW_UNSIGNED),
  604. CRASHTYPE(ARRAY_BOUNDS),
  605. CRASHTYPE(FAM_BOUNDS),
  606. CRASHTYPE(CORRUPT_LIST_ADD),
  607. CRASHTYPE(CORRUPT_LIST_DEL),
  608. CRASHTYPE(STACK_GUARD_PAGE_LEADING),
  609. CRASHTYPE(STACK_GUARD_PAGE_TRAILING),
  610. CRASHTYPE(UNSET_SMEP),
  611. CRASHTYPE(DOUBLE_FAULT),
  612. CRASHTYPE(CORRUPT_PAC),
  613. };
  614. struct crashtype_category bugs_crashtypes = {
  615. .crashtypes = crashtypes,
  616. .len = ARRAY_SIZE(crashtypes),
  617. };