1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 ARM Ltd.
6 #include <linux/bitops.h>
7 #include <linux/kernel.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>
19 #include <asm/barrier.h>
20 #include <asm/cpufeature.h>
22 #include <asm/ptrace.h>
23 #include <asm/sysreg.h>
25 u64 gcr_kernel_excl __ro_after_init;
27 static bool report_fault_once = true;
29 static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap)
31 pte_t old_pte = READ_ONCE(*ptep);
33 if (check_swap && is_swap_pte(old_pte)) {
34 swp_entry_t entry = pte_to_swp_entry(old_pte);
36 if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
40 page_kasan_tag_reset(page);
42 * We need smp_wmb() in between setting the flags and clearing the
43 * tags because if another thread reads page->flags and builds a
44 * tagged address out of it, there is an actual dependency to the
45 * memory access, but on the current thread we do not guarantee that
46 * the new page->flags are visible before the tags were updated.
49 mte_clear_page_tags(page_address(page));
52 void mte_sync_tags(pte_t *ptep, pte_t pte)
54 struct page *page = pte_page(pte);
55 long i, nr_pages = compound_nr(page);
56 bool check_swap = nr_pages == 1;
58 /* if PG_mte_tagged is set, tags have already been initialised */
59 for (i = 0; i < nr_pages; i++, page++) {
60 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
61 mte_sync_page_tags(page, ptep, check_swap);
65 int memcmp_pages(struct page *page1, struct page *page2)
70 addr1 = page_address(page1);
71 addr2 = page_address(page2);
72 ret = memcmp(addr1, addr2, PAGE_SIZE);
74 if (!system_supports_mte() || ret)
78 * If the page content is identical but at least one of the pages is
79 * tagged, return non-zero to avoid KSM merging. If only one of the
80 * pages is tagged, set_pte_at() may zero or change the tags of the
81 * other page via mte_sync_tags().
83 if (test_bit(PG_mte_tagged, &page1->flags) ||
84 test_bit(PG_mte_tagged, &page2->flags))
85 return addr1 != addr2;
90 void mte_init_tags(u64 max_tag)
92 static bool gcr_kernel_excl_initialized;
94 if (!gcr_kernel_excl_initialized) {
96 * The format of the tags in KASAN is 0xFF and in MTE is 0xF.
97 * This conversion extracts an MTE tag from a KASAN tag.
99 u64 incl = GENMASK(FIELD_GET(MTE_TAG_MASK >> MTE_TAG_SHIFT,
102 gcr_kernel_excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
103 gcr_kernel_excl_initialized = true;
106 /* Enable the kernel exclude mask for random tags generation. */
107 write_sysreg_s(SYS_GCR_EL1_RRND | gcr_kernel_excl, SYS_GCR_EL1);
110 void mte_enable_kernel(void)
112 /* Enable MTE Sync Mode for EL1. */
113 sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, SCTLR_ELx_TCF_SYNC);
117 void mte_set_report_once(bool state)
119 WRITE_ONCE(report_fault_once, state);
122 bool mte_report_once(void)
124 return READ_ONCE(report_fault_once);
127 static void update_sctlr_el1_tcf0(u64 tcf0)
129 /* ISB required for the kernel uaccess routines */
130 sysreg_clear_set(sctlr_el1, SCTLR_EL1_TCF0_MASK, tcf0);
134 static void set_sctlr_el1_tcf0(u64 tcf0)
137 * mte_thread_switch() checks current->thread.sctlr_tcf0 as an
138 * optimisation. Disable preemption so that it does not see
139 * the variable update before the SCTLR_EL1.TCF0 one.
142 current->thread.sctlr_tcf0 = tcf0;
143 update_sctlr_el1_tcf0(tcf0);
147 static void update_gcr_el1_excl(u64 excl)
151 * Note that the mask controlled by the user via prctl() is an
152 * include while GCR_EL1 accepts an exclude mask.
153 * No need for ISB since this only affects EL0 currently, implicit
156 sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, excl);
159 static void set_gcr_el1_excl(u64 excl)
161 current->thread.gcr_user_excl = excl;
164 * SYS_GCR_EL1 will be set to current->thread.gcr_user_excl value
165 * by mte_set_user_gcr() in kernel_exit,
169 void flush_mte_state(void)
171 if (!system_supports_mte())
174 /* clear any pending asynchronous tag fault */
176 write_sysreg_s(0, SYS_TFSRE0_EL1);
177 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
178 /* disable tag checking */
179 set_sctlr_el1_tcf0(SCTLR_EL1_TCF0_NONE);
180 /* reset tag generation mask */
181 set_gcr_el1_excl(SYS_GCR_EL1_EXCL_MASK);
184 void mte_thread_switch(struct task_struct *next)
186 if (!system_supports_mte())
189 /* avoid expensive SCTLR_EL1 accesses if no change */
190 if (current->thread.sctlr_tcf0 != next->thread.sctlr_tcf0)
191 update_sctlr_el1_tcf0(next->thread.sctlr_tcf0);
194 void mte_suspend_exit(void)
196 if (!system_supports_mte())
199 update_gcr_el1_excl(gcr_kernel_excl);
202 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
205 u64 gcr_excl = ~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
206 SYS_GCR_EL1_EXCL_MASK;
208 if (!system_supports_mte())
211 switch (arg & PR_MTE_TCF_MASK) {
212 case PR_MTE_TCF_NONE:
213 tcf0 = SCTLR_EL1_TCF0_NONE;
215 case PR_MTE_TCF_SYNC:
216 tcf0 = SCTLR_EL1_TCF0_SYNC;
218 case PR_MTE_TCF_ASYNC:
219 tcf0 = SCTLR_EL1_TCF0_ASYNC;
225 if (task != current) {
226 task->thread.sctlr_tcf0 = tcf0;
227 task->thread.gcr_user_excl = gcr_excl;
229 set_sctlr_el1_tcf0(tcf0);
230 set_gcr_el1_excl(gcr_excl);
236 long get_mte_ctrl(struct task_struct *task)
239 u64 incl = ~task->thread.gcr_user_excl & SYS_GCR_EL1_EXCL_MASK;
241 if (!system_supports_mte())
244 ret = incl << PR_MTE_TAG_SHIFT;
246 switch (task->thread.sctlr_tcf0) {
247 case SCTLR_EL1_TCF0_NONE:
248 ret |= PR_MTE_TCF_NONE;
250 case SCTLR_EL1_TCF0_SYNC:
251 ret |= PR_MTE_TCF_SYNC;
253 case SCTLR_EL1_TCF0_ASYNC:
254 ret |= PR_MTE_TCF_ASYNC;
262 * Access MTE tags in another process' address space as given in mm. Update
263 * the number of tags copied. Return 0 if any tags copied, error otherwise.
264 * Inspired by __access_remote_vm().
266 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
267 struct iovec *kiov, unsigned int gup_flags)
269 struct vm_area_struct *vma;
270 void __user *buf = kiov->iov_base;
271 size_t len = kiov->iov_len;
273 int write = gup_flags & FOLL_WRITE;
275 if (!access_ok(buf, len))
278 if (mmap_read_lock_killable(mm))
282 unsigned long tags, offset;
284 struct page *page = NULL;
286 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
292 * Only copy tags if the page has been mapped as PROT_MTE
293 * (PG_mte_tagged set). Otherwise the tags are not valid and
294 * not accessible to user. Moreover, an mprotect(PROT_MTE)
295 * would cause the existing tags to be cleared if the page
296 * was never mapped with PROT_MTE.
298 if (!(vma->vm_flags & VM_MTE)) {
303 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
305 /* limit access to the end of the page */
306 offset = offset_in_page(addr);
307 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
309 maddr = page_address(page);
311 tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
312 set_page_dirty_lock(page);
314 tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
318 /* error accessing the tracer's buffer */
324 addr += tags * MTE_GRANULE_SIZE;
326 mmap_read_unlock(mm);
328 /* return an error if no tags copied */
329 kiov->iov_len = buf - kiov->iov_base;
330 if (!kiov->iov_len) {
331 /* check for error accessing the tracee's address space */
342 * Copy MTE tags in another process' address space at 'addr' to/from tracer's
343 * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
345 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
346 struct iovec *kiov, unsigned int gup_flags)
348 struct mm_struct *mm;
351 mm = get_task_mm(tsk);
355 if (!tsk->ptrace || (current != tsk->parent) ||
356 ((get_dumpable(mm) != SUID_DUMP_USER) &&
357 !ptracer_capable(tsk, mm->user_ns))) {
362 ret = __access_remote_tags(mm, addr, kiov, gup_flags);
368 int mte_ptrace_copy_tags(struct task_struct *child, long request,
369 unsigned long addr, unsigned long data)
373 struct iovec __user *uiov = (void __user *)data;
374 unsigned int gup_flags = FOLL_FORCE;
376 if (!system_supports_mte())
379 if (get_user(kiov.iov_base, &uiov->iov_base) ||
380 get_user(kiov.iov_len, &uiov->iov_len))
383 if (request == PTRACE_POKEMTETAGS)
384 gup_flags |= FOLL_WRITE;
386 /* align addr to the MTE tag granule */
387 addr &= MTE_GRANULE_MASK;
389 ret = access_remote_tags(child, addr, &kiov, gup_flags);
391 ret = put_user(kiov.iov_len, &uiov->iov_len);