Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796159978; b=wRJhwaXpaka55sY2MsfSjD4ctqj4lhFbHmo3ClAUsyv/ZVswQqtdhX+RQV4dCHxNBYev yJNAcscUHrILOvaN401p3Acnn1oPfzfXJnH0wJdzWN0MM08enuY+uySDDogO38wl0tV6C e4hlEZsE2d592RTCEM3xWEeB/yA1hLcVq6SAedsmHp20vmG9CzQ404DB632v1Z85l7+GH 0Hn6YIi6MOI/AyA8dHmWagTFF/J3ZrNf6+v325xUWRGaRR3kJICvaWwX0fuDXrc1oxciG zw78OCWU6sizfavpE9/a3OUgnpFRIrib3kNBE40ahsxlOhIHAzME1q+U1rBavMman3Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796159978; 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=R++RAFKwc5YxlxeKm8JDzsG/mxpHnqtj+2TGtYIl1Ek=; b=WdWSm5dJsRHctH+UndLWLRBpnwyYPRMFfoR+fdUx7CTZaltT/13IPb4dPR4g9C5K+B50 vUR4jkv40/QAV7x6BniQGrVhJhS64R+X1XUCyBTHi5qs191nigB0565Lo425BDJDZMZYJ dB/6+8ZSMgnFSp+0yzZI2W3iHCVnqYfmO62oA/8ZiPK+IBGEvRkemsP7LEqZVDY4J2YWn 4kWiYCrYz0LIz6YxT0aJR6FJgTI9MzS3dyaeRwCHNkoyR7tGijWbiPYuGuce/pLmZ0H54 WGP+QdfZURdyT6myv/Uf7uoT51O4ITDLgUaj9enI8W5H5iGMHb2wY9eExaUD3ttaAgg== 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=jtHZU+2T; arc=none smtp.remote-ip=209.85.167.198 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.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 3A710606A31A for ; Tue, 1 Dec 2026 21:19:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3A710606A31A 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=jtHZU+2T Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-574f8578e48so10750129b6e.2 for ; Tue, 01 Dec 2026 13:19:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796159976; x=1796764776; 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=R++RAFKwc5YxlxeKm8JDzsG/mxpHnqtj+2TGtYIl1Ek=; b=jtHZU+2Tn5SFgQLwHxaMN0XyCZVoATlfjER0EJX9inkkAl2h0wnH/WMulnNLyuX+yl Lsk4zz0HitoAEggX2OD4v00yO40BISoj952qUVr94MyOqP6giTIvebHVHAi1U3uoX+gJ 1rgIZqcfrZ7AmPrl+kzMehy+7MJQVwy3BVVRXJBhHXfA0crDTo4m8hh1EcLjVXsj/Gej k/uEqYK2TVSImGGyAMrOy7Zab8LYiDULk7BUknH3PNcF9Y6z1lGfOBua9ezA+Fcb6fA/ uaT/81ZeZIZakz2coAls7U6F5qIDbcPCLnvi8Lt2ruxOImAmlEuScwLwgbUtMoPcs51k s5Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796159976; x=1796764776; 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=R++RAFKwc5YxlxeKm8JDzsG/mxpHnqtj+2TGtYIl1Ek=; b=nshb7/vHpKfdkRXzt2u9Rf+235o3jpEZETclUI8Z9C/yYcXriJU0+575BhUuzwYPFY jMaoePK8giia/t6Kx8zNdnNgsQ+dLvVMmiR+rtOpNiUnWRILUzTgZ/ZhX6sn2/42yQtJ L/ZPmmcZROh/M6/p68SpUiZAw4lvlfwEEVyDBJP1xGba3Y/M9ulALbRt5GkqWEvUXuUs DfC7NBEliyCTt52bSbBC3yPiUMcU4gpFrMvQegBP9cEL8L84Z4pQL1aBJSf3jT0AUfkC zhzYU2Nt1C1sWxxbyGhyADENtEx9oJkVGIUdxL8RNx0DdW8PLqBaBE71HU7UGPXkE91U OwqA== X-Forwarded-Encrypted: i=1; AL+hErGGoakqY3OSd2IfkaL5lmBzNxABjTqHdeWwAkcfaM/oardWHyxo37i+SHenseZPS1DfDKA=@vger.kernel.org X-Gm-Message-State: APuU4Z/5kSIKea3fBvIS1uOi3fG9HK/T2vN7mpy92pDjothjO4feeF/+ FNMyE9Kz+z2DSENEnByfSj6JjJcw0cLsiyF4siJrfOZjd90rJVxXTLB3UPVabYBk2+doLibRtOc a7LxGUMYEUChfAW0gQI9ZXQ== X-Received: from ilei3-n1.prod.google.com ([2002:a05:6e02:61c3:10b0:520:6d86:da98]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:f08:b0:4e9:7d56:846f with SMTP id 5614622812f47-57a0b041513mr41403573b6e.41.1796159975954; Tue, 01 Dec 2026 13:19:35 -0800 (PST) Date: Tue, 1 Dec 2026 21:19:26 +0000 X-Mailing-List: bpf@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: <20261201211926.3463573-1-jthoughton@google.com> Subject: [PATCH] mm/maccess: Check nmi_uaccess_okay() in user nofault helpers From: James Houghton To: David Hildenbrand , "Liam R. Howlett" , Andrew Morton Cc: stable@vger.kernel.org, Steven Rostedt , Masami Hiramatsu , Ingo Molnar , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , linux-mm@kvack.org, bpf@vger.kernel.org, James Houghton , Sashiko Content-Type: text/plain; charset="UTF-8" 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 Closes: https://sashiko.dev/bug/linux-5fcfa8c4-b00c-4ee4-95c6-4b24ec497bca Signed-off-by: James Houghton --- 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