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