Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796179177; b=sKbeOHsK7wvBY7m0nqF2WF/7ZjIgjER8C9CAk7uZ5QxDgJsm/Fv5kG7KZe7BMWdboqqd Z2f22Q/r1Mmp4zoRj2Y0JWWARuHo7QSeLaQE76oDIy2TNxfyQKADPokAy0hpy45YGut2o Sbj8+kCTYxn7w5QC7FT5maesNY/g6bbiiNs5+Bmt6my/dbvch61m1FK0bvpY+3mFbWruy 3Hoi4B5wrMM7qYNFuCPt5hDeUDn8Tuhc4D052cd0Pp90AMdte2HSpjzP/VDMTivdtrtgo lu9a02qX/X3/s+l0a+hu/KuH5BoP938RwOahBzJNSYLcm13ABuSPNKyX8B8yfTENwOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796179177; 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=3uXIVrX8BEny3n8isWbyFCZpXQoSk0AEcS3T7yrtU/0=; b=yO5LxKLE6aUmTYAzcfV9rjlIj3pzvdPjIASif8/GTWVPbTFSzxPimwUOqsvUM6FYDXRD WUWOdVkIqQfFR31LdDwyCriUlSeXS7djsXlef8mvvRZqomGTuIj/7tEzKLfotYRpyAg33 18JrCzJu40O7dl3AhxCTwoqHPDMSnkFXTfZ0eMCSOAWhAs+5zcqhdCu2+GTh72zqpmWpO ANxM/n3JlQO2yElbyXFwqGv71AfivcAs2LTgXN6U+W11X09nI/y8RMJikBTkGhFRcpe+A DLlWT9mlrhJsYDSXOgHnCJR5kNjJ6MP/+oKkB0FIBFj7PAMIOzoL9dgiMUQokuhofzQ== 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=VV0C31J8; arc=none smtp.remote-ip=209.85.210.199 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (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 8093760A6F7C for ; Wed, 2 Dec 2026 02:39:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8093760A6F7C 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=VV0C31J8 Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8a0619b1fe5so10325008b3a.3 for ; Tue, 01 Dec 2026 18:39:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796179175; x=1796783975; 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=3uXIVrX8BEny3n8isWbyFCZpXQoSk0AEcS3T7yrtU/0=; b=VV0C31J897gc5RAy0qvLUsBKdjvdWbKwE6N3krNt2wEfnp4uHJsasCR38Q8A3Bwj5c LbE1xafXbJN4jhVVdtkUn3wgAwnEtZhaO1L0hgdLcRCrOqQy/XY8qoNwC0tRisSOZ8Js C6M7rMHfEI5wXnqRBykxLq1yt6L2oH7mPy+V+XThqEC+ZLq18uMqYR1hrgPvcHdzza1r GIb1zCVtGYqeDx/RcuI3Ku42SoMi/6WDzPxCDrGddd2Dd0B85QlBv31gVFAqRmid+0sZ 6gWxWd7CEpBuzIsu5q6u6lxmc8koj902GLS0Q3sMlTxEWaw/TYnGuq4wTTCuk20n6D4l CYGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796179175; x=1796783975; 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=3uXIVrX8BEny3n8isWbyFCZpXQoSk0AEcS3T7yrtU/0=; b=SYBmxiVWGkNEq6ihBYJGr/KGiG+ucYD2VZC3TTXr9YrQwO9ruoO+R9ASDUgsIwoRWY lXJgmlicxn/5CwGJ2yTDwTkEjRJqGTs3e2tBUyO/bOe+Wbl86yd8f8S0Rfrlgi1G391m taT9pOdIEfNWJkl1aAlWlfb/qpHdWQLSsoQ+fcKsopuKva7H9vXePwE59Asu2DBlBj0v 8y/G/0I0RcGUYrCLk7mnjnJ3wcPkjYYfEZFmGdQjgQ0QtV2XV9uYxtUm/NsonVOZbTc9 a1lZvgJ1GqBnflPrpGzwOrkgm/Etbnn2i1qmGmK2DW6msvnhdNgMTx8lm6HIwPrZz2o4 P4lA== X-Forwarded-Encrypted: i=1; AL+hErH3tCg6tj9i5tY4gxeDJTt6ALBwlQv73iQ/BJbD9AwjV4BnMy7unqnpd1S+3SPxBKHjn0A=@vger.kernel.org X-Gm-Message-State: APuU4Z+fz5cgHGwTY1tTke7fTq8T9u105+OdNGA5P1vSqNrStW22pOGY YcmfRMctxMh+uGaxZc59uGuQiNwJ1j3dB6LD1DGOg9Vgj4neEHAsR6jbgFURwICaJQP5SJOu8iN OfTA2DnwJC8Nktwd9VcPhrg== X-Received: from pfgs41.prod.google.com ([2002:a05:6a00:17a9:b0:888:e7c7:5580]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:32a0:b0:3e2:90da:9594 with SMTP id adf61e73a8af0-4014a37929emr39892245637.9.1796179174354; Tue, 01 Dec 2026 18:39:34 -0800 (PST) Date: Wed, 2 Dec 2026 02:39:13 +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-7-jthoughton@google.com> Subject: [PATCH v2 06/12] bpf: Don't read user memory without an mm in tracing helpers 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" BPF tracing programs run in whatever context the function, tracepoint or event they are attached to happens to run in, including kernel threads and tasks that have already gone through exit_mm(). There is no user memory to access there, so the helpers that access the current task's user memory without faulting should simply fail: bpf_probe_read_user{,_str}(), bpf_probe_write_user(), bpf_probe_read_user{,_str}_dynptr(), and the user string arguments ("%s" or "%pus") of the bpf_bprintf_prepare() users, e.g. bpf_trace_printk() and bpf_snprintf(). They currently call the user nofault helpers regardless. Without an mm, those don't 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. (On x86, copy_from_user_nofault() already refuses this, as nmi_uaccess_okay() returns false when current->mm is not the loaded mm, but strncpy_from_user_nofault() doesn't.) Check for a NULL current->mm in these helpers, and fail as if the access had faulted. bpf_probe_write_user() already refuses to run in kernel threads and exiting tasks; also refuse it without an mm, which covers PID 1 and usermode helpers before they exec, as these don't have PF_KTHREAD set. A NULL mm here isn't a kernel bug, as the program doesn't choose the context it runs in, so this also keeps these helpers from triggering the warning that a later patch in this series adds to the uaccess helpers. These helpers have read user memory without checking for an mm since they were added: bpf_probe_read_user{,_str}() in commit 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers"), the user string arguments of bpf_trace_printk() (since moved to bpf_bprintf_prepare()) in commit b2a5212fb634 ("bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier"), and the user dynptr kfuncs in commit a498ee7576de ("bpf: Implement dynptr copy kfuncs"). bpf_probe_write_user() only became reachable without an mm with commit 753550eb0ce1 ("fork: Explicitly set PF_KTHREAD"), which stopped init and usermode helpers from inheriting PF_KTHREAD. Fixes: 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers") Fixes: b2a5212fb634 ("bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier") Fixes: 753550eb0ce1 ("fork: Explicitly set PF_KTHREAD") Fixes: a498ee7576de ("bpf: Implement dynptr copy kfuncs") Signed-off-by: James Houghton --- kernel/bpf/helpers.c | 17 +++++++++++++++-- kernel/trace/bpf_trace.c | 24 +++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 35f59fe203d8..64973cb04736 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -784,6 +784,19 @@ const struct bpf_func_proto bpf_this_cpu_ptr_proto = { .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID, }; +static int bpf_trace_copy_user_string(char *buf, void __user *user_ptr, + size_t bufsz) +{ + /* + * BPF programs may run in any context, including kernel threads and + * exiting tasks, which have no user memory to read. That's not a + * kernel bug, so fail before the user copy helpers warn about it. + */ + if (unlikely(!current->mm)) + return -EFAULT; + return strncpy_from_user_nofault(buf, user_ptr, bufsz); +} + static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, size_t bufsz) { @@ -795,13 +808,13 @@ static int bpf_trace_copy_string(char *buf, void *unsafe_ptr, char fmt_ptype, case 's': #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE if ((unsigned long)unsafe_ptr < TASK_SIZE) - return strncpy_from_user_nofault(buf, user_ptr, bufsz); + return bpf_trace_copy_user_string(buf, user_ptr, bufsz); fallthrough; #endif case 'k': return strncpy_from_kernel_nofault(buf, unsafe_ptr, bufsz); case 'u': - return strncpy_from_user_nofault(buf, user_ptr, bufsz); + return bpf_trace_copy_user_string(buf, user_ptr, bufsz); } return -EINVAL; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 0279169a37af..f073d3c7e016 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -204,7 +204,15 @@ bpf_probe_read_user_common(void *dst, u32 size, const void __user *unsafe_ptr) { int ret; - ret = copy_from_user_nofault(dst, unsafe_ptr, size); + /* + * Tracing programs may run in any context, including kernel threads + * and exiting tasks, which have no user memory to read. That's not a + * kernel bug, so fail before the user copy helpers warn about it. + */ + if (unlikely(!current->mm)) + ret = -EFAULT; + else + ret = copy_from_user_nofault(dst, unsafe_ptr, size); if (unlikely(ret < 0)) memset(dst, 0, size); return ret; @@ -241,7 +249,11 @@ bpf_probe_read_user_str_common(void *dst, u32 size, * and keys a hash map with it, then semantically identical strings can * occupy multiple entries in the map. */ - ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); + /* No user memory without an mm, see bpf_probe_read_user_common(). */ + if (unlikely(!current->mm)) + ret = -EFAULT; + else + ret = strncpy_from_user_nofault(dst, unsafe_ptr, size); if (unlikely(ret < 0)) memset(dst, 0, size); return ret; @@ -368,7 +380,7 @@ BPF_CALL_3(bpf_probe_write_user, void __user *, unsafe_ptr, const void *, src, * required to prevent the use of temporary mm. */ - if (unlikely(in_interrupt() || + if (unlikely(in_interrupt() || !current->mm || current->flags & (PF_KTHREAD | PF_EXITING))) return -EPERM; if (unlikely(!nmi_uaccess_okay())) @@ -3585,6 +3597,9 @@ static __always_inline int __bpf_dynptr_copy(const struct bpf_dynptr *dptr, u64 static __always_inline int copy_user_data_nofault(void *dst, const void *unsafe_src, u32 size, struct task_struct *tsk) { + /* No user memory without an mm, see bpf_probe_read_user_common(). */ + if (unlikely(!current->mm)) + return -EFAULT; return copy_from_user_nofault(dst, (const void __user *)unsafe_src, size); } @@ -3617,6 +3632,9 @@ static __always_inline int copy_kernel_data_nofault(void *dst, const void *unsaf static __always_inline int copy_user_str_nofault(void *dst, const void *unsafe_src, u32 size, struct task_struct *tsk) { + /* No user memory without an mm, see bpf_probe_read_user_common(). */ + if (unlikely(!current->mm)) + return -EFAULT; return strncpy_from_user_nofault(dst, (const void __user *)unsafe_src, size); } -- 2.56.1.911.g883d8bcb20-goog