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=1796179184; b=cgKTEW0JmVLjVE19qz8/UrN9BDvZxedv/iNY+bvQDVDw3wOttERVZ4XVuDtbdClDt2B6 EeD/hxXXY5NoGbLMHXA5zfnQ6jwAXRrMr0LbryPAOrv+yISg2yM9Svy9bwTEV/pMPmsSP JcBqTSgxgTBQTwLkZbT3h6Pcf7+b1jKiZFDUY8xbJqBHdk2xjJsoh4UfHlrJKwNUzx6Ud qP+0sVHAPvHP96tAOMR2HJXXYR5VcPsykPR3HscMaJ+8k6mEhVK4aNILyeFlkNY2Y46fK rH3F0N3ehWZ/hJdSP6Y1rELOMVPBsK3Lm5IsAqXwJk0DsT+kjvmzekfdusvd83W6IqA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179184; 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=tAhy6atnzxxdC+p76AUk4399k2YvRuzhZjTf1cwhl1S2V5fZLatBe5iTs2BDf2cBJ08D CP/DTpLrhLVkJ1lr8UADIsDZXzGpjCQw/znRN6It9WEqQDJI4CuW+YMig9VlL/Qthrv3N HeiTVF4X66+OavlNDYQ5G+yiVI2UP2sj1KFRF7AVhhG7IAGn/pIn534vtZUxbl3gNx9OS 3WGVV7mEPC45+h541gfNq9qiGOR8sR4MUww53p3twVZc0+xY1FWHnIlkzz9f2gYsjGfcS Q93wUhoUG8pKo0Eikn+apGO5v+P27Jvn2IL0ljHNucHMLjJHYQMTcXgEqgkibEXVB+A== 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 84A84606A6BB for ; Wed, 2 Dec 2026 02:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 84A84606A6BB 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-cc7361c62b9so8104153a12.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=eic5Aa2BxNQRz08SbJybAeHO60xCikw9aa4lkmeEycgaMAeYgMasWrhq5Nplz9UCl/ bZk8aDLqr1wp11tLxAu87LZFR8e+fWpYABc01qIQnef43F4L5WY7MYLFwTTZule3b790 lzk33YjIAq8O6Lt26vbBNvEetP8IqxkZohYqiV6DcmM/royGPO0rtlOev5vqZ4uNhcCh QHJdz7Kq4QRMlCeSIoxKIvvu2P6QMkVhKndQSlNeFC+j2VqF+D0o+3u9YLkcECSjyhHC w0LgQQSTVHxFi5ZpIKJ1NcTZtytpk7Z8rwmum2PFiIs+wfxlhv2JDJu74YA+RTurvh+k Xp4w== X-Forwarded-Encrypted: i=1; AL+hErGvwysbtlVLn72kZ08af8inivW6/TvGbuBzsEgr9MN3NfFRaOnRYB5JhkMPbuWBM7l5McE=@vger.kernel.org X-Gm-Message-State: APuU4Z99m7Ivye1Z/7N3kidbRPy3WQG3V6y6XOlHkhOokBQzr282Yv6V 9RJGOFrJQ68NUhsEgoqcA2+7ylQZmUvwedu4amvifaCqVTCFngn7gl7mEUXSbpG5xCCX02ABFtb T3z5by77IQMfsrVO6KhIdbw== 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: bpf@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