1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 ARM Ltd. 4 */ 5 6 #include <linux/bitops.h> 7 #include <linux/cpu.h> 8 #include <linux/kernel.h> 9 #include <linux/mm.h> 10 #include <linux/prctl.h> 11 #include <linux/ptrace.h> 12 #include <linux/sched.h> 13 #include <linux/sched/mm.h> 14 #include <linux/string.h> 15 #include <linux/swap.h> 16 #include <linux/swapops.h> 17 #include <linux/thread_info.h> 18 #include <linux/types.h> 19 #include <linux/uaccess.h> 20 #include <linux/uio.h> 21 22 #include <asm/barrier.h> 23 #include <asm/cpufeature.h> 24 #include <asm/mte.h> 25 #include <asm/ptrace.h> 26 #include <asm/sysreg.h> 27 28 static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred); 29 30 #ifdef CONFIG_KASAN_HW_TAGS 31 /* 32 * The asynchronous and asymmetric MTE modes have the same behavior for 33 * store operations. This flag is set when either of these modes is enabled. 34 */ 35 DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode); 36 EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode); 37 #endif 38 39 void mte_clear_folio_tags(struct folio *folio) 40 { 41 unsigned long i; 42 43 for (i = 0; i < folio_nr_pages(folio); i++) 44 mte_clear_page_tags(page_address(folio_page(folio, i))); 45 } 46 47 void mte_sync_tags(pte_t pte, unsigned int nr_pages) 48 { 49 struct page *page = pte_page(pte); 50 struct folio *folio = page_folio(page); 51 unsigned long i; 52 53 if (folio_test_hugetlb(folio)) { 54 /* A hugetlb folio is marked tagged as a whole, so clear every page */ 55 if (folio_try_hugetlb_mte_tagging(folio)) { 56 mte_clear_folio_tags(folio); 57 folio_set_hugetlb_mte_tagged(folio); 58 } 59 60 /* ensure the tags are visible before the PTE is set */ 61 smp_wmb(); 62 63 return; 64 } 65 66 /* if PG_mte_tagged is set, tags have already been initialised */ 67 for (i = 0; i < nr_pages; i++, page++) { 68 if (try_page_mte_tagging(page)) { 69 mte_clear_page_tags(page_address(page)); 70 set_page_mte_tagged(page); 71 } 72 } 73 74 /* ensure the tags are visible before the PTE is set */ 75 smp_wmb(); 76 } 77 78 int memcmp_pages(struct page *page1, struct page *page2) 79 { 80 char *addr1, *addr2; 81 int ret; 82 83 addr1 = page_address(page1); 84 addr2 = page_address(page2); 85 ret = memcmp(addr1, addr2, PAGE_SIZE); 86 87 if (!system_supports_mte() || ret) 88 return ret; 89 90 /* 91 * If the page content is identical but at least one of the pages is 92 * tagged, return non-zero to avoid KSM merging. If only one of the 93 * pages is tagged, __set_ptes() may zero or change the tags of the 94 * other page via mte_sync_tags(). 95 */ 96 if (page_mte_tagged(page1) || page_mte_tagged(page2)) 97 return addr1 != addr2; 98 99 return ret; 100 } 101 102 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf) 103 { 104 /* Enable MTE Sync Mode for EL1. */ 105 sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF_MASK, 106 SYS_FIELD_PREP(SCTLR_EL1, TCF, tcf)); 107 isb(); 108 109 pr_info_once("MTE: enabled in %s mode at EL1\n", mode); 110 } 111 112 #ifdef CONFIG_KASAN_HW_TAGS 113 void mte_enable_kernel_sync(void) 114 { 115 /* 116 * Make sure we enter this function when no PE has set 117 * async mode previously. 118 */ 119 WARN_ONCE(system_uses_mte_async_or_asymm_mode(), 120 "MTE async mode enabled system wide!"); 121 122 __mte_enable_kernel("synchronous", SCTLR_EL1_TCF_SYNC); 123 } 124 125 void mte_enable_kernel_async(void) 126 { 127 __mte_enable_kernel("asynchronous", SCTLR_EL1_TCF_ASYNC); 128 129 /* 130 * MTE async mode is set system wide by the first PE that 131 * executes this function. 132 * 133 * Note: If in future KASAN acquires a runtime switching 134 * mode in between sync and async, this strategy needs 135 * to be reviewed. 136 */ 137 if (!system_uses_mte_async_or_asymm_mode()) 138 static_branch_enable(&mte_async_or_asymm_mode); 139 } 140 141 void mte_enable_kernel_asymm(void) 142 { 143 if (cpus_have_cap(ARM64_MTE_ASYMM)) { 144 __mte_enable_kernel("asymmetric", SCTLR_EL1_TCF_ASYMM); 145 146 /* 147 * MTE asymm mode behaves as async mode for store 148 * operations. The mode is set system wide by the 149 * first PE that executes this function. 150 * 151 * Note: If in future KASAN acquires a runtime switching 152 * mode in between sync and async, this strategy needs 153 * to be reviewed. 154 */ 155 if (!system_uses_mte_async_or_asymm_mode()) 156 static_branch_enable(&mte_async_or_asymm_mode); 157 } else { 158 /* 159 * If the CPU does not support MTE asymmetric mode the 160 * kernel falls back on synchronous mode which is the 161 * default for kasan=on. 162 */ 163 mte_enable_kernel_sync(); 164 } 165 } 166 167 int mte_enable_kernel_store_only(void) 168 { 169 /* 170 * If the CPU does not support MTE store only, 171 * the kernel checks all operations. 172 */ 173 if (!cpus_have_cap(ARM64_MTE_STORE_ONLY)) 174 return -EINVAL; 175 176 sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCSO_MASK, 177 SYS_FIELD_PREP(SCTLR_EL1, TCSO, 1)); 178 isb(); 179 180 pr_info_once("MTE: enabled store only mode at EL1\n"); 181 182 return 0; 183 } 184 #endif 185 186 #ifdef CONFIG_KASAN_HW_TAGS 187 void mte_check_tfsr_el1(void) 188 { 189 u64 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1); 190 191 if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) { 192 /* 193 * Note: isb() is not required after this direct write 194 * because there is no indirect read subsequent to it 195 * (per ARM DDI 0487F.c table D13-1). 196 */ 197 write_sysreg_s(0, SYS_TFSR_EL1); 198 199 kasan_report_async(); 200 } 201 } 202 #endif 203 204 /* 205 * This is where we actually resolve the system and process MTE mode 206 * configuration into an actual value in SCTLR_EL1 that affects 207 * userspace. 208 */ 209 static void mte_update_sctlr_user(struct task_struct *task) 210 { 211 /* 212 * This must be called with preemption disabled and can only be called 213 * on the current or next task since the CPU must match where the thread 214 * is going to run. The caller is responsible for calling 215 * update_sctlr_el1() later in the same preemption disabled block. 216 */ 217 unsigned long sctlr = task->thread.sctlr_user; 218 unsigned long mte_ctrl = task->thread.mte_ctrl; 219 unsigned long pref, resolved_mte_tcf; 220 221 pref = __this_cpu_read(mte_tcf_preferred); 222 /* 223 * If there is no overlap between the system preferred and 224 * program requested values go with what was requested. 225 */ 226 resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl; 227 sctlr &= ~(SCTLR_EL1_TCF0_MASK | SCTLR_EL1_TCSO0_MASK); 228 /* 229 * Pick an actual setting. The order in which we check for 230 * set bits and map into register values determines our 231 * default order. 232 */ 233 if (resolved_mte_tcf & MTE_CTRL_TCF_ASYMM) 234 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, ASYMM); 235 else if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC) 236 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, ASYNC); 237 else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC) 238 sctlr |= SYS_FIELD_PREP_ENUM(SCTLR_EL1, TCF0, SYNC); 239 240 if (mte_ctrl & MTE_CTRL_STORE_ONLY) 241 sctlr |= SYS_FIELD_PREP(SCTLR_EL1, TCSO0, 1); 242 243 task->thread.sctlr_user = sctlr; 244 } 245 246 static void mte_update_gcr_excl(struct task_struct *task) 247 { 248 /* 249 * SYS_GCR_EL1 will be set to current->thread.mte_ctrl value by 250 * mte_set_user_gcr() in kernel_exit, but only if KASAN is enabled. 251 */ 252 if (kasan_hw_tags_enabled()) 253 return; 254 255 write_sysreg_s( 256 ((task->thread.mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) & 257 SYS_GCR_EL1_EXCL_MASK) | SYS_GCR_EL1_RRND, 258 SYS_GCR_EL1); 259 } 260 261 #ifdef CONFIG_KASAN_HW_TAGS 262 /* Only called from assembly, silence sparse */ 263 void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr, 264 __le32 *updptr, int nr_inst); 265 266 void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr, 267 __le32 *updptr, int nr_inst) 268 { 269 BUG_ON(nr_inst != 1); /* Branch -> NOP */ 270 271 if (kasan_hw_tags_enabled()) 272 *updptr = cpu_to_le32(aarch64_insn_gen_nop()); 273 } 274 #endif 275 276 void mte_thread_init_user(void) 277 { 278 if (!system_supports_mte()) 279 return; 280 281 /* clear any pending asynchronous tag fault */ 282 dsb(ish); 283 write_sysreg_s(0, SYS_TFSRE0_EL1); 284 clear_thread_flag(TIF_MTE_ASYNC_FAULT); 285 /* disable tag checking and reset tag generation mask */ 286 set_mte_ctrl(current, 0); 287 } 288 289 void mte_thread_switch(struct task_struct *next) 290 { 291 if (!system_supports_mte()) 292 return; 293 294 mte_update_sctlr_user(next); 295 mte_update_gcr_excl(next); 296 297 /* TCO may not have been disabled on exception entry for the current task. */ 298 mte_disable_tco_entry(next); 299 300 if (!system_uses_mte_async_or_asymm_mode()) 301 return; 302 303 /* 304 * Check if an async tag exception occurred at EL1. 305 * 306 * Note: On the context switch path we rely on the dsb() present 307 * in __switch_to() to guarantee that the indirect writes to TFSR_EL1 308 * are synchronized before this point. 309 */ 310 isb(); 311 mte_check_tfsr_el1(); 312 } 313 314 void mte_cpu_setup(void) 315 { 316 u64 rgsr; 317 318 /* 319 * CnP must be enabled only after the MAIR_EL1 register has been set 320 * up. Inconsistent MAIR_EL1 between CPUs sharing the same TLB may 321 * lead to the wrong memory type being used for a brief window during 322 * CPU power-up. 323 * 324 * CnP is not a boot feature so MTE gets enabled before CnP, but let's 325 * make sure that is the case. 326 */ 327 BUG_ON(read_sysreg(ttbr0_el1) & TTBRx_EL1_CnP); 328 BUG_ON(read_sysreg(ttbr1_el1) & TTBRx_EL1_CnP); 329 330 /* Normal Tagged memory type at the corresponding MAIR index */ 331 sysreg_clear_set(mair_el1, 332 MAIR_ATTRIDX(MAIR_ATTR_MASK, MT_NORMAL_TAGGED), 333 MAIR_ATTRIDX(MAIR_ATTR_NORMAL_TAGGED, 334 MT_NORMAL_TAGGED)); 335 336 write_sysreg_s(KERNEL_GCR_EL1, SYS_GCR_EL1); 337 338 /* 339 * If GCR_EL1.RRND=1 is implemented the same way as RRND=0, then 340 * RGSR_EL1.SEED must be non-zero for IRG to produce 341 * pseudorandom numbers. As RGSR_EL1 is UNKNOWN out of reset, we 342 * must initialize it. 343 */ 344 rgsr = (read_sysreg(CNTVCT_EL0) & SYS_RGSR_EL1_SEED_MASK) << 345 SYS_RGSR_EL1_SEED_SHIFT; 346 if (rgsr == 0) 347 rgsr = 1 << SYS_RGSR_EL1_SEED_SHIFT; 348 write_sysreg_s(rgsr, SYS_RGSR_EL1); 349 350 /* clear any pending tag check faults in TFSR*_EL1 */ 351 write_sysreg_s(0, SYS_TFSR_EL1); 352 write_sysreg_s(0, SYS_TFSRE0_EL1); 353 354 local_flush_tlb_all(); 355 } 356 357 void mte_suspend_enter(void) 358 { 359 if (!system_supports_mte()) 360 return; 361 362 if (!system_uses_mte_async_or_asymm_mode()) 363 return; 364 365 /* 366 * The barriers are required to guarantee that the indirect writes 367 * to TFSR_EL1 are synchronized before we report the state. 368 */ 369 dsb(nsh); 370 isb(); 371 372 /* Report SYS_TFSR_EL1 before suspend entry */ 373 mte_check_tfsr_el1(); 374 } 375 376 void mte_suspend_exit(void) 377 { 378 if (!system_supports_mte()) 379 return; 380 381 mte_cpu_setup(); 382 } 383 384 long set_mte_ctrl(struct task_struct *task, unsigned long arg) 385 { 386 u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) & 387 SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT; 388 389 if (!system_supports_mte()) 390 return 0; 391 392 if (arg & PR_MTE_TCF_ASYNC) 393 mte_ctrl |= MTE_CTRL_TCF_ASYNC; 394 if (arg & PR_MTE_TCF_SYNC) 395 mte_ctrl |= MTE_CTRL_TCF_SYNC; 396 397 /* 398 * If the system supports it and both sync and async modes are 399 * specified then implicitly enable asymmetric mode. 400 * Userspace could see a mix of both sync and async anyway due 401 * to differing or changing defaults on CPUs. 402 */ 403 if (cpus_have_cap(ARM64_MTE_ASYMM) && 404 (arg & PR_MTE_TCF_ASYNC) && 405 (arg & PR_MTE_TCF_SYNC)) 406 mte_ctrl |= MTE_CTRL_TCF_ASYMM; 407 408 if (arg & PR_MTE_STORE_ONLY) 409 mte_ctrl |= MTE_CTRL_STORE_ONLY; 410 411 task->thread.mte_ctrl = mte_ctrl; 412 if (task == current) { 413 preempt_disable(); 414 mte_update_sctlr_user(task); 415 mte_update_gcr_excl(task); 416 update_sctlr_el1(task->thread.sctlr_user); 417 preempt_enable(); 418 } 419 420 return 0; 421 } 422 423 long get_mte_ctrl(struct task_struct *task) 424 { 425 unsigned long ret; 426 u64 mte_ctrl = task->thread.mte_ctrl; 427 u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) & 428 SYS_GCR_EL1_EXCL_MASK; 429 430 if (!system_supports_mte()) 431 return 0; 432 433 ret = incl << PR_MTE_TAG_SHIFT; 434 if (mte_ctrl & MTE_CTRL_TCF_ASYNC) 435 ret |= PR_MTE_TCF_ASYNC; 436 if (mte_ctrl & MTE_CTRL_TCF_SYNC) 437 ret |= PR_MTE_TCF_SYNC; 438 if (mte_ctrl & MTE_CTRL_STORE_ONLY) 439 ret |= PR_MTE_STORE_ONLY; 440 441 return ret; 442 } 443 444 /* 445 * Access MTE tags in another process' address space as given in mm. Update 446 * the number of tags copied. Return 0 if any tags copied, error otherwise. 447 * Inspired by __access_remote_vm(). 448 */ 449 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr, 450 struct iovec *kiov, unsigned int gup_flags) 451 { 452 void __user *buf = kiov->iov_base; 453 size_t len = kiov->iov_len; 454 int err = 0; 455 int write = gup_flags & FOLL_WRITE; 456 457 if (!access_ok(buf, len)) 458 return -EFAULT; 459 460 if (mmap_read_lock_killable(mm)) 461 return -EIO; 462 463 while (len) { 464 struct vm_area_struct *vma; 465 unsigned long tags, offset; 466 void *maddr; 467 struct page *page = get_user_page_vma_remote(mm, addr, 468 gup_flags, &vma); 469 struct folio *folio; 470 471 if (IS_ERR(page)) { 472 err = PTR_ERR(page); 473 break; 474 } 475 476 /* 477 * Only copy tags if the page has been mapped as PROT_MTE 478 * (PG_mte_tagged set). Otherwise the tags are not valid and 479 * not accessible to user. Moreover, an mprotect(PROT_MTE) 480 * would cause the existing tags to be cleared if the page 481 * was never mapped with PROT_MTE. 482 */ 483 if (!(vma->vm_flags & VM_MTE)) { 484 err = -EIO; 485 put_page(page); 486 break; 487 } 488 489 folio = page_folio(page); 490 if (folio_test_hugetlb(folio)) 491 WARN_ON_ONCE(!folio_test_hugetlb_mte_tagged(folio) && 492 !is_huge_zero_folio(folio)); 493 else 494 WARN_ON_ONCE(!page_mte_tagged(page) && !is_zero_page(page)); 495 496 /* limit access to the end of the page */ 497 offset = offset_in_page(addr); 498 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE); 499 500 maddr = page_address(page); 501 if (write) { 502 tags = mte_copy_tags_from_user(maddr + offset, buf, tags); 503 set_page_dirty_lock(page); 504 } else { 505 tags = mte_copy_tags_to_user(buf, maddr + offset, tags); 506 } 507 put_page(page); 508 509 /* error accessing the tracer's buffer */ 510 if (!tags) 511 break; 512 513 len -= tags; 514 buf += tags; 515 addr += tags * MTE_GRANULE_SIZE; 516 } 517 mmap_read_unlock(mm); 518 519 /* return an error if no tags copied */ 520 kiov->iov_len = buf - kiov->iov_base; 521 if (!kiov->iov_len) { 522 /* check for error accessing the tracee's address space */ 523 if (err) 524 return -EIO; 525 else 526 return -EFAULT; 527 } 528 529 return 0; 530 } 531 532 /* 533 * Copy MTE tags in another process' address space at 'addr' to/from tracer's 534 * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm(). 535 */ 536 static int access_remote_tags(struct task_struct *tsk, unsigned long addr, 537 struct iovec *kiov, unsigned int gup_flags) 538 { 539 struct mm_struct *mm; 540 int ret; 541 542 mm = get_task_mm(tsk); 543 if (!mm) 544 return -EPERM; 545 546 if (!ptracer_access_allowed(tsk)) { 547 mmput(mm); 548 return -EPERM; 549 } 550 551 ret = __access_remote_tags(mm, addr, kiov, gup_flags); 552 mmput(mm); 553 return ret; 554 } 555 556 int mte_ptrace_copy_tags(struct task_struct *child, long request, 557 unsigned long addr, unsigned long data) 558 { 559 int ret; 560 struct iovec kiov; 561 struct iovec __user *uiov = (void __user *)data; 562 unsigned int gup_flags = FOLL_FORCE; 563 564 if (!system_supports_mte()) 565 return -EIO; 566 567 if (get_user(kiov.iov_base, &uiov->iov_base) || 568 get_user(kiov.iov_len, &uiov->iov_len)) 569 return -EFAULT; 570 571 if (request == PTRACE_POKEMTETAGS) 572 gup_flags |= FOLL_WRITE; 573 574 /* align addr to the MTE tag granule */ 575 addr &= MTE_GRANULE_MASK; 576 577 ret = access_remote_tags(child, addr, &kiov, gup_flags); 578 if (!ret) 579 ret = put_user(kiov.iov_len, &uiov->iov_len); 580 581 return ret; 582 } 583 584 static ssize_t mte_tcf_preferred_show(struct device *dev, 585 struct device_attribute *attr, char *buf) 586 { 587 switch (per_cpu(mte_tcf_preferred, dev->id)) { 588 case MTE_CTRL_TCF_ASYNC: 589 return sysfs_emit(buf, "async\n"); 590 case MTE_CTRL_TCF_SYNC: 591 return sysfs_emit(buf, "sync\n"); 592 case MTE_CTRL_TCF_ASYMM: 593 return sysfs_emit(buf, "asymm\n"); 594 default: 595 return sysfs_emit(buf, "???\n"); 596 } 597 } 598 599 static ssize_t mte_tcf_preferred_store(struct device *dev, 600 struct device_attribute *attr, 601 const char *buf, size_t count) 602 { 603 u64 tcf; 604 605 if (sysfs_streq(buf, "async")) 606 tcf = MTE_CTRL_TCF_ASYNC; 607 else if (sysfs_streq(buf, "sync")) 608 tcf = MTE_CTRL_TCF_SYNC; 609 else if (cpus_have_cap(ARM64_MTE_ASYMM) && sysfs_streq(buf, "asymm")) 610 tcf = MTE_CTRL_TCF_ASYMM; 611 else 612 return -EINVAL; 613 614 device_lock(dev); 615 per_cpu(mte_tcf_preferred, dev->id) = tcf; 616 device_unlock(dev); 617 618 return count; 619 } 620 static DEVICE_ATTR_RW(mte_tcf_preferred); 621 622 static int register_mte_tcf_preferred_sysctl(void) 623 { 624 unsigned int cpu; 625 626 if (!system_supports_mte()) 627 return 0; 628 629 for_each_possible_cpu(cpu) { 630 per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC; 631 device_create_file(get_cpu_device(cpu), 632 &dev_attr_mte_tcf_preferred); 633 } 634 635 return 0; 636 } 637 subsys_initcall(register_mte_tcf_preferred_sysctl); 638 639 /* 640 * Return 0 on success, the number of bytes not probed otherwise. 641 */ 642 size_t mte_probe_user_range(const char __user *uaddr, size_t size) 643 { 644 const char __user *end = uaddr + size; 645 char val; 646 647 __raw_get_user(val, uaddr, efault); 648 649 uaddr = PTR_ALIGN(uaddr, MTE_GRANULE_SIZE); 650 while (uaddr < end) { 651 /* 652 * A read is sufficient for mte, the caller should have probed 653 * for the pte write permission if required. 654 */ 655 __raw_get_user(val, uaddr, efault); 656 uaddr += MTE_GRANULE_SIZE; 657 } 658 (void)val; 659 660 return 0; 661 662 efault: 663 return end - uaddr; 664 } 665