9362928ba0d51db1516f9fa15d82f3362f065514
[linux-2.6-microblaze.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/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/prctl.h>
10 #include <linux/sched.h>
11 #include <linux/sched/mm.h>
12 #include <linux/string.h>
13 #include <linux/swap.h>
14 #include <linux/swapops.h>
15 #include <linux/thread_info.h>
16 #include <linux/types.h>
17 #include <linux/uio.h>
18
19 #include <asm/barrier.h>
20 #include <asm/cpufeature.h>
21 #include <asm/mte.h>
22 #include <asm/ptrace.h>
23 #include <asm/sysreg.h>
24
25 u64 gcr_kernel_excl __ro_after_init;
26
27 static bool report_fault_once = true;
28
29 /* Whether the MTE asynchronous mode is enabled. */
30 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
31 EXPORT_SYMBOL_GPL(mte_async_mode);
32
33 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
34 {
35         pte_t old_pte = READ_ONCE(*ptep);
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         page_kasan_tag_reset(page);
45         /*
46          * We need smp_wmb() in between setting the flags and clearing the
47          * tags because if another thread reads page->flags and builds a
48          * tagged address out of it, there is an actual dependency to the
49          * memory access, but on the current thread we do not guarantee that
50          * the new page->flags are visible before the tags were updated.
51          */
52         smp_wmb();
53         mte_clear_page_tags(page_address(page));
54 }
55
56 void mte_sync_tags(pte_t *ptep, pte_t pte)
57 {
58         struct page *page = pte_page(pte);
59         long i, nr_pages = compound_nr(page);
60         bool check_swap = nr_pages == 1;
61
62         /* if PG_mte_tagged is set, tags have already been initialised */
63         for (i = 0; i < nr_pages; i++, page++) {
64                 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
65                         mte_sync_page_tags(page, ptep, check_swap);
66         }
67 }
68
69 int memcmp_pages(struct page *page1, struct page *page2)
70 {
71         char *addr1, *addr2;
72         int ret;
73
74         addr1 = page_address(page1);
75         addr2 = page_address(page2);
76         ret = memcmp(addr1, addr2, PAGE_SIZE);
77
78         if (!system_supports_mte() || ret)
79                 return ret;
80
81         /*
82          * If the page content is identical but at least one of the pages is
83          * tagged, return non-zero to avoid KSM merging. If only one of the
84          * pages is tagged, set_pte_at() may zero or change the tags of the
85          * other page via mte_sync_tags().
86          */
87         if (test_bit(PG_mte_tagged, &page1->flags) ||
88             test_bit(PG_mte_tagged, &page2->flags))
89                 return addr1 != addr2;
90
91         return ret;
92 }
93
94 void mte_init_tags(u64 max_tag)
95 {
96         static bool gcr_kernel_excl_initialized;
97
98         if (!gcr_kernel_excl_initialized) {
99                 /*
100                  * The format of the tags in KASAN is 0xFF and in MTE is 0xF.
101                  * This conversion extracts an MTE tag from a KASAN tag.
102                  */
103                 u64 incl = GENMASK(FIELD_GET(MTE_TAG_MASK >> MTE_TAG_SHIFT,
104                                              max_tag), 0);
105
106                 gcr_kernel_excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
107                 gcr_kernel_excl_initialized = true;
108         }
109
110         /* Enable the kernel exclude mask for random tags generation. */
111         write_sysreg_s(SYS_GCR_EL1_RRND | gcr_kernel_excl, SYS_GCR_EL1);
112 }
113
114 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
115 {
116         /* Enable MTE Sync Mode for EL1. */
117         sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, tcf);
118         isb();
119
120         pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
121 }
122
123 void mte_enable_kernel_sync(void)
124 {
125         /*
126          * Make sure we enter this function when no PE has set
127          * async mode previously.
128          */
129         WARN_ONCE(system_uses_mte_async_mode(),
130                         "MTE async mode enabled system wide!");
131
132         __mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
133 }
134
135 void mte_enable_kernel_async(void)
136 {
137         __mte_enable_kernel("asynchronous", SCTLR_ELx_TCF_ASYNC);
138
139         /*
140          * MTE async mode is set system wide by the first PE that
141          * executes this function.
142          *
143          * Note: If in future KASAN acquires a runtime switching
144          * mode in between sync and async, this strategy needs
145          * to be reviewed.
146          */
147         if (!system_uses_mte_async_mode())
148                 static_branch_enable(&mte_async_mode);
149 }
150
151 void mte_set_report_once(bool state)
152 {
153         WRITE_ONCE(report_fault_once, state);
154 }
155
156 bool mte_report_once(void)
157 {
158         return READ_ONCE(report_fault_once);
159 }
160
161 static void update_sctlr_el1_tcf0(u64 tcf0)
162 {
163         /* ISB required for the kernel uaccess routines */
164         sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF0_MASK, tcf0);
165         isb();
166 }
167
168 static void set_sctlr_el1_tcf0(u64 tcf0)
169 {
170         /*
171          * mte_thread_switch() checks current->thread.sctlr_tcf0 as an
172          * optimisation. Disable preemption so that it does not see
173          * the variable update before the SCTLR_EL1.TCF0 one.
174          */
175         preempt_disable();
176         current->thread.sctlr_tcf0 = tcf0;
177         update_sctlr_el1_tcf0(tcf0);
178         preempt_enable();
179 }
180
181 static void update_gcr_el1_excl(u64 excl)
182 {
183
184         /*
185          * Note that the mask controlled by the user via prctl() is an
186          * include while GCR_EL1 accepts an exclude mask.
187          * No need for ISB since this only affects EL0 currently, implicit
188          * with ERET.
189          */
190         sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, excl);
191 }
192
193 static void set_gcr_el1_excl(u64 excl)
194 {
195         current->thread.gcr_user_excl = excl;
196
197         /*
198          * SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value
199          * by mte_set_user_gcr() in kernel_exit,
200          */
201 }
202
203 void flush_mte_state(void)
204 {
205         if (!system_supports_mte())
206                 return;
207
208         /* clear any pending asynchronous tag fault */
209         dsb(ish);
210         write_sysreg_s(0, SYS_TFSRE0_EL1);
211         clear_thread_flag(TIF_MTE_ASYNC_FAULT);
212         /* disable tag checking */
213         set_sctlr_el1_tcf0(SCTLR_EL1_TCF0_NONE);
214         /* reset tag generation mask */
215         set_gcr_el1_excl(SYS_GCR_EL1_EXCL_MASK);
216 }
217
218 void mte_thread_switch(struct task_struct *next)
219 {
220         if (!system_supports_mte())
221                 return;
222
223         /* avoid expensive SCTLR_EL1 accesses if no change */
224         if (current->thread.sctlr_tcf0 != next->thread.sctlr_tcf0)
225                 update_sctlr_el1_tcf0(next->thread.sctlr_tcf0);
226 }
227
228 void mte_suspend_exit(void)
229 {
230         if (!system_supports_mte())
231                 return;
232
233         update_gcr_el1_excl(gcr_kernel_excl);
234 }
235
236 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
237 {
238         u64 tcf0;
239         u64 gcr_excl = ~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
240                        SYS_GCR_EL1_EXCL_MASK;
241
242         if (!system_supports_mte())
243                 return 0;
244
245         switch (arg & PR_MTE_TCF_MASK) {
246         case PR_MTE_TCF_NONE:
247                 tcf0 = SCTLR_EL1_TCF0_NONE;
248                 break;
249         case PR_MTE_TCF_SYNC:
250                 tcf0 = SCTLR_EL1_TCF0_SYNC;
251                 break;
252         case PR_MTE_TCF_ASYNC:
253                 tcf0 = SCTLR_EL1_TCF0_ASYNC;
254                 break;
255         default:
256                 return -EINVAL;
257         }
258
259         if (task != current) {
260                 task->thread.sctlr_tcf0 = tcf0;
261                 task->thread.gcr_user_excl = gcr_excl;
262         } else {
263                 set_sctlr_el1_tcf0(tcf0);
264                 set_gcr_el1_excl(gcr_excl);
265         }
266
267         return 0;
268 }
269
270 long get_mte_ctrl(struct task_struct *task)
271 {
272         unsigned long ret;
273         u64 incl = ~task->thread.gcr_user_excl & SYS_GCR_EL1_EXCL_MASK;
274
275         if (!system_supports_mte())
276                 return 0;
277
278         ret = incl << PR_MTE_TAG_SHIFT;
279
280         switch (task->thread.sctlr_tcf0) {
281         case SCTLR_EL1_TCF0_NONE:
282                 ret |= PR_MTE_TCF_NONE;
283                 break;
284         case SCTLR_EL1_TCF0_SYNC:
285                 ret |= PR_MTE_TCF_SYNC;
286                 break;
287         case SCTLR_EL1_TCF0_ASYNC:
288                 ret |= PR_MTE_TCF_ASYNC;
289                 break;
290         }
291
292         return ret;
293 }
294
295 /*
296  * Access MTE tags in another process' address space as given in mm. Update
297  * the number of tags copied. Return 0 if any tags copied, error otherwise.
298  * Inspired by __access_remote_vm().
299  */
300 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
301                                 struct iovec *kiov, unsigned int gup_flags)
302 {
303         struct vm_area_struct *vma;
304         void __user *buf = kiov->iov_base;
305         size_t len = kiov->iov_len;
306         int ret;
307         int write = gup_flags & FOLL_WRITE;
308
309         if (!access_ok(buf, len))
310                 return -EFAULT;
311
312         if (mmap_read_lock_killable(mm))
313                 return -EIO;
314
315         while (len) {
316                 unsigned long tags, offset;
317                 void *maddr;
318                 struct page *page = NULL;
319
320                 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
321                                             &vma, NULL);
322                 if (ret <= 0)
323                         break;
324
325                 /*
326                  * Only copy tags if the page has been mapped as PROT_MTE
327                  * (PG_mte_tagged set). Otherwise the tags are not valid and
328                  * not accessible to user. Moreover, an mprotect(PROT_MTE)
329                  * would cause the existing tags to be cleared if the page
330                  * was never mapped with PROT_MTE.
331                  */
332                 if (!(vma->vm_flags & VM_MTE)) {
333                         ret = -EOPNOTSUPP;
334                         put_page(page);
335                         break;
336                 }
337                 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
338
339                 /* limit access to the end of the page */
340                 offset = offset_in_page(addr);
341                 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
342
343                 maddr = page_address(page);
344                 if (write) {
345                         tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
346                         set_page_dirty_lock(page);
347                 } else {
348                         tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
349                 }
350                 put_page(page);
351
352                 /* error accessing the tracer's buffer */
353                 if (!tags)
354                         break;
355
356                 len -= tags;
357                 buf += tags;
358                 addr += tags * MTE_GRANULE_SIZE;
359         }
360         mmap_read_unlock(mm);
361
362         /* return an error if no tags copied */
363         kiov->iov_len = buf - kiov->iov_base;
364         if (!kiov->iov_len) {
365                 /* check for error accessing the tracee's address space */
366                 if (ret <= 0)
367                         return -EIO;
368                 else
369                         return -EFAULT;
370         }
371
372         return 0;
373 }
374
375 /*
376  * Copy MTE tags in another process' address space at 'addr' to/from tracer's
377  * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
378  */
379 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
380                               struct iovec *kiov, unsigned int gup_flags)
381 {
382         struct mm_struct *mm;
383         int ret;
384
385         mm = get_task_mm(tsk);
386         if (!mm)
387                 return -EPERM;
388
389         if (!tsk->ptrace || (current != tsk->parent) ||
390             ((get_dumpable(mm) != SUID_DUMP_USER) &&
391              !ptracer_capable(tsk, mm->user_ns))) {
392                 mmput(mm);
393                 return -EPERM;
394         }
395
396         ret = __access_remote_tags(mm, addr, kiov, gup_flags);
397         mmput(mm);
398
399         return ret;
400 }
401
402 int mte_ptrace_copy_tags(struct task_struct *child, long request,
403                          unsigned long addr, unsigned long data)
404 {
405         int ret;
406         struct iovec kiov;
407         struct iovec __user *uiov = (void __user *)data;
408         unsigned int gup_flags = FOLL_FORCE;
409
410         if (!system_supports_mte())
411                 return -EIO;
412
413         if (get_user(kiov.iov_base, &uiov->iov_base) ||
414             get_user(kiov.iov_len, &uiov->iov_len))
415                 return -EFAULT;
416
417         if (request == PTRACE_POKEMTETAGS)
418                 gup_flags |= FOLL_WRITE;
419
420         /* align addr to the MTE tag granule */
421         addr &= MTE_GRANULE_MASK;
422
423         ret = access_remote_tags(child, addr, &kiov, gup_flags);
424         if (!ret)
425                 ret = put_user(kiov.iov_len, &uiov->iov_len);
426
427         return ret;
428 }