GNU Linux-libre 5.15.72-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         mte_clear_page_tags(page_address(page));
57 }
58
59 void mte_sync_tags(pte_t old_pte, pte_t pte)
60 {
61         struct page *page = pte_page(pte);
62         long i, nr_pages = compound_nr(page);
63         bool check_swap = nr_pages == 1;
64         bool pte_is_tagged = pte_tagged(pte);
65
66         /* Early out if there's nothing to do */
67         if (!check_swap && !pte_is_tagged)
68                 return;
69
70         /* if PG_mte_tagged is set, tags have already been initialised */
71         for (i = 0; i < nr_pages; i++, page++) {
72                 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
73                         mte_sync_page_tags(page, old_pte, check_swap,
74                                            pte_is_tagged);
75         }
76
77         /* ensure the tags are visible before the PTE is set */
78         smp_wmb();
79 }
80
81 int memcmp_pages(struct page *page1, struct page *page2)
82 {
83         char *addr1, *addr2;
84         int ret;
85
86         addr1 = page_address(page1);
87         addr2 = page_address(page2);
88         ret = memcmp(addr1, addr2, PAGE_SIZE);
89
90         if (!system_supports_mte() || ret)
91                 return ret;
92
93         /*
94          * If the page content is identical but at least one of the pages is
95          * tagged, return non-zero to avoid KSM merging. If only one of the
96          * pages is tagged, set_pte_at() may zero or change the tags of the
97          * other page via mte_sync_tags().
98          */
99         if (test_bit(PG_mte_tagged, &page1->flags) ||
100             test_bit(PG_mte_tagged, &page2->flags))
101                 return addr1 != addr2;
102
103         return ret;
104 }
105
106 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
107 {
108         /* Enable MTE Sync Mode for EL1. */
109         sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, tcf);
110         isb();
111
112         pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
113 }
114
115 #ifdef CONFIG_KASAN_HW_TAGS
116 void mte_enable_kernel_sync(void)
117 {
118         /*
119          * Make sure we enter this function when no PE has set
120          * async mode previously.
121          */
122         WARN_ONCE(system_uses_mte_async_mode(),
123                         "MTE async mode enabled system wide!");
124
125         __mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
126 }
127
128 void mte_enable_kernel_async(void)
129 {
130         __mte_enable_kernel("asynchronous", SCTLR_ELx_TCF_ASYNC);
131
132         /*
133          * MTE async mode is set system wide by the first PE that
134          * executes this function.
135          *
136          * Note: If in future KASAN acquires a runtime switching
137          * mode in between sync and async, this strategy needs
138          * to be reviewed.
139          */
140         if (!system_uses_mte_async_mode())
141                 static_branch_enable(&mte_async_mode);
142 }
143 #endif
144
145 #ifdef CONFIG_KASAN_HW_TAGS
146 void mte_check_tfsr_el1(void)
147 {
148         u64 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
149
150         if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
151                 /*
152                  * Note: isb() is not required after this direct write
153                  * because there is no indirect read subsequent to it
154                  * (per ARM DDI 0487F.c table D13-1).
155                  */
156                 write_sysreg_s(0, SYS_TFSR_EL1);
157
158                 kasan_report_async();
159         }
160 }
161 #endif
162
163 static void mte_update_sctlr_user(struct task_struct *task)
164 {
165         /*
166          * This must be called with preemption disabled and can only be called
167          * on the current or next task since the CPU must match where the thread
168          * is going to run. The caller is responsible for calling
169          * update_sctlr_el1() later in the same preemption disabled block.
170          */
171         unsigned long sctlr = task->thread.sctlr_user;
172         unsigned long mte_ctrl = task->thread.mte_ctrl;
173         unsigned long pref, resolved_mte_tcf;
174
175         pref = __this_cpu_read(mte_tcf_preferred);
176         resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
177         sctlr &= ~SCTLR_EL1_TCF0_MASK;
178         if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
179                 sctlr |= SCTLR_EL1_TCF0_ASYNC;
180         else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
181                 sctlr |= SCTLR_EL1_TCF0_SYNC;
182         task->thread.sctlr_user = sctlr;
183 }
184
185 void mte_thread_init_user(void)
186 {
187         if (!system_supports_mte())
188                 return;
189
190         /* clear any pending asynchronous tag fault */
191         dsb(ish);
192         write_sysreg_s(0, SYS_TFSRE0_EL1);
193         clear_thread_flag(TIF_MTE_ASYNC_FAULT);
194         /* disable tag checking and reset tag generation mask */
195         set_mte_ctrl(current, 0);
196 }
197
198 void mte_thread_switch(struct task_struct *next)
199 {
200         if (!system_supports_mte())
201                 return;
202
203         mte_update_sctlr_user(next);
204
205         /*
206          * Check if an async tag exception occurred at EL1.
207          *
208          * Note: On the context switch path we rely on the dsb() present
209          * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
210          * are synchronized before this point.
211          */
212         isb();
213         mte_check_tfsr_el1();
214 }
215
216 void mte_suspend_enter(void)
217 {
218         if (!system_supports_mte())
219                 return;
220
221         /*
222          * The barriers are required to guarantee that the indirect writes
223          * to TFSR_EL1 are synchronized before we report the state.
224          */
225         dsb(nsh);
226         isb();
227
228         /* Report SYS_TFSR_EL1 before suspend entry */
229         mte_check_tfsr_el1();
230 }
231
232 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
233 {
234         u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
235                         SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
236
237         if (!system_supports_mte())
238                 return 0;
239
240         if (arg & PR_MTE_TCF_ASYNC)
241                 mte_ctrl |= MTE_CTRL_TCF_ASYNC;
242         if (arg & PR_MTE_TCF_SYNC)
243                 mte_ctrl |= MTE_CTRL_TCF_SYNC;
244
245         task->thread.mte_ctrl = mte_ctrl;
246         if (task == current) {
247                 preempt_disable();
248                 mte_update_sctlr_user(task);
249                 update_sctlr_el1(task->thread.sctlr_user);
250                 preempt_enable();
251         }
252
253         return 0;
254 }
255
256 long get_mte_ctrl(struct task_struct *task)
257 {
258         unsigned long ret;
259         u64 mte_ctrl = task->thread.mte_ctrl;
260         u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
261                    SYS_GCR_EL1_EXCL_MASK;
262
263         if (!system_supports_mte())
264                 return 0;
265
266         ret = incl << PR_MTE_TAG_SHIFT;
267         if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
268                 ret |= PR_MTE_TCF_ASYNC;
269         if (mte_ctrl & MTE_CTRL_TCF_SYNC)
270                 ret |= PR_MTE_TCF_SYNC;
271
272         return ret;
273 }
274
275 /*
276  * Access MTE tags in another process' address space as given in mm. Update
277  * the number of tags copied. Return 0 if any tags copied, error otherwise.
278  * Inspired by __access_remote_vm().
279  */
280 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
281                                 struct iovec *kiov, unsigned int gup_flags)
282 {
283         struct vm_area_struct *vma;
284         void __user *buf = kiov->iov_base;
285         size_t len = kiov->iov_len;
286         int ret;
287         int write = gup_flags & FOLL_WRITE;
288
289         if (!access_ok(buf, len))
290                 return -EFAULT;
291
292         if (mmap_read_lock_killable(mm))
293                 return -EIO;
294
295         while (len) {
296                 unsigned long tags, offset;
297                 void *maddr;
298                 struct page *page = NULL;
299
300                 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
301                                             &vma, NULL);
302                 if (ret <= 0)
303                         break;
304
305                 /*
306                  * Only copy tags if the page has been mapped as PROT_MTE
307                  * (PG_mte_tagged set). Otherwise the tags are not valid and
308                  * not accessible to user. Moreover, an mprotect(PROT_MTE)
309                  * would cause the existing tags to be cleared if the page
310                  * was never mapped with PROT_MTE.
311                  */
312                 if (!(vma->vm_flags & VM_MTE)) {
313                         ret = -EOPNOTSUPP;
314                         put_page(page);
315                         break;
316                 }
317                 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
318
319                 /* limit access to the end of the page */
320                 offset = offset_in_page(addr);
321                 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
322
323                 maddr = page_address(page);
324                 if (write) {
325                         tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
326                         set_page_dirty_lock(page);
327                 } else {
328                         tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
329                 }
330                 put_page(page);
331
332                 /* error accessing the tracer's buffer */
333                 if (!tags)
334                         break;
335
336                 len -= tags;
337                 buf += tags;
338                 addr += tags * MTE_GRANULE_SIZE;
339         }
340         mmap_read_unlock(mm);
341
342         /* return an error if no tags copied */
343         kiov->iov_len = buf - kiov->iov_base;
344         if (!kiov->iov_len) {
345                 /* check for error accessing the tracee's address space */
346                 if (ret <= 0)
347                         return -EIO;
348                 else
349                         return -EFAULT;
350         }
351
352         return 0;
353 }
354
355 /*
356  * Copy MTE tags in another process' address space at 'addr' to/from tracer's
357  * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
358  */
359 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
360                               struct iovec *kiov, unsigned int gup_flags)
361 {
362         struct mm_struct *mm;
363         int ret;
364
365         mm = get_task_mm(tsk);
366         if (!mm)
367                 return -EPERM;
368
369         if (!tsk->ptrace || (current != tsk->parent) ||
370             ((get_dumpable(mm) != SUID_DUMP_USER) &&
371              !ptracer_capable(tsk, mm->user_ns))) {
372                 mmput(mm);
373                 return -EPERM;
374         }
375
376         ret = __access_remote_tags(mm, addr, kiov, gup_flags);
377         mmput(mm);
378
379         return ret;
380 }
381
382 int mte_ptrace_copy_tags(struct task_struct *child, long request,
383                          unsigned long addr, unsigned long data)
384 {
385         int ret;
386         struct iovec kiov;
387         struct iovec __user *uiov = (void __user *)data;
388         unsigned int gup_flags = FOLL_FORCE;
389
390         if (!system_supports_mte())
391                 return -EIO;
392
393         if (get_user(kiov.iov_base, &uiov->iov_base) ||
394             get_user(kiov.iov_len, &uiov->iov_len))
395                 return -EFAULT;
396
397         if (request == PTRACE_POKEMTETAGS)
398                 gup_flags |= FOLL_WRITE;
399
400         /* align addr to the MTE tag granule */
401         addr &= MTE_GRANULE_MASK;
402
403         ret = access_remote_tags(child, addr, &kiov, gup_flags);
404         if (!ret)
405                 ret = put_user(kiov.iov_len, &uiov->iov_len);
406
407         return ret;
408 }
409
410 static ssize_t mte_tcf_preferred_show(struct device *dev,
411                                       struct device_attribute *attr, char *buf)
412 {
413         switch (per_cpu(mte_tcf_preferred, dev->id)) {
414         case MTE_CTRL_TCF_ASYNC:
415                 return sysfs_emit(buf, "async\n");
416         case MTE_CTRL_TCF_SYNC:
417                 return sysfs_emit(buf, "sync\n");
418         default:
419                 return sysfs_emit(buf, "???\n");
420         }
421 }
422
423 static ssize_t mte_tcf_preferred_store(struct device *dev,
424                                        struct device_attribute *attr,
425                                        const char *buf, size_t count)
426 {
427         u64 tcf;
428
429         if (sysfs_streq(buf, "async"))
430                 tcf = MTE_CTRL_TCF_ASYNC;
431         else if (sysfs_streq(buf, "sync"))
432                 tcf = MTE_CTRL_TCF_SYNC;
433         else
434                 return -EINVAL;
435
436         device_lock(dev);
437         per_cpu(mte_tcf_preferred, dev->id) = tcf;
438         device_unlock(dev);
439
440         return count;
441 }
442 static DEVICE_ATTR_RW(mte_tcf_preferred);
443
444 static int register_mte_tcf_preferred_sysctl(void)
445 {
446         unsigned int cpu;
447
448         if (!system_supports_mte())
449                 return 0;
450
451         for_each_possible_cpu(cpu) {
452                 per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC;
453                 device_create_file(get_cpu_device(cpu),
454                                    &dev_attr_mte_tcf_preferred);
455         }
456
457         return 0;
458 }
459 subsys_initcall(register_mte_tcf_preferred_sysctl);