From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34A66606E962 for ; Wed, 2 Dec 2026 02:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=tfPb4vMJ; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=L1dnie0L DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Unsubscribe-Post:List-Owner; bh=ZH/v6bHTgI9DugQpkjfMWJglqq4tQol1+Opshtjjdos=; b=tfPb4vMJzOr3fRHWaLGxJTrZxn VCxqgwflJEZv7Cd4aPEow5bLF/PA8hQWBsW8yf3v0pc5+K8M8H034e+q2icoDyu309bSEZBuL8zLC 1H/shtl6JJk0q4eDaefYseOKt4/145tkg9yZR4kLCLh7C09pwMijQfd1JtK6AWMk0IIAbenaCJyJc R7JGk5l/q360SUA/4e8Nl6AA+Mx8hwjQ3wIscKqMXdpXF45bO0dZd365zmn6NdMPI+gn6N686Bs/n MQPRsRldnElkVAuZqN6IlmZyv5UTsCC+T4gK1XHHw3vhQhOwMJQuXri7s7A/uc1ce//1/1FnVVcTp exiLLoKQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYaFw-00000009yeA-3i7h; Wed, 02 Dec 2026 02:39:40 +0000 Received: from mail-pj1-x1046.google.com ([2607:f8b0:4864:20::1046]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYaFo-00000009yYe-0NRV for linux-riscv@lists.infradead.org; Wed, 02 Dec 2026 02:39:39 +0000 Received: by mail-pj1-x1046.google.com with SMTP id 98e67ed59e1d1-3b44bcdb096so9645031a91.2 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=lists.infradead.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=L1dnie0LMds/DQxJasjgdwLd+Sezvf5saMzcxosz4iIczUbW7SGDtwP7rYkFfUIDoS eUV7e694nP7lu1h8psbA6d/KDG+JGAWv6VbJSQBRlJa1wtipoY1cVpwkE8wOpmN8JleL x08yGGMA1TXBUMDHq1682GK/epzDQsyd87saCG7mztk80r1Y694Wn2nxwTKsN7lzFmgT BttHMhbVEFYmgwsKv890F7Kl78s7dVel6pBLEL+WbmuZNiTEEvQU+noLqFucvtnzENT8 XgzCX46RiLNMKD5kYNEJP4pjzDqvkHSUyjKu/q9atmZqzXLUfssa6PnnIvHoNu8WZxeu 5++A== 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=yiULevILPDT2q/N02o5y3XGBohbVFL2fPx+ri9iAtrQo5NqHOQGgsBjrlcQnme7A24 6VdQ0cbmFLR/v+AVZkefeeTH4mvxvSZRG1w0DxXtvcQuZTX52IW5SAP7bSEhYIrj0yti z+fNscbtwbL/aEUCfnBeCPJ6ZTQtRX9F5EbT9H3Y8jqG+M3benW5p/vLZW4qBKQPVe+I Sm4tUItzlp2+yfJzMKpGYZnVduXBefGwFb6fJ++Wyrbs2WH2KTCf3whQ4hGSrmv8ks7n wVi7jcbNENsEkqtVE45MWUQahfPCOoC8GWqivsWLmCtrTueY25DyF/cU7vxBq+uud7gw mmcA== X-Forwarded-Encrypted: i=1; AL+hErESIAWaxKyOBugZsXzAEqnXXZCYMjdqlF+uRUdeas9EpaJAccrYLEbBVhZN2DRX4SuYGIRGUVJHW8D6XQ==@lists.infradead.org X-Gm-Message-State: APuU4Z+1O6FRTg11VRKtt+KVmtJzVw9yC6Hk2iOc3hUw6f6WaZr1nj6k pdHlG4YRqzJWmNDtu0pNR8Wy8UHLcjiHMAcx6afirdnjJ+uF26/rr91jg9CHPFvV6e6H+t8Kcwo Vsy4AlnYNJzHIGb4uRJXY5w== 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-Archived-By: 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261201_183932_136770_902A9CFB X-CRM114-Status: GOOD ( 20.33 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv