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=1796228616; b=thEykUA49u4Zupqmunt4fzdxZeWIkixe65n5Q6fCbAJ+0bJcXGswd1JrbIk2a8TBpYye E2U3WLCgQm1cjWQtxuSxSZpQxcMrRhYWthuyLWBux8NMvEl/6ZTuVkDN/F8AwepJNJRZ/ 22KYfIXDp/R3Mtp/8iPDqFipgYe8lQ6egv2Lbb9U9HX/HaQs4fbf4VuTxhOat+XJh9zEl oOteYVh0G78ybW/zuuxPGXxAYi3iOYLOkyXysZV+4O8JieXZNGFHGbsyoR0ZeRI2xKoAz 544o8yrt+LDWpbSToQaW/9TmjPa0Z2usIghzuDWlJMYKawNo5addcPhTq/z0HvrB9+Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796228616; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=3BYhkOnMtA+IvI1Y+7wKoqFzQzksUVx8f0+fG8MXOXI=; b=pd+6NYfk8uiHJNsVmcctfZNf1AqKFJ4OPUmww0naxvDEeYDni00q94zR8fjdXin1LbIa 8yHwE15zjzhs2JxsUmhUHkH76kR/MSevwSeKkcSU3p5FEe6ctBWP6uccR9KH+zS3hV7zU z/4UtBJO8D3vwLe2NEVai+GQSIhB6YIu9MNqJS/U9Aoqw2LCdAufGZ3iKwD9/FfABIPZP dhYqAp7GC0ywF6dxYww22Qk085m+a0b8+t88A28PpEScaQ2NAhm3D4R7OtHGVDi/uy3I6 v/TktuuOFJQd16ZBUAJTYgYJBsHekgSWwub1//9KEd8WajaOoLLyzYI4snqQJSnrqJA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dPXCR7SJ; 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 83681606A322; Wed, 2 Dec 2026 16:23:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dPXCR7SJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAD681F00896; Wed, 2 Dec 2026 16:23:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796228616; bh=3BYhkOnMtA+IvI1Y+7wKoqFzQzksUVx8f0+fG8MXOXI=; h=From:To:Cc:Subject:Date; b=dPXCR7SJQQlFM+E7QLi0C/m/d9m4IpnZwj4jbgnzeMByJU233K3uhycM87j85tt8i EgXOh2EWAc8+qcjMiH9RA7X1W9h/WQ5GIOmc1xYfn5RdN10mcLN4rpYDyFj/Yz0rLx HShw0bIch0G4lfj2dWXgZo8fmq8tb8qG4domekVSWeSjYWR1uexzjzqse+leTQfP21 t1Mk2G9Vi8UYQXDER+5BaqqfKjLECPFwCBGOkPtOVgHxyu4PcqZxpGVV6y/m05Yp6m dB/FxTyxipIAlgmc+1ZFJ+r1gR0kCTPwdeOZxUE/UFpqcCsejtQVofcHayi/HEwbkc +YAuzF3wSdm1g== From: Antoine Tenart To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com Cc: Antoine Tenart , vmalik@redhat.com, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, "Uladzislau Rezki (Sony)" Subject: [PATCH v2] tracing/probes: fix race between probe execution and event file removal Date: Wed, 2 Dec 2026 17:23:07 +0100 Message-ID: <20261202162333.168965-1-atenart@kernel.org> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-trace-kernel@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 A probe being executed can race with its unregistration happening on a different CPU and lead to a UaF, which can trigger KASAN or crash the kernel. BUG: KASAN: slab-use-after-free in kretprobe_trace_func+0x68d/0x7e0 Read of size 8 at addr ffff888173328390 by task hammer/1619 CPU: 32 UID: 0 PID: 1619 Comm: hammer Tainted: G S 7.4.0-rc1+ #1 PREEMPT(lazy) Call Trace: kretprobe_trace_func+0x68d/0x7e0 kretprobe_dispatcher+0x128/0x220 kretprobe_rethook_handler+0x10f/0x1d0 rethook_trampoline_handler+0x181/0x510 arch_rethook_trampoline_callback+0xcd/0x160 arch_rethook_trampoline+0x2c/0x60 entry_SYSCALL_64_after_hwframe+0x76/0x7e The race is the following: CPU 0 CPU 1 kretprobe_dispatcher kretprobe_trace_func file = link->file disable_trace_kprobe trace_probe_remove_file // Frees link after an RCU grace period. kvfree_rcu_mightsleep(link) unregister_trace_kprobe unregister_kprobe_event probe_remove_event_call event_remove remove_event_from_tracers // Frees link->file. event_file_put // UaF. file->... Looking back at history, disable_trace_kprobe used to have the following snippet: /* Synchronize with kprobe_trace_func/kretprobe_trace_func * to ensure disabled (all running handlers are finished). * This is not only for kfree(), but also the caller, * trace_remove_event_call() supposes it for releasing * event_call related objects, which will be accessed in * the kprobe_trace_func/kretprobe_trace_func. */ synchronize_rcu(); kfree(link); It made clear synchronize_rcu was not only there for link users, but also related objects. The comment was lost during refactoring in b5f935ee1339 ("tracing/probe: Add trace_event_file access APIs for trace_probe") but synchronize_rcu stayed, in trace_probe_remove_file. Later, a6ed2aee5464 ("tracing: Switch to kvfree_rcu() API") removed the synchronize_rcu/kfree pair into a single kvfree_rcu call. This works for freeing the link but the side effect of protecting related objects was lost, leading to the UaF described here. We do not want to put back the synchronize_rcu call as it has a noticeable impact when removing probes. Instead, defer the freeing of the link after an RCU grace period. The other snippet of the commit mentioned in the Fixes tag was fixed in commit fe58f457ad8d ("tracing/osnoise: Call synchronize_rcu() when unregistering"). Fixes: a6ed2aee5464 ("tracing: Switch to kvfree_rcu() API") Cc: Uladzislau Rezki (Sony) Suggested-by: Steven Rostedt Signed-off-by: Antoine Tenart --- Since v1: - Do not add back the synchronize_rcu() call and use Steven's suggestion instead (freeing the object after an RCU grace period). kernel/trace/trace_probe.c | 11 ++++++++++- kernel/trace/trace_probe.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index cff55dcae36f..c249256dea80 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -2812,6 +2812,7 @@ int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) return -ENOMEM; link->file = file; + event_file_get(file); INIT_LIST_HEAD(&link->list); list_add_tail_rcu(&link->list, &tp->event->files); trace_probe_set_flag(tp, TP_FLAG_TRACE); @@ -2831,6 +2832,14 @@ struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, return NULL; } +static void free_link_rcu(struct rcu_head *rcu) +{ + struct event_file_link *link = container_of(rcu, struct event_file_link, rcu); + + event_file_put(link->file); + kvfree(link); +} + int trace_probe_remove_file(struct trace_probe *tp, struct trace_event_file *file) { @@ -2841,7 +2850,7 @@ int trace_probe_remove_file(struct trace_probe *tp, return -ENOENT; list_del_rcu(&link->list); - kvfree_rcu_mightsleep(link); + call_rcu(&link->rcu, free_link_rcu); if (list_empty(&tp->event->files)) trace_probe_clear_flag(tp, TP_FLAG_TRACE); diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h index d1fb3520700f..aecc9f24951d 100644 --- a/kernel/trace/trace_probe.h +++ b/kernel/trace/trace_probe.h @@ -281,6 +281,7 @@ struct trace_probe { struct event_file_link { struct trace_event_file *file; struct list_head list; + struct rcu_head rcu; }; static inline unsigned int trace_probe_load_flag(struct trace_probe *tp) -- 2.56.1