Changes in 5.15.82
arm64: mte: Avoid setting PG_mte_tagged if no tags cleared or restored
drm/i915: Create a dummy object for gen6 ppgtt
drm/i915/gt: Use i915_vm_put on ppgtt_create error paths
erofs: fix order >= MAX_ORDER warning due to crafted negative i_size
btrfs: sink iterator parameter to btrfs_ioctl_logical_to_ino
btrfs: free btrfs_path before copying inodes to userspace
spi: spi-imx: Fix spi_bus_clk if requested clock is higher than input clock
btrfs: move QUOTA_ENABLED check to rescan_should_stop from btrfs_qgroup_rescan_worker
btrfs: qgroup: fix sleep from invalid context bug in btrfs_qgroup_inherit()
drm/display/dp_mst: Fix drm_dp_mst_add_affected_dsc_crtcs() return code
drm/amdgpu: update drm_display_info correctly when the edid is read
drm/amdgpu: Partially revert "drm/amdgpu: update drm_display_info correctly when the edid is read"
iio: health: afe4403: Fix oob read in afe4403_read_raw
iio: health: afe4404: Fix oob read in afe4404_[read|write]_raw
iio: light: rpr0521: add missing Kconfig dependencies
bpf, perf: Use subprog name when reporting subprog ksymbol
scripts/faddr2line: Fix regression in name resolution on ppc64le
ARM: at91: rm9200: fix usb device clock id
libbpf: Handle size overflow for ringbuf mmap
hwmon: (ltc2947) fix temperature scaling
hwmon: (ina3221) Fix shunt sum critical calculation
hwmon: (i5500_temp) fix missing pci_disable_device()
hwmon: (ibmpex) Fix possible UAF when ibmpex_register_bmc() fails
bpf: Do not copy spin lock field from user in bpf_selem_alloc
nvmem: rmem: Fix return value check in rmem_read()
of: property: decrement node refcount in of_fwnode_get_reference_args()
ixgbevf: Fix resource leak in ixgbevf_init_module()
i40e: Fix error handling in i40e_init_module()
fm10k: Fix error handling in fm10k_init_module()
iavf: remove redundant ret variable
iavf: Fix error handling in iavf_init_module()
e100: Fix possible use after free in e100_xmit_prepare
net/mlx5: DR, Rename list field in matcher struct to list_node
net/mlx5: DR, Fix uninitialized var warning
net/mlx5: Fix uninitialized variable bug in outlen_write()
net/mlx5e: Fix use-after-free when reverting termination table
can: sja1000_isa: sja1000_isa_probe(): add missing free_sja1000dev()
can: cc770: cc770_isa_probe(): add missing free_cc770dev()
can: etas_es58x: es58x_init_netdev(): free netdev when register_candev()
can: m_can: pci: add missing m_can_class_free_dev() in probe/remove methods
can: m_can: Add check for devm_clk_get
qlcnic: fix sleep-in-atomic-context bugs caused by msleep
aquantia: Do not purge addresses when setting the number of rings
wifi: cfg80211: fix buffer overflow in elem comparison
wifi: cfg80211: don't allow multi-BSSID in S1G
wifi: mac8021: fix possible oob access in ieee80211_get_rate_duration
net: phy: fix null-ptr-deref while probe() failed
net: ethernet: ti: am65-cpsw: fix error handling in am65_cpsw_nuss_probe()
net: net_netdev: Fix error handling in ntb_netdev_init_module()
net/9p: Fix a potential socket leak in p9_socket_open
net: ethernet: nixge: fix NULL dereference
net: wwan: iosm: fix kernel test robot reported error
net: wwan: iosm: fix dma_alloc_coherent incompatible pointer type
dsa: lan9303: Correct stat name
tipc: re-fetch skb cb after tipc_msg_validate
net: hsr: Fix potential use-after-free
net: mdiobus: fix unbalanced node reference count
afs: Fix fileserver probe RTT handling
net: tun: Fix use-after-free in tun_detach()
packet: do not set TP_STATUS_CSUM_VALID on CHECKSUM_COMPLETE
sctp: fix memory leak in sctp_stream_outq_migrate()
net: ethernet: renesas: ravb: Fix promiscuous mode after system resumed
hwmon: (coretemp) Check for null before removing sysfs attrs
hwmon: (coretemp) fix pci device refcount leak in nv1a_ram_new()
riscv: vdso: fix section overlapping under some conditions
riscv: mm: Proper page permissions after initmem free
ALSA: dice: fix regression for Lexicon I-ONIX FW810S
error-injection: Add prompt for function error injection
tools/vm/slabinfo-gnuplot: use "grep -E" instead of "egrep"
nilfs2: fix NULL pointer dereference in nilfs_palloc_commit_free_entry()
x86/bugs: Make sure MSR_SPEC_CTRL is updated properly upon resume from S3
pinctrl: intel: Save and restore pins in "direct IRQ" mode
v4l2: don't fall back to follow_pfn() if pin_user_pages_fast() fails
net: stmmac: Set MAC's flow control register to reflect current settings
mmc: mmc_test: Fix removal of debugfs file
mmc: core: Fix ambiguous TRIM and DISCARD arg
mmc: sdhci-esdhc-imx: correct CQHCI exit halt state check
mmc: sdhci-sprd: Fix no reset data and command after voltage switch
mmc: sdhci: Fix voltage switch delay
drm/amdgpu: temporarily disable broken Clang builds due to blown stack-frame
drm/amdgpu: enable Vangogh VCN indirect sram mode
drm/i915: Fix negative value passed as remaining time
drm/i915: Never return 0 if not all requests retired
tracing/osnoise: Fix duration type
tracing: Fix race where histograms can be called before the event
tracing: Free buffers when a used dynamic event is removed
io_uring: update res mask in io_poll_check_events
io_uring: fix tw losing poll events
io_uring: cmpxchg for poll arm refs release
io_uring: make poll refs more robust
io_uring/poll: fix poll_refs race with cancelation
KVM: x86/mmu: Fix race condition in direct_page_fault
ASoC: ops: Fix bounds check for _sx controls
pinctrl: single: Fix potential division by zero
riscv: Sync efi page table's kernel mappings before switching
riscv: fix race when vmap stack overflow
riscv: kexec: Fixup irq controller broken in kexec crash path
nvme: fix SRCU protection of nvme_ns_head list
iommu/vt-d: Fix PCI device refcount leak in has_external_pci()
iommu/vt-d: Fix PCI device refcount leak in dmar_dev_scope_init()
mm: __isolate_lru_page_prepare() in isolate_migratepages_block()
mm: migrate: fix THP's mapcount on isolation
parisc: Increase FRAME_WARN to 2048 bytes on parisc
Kconfig.debug: provide a little extra FRAME_WARN leeway when KASAN is enabled
selftests: net: add delete nexthop route warning test
selftests: net: fix nexthop warning cleanup double ip typo
ipv4: Handle attempt to delete multipath route when fib_info contains an nh reference
ipv4: Fix route deletion when nexthop info is not specified
serial: stm32: Factor out GPIO RTS toggling into separate function
serial: stm32: Use TC interrupt to deassert GPIO RTS in RS485 mode
serial: stm32: Deassert Transmit Enable on ->rs485_config()
i2c: npcm7xx: Fix error handling in npcm_i2c_init()
i2c: imx: Only DMA messages with I2C_M_DMA_SAFE flag set
ACPI: HMAT: remove unnecessary variable initialization
ACPI: HMAT: Fix initiator registration for single-initiator systems
Revert "clocksource/drivers/riscv: Events are stopped during CPU suspend"
char: tpm: Protect tpm_pm_suspend with locks
Input: raydium_ts_i2c - fix memory leak in raydium_i2c_send()
ipc/sem: Fix dangling sem_array access in semtimedop race
proc: avoid integer type confusion in get_proc_long
proc: proc_skip_spaces() shouldn't think it is working on C strings
Linux 5.15.82
Change-Id: I4ce52cb5917c9036339810c816ab005a4e9489fb
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
602 lines
15 KiB
C
602 lines
15 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Copyright (C) 2020 ARM Ltd.
|
|
*/
|
|
|
|
#include <linux/bitops.h>
|
|
#include <linux/cpu.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/prctl.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/sched/mm.h>
|
|
#include <linux/string.h>
|
|
#include <linux/swap.h>
|
|
#include <linux/swapops.h>
|
|
#include <linux/thread_info.h>
|
|
#include <linux/types.h>
|
|
#include <linux/uio.h>
|
|
|
|
#include <asm/barrier.h>
|
|
#include <asm/cpufeature.h>
|
|
#include <asm/mte.h>
|
|
#include <asm/ptrace.h>
|
|
#include <asm/sysreg.h>
|
|
|
|
static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred);
|
|
|
|
#ifdef CONFIG_KASAN_HW_TAGS
|
|
/*
|
|
* The asynchronous and asymmetric MTE modes have the same behavior for
|
|
* store operations. This flag is set when either of these modes is enabled.
|
|
*/
|
|
DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode);
|
|
EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode);
|
|
#endif
|
|
|
|
static void mte_sync_page_tags(struct page *page, pte_t old_pte,
|
|
bool check_swap, bool pte_is_tagged)
|
|
{
|
|
if (check_swap && is_swap_pte(old_pte)) {
|
|
swp_entry_t entry = pte_to_swp_entry(old_pte);
|
|
|
|
if (!non_swap_entry(entry) && mte_restore_tags(entry, page))
|
|
return;
|
|
}
|
|
|
|
if (!pte_is_tagged)
|
|
return;
|
|
|
|
page_kasan_tag_reset(page);
|
|
/*
|
|
* We need smp_wmb() in between setting the flags and clearing the
|
|
* tags because if another thread reads page->flags and builds a
|
|
* tagged address out of it, there is an actual dependency to the
|
|
* memory access, but on the current thread we do not guarantee that
|
|
* the new page->flags are visible before the tags were updated.
|
|
*/
|
|
smp_wmb();
|
|
/*
|
|
* Test PG_mte_tagged again in case it was racing with another
|
|
* set_pte_at().
|
|
*/
|
|
if (!test_and_set_bit(PG_mte_tagged, &page->flags))
|
|
mte_clear_page_tags(page_address(page));
|
|
}
|
|
|
|
void mte_sync_tags(pte_t old_pte, pte_t pte)
|
|
{
|
|
struct page *page = pte_page(pte);
|
|
long i, nr_pages = compound_nr(page);
|
|
bool check_swap = nr_pages == 1;
|
|
bool pte_is_tagged = pte_tagged(pte);
|
|
|
|
/* Early out if there's nothing to do */
|
|
if (!check_swap && !pte_is_tagged)
|
|
return;
|
|
|
|
/* if PG_mte_tagged is set, tags have already been initialised */
|
|
for (i = 0; i < nr_pages; i++, page++) {
|
|
if (!test_bit(PG_mte_tagged, &page->flags))
|
|
mte_sync_page_tags(page, old_pte, check_swap,
|
|
pte_is_tagged);
|
|
}
|
|
|
|
/* ensure the tags are visible before the PTE is set */
|
|
smp_wmb();
|
|
}
|
|
|
|
int memcmp_pages(struct page *page1, struct page *page2)
|
|
{
|
|
char *addr1, *addr2;
|
|
int ret;
|
|
|
|
addr1 = page_address(page1);
|
|
addr2 = page_address(page2);
|
|
ret = memcmp(addr1, addr2, PAGE_SIZE);
|
|
|
|
if (!system_supports_mte() || ret)
|
|
return ret;
|
|
|
|
/*
|
|
* If the page content is identical but at least one of the pages is
|
|
* tagged, return non-zero to avoid KSM merging. If only one of the
|
|
* pages is tagged, set_pte_at() may zero or change the tags of the
|
|
* other page via mte_sync_tags().
|
|
*/
|
|
if (test_bit(PG_mte_tagged, &page1->flags) ||
|
|
test_bit(PG_mte_tagged, &page2->flags))
|
|
return addr1 != addr2;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline void __mte_enable_kernel(const char *mode, unsigned long tcf)
|
|
{
|
|
/* Enable MTE Sync Mode for EL1. */
|
|
sysreg_clear_set(sctlr_el1, SCTLR_ELx_TCF_MASK, tcf);
|
|
isb();
|
|
|
|
pr_info_once("MTE: enabled in %s mode at EL1\n", mode);
|
|
}
|
|
|
|
#ifdef CONFIG_KASAN_HW_TAGS
|
|
void mte_enable_kernel_sync(void)
|
|
{
|
|
/*
|
|
* Make sure we enter this function when no PE has set
|
|
* async mode previously.
|
|
*/
|
|
WARN_ONCE(system_uses_mte_async_or_asymm_mode(),
|
|
"MTE async mode enabled system wide!");
|
|
|
|
__mte_enable_kernel("synchronous", SCTLR_ELx_TCF_SYNC);
|
|
}
|
|
|
|
void mte_enable_kernel_async(void)
|
|
{
|
|
__mte_enable_kernel("asynchronous", SCTLR_ELx_TCF_ASYNC);
|
|
|
|
/*
|
|
* MTE async mode is set system wide by the first PE that
|
|
* executes this function.
|
|
*
|
|
* Note: If in future KASAN acquires a runtime switching
|
|
* mode in between sync and async, this strategy needs
|
|
* to be reviewed.
|
|
*/
|
|
if (!system_uses_mte_async_or_asymm_mode())
|
|
static_branch_enable(&mte_async_or_asymm_mode);
|
|
}
|
|
|
|
void mte_enable_kernel_asymm(void)
|
|
{
|
|
if (cpus_have_cap(ARM64_MTE_ASYMM)) {
|
|
__mte_enable_kernel("asymmetric", SCTLR_ELx_TCF_ASYMM);
|
|
|
|
/*
|
|
* MTE asymm mode behaves as async mode for store
|
|
* operations. The mode is set system wide by the
|
|
* first PE that executes this function.
|
|
*
|
|
* Note: If in future KASAN acquires a runtime switching
|
|
* mode in between sync and async, this strategy needs
|
|
* to be reviewed.
|
|
*/
|
|
if (!system_uses_mte_async_or_asymm_mode())
|
|
static_branch_enable(&mte_async_or_asymm_mode);
|
|
} else {
|
|
/*
|
|
* If the CPU does not support MTE asymmetric mode the
|
|
* kernel falls back on synchronous mode which is the
|
|
* default for kasan=on.
|
|
*/
|
|
mte_enable_kernel_sync();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef CONFIG_KASAN_HW_TAGS
|
|
void mte_check_tfsr_el1(void)
|
|
{
|
|
u64 tfsr_el1 = read_sysreg_s(SYS_TFSR_EL1);
|
|
|
|
if (unlikely(tfsr_el1 & SYS_TFSR_EL1_TF1)) {
|
|
/*
|
|
* Note: isb() is not required after this direct write
|
|
* because there is no indirect read subsequent to it
|
|
* (per ARM DDI 0487F.c table D13-1).
|
|
*/
|
|
write_sysreg_s(0, SYS_TFSR_EL1);
|
|
|
|
kasan_report_async();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* This is where we actually resolve the system and process MTE mode
|
|
* configuration into an actual value in SCTLR_EL1 that affects
|
|
* userspace.
|
|
*/
|
|
static void mte_update_sctlr_user(struct task_struct *task)
|
|
{
|
|
/*
|
|
* This must be called with preemption disabled and can only be called
|
|
* on the current or next task since the CPU must match where the thread
|
|
* is going to run. The caller is responsible for calling
|
|
* update_sctlr_el1() later in the same preemption disabled block.
|
|
*/
|
|
unsigned long sctlr = task->thread.sctlr_user;
|
|
unsigned long mte_ctrl = task->thread.mte_ctrl;
|
|
unsigned long pref, resolved_mte_tcf;
|
|
|
|
pref = __this_cpu_read(mte_tcf_preferred);
|
|
/*
|
|
* If there is no overlap between the system preferred and
|
|
* program requested values go with what was requested.
|
|
*/
|
|
resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl;
|
|
sctlr &= ~SCTLR_EL1_TCF0_MASK;
|
|
/*
|
|
* Pick an actual setting. The order in which we check for
|
|
* set bits and map into register values determines our
|
|
* default order.
|
|
*/
|
|
if (resolved_mte_tcf & MTE_CTRL_TCF_ASYMM)
|
|
sctlr |= SCTLR_EL1_TCF0_ASYMM;
|
|
else if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC)
|
|
sctlr |= SCTLR_EL1_TCF0_ASYNC;
|
|
else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC)
|
|
sctlr |= SCTLR_EL1_TCF0_SYNC;
|
|
task->thread.sctlr_user = sctlr;
|
|
}
|
|
|
|
static void mte_update_gcr_excl(struct task_struct *task)
|
|
{
|
|
/*
|
|
* SYS_GCR_EL1 will be set to current->thread.mte_ctrl value by
|
|
* mte_set_user_gcr() in kernel_exit, but only if KASAN is enabled.
|
|
*/
|
|
if (kasan_hw_tags_enabled())
|
|
return;
|
|
|
|
write_sysreg_s(
|
|
((task->thread.mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
|
|
SYS_GCR_EL1_EXCL_MASK) | SYS_GCR_EL1_RRND,
|
|
SYS_GCR_EL1);
|
|
}
|
|
|
|
void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr,
|
|
__le32 *updptr, int nr_inst)
|
|
{
|
|
BUG_ON(nr_inst != 1); /* Branch -> NOP */
|
|
|
|
if (kasan_hw_tags_enabled())
|
|
*updptr = cpu_to_le32(aarch64_insn_gen_nop());
|
|
}
|
|
|
|
void mte_thread_init_user(void)
|
|
{
|
|
if (!system_supports_mte())
|
|
return;
|
|
|
|
/* clear any pending asynchronous tag fault */
|
|
dsb(ish);
|
|
write_sysreg_s(0, SYS_TFSRE0_EL1);
|
|
clear_thread_flag(TIF_MTE_ASYNC_FAULT);
|
|
/* disable tag checking and reset tag generation mask */
|
|
set_mte_ctrl(current, 0);
|
|
}
|
|
|
|
void mte_thread_switch(struct task_struct *next)
|
|
{
|
|
if (!system_supports_mte())
|
|
return;
|
|
|
|
mte_update_sctlr_user(next);
|
|
mte_update_gcr_excl(next);
|
|
|
|
/*
|
|
* Check if an async tag exception occurred at EL1.
|
|
*
|
|
* Note: On the context switch path we rely on the dsb() present
|
|
* in __switch_to() to guarantee that the indirect writes to TFSR_EL1
|
|
* are synchronized before this point.
|
|
*/
|
|
isb();
|
|
mte_check_tfsr_el1();
|
|
}
|
|
|
|
void mte_cpu_setup(void)
|
|
{
|
|
u64 rgsr;
|
|
|
|
/*
|
|
* CnP must be enabled only after the MAIR_EL1 register has been set
|
|
* up. Inconsistent MAIR_EL1 between CPUs sharing the same TLB may
|
|
* lead to the wrong memory type being used for a brief window during
|
|
* CPU power-up.
|
|
*
|
|
* CnP is not a boot feature so MTE gets enabled before CnP, but let's
|
|
* make sure that is the case.
|
|
*/
|
|
BUG_ON(read_sysreg(ttbr0_el1) & TTBR_CNP_BIT);
|
|
BUG_ON(read_sysreg(ttbr1_el1) & TTBR_CNP_BIT);
|
|
|
|
/* Normal Tagged memory type at the corresponding MAIR index */
|
|
sysreg_clear_set(mair_el1,
|
|
MAIR_ATTRIDX(MAIR_ATTR_MASK, MT_NORMAL_TAGGED),
|
|
MAIR_ATTRIDX(MAIR_ATTR_NORMAL_TAGGED,
|
|
MT_NORMAL_TAGGED));
|
|
|
|
write_sysreg_s(KERNEL_GCR_EL1, SYS_GCR_EL1);
|
|
|
|
/*
|
|
* If GCR_EL1.RRND=1 is implemented the same way as RRND=0, then
|
|
* RGSR_EL1.SEED must be non-zero for IRG to produce
|
|
* pseudorandom numbers. As RGSR_EL1 is UNKNOWN out of reset, we
|
|
* must initialize it.
|
|
*/
|
|
rgsr = (read_sysreg(CNTVCT_EL0) & SYS_RGSR_EL1_SEED_MASK) <<
|
|
SYS_RGSR_EL1_SEED_SHIFT;
|
|
if (rgsr == 0)
|
|
rgsr = 1 << SYS_RGSR_EL1_SEED_SHIFT;
|
|
write_sysreg_s(rgsr, SYS_RGSR_EL1);
|
|
|
|
/* clear any pending tag check faults in TFSR*_EL1 */
|
|
write_sysreg_s(0, SYS_TFSR_EL1);
|
|
write_sysreg_s(0, SYS_TFSRE0_EL1);
|
|
|
|
local_flush_tlb_all();
|
|
}
|
|
|
|
void mte_suspend_enter(void)
|
|
{
|
|
if (!system_supports_mte())
|
|
return;
|
|
|
|
/*
|
|
* The barriers are required to guarantee that the indirect writes
|
|
* to TFSR_EL1 are synchronized before we report the state.
|
|
*/
|
|
dsb(nsh);
|
|
isb();
|
|
|
|
/* Report SYS_TFSR_EL1 before suspend entry */
|
|
mte_check_tfsr_el1();
|
|
}
|
|
|
|
void mte_suspend_exit(void)
|
|
{
|
|
if (!system_supports_mte())
|
|
return;
|
|
|
|
mte_cpu_setup();
|
|
}
|
|
|
|
long set_mte_ctrl(struct task_struct *task, unsigned long arg)
|
|
{
|
|
u64 mte_ctrl = (~((arg & PR_MTE_TAG_MASK) >> PR_MTE_TAG_SHIFT) &
|
|
SYS_GCR_EL1_EXCL_MASK) << MTE_CTRL_GCR_USER_EXCL_SHIFT;
|
|
|
|
if (!system_supports_mte())
|
|
return 0;
|
|
|
|
if (arg & PR_MTE_TCF_ASYNC)
|
|
mte_ctrl |= MTE_CTRL_TCF_ASYNC;
|
|
if (arg & PR_MTE_TCF_SYNC)
|
|
mte_ctrl |= MTE_CTRL_TCF_SYNC;
|
|
|
|
/*
|
|
* If the system supports it and both sync and async modes are
|
|
* specified then implicitly enable asymmetric mode.
|
|
* Userspace could see a mix of both sync and async anyway due
|
|
* to differing or changing defaults on CPUs.
|
|
*/
|
|
if (cpus_have_cap(ARM64_MTE_ASYMM) &&
|
|
(arg & PR_MTE_TCF_ASYNC) &&
|
|
(arg & PR_MTE_TCF_SYNC))
|
|
mte_ctrl |= MTE_CTRL_TCF_ASYMM;
|
|
|
|
task->thread.mte_ctrl = mte_ctrl;
|
|
if (task == current) {
|
|
preempt_disable();
|
|
mte_update_sctlr_user(task);
|
|
mte_update_gcr_excl(task);
|
|
update_sctlr_el1(task->thread.sctlr_user);
|
|
preempt_enable();
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
long get_mte_ctrl(struct task_struct *task)
|
|
{
|
|
unsigned long ret;
|
|
u64 mte_ctrl = task->thread.mte_ctrl;
|
|
u64 incl = (~mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) &
|
|
SYS_GCR_EL1_EXCL_MASK;
|
|
|
|
if (!system_supports_mte())
|
|
return 0;
|
|
|
|
ret = incl << PR_MTE_TAG_SHIFT;
|
|
if (mte_ctrl & MTE_CTRL_TCF_ASYNC)
|
|
ret |= PR_MTE_TCF_ASYNC;
|
|
if (mte_ctrl & MTE_CTRL_TCF_SYNC)
|
|
ret |= PR_MTE_TCF_SYNC;
|
|
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Access MTE tags in another process' address space as given in mm. Update
|
|
* the number of tags copied. Return 0 if any tags copied, error otherwise.
|
|
* Inspired by __access_remote_vm().
|
|
*/
|
|
static int __access_remote_tags(struct mm_struct *mm, unsigned long addr,
|
|
struct iovec *kiov, unsigned int gup_flags)
|
|
{
|
|
struct vm_area_struct *vma;
|
|
void __user *buf = kiov->iov_base;
|
|
size_t len = kiov->iov_len;
|
|
int ret;
|
|
int write = gup_flags & FOLL_WRITE;
|
|
|
|
if (!access_ok(buf, len))
|
|
return -EFAULT;
|
|
|
|
if (mmap_read_lock_killable(mm))
|
|
return -EIO;
|
|
|
|
while (len) {
|
|
unsigned long tags, offset;
|
|
void *maddr;
|
|
struct page *page = NULL;
|
|
|
|
ret = get_user_pages_remote(mm, addr, 1, gup_flags, &page,
|
|
&vma, NULL);
|
|
if (ret <= 0)
|
|
break;
|
|
|
|
/*
|
|
* Only copy tags if the page has been mapped as PROT_MTE
|
|
* (PG_mte_tagged set). Otherwise the tags are not valid and
|
|
* not accessible to user. Moreover, an mprotect(PROT_MTE)
|
|
* would cause the existing tags to be cleared if the page
|
|
* was never mapped with PROT_MTE.
|
|
*/
|
|
if (!(vma->vm_flags & VM_MTE)) {
|
|
ret = -EOPNOTSUPP;
|
|
put_page(page);
|
|
break;
|
|
}
|
|
WARN_ON_ONCE(!test_bit(PG_mte_tagged, &page->flags));
|
|
|
|
/* limit access to the end of the page */
|
|
offset = offset_in_page(addr);
|
|
tags = min(len, (PAGE_SIZE - offset) / MTE_GRANULE_SIZE);
|
|
|
|
maddr = page_address(page);
|
|
if (write) {
|
|
tags = mte_copy_tags_from_user(maddr + offset, buf, tags);
|
|
set_page_dirty_lock(page);
|
|
} else {
|
|
tags = mte_copy_tags_to_user(buf, maddr + offset, tags);
|
|
}
|
|
put_page(page);
|
|
|
|
/* error accessing the tracer's buffer */
|
|
if (!tags)
|
|
break;
|
|
|
|
len -= tags;
|
|
buf += tags;
|
|
addr += tags * MTE_GRANULE_SIZE;
|
|
}
|
|
mmap_read_unlock(mm);
|
|
|
|
/* return an error if no tags copied */
|
|
kiov->iov_len = buf - kiov->iov_base;
|
|
if (!kiov->iov_len) {
|
|
/* check for error accessing the tracee's address space */
|
|
if (ret <= 0)
|
|
return -EIO;
|
|
else
|
|
return -EFAULT;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Copy MTE tags in another process' address space at 'addr' to/from tracer's
|
|
* iovec buffer. Return 0 on success. Inspired by ptrace_access_vm().
|
|
*/
|
|
static int access_remote_tags(struct task_struct *tsk, unsigned long addr,
|
|
struct iovec *kiov, unsigned int gup_flags)
|
|
{
|
|
struct mm_struct *mm;
|
|
int ret;
|
|
|
|
mm = get_task_mm(tsk);
|
|
if (!mm)
|
|
return -EPERM;
|
|
|
|
if (!tsk->ptrace || (current != tsk->parent) ||
|
|
((get_dumpable(mm) != SUID_DUMP_USER) &&
|
|
!ptracer_capable(tsk, mm->user_ns))) {
|
|
mmput(mm);
|
|
return -EPERM;
|
|
}
|
|
|
|
ret = __access_remote_tags(mm, addr, kiov, gup_flags);
|
|
mmput(mm);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int mte_ptrace_copy_tags(struct task_struct *child, long request,
|
|
unsigned long addr, unsigned long data)
|
|
{
|
|
int ret;
|
|
struct iovec kiov;
|
|
struct iovec __user *uiov = (void __user *)data;
|
|
unsigned int gup_flags = FOLL_FORCE;
|
|
|
|
if (!system_supports_mte())
|
|
return -EIO;
|
|
|
|
if (get_user(kiov.iov_base, &uiov->iov_base) ||
|
|
get_user(kiov.iov_len, &uiov->iov_len))
|
|
return -EFAULT;
|
|
|
|
if (request == PTRACE_POKEMTETAGS)
|
|
gup_flags |= FOLL_WRITE;
|
|
|
|
/* align addr to the MTE tag granule */
|
|
addr &= MTE_GRANULE_MASK;
|
|
|
|
ret = access_remote_tags(child, addr, &kiov, gup_flags);
|
|
if (!ret)
|
|
ret = put_user(kiov.iov_len, &uiov->iov_len);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t mte_tcf_preferred_show(struct device *dev,
|
|
struct device_attribute *attr, char *buf)
|
|
{
|
|
switch (per_cpu(mte_tcf_preferred, dev->id)) {
|
|
case MTE_CTRL_TCF_ASYNC:
|
|
return sysfs_emit(buf, "async\n");
|
|
case MTE_CTRL_TCF_SYNC:
|
|
return sysfs_emit(buf, "sync\n");
|
|
case MTE_CTRL_TCF_ASYMM:
|
|
return sysfs_emit(buf, "asymm\n");
|
|
default:
|
|
return sysfs_emit(buf, "???\n");
|
|
}
|
|
}
|
|
|
|
static ssize_t mte_tcf_preferred_store(struct device *dev,
|
|
struct device_attribute *attr,
|
|
const char *buf, size_t count)
|
|
{
|
|
u64 tcf;
|
|
|
|
if (sysfs_streq(buf, "async"))
|
|
tcf = MTE_CTRL_TCF_ASYNC;
|
|
else if (sysfs_streq(buf, "sync"))
|
|
tcf = MTE_CTRL_TCF_SYNC;
|
|
else if (cpus_have_cap(ARM64_MTE_ASYMM) && sysfs_streq(buf, "asymm"))
|
|
tcf = MTE_CTRL_TCF_ASYMM;
|
|
else
|
|
return -EINVAL;
|
|
|
|
device_lock(dev);
|
|
per_cpu(mte_tcf_preferred, dev->id) = tcf;
|
|
device_unlock(dev);
|
|
|
|
return count;
|
|
}
|
|
static DEVICE_ATTR_RW(mte_tcf_preferred);
|
|
|
|
static int register_mte_tcf_preferred_sysctl(void)
|
|
{
|
|
unsigned int cpu;
|
|
|
|
if (!system_supports_mte())
|
|
return 0;
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC;
|
|
device_create_file(get_cpu_device(cpu),
|
|
&dev_attr_mte_tcf_preferred);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
subsys_initcall(register_mte_tcf_preferred_sysctl);
|