From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179183; b=19A9+K/+5+QcAlPsjneozfx4HVfZ3tw/wzxQt8mam+guaaRzU2v3PSffNftHFBKFPwSx 3sujaWiW1vPDDWiNGVBHPzDsanb2XStyQNVW8RwNvGAWvDqHF3gOY7VKRK1QXJ2hzIW9g rZmlDZOpM9b3hXloBtAttxzhLBx2nt/9N07m3T3bCtF681g/WPXbfE1Bs6dYPVAxDt0D8 yWUpI6s9bcZ06Pu5fJkdrt4eQ/WE2dx0gapWOtHKg+/8V43dhLspLmCzIw88HbwEn1tOQ QcROHKmLe6rmlbWzqA8v10hCOZHvp6AdB95zWpxUBZGJ7nCFVlE+5mIKCl6B/eBP3Qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179183; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=YzwDy+tWxS+4zgdZfjchBFrZz6WSRxFROtgSmfrulws=; b=lC4lIxx2+qw/opuyBH1ts3gqrs2giDXieXG5mnbE0bqs0XbQZ8YlN+pWI+hRo4wNpqX0 mX8kmRKhUg1a5hJHtLI7iWOpir9JdJwzbo4My8FhBVa5bx4xh6XwnsoDPJOB6TS0CHrBs wuS1ppVL3eGbXK5nr8UD/dpEJHsGFk1Dx9b3KPbOhD7TPyAmhoAtnAQQh03UxTAtJNZkl Q3sYjfD9jtrf8PhD+p3RUcOLZEtESen0IOahwIwW4ns+eOdgyQO+Pa0wPxQ0sK51oGjhC a247z0RMzwqvTGJ6cBDIubbo5l0YrHbToLh4Vj04cZEW34wtTudhT7kmiOZ7/Ctr0+g== 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=KQkUa8U1; arc=none smtp.remote-ip=209.85.215.197 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (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 84AB3606E957 for ; Wed, 2 Dec 2026 02:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 84AB3606E957 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=KQkUa8U1 Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cd652c0385bso9335962a12.2 for ; Tue, 01 Dec 2026 18:39:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179183; x=1796783983; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YzwDy+tWxS+4zgdZfjchBFrZz6WSRxFROtgSmfrulws=; b=KQkUa8U1X91JgG+yTvUGMPbgZDNDrqrEKoJAdiM/qYSNWgT73doycy+RM7OFYXz8fg 6Pbd38lXeZOOcwa1Q1Xwa5/ezJ4ISuC6QesUE3lZvmc6WuKojtUvNONevm/Ahs3sqdIe A0YBudE0Iz57+JOEcXdjOr/uHjyX2Q/iYmuUKPzJnlfv2cleS3M2bquXZY4yvQDwlqcA cs7dpTt7bSbcv68hwdLP6/kEUnGxyYLdZP+zutMV+5T4H6nzOqFAUwl9+LU3l4zS54Hv 9X7WoR55cxvGx7Vod3Ntuznv9Pol/bjbpePKVt3RJa1RQK1tJzh8MZwi5WXDARhWJgxj jKMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179183; x=1796783983; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=YzwDy+tWxS+4zgdZfjchBFrZz6WSRxFROtgSmfrulws=; b=Bpr3EgCyi+1jOP3yzmpYJJvgaudGWh5eKRVjTJQEn3/NxgBEFCra69vE9szMbdYIB+ uxWu11hv6gpQ2OMv0dplHNICIOeYecL2EW0wfw59HZZ2e0uS4pvBkqnOhpmxPjEnHC/k IXI7xKxdlj6MUnkpbVO67uzKE8lB0yjtA+cQXkRzY60rOi/pZzjLD34SF/r+QBy6aiju jJZXZbdeCGbkx1VzT3cFg/e1r1zCt2GQv8SoSx0aEY1d//C7aufXhgXZPK4EMbqnXAfA y9obIa65Wna9VqG/BdWEAjRcguSE021jxjotmnP07qvpxsOZnAAOXwJIa0pem0bfHEnn CLgA== X-Forwarded-Encrypted: i=1; AL+hErHqKZf7Rpg+U41J7wB0drgC1N67EvD39EwuzXBuJNgbxsXR3LLkKRIlbYfXMm1l32tjv5E9DXUbnWIUWZA=@vger.kernel.org X-Gm-Message-State: APuU4Z+CkHW2bJinsv+qmRva4HixEEPZd3NFv9YVpzkiUu3z1NMb5JTb RfydJJXLS5nxL9UXst1fIZVhKqy32ed69lE3dYpVa045JSk60hHpH0AFvo4hLn8kHxHvAKPx4dS vn1lefhccOP1YfqPDTOddKw== X-Received: from pjbgj2.prod.google.com ([2002:a17:90b:1082:b0:3b4:119b:5835]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc2:b0:3b5:32db:117 with SMTP id 98e67ed59e1d1-3b532db0508mr18734575a91.53.1796179182548; Tue, 01 Dec 2026 18:39:42 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:19 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202023920.595207-1-jthoughton@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202023920.595207-13-jthoughton@google.com> Subject: [PATCH v2 12/12] uaccess: Refuse and warn on unchecked user accesses without an mm 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" __copy_{from,to}_user() and __copy_{from,to}_user_inatomic() don't call access_ok(): they rely on the caller having called it earlier. That earlier check may have happened in a different context, e.g. KVM checks a guest's memory slots when they are set up, and then accesses them through __copy_{from,to}_user() from whichever task happens to run the vCPU, or to tear down the VM. So the check in access_ok() doesn't cover these accesses. Add uaccess_should_fail(), which combines the NULL current->mm check with the existing should_fail_usercopy() fault injection, and use it in place of should_fail_usercopy() in __copy_{from,to}_user() and __copy_to_user_inatomic(). __copy_from_user_inatomic() never called should_fail_usercopy(), so only add the NULL mm check there, rather than start injecting failures that some of its callers don't expect, e.g. __wp_page_copy_user() warns if the copy fails twice. arm64's copy_from_user_flushcache() also relies on an earlier access_ok(), so add the check to it as well. Ideally, the remaining users of these unchecked accessors would be converted to the checked ones, which would make these checks unnecessary, but that can be done separately. The callers of copy_from_user_nofault() that may run without an mm check for it first (see "uaccess: Document that the user nofault helpers need an mm"), and the generic user stack unwinding entry points (perf_callchain(), get_perf_callchain() and stack_trace_save_user()) only unwind the user stack of a user task with an mm, so neither is affected by the check in __copy_from_user_inatomic(). Signed-off-by: James Houghton --- arch/arm64/include/asm/uaccess.h | 3 +++ include/linux/uaccess.h | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 6ce7d0fe3bf9..be4d34dfe63f 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -484,6 +484,9 @@ extern unsigned long __must_check __copy_user_flushcache(void *to, const void __ static inline size_t copy_from_user_flushcache(void *dst, const void __user *src, size_t size) { kasan_check_write(dst, size); + /* Like __copy_from_user(), this relies on an earlier access_ok(). */ + if (uaccess_without_mm()) + return size; return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size); } #endif diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index ff7132cdda65..dc60acd25617 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -11,6 +11,15 @@ #include #include +/* + * Return true if a user copy must not be attempted, either because the + * current task has no mm, or because a failure is being injected. + */ +static __always_inline bool uaccess_should_fail(void) +{ + return uaccess_without_mm() || should_fail_usercopy(); +} + #include /* @@ -101,6 +110,8 @@ __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) { unsigned long res; + if (uaccess_without_mm()) + return n; instrument_copy_from_user_before(to, from, n); check_object_size(to, n, false); res = raw_copy_from_user(to, from, n); @@ -115,7 +126,7 @@ __copy_from_user(void *to, const void __user *from, unsigned long n) might_fault(); instrument_copy_from_user_before(to, from, n); - if (should_fail_usercopy()) + if (uaccess_should_fail()) return n; check_object_size(to, n, false); res = raw_copy_from_user(to, from, n); @@ -139,7 +150,7 @@ __copy_from_user(void *to, const void __user *from, unsigned long n) static __always_inline __must_check unsigned long __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) { - if (should_fail_usercopy()) + if (uaccess_should_fail()) return n; instrument_copy_to_user(to, from, n); check_object_size(from, n, true); @@ -150,7 +161,7 @@ static __always_inline __must_check unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n) { might_fault(); - if (should_fail_usercopy()) + if (uaccess_should_fail()) return n; instrument_copy_to_user(to, from, n); check_object_size(from, n, true); -- 2.56.1.911.g883d8bcb20-goog