From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179171; b=KnlKci3NjcmbhNJAULg2Oiq2LS4jNBFvHscAofJLJO2GDInKObfTcyA0YOz/L0rrZH3I fs7g95pJGUgRvEOKtOY6bVsK0DiUyCOF8ypNFq9S59k18lJsoAeYGf1QfFdkmJdYqENTA ElkMqtmG3NvOV48G38HGGZz0ckz15cOzyYVe+ZzYAg4fUm3yMbAATl1OSxSNchegijVvA 245FNbifUH36DnXpYiXvFLUCS4+DBebn1A07mTCHmw41c0yhVDhdSvig/TSqvHx+7HQ0B 6lpJEGmmImDsxduWlCKLt73X3KKNr3xLLbH8K65UX1QagTVeZXjw70M4gYNztelk/UQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179171; 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=tT78+r+FZ/F9ob56M9gdwle0KeZsweyHeDouMPnJXUda1h8YkqExcLkux+x9zDZI8wEX MmDGQ2Dl2Ok3XkOB3mYb4dfKemw6DSSbhTddWmcSFi19StziRI98NwpwiCqLzTahFmXKO V5xOsemOoIsTFMnk7zrmGYv7bMLE/sls+TC/CjXZQJ55qiognM3yEbhYnLmw6klTVOK+m qTREZE4KfGzQTp+ik8lbFiraspWTXxbhhmGqgxAez6LFXKuBxv/JsgZDjMPmED2M7s4aT /P68aPTDT3FZ2AMWlxJOtGnoIv6X/HLgvMesZJz9cyZ0HgJCJSeW7GaaLFxAwdgMWZA== 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.70 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (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 25DA460A263B for ; Wed, 2 Dec 2026 02:39:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25DA460A263B 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-f70.google.com with SMTP id 98e67ed59e1d1-3b421417c3cso10829170a91.0 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=nLrW0yzNI26D0kooMEwpowex1gnfUXlGU8D4/SKspHCWoLkkNZ84MDmbLzxjBGHd8Q aH1tLFMl8Q/6htgBCCSufkoSURF6hHf/QdPj+mdA3ClqXU2QerwTIgcHlKDuEXeOsDCF tbXySn2jS4q5aT4F1WSPok7/YRPKYaXdckQtiH8DznCtNz32LuUh1Py09Onr7PawofmK Pk4ZDTs21whCJTY/xI8Z/ziLM8Kg7CEzb+XZFiei5Fuap078HpX7piV1cRVlIcRwUutb yYn+DqVxQa3E5ClNbHdINtCYxqWE/X/oH9OPEE6tJTDP2CO3TZwIctPU/pY3irVAU3ik 0qfg== X-Forwarded-Encrypted: i=1; AL+hErH1zlJZR2geU6hHqRkM+/4hwUdAGAIdEB9gciUzMNaatnh1WWiOn/zDyBbchjQbuZ1naH8mNqUfNc2UmdU=@vger.kernel.org X-Gm-Message-State: APuU4Z+/HqlAaPXH1QY8jdP3ZEXIqRkBhZPvikdT91uxZy1xm6s4jpeu nuVfdg2jVxOgP/Ol5IfA3lwU1kY5B1mQGzzZSygPQO7vclUy6xn4CqfEe1S+rir75XrPRjpXJGE +5BOYpSja9jFeOnZbrlsX/Q== 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-kernel@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