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