Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: James Houghton <jthoughton@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	 Arnd Bergmann <arnd@arndb.de>
Cc: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Jim Mattson <jmattson@google.com>, Jens Axboe <axboe@kernel.dk>,
	 David Hildenbrand <david@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	 Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org,  Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,  Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	 Madhavan Srinivasan <maddy@linux.ibm.com>,
	Chris Zankel <chris@zankel.net>,
	 Max Filippov <jcmvbkbc@gmail.com>,
	Dinh Nguyen <dinguyen@kernel.org>,
	 Simon Schuster <schuster.simon@siemens-energy.com>,
	David Sterba <dsterba@suse.com>,
	 Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	 Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	 Matt Bobrowski <matt@bobrowski.net>,
	Peter Zijlstra <peterz@infradead.org>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	 Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	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 <jthoughton@google.com>
Subject: [PATCH v2 00/12] uaccess: Refuse and warn on user accesses when current->mm is NULL
Date: Wed,  2 Dec 2026 02:39:07 +0000	[thread overview]
Message-ID: <20261202023920.595207-1-jthoughton@google.com> (raw)

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



             reply	other threads:[~2026-12-02  2:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  2:39 James Houghton [this message]
2026-12-02  2:39 ` [PATCH v2 01/12] xtensa, nios2: Don't use __copy_{from,to}_user() in __{get,put}_user() James Houghton
2026-12-02  2:39 ` [PATCH v2 02/12] btrfs: Don't copy encoded read data to user memory when canceled James Houghton
2026-12-02  2:39 ` [PATCH v2 03/12] bpf: Fail sleepable user copy helpers when current->mm is NULL James Houghton
2026-12-02  2:39 ` [PATCH v2 04/12] stacktrace: Don't unwind the user stack without an mm James Houghton
2026-12-02  2:39 ` [PATCH v2 05/12] perf/core: " James Houghton
2026-12-02  2:39 ` [PATCH v2 06/12] bpf: Don't read user memory without an mm in tracing helpers James Houghton
2026-12-02  2:39 ` [PATCH v2 07/12] tracing: Don't read user memory without an mm James Houghton
2026-12-02  2:39 ` [PATCH v2 08/12] powerpc/perf: Don't read user memory without an mm in BHRB decoding James Houghton
2026-12-02  2:39 ` [PATCH v2 09/12] uaccess: Document that the user nofault helpers need an mm James Houghton
2026-12-02  2:39 ` [PATCH v2 10/12] uaccess: Refuse and warn in access_ok() when current->mm is NULL James Houghton
2026-12-02  2:53   ` sashiko-bot
2026-12-02  2:39 ` [PATCH v2 11/12] uaccess: Refuse and warn on masked user accesses without an mm James Houghton
2026-12-02  2:39 ` [PATCH v2 12/12] uaccess: Refuse and warn on unchecked " James Houghton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202023920.595207-1-jthoughton@google.com \
    --to=jthoughton@google.com \
    --cc=acme@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=arnd@arndb.de \
    --cc=ast@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=chris@zankel.net \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@kernel.org \
    --cc=dinguyen@kernel.org \
    --cc=dsterba@suse.com \
    --cc=eddyz87@gmail.com \
    --cc=io-uring@vger.kernel.org \
    --cc=jcmvbkbc@gmail.com \
    --cc=jmattson@google.com \
    --cc=kpsingh@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=matt@bobrowski.net \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=schuster.simon@siemens-energy.com \
    --cc=seanjc@google.com \
    --cc=tglx@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox