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 #ifdef CONFIG_KASAN_HW_TAGS
30 /* Whether the MTE asynchronous mode is enabled. */
31 DEFINE_STATIC_KEY_FALSE(mte_async_mode);
32 EXPORT_SYMBOL_GPL(mte_async_mode);
35 static void mte_sync_page_tags(struct page *page, pte_t old_pte,
36 bool check_swap, bool pte_is_tagged)
38 if (check_swap && is_swap_pte(old_pte)) {
39 swp_entry_t entry = pte_to_swp_entry(old_pte);
41 if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
48 page_kasan_tag_reset(page);
50 * We need smp_wmb() in between setting the flags and clearing the
51 * tags because if another thread reads page->flags and builds a
52 * tagged address out of it, there is an actual dependency to the
53 * memory access, but on the current thread we do not guarantee that
54 * the new page->flags are visible before the tags were updated.
57 mte_clear_page_tags(page_address(page));
60 void mte_sync_tags(pte_t old_pte, pte_t pte)
62 struct page *page = pte_page(pte);
63 long i, nr_pages = compound_nr(page);
64 bool check_swap = nr_pages == 1;
65 bool pte_is_tagged = pte_tagged(pte);
67 /* Early out if there's nothing to do */
68 if (!check_swap && !pte_is_tagged)
71 /* if PG_mte_tagged is set, tags have already been initialised */
72 for (i = 0; i < nr_pages; i++, page++) {
73 if (!test_and_set_bit(PG_mte_tagged, &page->flags))
74 mte_sync_page_tags(page, old_pte, check_swap,
79 int memcmp_pages(struct page *page1, struct page *page2)
84 addr1 = page_address(page1);
85 addr2 = page_address(page2);
86 ret = memcmp(addr1, addr2, PAGE_SIZE);
88 if (!system_supports_mte() || ret)
92 * If the page content is identical but at least one of the pages is
93 * tagged, return non-zero to avoid KSM merging. If only one of the
94 * pages is tagged, set_pte_at() may zero or change the tags of the
95 * other page via mte_sync_tags().
97 if (test_bit(PG_mte_tagged, &page1->flags) ||
98 test_bit(PG_mte_tagged, &page2->flags))
99 return addr1 != addr2;
104 void mte_init_tags(u64 max_tag)
106 static bool gcr_kernel_excl_initialized;
108 if (!gcr_kernel_excl_initialized) {
110 * The format of the tags in KASAN is 0xFF and in MTE is 0xF.
111 * This conversion extracts an MTE tag from a KASAN tag.
113 u64 incl = GENMASK(FIELD_GET(MTE_TAG_MASK >> MTE_TAG_SHIFT,
116 gcr_kernel_excl = ~incl & SYS_GCR_EL1_EXCL_MASK;
117 gcr_kernel_excl_initialized = true;
120 /* Enable the kernel exclude mask for random tags generation. */
121 write_sysreg_s(SYS_GCR_EL1_RRND | gcr_kernel_excl, SYS_GCR_EL1);
124 static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
126 /* Enable MTE Sync Mode for EL1. */
127 sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, tcf);
130 pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
133 #ifdef CONFIG_KASAN_HW_TAGS
134 void mte_enable_kernel_sync(void)
137 * Make sure we enter this function when no PE has set
138 * async mode previously.
140 WARN_ONCE(system_uses_mte_async_mode(),
141 "MTE async mode enabled system wide!");
143 __mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
146 void mte_enable_kernel_async(void)
148 __mte_enable_kernel("asynchronous", SCTLR_ELx_TCF_ASYNC);
151 * MTE async mode is set system wide by the first PE that
152 * executes this function.
154 * Note: If in future KASAN acquires a runtime switching
155 * mode in between sync and async, this strategy needs
158 if (!system_uses_mte_async_mode())
159 static_branch_enable(&mte_async_mode);
163 void mte_set_report_once(bool state)
165 WRITE_ONCE(report_fault_once, state);
168 bool mte_report_once(void)
170 return READ_ONCE(report_fault_once);
173 #ifdef CONFIG_KASAN_HW_TAGS
174 void mte_check_tfsr_el1(void)
178 if (!system_supports_mte())
181 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
183 if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
185 * Note: isb() is not required after this direct write
186 * because there is no indirect read subsequent to it
187 * (per ARM DDI 0487F.c table D13-1).
189 write_sysreg_s(0, SYS_TFSR_EL1);
191 kasan_report_async();
196 static void mte_update_sctlr_user(struct task_struct *task)
198 unsigned long sctlr = task->thread.sctlr_user;
199 unsigned long pref = MTE_CTRL_TCF_ASYNC;
200 unsigned long mte_ctrl = task->thread.mte_ctrl;
201 unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
203 sctlr &= ~SCTLR_EL1_TCF0_MASK;
204 if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
205 sctlr |= SCTLR_EL1_TCF0_ASYNC;
206 else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
207 sctlr |= SCTLR_EL1_TCF0_SYNC;
208 task->thread.sctlr_user = sctlr;
211 void mte_thread_init_user(void)
213 if (!system_supports_mte())
216 /* clear any pending asynchronous tag fault */
218 write_sysreg_s(0, SYS_TFSRE0_EL1);
219 clear_thread_flag(TIF_MTE_ASYNC_FAULT);
220 /* disable tag checking and reset tag generation mask */
221 current->thread.mte_ctrl = MTE_CTRL_GCR_USER_EXCL_MASK;
222 mte_update_sctlr_user(current);
223 set_task_sctlr_el1(current->thread.sctlr_user);
226 void mte_thread_switch(struct task_struct *next)
228 mte_update_sctlr_user(next);
231 * Check if an async tag exception occurred at EL1.
233 * Note: On the context switch path we rely on the dsb() present
234 * in __switch_to() to guarantee that the indirect writes to TFSR_EL1
235 * are synchronized before this point.
238 mte_check_tfsr_el1();
241 void mte_suspend_enter(void)
243 if (!system_supports_mte())
247 * The barriers are required to guarantee that the indirect writes
248 * to TFSR_EL1 are synchronized before we report the state.
253 /* Report SYS_TFSR_EL1 before suspend entry */
254 mte_check_tfsr_el1();
257 void mte_suspend_exit(void)
259 if (!system_supports_mte())
262 sysreg_clear_set_s(SYS_GCR_EL1, SYS_GCR_EL1_EXCL_MASK, gcr_kernel_excl);
266 long set_mte_ctrl(struct task_struct *task, unsigned long arg)
268 u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
269 SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
271 if (!system_supports_mte())
274 if (arg & PR_MTE_TCF_ASYNC)
275 mte_ctrl |= MTE_CTRL_TCF_ASYNC;
276 if (arg & PR_MTE_TCF_SYNC)
277 mte_ctrl |= MTE_CTRL_TCF_SYNC;
279 task->thread.mte_ctrl = mte_ctrl;
280 if (task == current) {
281 mte_update_sctlr_user(task);
282 set_task_sctlr_el1(task->thread.sctlr_user);
288 long get_mte_ctrl(struct task_struct *task)
291 u64 mte_ctrl = task->thread.mte_ctrl;
292 u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
293 SYS_GCR_EL1_EXCL_MASK;
295 if (!system_supports_mte())
298 ret = incl << PR_MTE_TAG_SHIFT;
299 if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
300 ret |= PR_MTE_TCF_ASYNC;
301 if (mte_ctrl & MTE_CTRL_TCF_SYNC)
302 ret |= PR_MTE_TCF_SYNC;
308 * Access MTE tags in another process' address space as given in mm. Update
309 * the number of tags copied. Return 0 if any tags copied, error otherwise.
310 * Inspired by __access_remote_vm().
312 static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
313 struct iovec *kiov, unsigned int gup_flags)
315 struct vm_area_struct *vma;
316 void __user *buf = kiov->iov_base;
317 size_t len = kiov->iov_len;
319 int write = gup_flags & FOLL_WRITE;
321 if (!access_ok(buf, len))
324 if (mmap_read_lock_killable(mm))
328 unsigned long tags, offset;
330 struct page *page = NULL;
332 ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
338 * Only copy tags if the page has been mapped as PROT_MTE
339 * (PG_mte_tagged set). Otherwise the tags are not valid and
340 * not accessible to user. Moreover, an mprotect(PROT_MTE)
341 * would cause the existing tags to be cleared if the page
342 * was never mapped with PROT_MTE.
344 if (!(vma->vm_flags & VM_MTE)) {
349 WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
351 /* limit access to the end of the page */
352 offset = offset_in_page(addr);
353 tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
355 maddr = page_address(page);
357 tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
358 set_page_dirty_lock(page);
360 tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
364 /* error accessing the tracer's buffer */
370 addr += tags * MTE_GRANULE_SIZE;
372 mmap_read_unlock(mm);
374 /* return an error if no tags copied */
375 kiov->iov_len = buf - kiov->iov_base;
376 if (!kiov->iov_len) {
377 /* check for error accessing the tracee's address space */
388 * Copy MTE tags in another process' address space at 'addr' to/from tracer's
389 * iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
391 static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
392 struct iovec *kiov, unsigned int gup_flags)
394 struct mm_struct *mm;
397 mm = get_task_mm(tsk);
401 if (!tsk->ptrace || (current != tsk->parent) ||
402 ((get_dumpable(mm) != SUID_DUMP_USER) &&
403 !ptracer_capable(tsk, mm->user_ns))) {
408 ret = __access_remote_tags(mm, addr, kiov, gup_flags);
414 int mte_ptrace_copy_tags(struct task_struct *child, long request,
415 unsigned long addr, unsigned long data)
419 struct iovec __user *uiov = (void __user *)data;
420 unsigned int gup_flags = FOLL_FORCE;
422 if (!system_supports_mte())
425 if (get_user(kiov.iov_base, &uiov->iov_base) ||
426 get_user(kiov.iov_len, &uiov->iov_len))
429 if (request == PTRACE_POKEMTETAGS)
430 gup_flags |= FOLL_WRITE;
432 /* align addr to the MTE tag granule */
433 addr &= MTE_GRANULE_MASK;
435 ret = access_remote_tags(child, addr, &kiov, gup_flags);
437 ret = put_user(kiov.iov_len, &uiov->iov_len);