From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179172; b=HofDO0xd4cWTThc8FYrJ+Aiyzy2xtsByM1xrEgwj1jTOfIrwBKGmBXRQoYKYePUOz4hL a7VxwweDvxIyun3nwmvv45jF+KUDTxgcKzbhfUSpUG/VbhZrJeFsvci85qdXwXjK3Kx2o tkw3JiyxCUaTB6A7/jVpqeur4LLmvGEpRZKNTOhe7AnFrxK4ilfJUYqooVoO8kE38G970 0m8YTPrGQakGhfkGcJvC9d8umrL9iPgmgJ2ni2M+DDt2KWGTawdFdQW1em8HdVa1stp63 +c2gPsAcO5hPAdfKt8vEnQ8LZlVNcyEgzNgaQ85gI2F5bdXfzdOJKulcaVtBJUpyzQg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179172; 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=XOfc+B9w1wztxJFjul51djZXA4GFrYS5waC3jbFTvSo=; b=E9ZWmhBeU96q/3gA/1FfTLlgJVYFRHeJEmkUc0CzMjKIHGqEV/9OclOiP8gr8jFdr0Bh NbxGiPDMwUKg8lLf4mLzXSR/Gs+ntrvcRA05zvUkyd9pTMdfVBC9qqUSJ4147qzIDZ4aw lUQCBpXS5B/jKcPynuF60speb2XW/ZP9C5QJlBhesgUZVuMampTzYRPcHJR4BRGFc/5h0 CzOu3K2675oloF6oo3l0pMDLSi/76iE9jglLK35uOoN6zH2dq0i2MwvdpPGZOwVqJXAIr QntP+VgNJWt4v2zp6oK3tWX86wb6+q2o1+AhY1detkgjI3e9QxgMMXWO2N9bWcc+7Sw== 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=KAcB8w9P; arc=none smtp.remote-ip=209.85.216.72 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 2057660A261C for ; Wed, 2 Dec 2026 02:39:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2057660A261C 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=KAcB8w9P Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-3b5361fb600so4328698a91.1 for ; Tue, 01 Dec 2026 18:39:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179170; x=1796783970; 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=XOfc+B9w1wztxJFjul51djZXA4GFrYS5waC3jbFTvSo=; b=KAcB8w9P9BJMVdTlomYTSQHZqW5Mg+qFHdtWSd88YUaIEZEUBWaqpJ69IqGh9v5MAR gb4YSZYw5cKKKg3myIcMjRaZQGOwzdUHK6PCNoxs8BT5O27s536jg/QqK2CuzrkjhjT/ eWXar3FVKM06mPi/JyHIyV5peeeh+EiMucKiazAr9CyhpZVK6hbbd1NF4PnVAzN7976g 82gPlREziKhzPnaKKLpA0Bb5L7SP7TizYFJ/1RgbrW6PbipfNoRuvHLiNywcW79aUBJM 4dEnuhzy5FbKnYy8ljb7C0Qn9bEgAL5nmFaEZj4O8/sBRrFAtOFUtvb3+t7l1bp2Hebr KsWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179170; x=1796783970; 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=XOfc+B9w1wztxJFjul51djZXA4GFrYS5waC3jbFTvSo=; b=wvEShWRIp2G4osyyvJcRK2orMNIYd/hHbJdMsF4cglxNsNd9AuI0hIlKBi8eij43KP mphoCKCloGs/m6Ws1ZRZb904paOfwSjSwsIq9h0xTQ1CwYutQD7ZBFTiigl2N+Wa4P3L dPCsGOCsfusCSXFBxEGEWkhqn93M1mpaE3Tkpr+ACOBAb5/q5tlYslSPkRn7j2EPf2VN jEDsV4tyT5Qe8kY4H8CPW/7VHdkazSB/hSLUpdLq48J7HkV3YP2TJoARvkqHL0JDX5Lp m/BfpqKQgOBS0OFHZ587VlkFxED+BjhtnKYA3BqXEdn0ASO9m2/rpeT04bZ2ZXVQp8hg IPmg== X-Forwarded-Encrypted: i=1; AL+hErHVi13v3DV8CFavVAjp5uHc080NukBx4biaKPN+9WF0EbLH0hpjadw2CafXdPNIhK8Bk0mBDMZMEcHpcK9BN0mh@vger.kernel.org X-Gm-Message-State: APuU4Z8YNP+KjJ+SwEp9AEuyd4YF2gyFc0oIXf8mMBJoTkUfS4V9wqMw xcccwbrDRuxeqfgzfsdUW+vaD6DZ/qnsg2thK2xX+9770Ml/Wqnwu5pEjsEdmhkFpMhZ5cTsxZw eRskVr+THAOiHaI/Qki2bqQ== X-Received: from pjbca9.prod.google.com ([2002:a17:90a:f309:b0:3b4:feb5:db45]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2688:b0:3b3:b71e:b2e7 with SMTP id 98e67ed59e1d1-3b41c92a209mr37446892a91.36.1796179170114; Tue, 01 Dec 2026 18:39:30 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:10 +0000 In-Reply-To: <20261202023920.595207-1-jthoughton@google.com> X-Mailing-List: linux-perf-users@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-4-jthoughton@google.com> Subject: [PATCH v2 03/12] bpf: Fail sleepable user copy helpers when current->mm is NULL 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" Sleepable BPF programs can run on behalf of a kernel thread, or of a task that has already gone through exit_mm(). For example, bpf_wq callbacks always run from a kworker, bpf_task_work callbacks queued late enough run from exit_task_work(), lsm.s programs may be attached to hooks reached from kernel threads or from exit_task_work() (e.g. bpf_lsm_inode_free_security() when the final __fput() of an unlinked file runs from delayed_fput() or from exit_task_work(), or bpf_lsm_bprm_check_security() during a usermode helper's kernel_execve()), and fentry.s programs may be attached to error-injectable functions called from kernel threads (e.g. btrfs_should_cancel_balance() from the btrfs-balance kthread). In such contexts, bpf_copy_from_user(), bpf_copy_from_user_str() and bpf_copy_from_user{,_str}_dynptr() (when reading from the current task) call copy_from_user() or strncpy_from_user() without an mm. That access does not reliably fail: the user half of the address space belongs to whatever mm happens to be lazily loaded, if any, so the program may read another task's memory. As there is no user memory to read from in this case, fail with -EFAULT without attempting the access. Fixes: 07be4c4a3e7a ("bpf: Add bpf_copy_from_user() helper.") Fixes: 65ab5ac4df01 ("bpf: Add bpf_copy_from_user_str kfunc") Fixes: a498ee7576de ("bpf: Implement dynptr copy kfuncs") Signed-off-by: James Houghton --- Tested with a small bpf_wq reproducer: a SEC("syscall") program arms a bpf_wq whose callback (which runs in a kworker) calls bpf_copy_from_user() on an address in the loader. With the uaccess check from later in this series but without this patch, that triggers the new WARN from bpf_copy_from_user(); with this patch, the helper returns -EFAULT without warning. I can turn this into a selftest under tools/testing/selftests/bpf if that's useful. --- kernel/bpf/helpers.c | 11 +++++++++-- kernel/trace/bpf_trace.c | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index cebf9d8dfe36..35f59fe203d8 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -660,7 +660,11 @@ const struct bpf_func_proto bpf_event_output_data_proto = { BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size, const void __user *, user_ptr) { - int ret = copy_from_user(dst, user_ptr, size); + int ret = -EFAULT; + + /* Sleepable programs may run without an mm, e.g. on kernel threads. */ + if (likely(current->mm)) + ret = copy_from_user(dst, user_ptr, size); if (unlikely(ret)) { memset(dst, 0, size); @@ -3726,7 +3730,10 @@ __bpf_kfunc int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void __user if (unlikely(!dst__sz)) return 0; - ret = strncpy_from_user(dst, unsafe_ptr__ign, dst__sz - 1); + if (unlikely(!current->mm)) + ret = -EFAULT; + else + ret = strncpy_from_user(dst, unsafe_ptr__ign, dst__sz - 1); if (ret < 0) { if (flags & BPF_F_PAD_ZEROS) memset((char *)dst, 0, dst__sz); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 19e6b51bb2f0..0279169a37af 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -3594,6 +3594,8 @@ static __always_inline int copy_user_data_sleepable(void *dst, const void *unsaf int ret; if (!tsk) { /* Read from the current task */ + if (unlikely(!current->mm)) + return -EFAULT; ret = copy_from_user(dst, (const void __user *)unsafe_src, size); if (ret) return -EFAULT; @@ -3628,6 +3630,8 @@ static __always_inline int copy_user_str_sleepable(void *dst, const void *unsafe if (tsk) { ret = copy_remote_vm_str(tsk, (unsigned long)unsafe_src, dst, size, 0); + } else if (unlikely(!current->mm)) { + ret = -EFAULT; } else { ret = strncpy_from_user(dst, (const void __user *)unsafe_src, size - 1); /* strncpy_from_user does not guarantee NUL termination */ -- 2.56.1.911.g883d8bcb20-goog