From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796388017; b=MQY/s6ZDvdDOv3G8of8fJMUXHMskuKy4+Wd08Vx4kJCtZXnYW2s6cDLqN9WW/m8ek9xi Bl6fw2mcBjOXRQbpNYvO2jS7gGP2BE7n3Kx6v/5eBskbjiV4r3lkdjsNLt/8wBsswl9x0 /QalVTMU8z5E4AK+mmGGf8IyWD2/+rooWF5cgqs9URl72XeUOlq5cOCnq9FNqsAgasz81 /EaVI4VC3qfGH5yw9QhvD7YuXzC9E1Zr1ZJZ6kpfSvFALo1EpXG/wlCWahjW//OmFDpBu dpWljFzaZf7fi9Nj87Rdk4/oTrtjttE21A4QIkQXDInoe8DO7tNmAaUb+2cS/pApf/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388017; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=JE/8u2b8vsvte6U7YnKIZJv6W6S8AoYRFEZbNL5Ua0I=; b=DVPC2Ed8Eiz90RLb9/8j9lrdS2uSqKoLgboKQsUMOzOzpTMZtwXuUfjkPufHz4EHec0Y HmzLbvQiMDKPFjwJkcxjHS2yUesDv2GFN+XgaHznTH9Pnf9zJ8aXqhuCGbx9Tm8N3O6+x wPyVNunIjWXS8ADi7KREjs6kvR9X+34BLKBu40xaKpa8mlvWQJG4nWfFU2PKprKpzzOYL OeMwFZK8gQ+S8eUFTnjRfBXYdAM7lYJeDKbPrQuR5r2jgc0DnSUUBwgUtpyFB6u4v/MDn e8/K5/ojf0nisYdY0tUo+inKcSzxEPd0kI6bepmOdD0KWHFn5LowaFc1U9U0TomoHzw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qTV4LjJF; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C600606A322; Fri, 4 Dec 2026 12:40:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qTV4LjJF Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB5AA1F000CE; Fri, 4 Dec 2026 12:40:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388016; bh=JE/8u2b8vsvte6U7YnKIZJv6W6S8AoYRFEZbNL5Ua0I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=qTV4LjJFgeo1NZIPeFixGPmD1oVVM5WvZYvAGMNvkY/xS53iPUT0hFjHyfnqhlfch KCaK6R0wnjOpBnXrfFv06DVp6UYI4vg7GdWlBDdghJ27BfLlMKghQ0bRWf/oxv4XUq jg4Fbk0Hmv2ImtzUTJpND4D5phtOtKpIfH3Wfz/M= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Josef Bacik , Alexei Starovoitov , Sasha Levin Subject: [PATCH 6.1 0412/1059] bpf: Read the probe of a perf event under event_mutex Date: Fri, 4 Dec 2026 18:59:03 +0900 Message-ID: <20261204100655.012509794@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Josef Bacik [ Upstream commit 3ccd700543f16a2b2ab656f35dcaeb6bd7c45e22 ] For a perf event on a kprobe or uprobe event which was made in tracefs, bpf_get_kprobe_info() and bpf_get_uprobe_info() look the probe up by name in the list of dynamic events, and hand a pointer to its symbol or file name back to the caller. None of the callers holds event_mutex. The perf event keeps its trace event alive, nothing else. Other dynamic events are removed from the list and freed during the walk. And a probe which shares its event with other probes can be deleted while the event is in use, so even the probe which is found can be freed, together with the string, before the caller has measured and copied it. The result is a use-after-free in the walk, in strlen() and in the copy to user space, or a NULL pointer dereference. The readers are BPF_OBJ_GET_INFO_BY_FD and fdinfo of a perf link, for whoever holds a descriptor of the link, and BPF_TASK_FD_QUERY. The other side is anybody who may write to dynamic_events, kprobe_events or uprobe_events. Take event_mutex in bpf_get_perf_event_info() and give the caller a copy of the name, which it has to free. Fixes: 41bdc4b40ed6 ("bpf: introduce bpf subcommand BPF_TASK_FD_QUERY") Signed-off-by: Josef Bacik Signed-off-by: Alexei Starovoitov Link: https://patch.msgid.link/20261017163745.3341787-10-alexei.starovoitov@gmail.com [ sashal: Dropped the perf-link and fdinfo hunks since those functions don't exist here, and added kfree_const(buf) in bpf_task_fd_query(), the only caller of bpf_get_perf_event_info(). ] Signed-off-by: Sasha Levin --- kernel/bpf/syscall.c | 4 +++- kernel/trace/bpf_trace.c | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 97dbfb8832aef..93d9326482245 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -4568,11 +4568,13 @@ static int bpf_task_fd_query(const union bpf_attr *attr, err = bpf_get_perf_event_info(event, &prog_id, &fd_type, &buf, &probe_offset, &probe_addr); - if (!err) + if (!err) { err = bpf_task_fd_query_copy(attr, uattr, prog_id, fd_type, buf, probe_offset, probe_addr); + kfree_const(buf); + } goto put_file; } diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index aecce50519d56..5521556fedf25 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2364,6 +2364,7 @@ int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog) return tracepoint_probe_unregister(btp->tp, (void *)btp->bpf_func, prog); } +/* On success the caller releases *buf with kfree_const() */ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, u32 *fd_type, const char **buf, u64 *probe_offset, u64 *probe_addr) @@ -2372,6 +2373,12 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, struct bpf_prog *prog; int flags, err = 0; + /* + * The list of dynamic events is walked, and a probe which has siblings + * can be deleted, with its strings, while its event is in use. + */ + guard(mutex)(&event_mutex); + prog = event->prog; if (!prog) return -ENOENT; @@ -2408,6 +2415,12 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, #endif } + if (!err && *buf) { + *buf = kstrdup_const(*buf, GFP_KERNEL); + if (!*buf) + err = -ENOMEM; + } + return err; } -- 2.53.0