From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179185; b=izODmrh1b1xQD+ZTiwxM+fLlKclnEQjiUYLTfiVPMgTKuFQZejmOWySbpOZVH/48uLER tDt2DUKLTiWIW88qJvkKszbiWRGzdyRNL+UaFVTreqbhTdqYm85wu5vvGvSVnT+lSG133 WJoWjYLwlRXAW+OJ8TM++Vp+IUEcwhloUCOk6IDN8Y47iZu0cKjpYIO0tJMyK3mgFtajq tlpi4TWf6rz88tWC4vHXo8p2ICHY8pEXxI6+EYKct/c+7Kob15jrs2sxpmNLSg9QfC0DS QH7MhF4uJB+QOYLWqGLw9XtgtJJPtZrWtX8lsmsl4Evh2BuNDA6IBnlkuJA5VEXSSPg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179185; 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=4zhDVNzXRpe05jILTClMeHanlsxdZfsZ+WR8vMyi9oDODdQ5UjJrRTSG61Rgo8gs2B9F 8hx9X+cbmqzmNuBvugRJQk8S3s+LfHvk1v/3wjn2XqYfrhWUp47AkyB5oy/FF+tP0jwfw CeC2LfLyLx2bWOYShVWQXZJ8tNrMXbRC/HgHs2vHmiA+WxedxNukry0JQK2STnrtC33N9 UmkZlMoH9Zs2/CPuqvQhou9HHGMeaGBx6/N63nQjkyS4yRQUFBGXfcY/sS/KwOuIpXDWq 2EkVqKgMBrHCTGtS4/BxlRV12yfhw/DwLmBE1BRCJLzsiY1QvzCV4eRLCt22+APLGFg== 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.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.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 944D6606E961 for ; Wed, 2 Dec 2026 02:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 944D6606E961 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-f198.google.com with SMTP id 41be03b00d2f7-cc7361c62b9so8104149a12.3 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=VP2VAN7PAeZmc3/XKob17/hxp7vp/Eh/yEfJ8x6FpQJYPYSVwagKteIY2C1pUn+kiK slTRvCYqf1W4e40osQ+nu4/+oNzTSo0CkrH5Shb1JtQrxYv0fEqcn6oR1wuE86xlR8oy HA1/4xU9Hvrex0kf4q3f4qfeMfwyEZg71BKtj2EqJFeC2bPjZo2Mo2Uk8QiquXmhmNHj jJrMwWYbjQu2kKr6S4KruvRUbAVFFCwWSpxEKlw18MgJ2x9jWEyKDXI3VRSYb6kzNWjI TX/UpqTvqTVCbVj81qsdgYzhWk5f4cF8GJpduFR31qCrP2H6VuRVl+2mxa7ntd6F0auV yMaQ== X-Forwarded-Encrypted: i=1; AL+hErEAOEnbFY+NgthJRQP8nK89Cqt3amfz7VRKE9QKFBIkpV42HvJeF17oRhH7yk6e5I1I+aI/Gxb7wU5V@vger.kernel.org X-Gm-Message-State: APuU4Z948MRYBK6oVhjYUle/AOgdcm2m/EanlDugYR+mmL8zryfLr28Q t428RhNYtK7wv6D15nJCW3F13tSeRQ+Q8+iscyZtQClVPQD42a7l8ZLVbe31S6JAKWrdro0EdAC ZENr2+3QJjzPihDqHvkxuhA== 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-arch@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