* [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers
@ 2026-12-01 21:19 James Houghton
2026-12-02 1:00 ` Alexei Starovoitov
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: James Houghton @ 2026-12-01 21:19 UTC (permalink / raw)
To: David Hildenbrand, Liam R. Howlett, Andrew Morton
Cc: stable, Steven Rostedt, Masami Hiramatsu, Ingo Molnar,
Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann, linux-mm,
bpf, James Houghton, Sashiko
Unlike copy_from_user_nofault(), which checks nmi_uaccess_okay() before
accessing user memory, copy_to_user_nofault(), strncpy_from_user_nofault(),
and strnlen_user_nofault() do not.
On x86, nmi_uaccess_okay() verifies that cpu_tlbstate.loaded_mm matches
current->mm. Even when current->mm is non-NULL, the active CR3 can point to
a different address space in several windows:
- during context_switch() between switch_mm_irqs_off() and switch_to(),
- during kthread_use_mm() between assigning tsk->mm and completing
switch_mm_irqs_off(), and
- while a temporary mm (such as text_poke_mm or efi_mm) is loaded via
use_temporary_mm().
When an NMI, kprobe, tracepoint, or BPF tracing program invokes
strncpy_from_user_nofault() or strnlen_user_nofault() (for example via
bpf_probe_read_user_str() or fetch_store_string_user()) during one of these
windows, the access proceeds against the wrong page tables. While
bpf_probe_write_user() already checks nmi_uaccess_okay() itself,
copy_to_user_nofault() is exported and should provide the same guarantee as
the rest of the user nofault helpers.
Check nmi_uaccess_okay() in copy_to_user_nofault(),
strncpy_from_user_nofault(), and strnlen_user_nofault(), failing with
-EFAULT (or 0 for strnlen_user_nofault()) if the loaded mm does not match
current->mm.
Fixes: 3d7081822f7f ("uaccess: Add non-pagefault user-space read functions")
Fixes: 1d1585ca0f48 ("uaccess: Add non-pagefault user-space write function")
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/bug/linux-5fcfa8c4-b00c-4ee4-95c6-4b24ec497bca
Signed-off-by: James Houghton <jthoughton@google.com>
---
Sashiko found this issue after looking at the uaccess hardening series
I posted recently[1] (v2 coming soon!). Some discussion about why this is
not a big security concern:
- No cross-address-space write primitive: bpf_probe_write_user() already
checks nmi_uaccess_okay() and in_interrupt() before calling
copy_to_user_nofault() (since v5.2, commit c7b6f29b6257). All other
in-tree callers of copy_to_user_nofault() across v5.10, v5.15, v6.1,
v6.6, v6.12, v6.18, and mainline run with loaded_mm == current->mm (or
are powerpc-only where nmi_uaccess_okay() is a no-op). Thus there is no
in-tree path to write into text_poke_mm (W^X bypass) or another task's
mm.
- Privileged readers only: all reachable callers of
strncpy_from_user_nofault() and strnlen_user_nofault() are privileged
tracing/BPF interfaces (CAP_SYS_ADMIN, or CAP_BPF + CAP_PERFMON), which
can already read arbitrary kernel memory outside of confidentiality
lockdown. Unprivileged perf callchain unwinding on x86 uses
copy_from_user_nmi(), which already checks nmi_uaccess_okay().
- Under kernel lockdown (confidentiality mode), bpf_probe_read_kernel*()
is blocked by LOCKDOWN_BPF_READ_KERNEL while bpf_probe_read_user_str()
remains allowed, so reading text_poke_mm or efi_mm lower-half mappings
is a minor confidentiality-lockdown gap (and reading efi_mm 1:1
EFI_MEMORY_MAPPED_IO ranges during an EFI runtime call is a potential
stability hazard), but still requires privileged tracing capabilities.
I used an LLM to help me audit the callers of the broken uaccess
routines to see if this bug was truly sensitive.
[1] https://lore.kernel.org/linux-mm/20261026215226.3713941-1-jthoughton@google.com/
mm/maccess.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/mm/maccess.c b/mm/maccess.c
index 486559d68858..7221cd7b98c5 100644
--- a/mm/maccess.c
+++ b/mm/maccess.c
@@ -150,6 +150,9 @@ long copy_to_user_nofault(void __user *dst, const void *src, size_t size)
{
long ret = -EFAULT;
+ if (!nmi_uaccess_okay())
+ return ret;
+
if (access_ok(dst, size)) {
pagefault_disable();
ret = __copy_to_user_inatomic(dst, src, size);
@@ -188,6 +191,9 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
if (unlikely(count <= 0))
return 0;
+ if (!nmi_uaccess_okay())
+ return -EFAULT;
+
pagefault_disable();
ret = strncpy_from_user(dst, unsafe_addr, count);
pagefault_enable();
@@ -222,6 +228,9 @@ long strnlen_user_nofault(const void __user *unsafe_addr, long count)
{
int ret;
+ if (!nmi_uaccess_okay())
+ return 0;
+
pagefault_disable();
ret = strnlen_user(unsafe_addr, count);
pagefault_enable();
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers
2026-12-01 21:19 [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers James Houghton
@ 2026-12-02 1:00 ` Alexei Starovoitov
2026-12-02 13:32 ` David Hildenbrand (Arm)
2026-12-02 20:40 ` + mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch added to mm-new branch Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Alexei Starovoitov @ 2026-12-02 1:00 UTC (permalink / raw)
To: James Houghton, David Hildenbrand, Liam R. Howlett, Andrew Morton
Cc: stable, Steven Rostedt, Masami Hiramatsu, Ingo Molnar,
Andrii Nakryiko, Daniel Borkmann, linux-mm, bpf, Sashiko
On Tue, Dec 01, 2026 at 01:19 PM James Houghton <jthoughton@google.com> wrote:
> Unlike copy_from_user_nofault(), which checks nmi_uaccess_okay() before
> accessing user memory, copy_to_user_nofault(), strncpy_from_user_nofault(),
> and strnlen_user_nofault() do not.
Acked-by: Alexei Starovoitov <ast@kernel.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers
2026-12-01 21:19 [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers James Houghton
2026-12-02 1:00 ` Alexei Starovoitov
@ 2026-12-02 13:32 ` David Hildenbrand (Arm)
2026-12-02 20:40 ` + mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch added to mm-new branch Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-12-02 13:32 UTC (permalink / raw)
To: James Houghton, Liam R. Howlett, Andrew Morton
Cc: stable, Steven Rostedt, Masami Hiramatsu, Ingo Molnar,
Andrii Nakryiko, Alexei Starovoitov, Daniel Borkmann, linux-mm,
bpf, Sashiko
On 12/1/26 22:19, James Houghton wrote:
> Unlike copy_from_user_nofault(), which checks nmi_uaccess_okay() before
> accessing user memory, copy_to_user_nofault(), strncpy_from_user_nofault(),
> and strnlen_user_nofault() do not.
>
> On x86, nmi_uaccess_okay() verifies that cpu_tlbstate.loaded_mm matches
> current->mm. Even when current->mm is non-NULL, the active CR3 can point to
> a different address space in several windows:
>
> - during context_switch() between switch_mm_irqs_off() and switch_to(),
> - during kthread_use_mm() between assigning tsk->mm and completing
> switch_mm_irqs_off(), and
> - while a temporary mm (such as text_poke_mm or efi_mm) is loaded via
> use_temporary_mm().
>
> When an NMI, kprobe, tracepoint, or BPF tracing program invokes
> strncpy_from_user_nofault() or strnlen_user_nofault() (for example via
> bpf_probe_read_user_str() or fetch_store_string_user()) during one of these
> windows, the access proceeds against the wrong page tables. While
> bpf_probe_write_user() already checks nmi_uaccess_okay() itself,
> copy_to_user_nofault() is exported and should provide the same guarantee as
> the rest of the user nofault helpers.
>
> Check nmi_uaccess_okay() in copy_to_user_nofault(),
> strncpy_from_user_nofault(), and strnlen_user_nofault(), failing with
> -EFAULT (or 0 for strnlen_user_nofault()) if the loaded mm does not match
> current->mm.
>
> Fixes: 3d7081822f7f ("uaccess: Add non-pagefault user-space read functions")
> Fixes: 1d1585ca0f48 ("uaccess: Add non-pagefault user-space write function")
> Cc: stable@vger.kernel.org
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/bug/linux-5fcfa8c4-b00c-4ee4-95c6-4b24ec497bca
> Signed-off-by: James Houghton <jthoughton@google.com>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 4+ messages in thread* + mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch added to mm-new branch
2026-12-01 21:19 [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers James Houghton
2026-12-02 1:00 ` Alexei Starovoitov
2026-12-02 13:32 ` David Hildenbrand (Arm)
@ 2026-12-02 20:40 ` Andrew Morton
2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-02 20:40 UTC (permalink / raw)
To: mm-commits, stable, rostedt, mingo, mhiramat, liam, david, daniel,
ast, andrii, jthoughton, akpm
The patch titled
Subject: mm/maccess: check nmi_uaccess_okay() in user nofault helpers
has been added to the -mm mm-new branch. Its filename is
mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch
This patch will later appear in the mm-new branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews. Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.
The mm-new branch of mm.git is not included in linux-next
If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: James Houghton <jthoughton@google.com>
Subject: mm/maccess: check nmi_uaccess_okay() in user nofault helpers
Date: Tue, 01 Dec 2026 21:19:26 +0000
Unlike copy_from_user_nofault(), which checks nmi_uaccess_okay() before
accessing user memory, copy_to_user_nofault(),
strncpy_from_user_nofault(), and strnlen_user_nofault() do not.
On x86, nmi_uaccess_okay() verifies that cpu_tlbstate.loaded_mm matches
current->mm. Even when current->mm is non-NULL, the active CR3 can point
to a different address space in several windows:
- during context_switch() between switch_mm_irqs_off() and switch_to(),
- during kthread_use_mm() between assigning tsk->mm and completing
switch_mm_irqs_off(), and
- while a temporary mm (such as text_poke_mm or efi_mm) is loaded via
use_temporary_mm().
When an NMI, kprobe, tracepoint, or BPF tracing program invokes
strncpy_from_user_nofault() or strnlen_user_nofault() (for example via
bpf_probe_read_user_str() or fetch_store_string_user()) during one of
these windows, the access proceeds against the wrong page tables. While
bpf_probe_write_user() already checks nmi_uaccess_okay() itself,
copy_to_user_nofault() is exported and should provide the same guarantee
as the rest of the user nofault helpers.
Check nmi_uaccess_okay() in copy_to_user_nofault(),
strncpy_from_user_nofault(), and strnlen_user_nofault(), failing with
-EFAULT (or 0 for strnlen_user_nofault()) if the loaded mm does not match
current->mm.
Link: https://patch.msgid.link/20261201211926.3463573-1-jthoughton@google.com
Fixes: 3d7081822f7f ("uaccess: Add non-pagefault user-space read functions")
Fixes: 1d1585ca0f48 ("uaccess: Add non-pagefault user-space write function")
Signed-off-by: James Houghton <jthoughton@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/bug/linux-5fcfa8c4-b00c-4ee4-95c6-4b24ec497bca
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: <stable@vger.kernel.org>
mm/maccess.c | 9 +++++++++
1 file changed, 9 insertions(+)
--- a/mm/maccess.c~mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers
+++ a/mm/maccess.c
@@ -150,6 +150,9 @@ long copy_to_user_nofault(void __user *d
{
long ret = -EFAULT;
+ if (!nmi_uaccess_okay())
+ return ret;
+
if (access_ok(dst, size)) {
pagefault_disable();
ret = __copy_to_user_inatomic(dst, src, size);
@@ -188,6 +191,9 @@ long strncpy_from_user_nofault(char *dst
if (unlikely(count <= 0))
return 0;
+ if (!nmi_uaccess_okay())
+ return -EFAULT;
+
pagefault_disable();
ret = strncpy_from_user(dst, unsafe_addr, count);
pagefault_enable();
@@ -222,6 +228,9 @@ long strnlen_user_nofault(const void __u
{
int ret;
+ if (!nmi_uaccess_okay())
+ return 0;
+
pagefault_disable();
ret = strnlen_user(unsafe_addr, count);
pagefault_enable();
_
Patches currently in -mm which might be from jthoughton@google.com are
hugetlb-dont-restore-vmemmap-of-non-hvoed-folios-on-bulk-restore-error.patch
mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 20:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 21:19 [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers James Houghton
2026-12-02 1:00 ` Alexei Starovoitov
2026-12-02 13:32 ` David Hildenbrand (Arm)
2026-12-02 20:40 ` + mm-maccess-check-nmi_uaccess_okay-in-user-nofault-helpers.patch added to mm-new branch Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox