From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179166; b=I2yi/BkaLyNDCHQkq8i5EZV7bUdBf/dreBy2tTCEcuCdoTA6FbOrxHrdHQOkvF8lLTiK lCxUr6nKZMRKxLwqD5Ga6Z6xMats65UvXHeM1Boj3fU3fjqJQxCxRJoMdD8Ex8oIUQoqV cSTYHB7zLs22y1wo6C0FISZfQNCu1yBBBLL0xxJt5qrSMjeNOfe4n2aFk4kKgKHHwhCTc hlW+8F+91OhjK9Hnmdj8H2+9kVDQSCg40HykfuhpASOXlOpXXJjV71YO0rDrXdTFO/o2U te/4HvNJYDBeL6+NwhXbT42evnd1KRoqjNOpTzS1A1rFN6R6I13O+HUMp4I29vkK7CQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179166; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=7538wh6lHdPxkv7SFvxWaBwBgEWsRXn/rkng1lbJ71E=; b=B99D+sdFxXngahI9W8tXhZ1h+aKbegBdBjq77l8ZrfJ8CiukZfSXY7BMh0f4hDPH5dz4 bx0USQ5SvJh04i66Ufx99rOi/wbYvFGhd3HpWf1UzOQm7Bpyaq/GzLJ78stUSh50gducD XVGUR7/SOF5M4lAggpL/VGglyp/XvTPzZZSqK/SmLTh50rUvZhCxYykp5VmgUuQqvhfB8 v7iOxCZtR0pa2e0Tzlq77D6FtcYE9J7oOaPlBSU8N4qt87kZfgtO+fyimWaHsVT5Vq13L T3NeUxVT+/UuTMa2kwsWJ+NovpLihEQrTbTHbVVX95/h5szWRbvgRshVujdePV6mbwQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=hZVJ40F8; arc=none smtp.remote-ip=209.85.214.198 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DDBA606A31A for ; Wed, 2 Dec 2026 02:39:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1DDBA606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=hZVJ40F8 Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2f074b87307so89584985ad.2 for ; Tue, 01 Dec 2026 18:39:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179164; x=1796783964; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=7538wh6lHdPxkv7SFvxWaBwBgEWsRXn/rkng1lbJ71E=; b=hZVJ40F88L0AADgAmdTgwrBQ9J+zHRt0dmZ1dmvPAKWBLmVLbTUJbawFaaJt4tFa9x v/TTvCnUR25v1parAkCyjXaUSHUF/fwdla4NybLwZENacKAmRjWE5qzA/Q9W/3rHCpfR qbZsJfu8s78zfTVkc4PHQ/VRmgWv7HjWj2GvEvmxetm80kBNlpm7oSvbx0ov9j157fpW ZgoKYdrzfSS40CO1Gc5TjgYcStxpbT4LwBf2hik04N+HeuR2Xtdu7pi9KflakdiXcets J37o2Q7wzIHEuz8G+OySMcMIs8vsE37JUWAvK4eJgGlguPnGz0bJTWY9E5d5G1bC2Obr UMcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179164; x=1796783964; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=7538wh6lHdPxkv7SFvxWaBwBgEWsRXn/rkng1lbJ71E=; b=UQy65Jkt69tCXcAp5L5+j+1H+BupDJag+eqGCEo19sE0MKHLqOzerjU8o5hcJ7Slc9 Ks7Q+/Pns+9Gyk1HwGICOWeAdPad/2jDecATWYx7G+1cKbuNQKKy1/e5T2HGQaDslMsm yjh4/VlET6hJ1Yq05ybItYIFujNvX88vqz7dgiFEkSJ7DC3D6mdv5rg5hFViibaiZzOS JITbdevXyfl/4C0OPQESpHeAwalukw8+HarU+hj975dBh01Ufhu6iFZmTGaK8KsYDgPd oRUi/yjXUAIzupVhxl9MWCsFos03gJSNbD9EcQuH6NYUMRkEYuFxzFPU4tvOFYnyRsgM kMrA== X-Forwarded-Encrypted: i=1; AL+hErEFvqbsTxOjEeEtOx45AeQyotkuUNqPz4l41YoEoKIRgIIK75EmolfKcPjmP7iUSACUVD3T1JBUXA==@vger.kernel.org X-Gm-Message-State: APuU4Z8aQHTr7ek1n/lJRX7osyhvv4n4qi4EqqfeesDpBz2dcyV6G1DN 4UMxjehr2sAQKLUjLJkGstE+t4POvbdTy3mQroMOdEHChbrnVCjsEl2vBSt0A3KFRSRkm4GTdZm 9fplpo2e+G9pCycEcQx776A== X-Received: from plfn18.prod.google.com ([2002:a17:902:e552:b0:2f0:424a:82c6]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:19cb:b0:2ed:2afa:9221 with SMTP id d9443c01a7336-2f03f1707f5mr410930735ad.27.1796179163990; Tue, 01 Dec 2026 18:39:23 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:07 +0000 X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-1-jthoughton@google.com> Subject: [PATCH v2 00/12] uaccess: Refuse and warn on user accesses when current->mm is NULL From: James Houghton To: Andrew Morton , Alexander Viro , Arnd Bergmann Cc: Sean Christopherson , Paolo Bonzini , Jim Mattson , Jens Axboe , David Hildenbrand , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Madhavan Srinivasan , Chris Zankel , Max Filippov , Dinh Nguyen , Simon Schuster , David Sterba , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , KP Singh , Matt Bobrowski , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Steven Rostedt , Masami Hiramatsu , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-riscv@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, io-uring@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" Hi everyone, This series comes out of a set of bugs that were found in KVM where the kernel can sometimes write to a random process's user memory[1]. Sean's KVM fixes for those have since been merged, e.g. commit 2520694e6048 ("KVM: Reject user accesses to guest memory if current->mm != kvm->mm"). They are still required with this series, as they also handle the case where the VM is destroyed from another process, which this series can't detect. We hit one of the KVM bugs in production, and it was challenging to debug. This series makes such bugs, where the kernel is doing uaccess without an mm, much more obvious. The beginning of this series includes fixes for similar bugs found through static analysis with Claude Opus 5.5. The main change in v2 is where the new check lives. Following Arnd's suggestion[2], it is now in access_ok() itself, plus the masked user access paths and the unchecked __copy_{from,to}_user{,_inatomic}() accessors, instead of in each generic copy helper. This covers more (e.g. get_user(), put_user() and clear_user() on most architectures), and makes most new user access code get the check automatically. See "Changes in v2" below for the full list. I've also taken the liberty of dropping the "RFC" label from this series. :) The Cc list is wide because the fixes at the start of the series touch several subsystems: xtensa/nios2 (patch 1), btrfs (patch 2), BPF (patches 3 and 6), tracing (patches 4 and 7), perf (patch 5) and powerpc (patch 8). They're included because the new warning in patches 10-12 would otherwise fire on those paths. The core uaccess change is in patches 9-12. == Introduction == Accessing user memory from a task that has no mm is always a bug. This can happen from a kernel thread that is not using kthread_use_mm(), or from a task that has already gone through exit_mm(), e.g. when the last reference to a file is dropped at exit and its release handler runs from exit_task_work(). For example, KVM's nVMX code could write to guest memory while a vCPU was being freed during VM destruction[1], and other drivers may have similar bugs. Such an access does not reliably fail. The user portion of the address space belongs to whatever mm happens to be lazily loaded, if any. If the access faults, the fault handler refuses to handle it and the copy fails, but if the page is already mapped, the access silently reads or writes another task's memory. == Fixes == Patches 1-8 fix or prepare places that would access user memory without an mm, or that would otherwise trigger the new warning: - Patch 1 makes xtensa and nios2 implement 8-byte __get_user() and __put_user() with raw_copy_{from,to}_user(), so that the generic kernel nofault accessors, which use them on kernel addresses, don't hit the new check. - Patch 2 fixes btrfs's io_uring encoded read completion, which copies to the submitter's user memory even when io_uring runs it canceled from a kworker, e.g. after the submitter exited. (The equivalent scsi_bsg fix from v1 has been merged.) - Patch 3 makes the sleepable BPF user copy helpers fail when current->mm is NULL, e.g. for bpf_wq callbacks or lsm.s programs running from kernel threads or exiting tasks. - Patches 4 and 5 make stack_trace_save_user() (ftrace's userstacktrace option) and get_perf_callchain() (bpf_get_task_stack() on the current task) use is_user_task(), so that they don't unwind the user stack of PID 1 before exec, usermode helpers before exec, or tasks that have gone through exit_mm(). - Patches 6-8 make the users of the user nofault helpers that run in whatever context they're attached to (BPF tracing helpers, probe events, event filters, synthetic events and powerpc's BHRB decoding) check for an mm themselves. A missing mm there isn't a kernel bug, so these must not trigger the new warning. Patch 9 documents that requirement in the nofault helpers; their other users run in process context, where a missing mm would be a bug, and now get the warning. == The New WARN == The rest of the series makes user accesses WARN_ON_ONCE() and refuse the access when current->mm is NULL: - Patch 10 adds the check to access_ok(): the generic version, and the arm64 and riscv ones. __access_ok() stays a plain range check, for callers like copy_from_user_nofault() that handle a missing mm themselves. This covers copy_{to,from}_user() where masked user access isn't used, user_access_begin() and everything built on it, and get_user(), put_user() and clear_user() on the architectures that use access_ok() for them. - Patch 11 adds the check to the callers of mask_user_address() and masked_user_*_access_begin() (x86-64 and powerpc), which don't call access_ok(). There are about a dozen of them. - Patch 12 adds the check to the unchecked accessors, __copy_{from,to}_user() and __copy_{from,to}_user_inatomic(), which rely on an access_ok() done beforehand, possibly from a different context. This is the path KVM's guest memory accessors took. I'm preparing a separate series that removes these accessors, as Arnd suggested, after which these checks can go away. I've uploaded a draft here[3]. With defconfig + KVM, patches 10-12 grow vmlinux by +11,702 B (+0.027%) on x86_64 and +65,428 B (+0.15%) on arm64. v1's last patch, which moved the warning out of line, is dropped: it would save ~25 KB of __bug_table on arm64 (but add ~4 KB of text, and ~3 KB overall on x86_64), at the cost of warning only once system-wide, and of fuzzers like syzkaller reporting every such bug as a warning in the same helper, instead of in its caller. What's still not covered: x86's get_user()/put_user(), which do their own range check in assembly, a few architecture-specific paths that call __access_ok() directly (e.g. on alpha and sparc32), and arch code that calls __get_user()/__put_user() without access_ok(), mostly trap handlers for user-mode faults. I haven't found any of those that can run without an mm. The check also cannot catch a valid mm that isn't the one the caller expects, e.g. when a file's release or ioctl handlers run in a different process than the one that created it. Code that keeps user addresses belonging to a specific mm still has to compare current->mm against that mm itself, as KVM now does[1]. == Performance == I measured the overhead of the new check with syscall microbenchmarks on a two-socket Haswell server (Xeon E5-2696 v3, PTI enabled). I compared v7.4-rc5 with and without this series. Each benchmark runs in a tight loop pinned to one CPU, 10 times. The numbers are the median kernel-mode cycles and instructions per operation, from perf stat -e cycles:k,instructions:k: v7.4-rc5 + this series benchmark cycles insns cycles insns getppid() (no user access) 1293 266 -0.9% +0 read(/dev/zero, 1B) 1434 501 -1.0% +5 read(/dev/zero, 64B) 1442 509 -1.4% +5 pipe write + read, 8B 3165 1627 -1.2% +8 fstat() 1547 705 +1.7% +3 uname() 1439 484 +0.6% +3 getrusage() 1928 1283 -0.5% +3 rt_sigaction() 1348 492 +0.7% +7 poll(), 64 fds 6816 8000 +1.1% +11 readv(/dev/zero), 16 x 8B 1913 1921 +1.7% +100 signal delivery + sigreturn 7038 4329 -0.2% +21 pipe ping-pong, 2 tasks/CPU 14634 12985 -0.2% +19 The check adds about 3-5 instructions per user access (loading current->mm, testing it and a branch that isn't taken), so the readv() case, which makes 16+ small user accesses, adds about 100. I couldn't measure any change in cycles: the deltas range from -1.4% to +1.7%, with no consistent sign, and are within the run-to-run spread (0.3-5%). Wall time didn't change measurably either. For comparison, I also measured making the x86 lazy TLB mode eager, i.e. making enter_lazy_tlb() switch to init_mm, so that a task without an mm couldn't reach any user mapping at all. I tested it with and without an exception for the idle task, and counted system-wide so that work done in the idle task and in kernel threads is included: v7.4-rc5 eager eager, benchmark cycles insns idle lazy pipe ping-pong, 2 CPUs 28814 18794 +5.8%/+408 0.0%/+26 1us nanosleep() loop 13775 8616 +10.0%/+244 +3.5%/+51 Each time a CPU goes idle and wakes up, the eager switch costs about 200 instructions and 900-1400 cycles, even though with PTI the CR3 write on kernel entry is already paid. Keeping the idle task lazy recovers almost all of that. Switching to and from a kworker costs a similar amount per switch, but that was too small to resolve with a schedule_on_each_cpu() benchmark (under 1% of the ~180k cycles per operation). Without PTI, the eager switch would cost relatively more. == Testing == Build-tested on x86_64 (defconfig + KVM, defconfig without IA32_EMULATION, and allnoconfig), i386 (defconfig), arm64 (defconfig and allnoconfig), riscv (defconfig), powerpc (ppc64_defconfig with KVM HV), arm (multi_v7_defconfig), s390 (defconfig) and loongarch (defconfig). As in v1, I have a reproducer only for the bug that patch 3 fixes: a bpf_wq callback calling bpf_copy_from_user(), which triggers the new warning without the patch, and fails with -EFAULT with it. I ran it on v1; the code in patch 3 hasn't changed since. The other fixes are only build-tested. Let me know if you'd like to see reproducers for them. I've been fuzzing this version with syzkaller on x86_64 (syzbot's upstream KASAN config, nested VMX enabled) for about four days in total, before and after rebasing onto v7.4-rc5 (over 35 million programs). syzkaller hasn't triggered the new warning, i.e. it hasn't found any other path that accesses user memory without an mm, despite the check now being in access_ok(). == Changes in v2 == - Move the check into access_ok(), the masked user access paths and the unchecked accessors, instead of the generic copy helpers, which also covers user_access_begin() without a separate check (Arnd). - Instead of making the user nofault helpers fail quietly without an mm, check for it in their callers that can run in any context (BPF, tracing, powerpc BHRB decoding), and document the requirement, so the remaining process-context users still warn (Arnd). - Drop the scsi_bsg fix, which has been merged as commit 2b2bb55990cf ("scsi: bsg: Don't copy sense data to user memory when canceled"). - Add Jens's Reviewed-by to "btrfs: Don't copy encoded read data to user memory when canceled". - Add "xtensa, nios2: Don't use __copy_{from,to}_user() in __{get,put}_user()" (reported by the BPF CI AI review[4]). - Fix the examples in the changelog of "bpf: Fail sleepable user copy helpers when current->mm is NULL" (BPF CI AI review). - Fix the Fixes: tag of "stacktrace: Don't unwind the user stack without an mm". - Clarify the changelog of "perf/core: Don't unwind the user stack without an mm" (Peter). - Drop "uaccess: Move the NULL mm warning out of line", which mostly saved __bug_table space on arm64, so that each offending call site warns, and is reported, separately. - Rebase onto v7.4-rc5. v1: https://lore.kernel.org/all/20261026215226.3713941-1-jthoughton@google.com/ Please let me know what you think. Thank you! [1] https://lore.kernel.org/kvm/20261001202234.3794060-1-seanjc@google.com [2] https://lore.kernel.org/lkml/32fad21b-5bad-4640-97df-ef0f638cd9e9@app.fastmail.com/ [3] https://github.com/48ca/linux/tree/no-unchecked-uaccess [4] https://lore.kernel.org/lkml/b9959bd7711dee7a83a07b012b5967980d178c95eae90adeebebcb6040fc526f@mail.kernel.org/ James Houghton (12): xtensa, nios2: Don't use __copy_{from,to}_user() in __{get,put}_user() btrfs: Don't copy encoded read data to user memory when canceled bpf: Fail sleepable user copy helpers when current->mm is NULL stacktrace: Don't unwind the user stack without an mm perf/core: Don't unwind the user stack without an mm bpf: Don't read user memory without an mm in tracing helpers tracing: Don't read user memory without an mm powerpc/perf: Don't read user memory without an mm in BHRB decoding uaccess: Document that the user nofault helpers need an mm uaccess: Refuse and warn in access_ok() when current->mm is NULL uaccess: Refuse and warn on masked user accesses without an mm uaccess: Refuse and warn on unchecked user accesses without an mm arch/arm64/include/asm/uaccess.h | 7 ++++++ arch/nios2/include/asm/uaccess.h | 2 +- arch/powerpc/include/asm/futex.h | 4 ++++ arch/powerpc/lib/pmem.c | 2 ++ arch/powerpc/perf/core-book3s.c | 7 ++++++ arch/riscv/include/asm/uaccess.h | 3 ++- arch/x86/include/asm/uaccess_64.h | 6 ++++++ arch/x86/lib/usercopy_64.c | 2 ++ arch/xtensa/include/asm/uaccess.h | 4 ++-- fs/btrfs/ioctl.c | 10 +++++++++ include/asm-generic/access_ok.h | 10 ++++++++- include/linux/uaccess.h | 34 ++++++++++++++++++++++++------ include/linux/uaccess_mm.h | 33 +++++++++++++++++++++++++++++ kernel/bpf/helpers.c | 28 ++++++++++++++++++++---- kernel/events/callchain.c | 12 +++++++---- kernel/stacktrace.c | 8 +++++-- kernel/trace/bpf_trace.c | 28 +++++++++++++++++++++--- kernel/trace/trace_events_filter.c | 8 +++++++ kernel/trace/trace_events_synth.c | 15 ++++++++++--- kernel/trace/trace_probe_kernel.h | 16 +++++++++++++- lib/iov_iter.c | 3 +++ lib/strncpy_from_user.c | 3 +++ lib/strnlen_user.c | 3 +++ mm/maccess.c | 16 ++++++++++++++ 24 files changed, 235 insertions(+), 29 deletions(-) create mode 100644 include/linux/uaccess_mm.h base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.56.1.911.g883d8bcb20-goog