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=1796179184; b=V+41QQEcGAqqxpsW0IiHxtAA0YKURfJ14uGXPFE8iIps3vqoiTKetj9DEfcS4MIbEGXF Ncu6ZUutSgTkX6gsNSMV6CDqGbQZAL7h4SEc/llCTxP+oaaNFEJyVDc1Qq3/AAhs6974s r2MotwTU1pcVU9G+5fAbTKTZX0wC8pIyS6NYaW5gfzPSzw6Xcrn5hMikz1r1rHsOb+wEx nEGWIQwccceJFNiHzUOAC8XMKt6p+hwHyyg7lk7tGrq15hUXb3s3jGgqc/Izj2Xjqzx1e UDKSm6GyYeIIKt9dT5+Dl3n6CxNPwhKVWcgOiqHT7fpVnsfy0vtd4EyQuocIGZYT+9w== 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=m9KiWj0lHe8/XJLUvwN8ET9ywuKPgn+V1tjCeYZjtlYb62xYYSAqVQtb9KQDS7oug7nh 4a2LmbItVwXG+yqDPN0Ut66aeVVGEjO12gWq1vDy2S3dIwWQqiow5QGPlUdf3X2LiNZWb RV5+sRsLImK7IeecYtbyysL9606uF8JvW9zPTjZSzUVaHARBv+Xbd6ChXt/GZFsI3xamJ FNlkJfC+1TvqLGudTeTiuBJRY5WI6eAJtJkwUo+0Q9dSe5jRsRvKVxM7zibQp8a94gMei J70aMTGdw2px5eGQt6yJzQ14+n6oXvulw3BtVNI3yUOUepaXFNlBk2WHSLFDwITyjBQ== 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 94505606E962 for ; Wed, 2 Dec 2026 02:39:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 94505606E962 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-cd60e4e8bc3so8690105a12.1 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=Wso3gieqArwKc9q7wax1YItaLOkV2ABR8ITv4Mxu7zQoVEuPMI5RAtLGsmOx2d9HkZ mAqYqZgJBmso37AhM2JscamjPa2mMFM4tIwfOLMw0VMa7UaiG06Yf7G01P2UkXPg+IXJ qtQyVmAlvfaZ3nSzm76s1b1ucF2plMkrXIddQEdFI3mOPzU/LYmjiZqp03n7i87Eb2hC Su+8t9d3GrDHnCw7aE19zGxoeILEYr95eqB+RGmVqwtQ8bfZh0DuNbFqAMNfiMkjCjw8 QbPXPP0B5lcFJjLaVOMAirCvSUxCHnYiq4I0b85rDqKwotlZGZ2dvuDONkxVLPqzFZDu LbzQ== X-Forwarded-Encrypted: i=1; AL+hErFOMTGv0F54XzYxV380lGXXPtuLeH8BV8QYbIcyf1rTLKNAoGUzxNibs/3IWIYXU3qCm2uYCa8LsQ==@vger.kernel.org X-Gm-Message-State: APuU4Z9PQgV/VfhgVBmC2pbFEk6FTfQnFsvUWE4WXfYx/nM45Pqr/2QY tYjAoP+c6G373C8gCz7E6ihD/nQxKM/jFYkIHEqcwFEUauSGpuVdiI0z2+OEWFUA5yUsg5FPhtd rXjBacU789l2SCIvPZIx9MQ== 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: io-uring@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