From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796159977; b=Couoy6BO7tHZB7f3f+mG1pvz+LL5eBd93FfpAKXaVsH34il2qnAE4vrxe9ZboGliaTlT GsZSCTFENA6L3EgC3YJWvVkIZwXxC8BGF5/XwOkIXMtzGBFrr9iTDNoSKBJbZl7C2Jkbq MOnQOLycV1qDsHB/fcYAMpzg8NW0s/2ImnegQJWZhqSKjn3PKWwuI54S5ZjMyeZt3eTMa qzUf0WxDF+/xYjzVz8d/ELW3OT2g+f7IEXVmUxJZKsKiZu+I3NtiWCF30J8GzcPzwYMn4 wgmfMj85ARC42Sm03hp4VnRIxIHAzmymgdwbADV9KSgSzvtpDNnSu7gajYx7l7vwx8A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796159977; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: 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=3i2pyC1sH5BbnG0DwTGhcDUc0D/O7jlRwsVpvJ0VAfxJL+6CI2rlQy/CN/NkF4SU4351 A5Ha/zNmWO92RoU9UmRrzIdnhCNfR4tqcamlm0Hg5alDTDtRBeYoXIbWB1Go6RSp+E6V8 2Uie2KBI1xdRkzPWlR8pHSxre34ZIzDSQ8X7tvoJFhNZUJh82USZtlVHr7AHWQTd53NIt bsFH/qPNxgfD00aD2KA8v4X81StgppTRX1R6lC7JMiO2sNiX2aVrjaxOL7WUbQwebFRdI P8/Q7OWkzAWUhkM3CllNpywvAx6eYzI1traq+aFGuX6w8YZtFbHs/bLc6X3DjY1wgzw== 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.200 Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) (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 3FCF6606A320 for ; Tue, 1 Dec 2026 21:19:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3FCF6606A320 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-f200.google.com with SMTP id 5614622812f47-4ea2c058e67so13052412b6e.1 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=uSjBQYCqrmDR8cfxeC/becf8cwCOXfjUn3wXpuNfwbiSweYkm4mSRFRnKVDDb0Iidn odzIVvcGn8YoP2R2JXiINfVXdDDTlFdntVJJ8eV/9uryDWmSb9lg59lSfBrzufx6TIBf 00ajmrJsaC8KKayRa/D9zo4czEIKcaeJdPngXaPbHp1M9GJEOybNMbhLLULhPWWUA3T9 +1I8E61yjkJpY1Du7AQLmhDbVfTiSuAqGwJV2FSvLHNU+FG9R6RM+8D7V7+JiURhJzom xBhQMxhb8Ip//SvTvg2SjIZW9tO1wqzwHaG7ekSpZL+FaEFOh1bYii6aifbP/Dos+Zbg pwrQ== X-Gm-Message-State: APuU4Z9aQRCjwMrNJRSfNJgp4sedcjXVyWU7I/2DRHvVeKLZuqI801Lb 9UsYyFXmOhIzevu9t4inQvwk5CCyJwOW8gJGqjIdNfosZ//vAGWgPZYwz9GUGgJjLM4WqMTtXlH knl6ekPn0hglV6uNIJ3yPGA== 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: stable@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