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