GNU Linux-libre 5.15.137-gnu
[releases.git] / arch / arm64 / kernel / mte.c
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 /* Whether the MTE asynchronous mode is enabled. */
30 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
31 EXPORT_SYMBOL_GPL(mte_async_mode);
32 #endif
33
34 static void mte_sync_page_tags(struct page *page, pte_t old_pte,
35                                bool check_swap, bool pte_is_tagged)
36 {
37         if (check_swap && is_swap_pte(old_pte)) {
38                 swp_entry_t entry = pte_to_swp_entry(old_pte);
39
40                 if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
41                         return;
42         }
43
44         if (!pte_is_tagged)
45                 return;
46
47         page_kasan_tag_reset(page);
48         /*
49          * We need smp_wmb() in between setting the flags and clearing the
50          * tags because if another thread reads page->flags and builds a
51          * tagged address out of it, there is an actual dependency to the
52          * memory access, but on the current thread we do not guarantee that
53          * the new page->flags are visible before the tags were updated.
54          */
55         smp_wmb();
56         /*
57          * Test PG_mte_tagged again in case it was racing with another
58          * set_pte_at().
59          */
60         if (!test_and_set_bit(PG_mte_tagged, &page->flags))
61                 mte_clear_page_tags(page_address(page));
62 }
63
64 void mte_sync_tags(pte_t old_pte, pte_t pte)
65 {
66         struct page *page = pte_page(pte);
67         long i, nr_pages = compound_nr(page);
68         bool check_swap = nr_pages == 1;
69         bool pte_is_tagged = pte_tagged(pte);
70
71         /* Early out if there's nothing to do */
72         if (!check_swap && !pte_is_tagged)
73                 return;
74
75         /* if PG_mte_tagged is set, tags have already been initialised */
76         for (i = 0; i < nr_pages; i++, page++) {
77                 if (!test_bit(PG_mte_tagged, &page->flags))
78                         mte_sync_page_tags(page, old_pte, check_swap,
79                                            pte_is_tagged);
80         }
81
82         /* ensure the tags are visible before the PTE is set */
83         smp_wmb();
84 }
85
86 int memcmp_pages(struct page *page1, struct page *page2)
87 {
88         char *addr1, *addr2;
89         int ret;
90
91         addr1 = page_address(page1);
92         addr2 = page_address(page2);
93         ret = memcmp(addr1, addr2, PAGE_SIZE);
94
95         if (!system_supports_mte() || ret)
96                 return ret;
97
98         /*
99          * If the page content is identical but at least one of the pages is
100          * tagged, return non-zero to avoid KSM merging. If only one of the
101          * pages is tagged, set_pte_at() may zero or change the tags of the
102          * other page via mte_sync_tags().
103          */
104         if (test_bit(PG_mte_tagged, &page1->flags) ||
105             test_bit(PG_mte_tagged, &page2->flags))
106                 return addr1 != addr2;
107
108         return ret;
109 }
110
111 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
112 {
113         /* Enable MTE Sync Mode for EL1. */
114         sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, 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
121 void 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_mode(),
128                         "MTE async mode enabled system wide!");
129
130         __mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
131 }
132
133 void mte_enable_kernel_async(void)
134 {
135         __mte_enable_kernel("asynchronous", SCTLR_ELx_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_mode())
146                 static_branch_enable(&mte_async_mode);
147 }
148 #endif
149
150 #ifdef CONFIG_KASAN_HW_TAGS
151 void mte_check_tfsr_el1(void)
152 {
153         u64 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
154
155         if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
156                 /*
157                  * Note: isb() is not required after this direct write
158                  * because there is no indirect read subsequent to it
159                  * (per ARM DDI 0487F.c table D13-1).
160                  */
161                 write_sysreg_s(0, SYS_TFSR_EL1);
162
163                 kasan_report_async();
164         }
165 }
166 #endif
167
168 static void mte_update_sctlr_user(struct task_struct *task)
169 {
170         /*
171          * This must be called with preemption disabled and can only be called
172          * on the current or next task since the CPU must match where the thread
173          * is going to run. The caller is responsible for calling
174          * update_sctlr_el1() later in the same preemption disabled block.
175          */
176         unsigned long sctlr = task->thread.sctlr_user;
177         unsigned long mte_ctrl = task->thread.mte_ctrl;
178         unsigned long pref, resolved_mte_tcf;
179
180         pref = __this_cpu_read(mte_tcf_preferred);
181         resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
182         sctlr &= ~SCTLR_EL1_TCF0_MASK;
183         if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
184                 sctlr |= SCTLR_EL1_TCF0_ASYNC;
185         else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
186                 sctlr |= SCTLR_EL1_TCF0_SYNC;
187         task->thread.sctlr_user = sctlr;
188 }
189
190 void mte_thread_init_user(void)
191 {
192         if (!system_supports_mte())
193                 return;
194
195         /* clear any pending asynchronous tag fault */
196         dsb(ish);
197         write_sysreg_s(0, SYS_TFSRE0_EL1);
198         clear_thread_flag(TIF_MTE_ASYNC_FAULT);
199         /* disable tag checking and reset tag generation mask */
200         set_mte_ctrl(current, 0);
201 }
202
203 void mte_thread_switch(struct task_struct *next)
204 {
205         if (!system_supports_mte())
206                 return;
207
208         mte_update_sctlr_user(next);
209
210         /*
211          * Check if an async tag exception occurred at EL1.
212          *
213          * Note: On the context switch path we rely on the dsb() present
214          * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
215          * are synchronized before this point.
216          */
217         isb();
218         mte_check_tfsr_el1();
219 }
220
221 void mte_cpu_setup(void)
222 {
223         u64 rgsr;
224
225         /*
226          * CnP must be enabled only after the MAIR_EL1 register has been set
227          * up. Inconsistent MAIR_EL1 between CPUs sharing the same TLB may
228          * lead to the wrong memory type being used for a brief window during
229          * CPU power-up.
230          *
231          * CnP is not a boot feature so MTE gets enabled before CnP, but let's
232          * make sure that is the case.
233          */
234         BUG_ON(read_sysreg(ttbr0_el1) & TTBR_CNP_BIT);
235         BUG_ON(read_sysreg(ttbr1_el1) & TTBR_CNP_BIT);
236
237         /* Normal Tagged memory type at the corresponding MAIR index */
238         sysreg_clear_set(mair_el1,
239                          MAIR_ATTRIDX(MAIR_ATTR_MASK, MT_NORMAL_TAGGED),
240                          MAIR_ATTRIDX(MAIR_ATTR_NORMAL_TAGGED,
241                                       MT_NORMAL_TAGGED));
242
243         write_sysreg_s(KERNEL_GCR_EL1, SYS_GCR_EL1);
244
245         /*
246          * If GCR_EL1.RRND=1 is implemented the same way as RRND=0, then
247          * RGSR_EL1.SEED must be non-zero for IRG to produce
248          * pseudorandom numbers. As RGSR_EL1 is UNKNOWN out of reset, we
249          * must initialize it.
250          */
251         rgsr = (read_sysreg(CNTVCT_EL0) & SYS_RGSR_EL1_SEED_MASK) <<
252                SYS_RGSR_EL1_SEED_SHIFT;
253         if (rgsr == 0)
254                 rgsr = 1 << SYS_RGSR_EL1_SEED_SHIFT;
255         write_sysreg_s(rgsr, SYS_RGSR_EL1);
256
257         /* clear any pending tag check faults in TFSR*_EL1 */
258         write_sysreg_s(0, SYS_TFSR_EL1);
259         write_sysreg_s(0, SYS_TFSRE0_EL1);
260
261         local_flush_tlb_all();
262 }
263
264 void mte_suspend_enter(void)
265 {
266         if (!system_supports_mte())
267                 return;
268
269         /*
270          * The barriers are required to guarantee that the indirect writes
271          * to TFSR_EL1 are synchronized before we report the state.
272          */
273         dsb(nsh);
274         isb();
275
276         /* Report SYS_TFSR_EL1 before suspend entry */
277         mte_check_tfsr_el1();
278 }
279
280 void mte_suspend_exit(void)
281 {
282         if (!system_supports_mte())
283                 return;
284
285         mte_cpu_setup();
286 }
287
288 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
289 {
290         u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
291                         SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
292
293         if (!system_supports_mte())
294                 return 0;
295
296         if (arg & PR_MTE_TCF_ASYNC)
297                 mte_ctrl |= MTE_CTRL_TCF_ASYNC;
298         if (arg & PR_MTE_TCF_SYNC)
299                 mte_ctrl |= MTE_CTRL_TCF_SYNC;
300
301         task->thread.mte_ctrl = mte_ctrl;
302         if (task == current) {
303                 preempt_disable();
304                 mte_update_sctlr_user(task);
305                 update_sctlr_el1(task->thread.sctlr_user);
306                 preempt_enable();
307         }
308
309         return 0;
310 }
311
312 long get_mte_ctrl(struct task_struct *task)
313 {
314         unsigned long ret;
315         u64 mte_ctrl = task->thread.mte_ctrl;
316         u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
317                    SYS_GCR_EL1_EXCL_MASK;
318
319         if (!system_supports_mte())
320                 return 0;
321
322         ret = incl << PR_MTE_TAG_SHIFT;
323         if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
324                 ret |= PR_MTE_TCF_ASYNC;
325         if (mte_ctrl & MTE_CTRL_TCF_SYNC)
326                 ret |= PR_MTE_TCF_SYNC;
327
328         return ret;
329 }
330
331 /*
332  * Access MTE tags in another process' address space as given in mm. Update
333  * the number of tags copied. Return 0 if any tags copied, error otherwise.
334  * Inspired by __access_remote_vm().
335  */
336 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
337                                 struct iovec *kiov, unsigned int gup_flags)
338 {
339         struct vm_area_struct *vma;
340         void __user *buf = kiov->iov_base;
341         size_t len = kiov->iov_len;
342         int ret;
343         int write = gup_flags & FOLL_WRITE;
344
345         if (!access_ok(buf, len))
346                 return -EFAULT;
347
348         if (mmap_read_lock_killable(mm))
349                 return -EIO;
350
351         while (len) {
352                 unsigned long tags, offset;
353                 void *maddr;
354                 struct page *page = NULL;
355
356                 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
357                                             &vma, NULL);
358                 if (ret <= 0)
359                         break;
360
361                 /*
362                  * Only copy tags if the page has been mapped as PROT_MTE
363                  * (PG_mte_tagged set). Otherwise the tags are not valid and
364                  * not accessible to user. Moreover, an mprotect(PROT_MTE)
365                  * would cause the existing tags to be cleared if the page
366                  * was never mapped with PROT_MTE.
367                  */
368                 if (!(vma->vm_flags & VM_MTE)) {
369                         ret = -EOPNOTSUPP;
370                         put_page(page);
371                         break;
372                 }
373                 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
374
375                 /* limit access to the end of the page */
376                 offset = offset_in_page(addr);
377                 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
378
379                 maddr = page_address(page);
380                 if (write) {
381                         tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
382                         set_page_dirty_lock(page);
383                 } else {
384                         tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
385                 }
386                 put_page(page);
387
388                 /* error accessing the tracer's buffer */
389                 if (!tags)
390                         break;
391
392                 len -= tags;
393                 buf += tags;
394                 addr += tags * MTE_GRANULE_SIZE;
395         }
396         mmap_read_unlock(mm);
397
398         /* return an error if no tags copied */
399         kiov->iov_len = buf - kiov->iov_base;
400         if (!kiov->iov_len) {
401                 /* check for error accessing the tracee's address space */
402                 if (ret <= 0)
403                         return -EIO;
404                 else
405                         return -EFAULT;
406         }
407
408         return 0;
409 }
410
411 /*
412  * Copy MTE tags in another process' address space at 'addr' to/from tracer's
413  * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
414  */
415 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
416                               struct iovec *kiov, unsigned int gup_flags)
417 {
418         struct mm_struct *mm;
419         int ret;
420
421         mm = get_task_mm(tsk);
422         if (!mm)
423                 return -EPERM;
424
425         if (!tsk->ptrace || (current != tsk->parent) ||
426             ((get_dumpable(mm) != SUID_DUMP_USER) &&
427              !ptracer_capable(tsk, mm->user_ns))) {
428                 mmput(mm);
429                 return -EPERM;
430         }
431
432         ret = __access_remote_tags(mm, addr, kiov, gup_flags);
433         mmput(mm);
434
435         return ret;
436 }
437
438 int mte_ptrace_copy_tags(struct task_struct *child, long request,
439                          unsigned long addr, unsigned long data)
440 {
441         int ret;
442         struct iovec kiov;
443         struct iovec __user *uiov = (void __user *)data;
444         unsigned int gup_flags = FOLL_FORCE;
445
446         if (!system_supports_mte())
447                 return -EIO;
448
449         if (get_user(kiov.iov_base, &uiov->iov_base) ||
450             get_user(kiov.iov_len, &uiov->iov_len))
451                 return -EFAULT;
452
453         if (request == PTRACE_POKEMTETAGS)
454                 gup_flags |= FOLL_WRITE;
455
456         /* align addr to the MTE tag granule */
457         addr &= MTE_GRANULE_MASK;
458
459         ret = access_remote_tags(child, addr, &kiov, gup_flags);
460         if (!ret)
461                 ret = put_user(kiov.iov_len, &uiov->iov_len);
462
463         return ret;
464 }
465
466 static ssize_t mte_tcf_preferred_show(struct device *dev,
467                                       struct device_attribute *attr, char *buf)
468 {
469         switch (per_cpu(mte_tcf_preferred, dev->id)) {
470         case MTE_CTRL_TCF_ASYNC:
471                 return sysfs_emit(buf, "async\n");
472         case MTE_CTRL_TCF_SYNC:
473                 return sysfs_emit(buf, "sync\n");
474         default:
475                 return sysfs_emit(buf, "???\n");
476         }
477 }
478
479 static ssize_t mte_tcf_preferred_store(struct device *dev,
480                                        struct device_attribute *attr,
481                                        const char *buf, size_t count)
482 {
483         u64 tcf;
484
485         if (sysfs_streq(buf, "async"))
486                 tcf = MTE_CTRL_TCF_ASYNC;
487         else if (sysfs_streq(buf, "sync"))
488                 tcf = MTE_CTRL_TCF_SYNC;
489         else
490                 return -EINVAL;
491
492         device_lock(dev);
493         per_cpu(mte_tcf_preferred, dev->id) = tcf;
494         device_unlock(dev);
495
496         return count;
497 }
498 static DEVICE_ATTR_RW(mte_tcf_preferred);
499
500 static int register_mte_tcf_preferred_sysctl(void)
501 {
502         unsigned int cpu;
503
504         if (!system_supports_mte())
505                 return 0;
506
507         for_each_possible_cpu(cpu) {
508                 per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC;
509                 device_create_file(get_cpu_device(cpu),
510                                    &dev_attr_mte_tcf_preferred);
511         }
512
513         return 0;
514 }
515 subsys_initcall(register_mte_tcf_preferred_sysctl);