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=1796388646; b=j79+tbDrwgJP/8Yea6P+5EKYF4eDkbhCsZko6pFhAogyLI6UmcSXne4LSyd51TKhqwjq 6YW7wFoqbpMvMXwtwMt/ME6UthB4gO5v2cf9C/x52oqslE/LS7m9Vr9Gb3G2MHzkL7Ud/ +dJuF8lv7ZLH/9L6kTi9EXjHzPQZBrukVPdZGH606xt0a2HauyHIYh8vsaQWGMUsfZLQL EddObC+bZQ/EdH8l1j42VlS2ztmch/NHAj4oEYSl2d0atzkVCjj8kzrqQ+sZMlpwFnUpy F8czaHSwJ1+J4e17tBa+3kZ7To/NS9TDtYcinfiwV+FSsIjiL3x9g611MyjqQzq6I4A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388646; 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=M1Rh16mmD8X6S5gKYERLH+T1DpJKX6F0iFR5nPxlytc=; b=k/RNbHK1MXXSJ3k+OJWY94Yx7WXAQd12b9W5ipId+weLasdJ2FCtB7NKSgJbj72h9HWX 8w7W/++KNvupTlo8pjSYTiJDcxxH/M6RtDraCUvvICqVlsp5va2Dw7Do0nuvdjTwE357N atQ9TPJCBu20x0023wcGZ3KGboR/T8yUpX5K5QqxoltlrdosqEzvIJNTLD+2wKVFGApoo C2QwDcQc7vQhel+7KSXxOwR0QwRRVMhgcwQtTIE36PJCCeC4Vd2q4aaOYqwXnIq/9JjdM IwkFjL4s4yC+wQbzH7hPY9W4HC1SuJF9+bG4XH1AkQJbRu2JTZPNMNqdIgzxD9R0D0A== 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=bJu0dcQO; 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 CD437606A33D; Fri, 4 Dec 2026 12:50:46 +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=bJu0dcQO Received: by smtp.kernel.org (Postfix) with ESMTPSA id D88E71F00A06; Fri, 4 Dec 2026 12:50:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796388646; bh=M1Rh16mmD8X6S5gKYERLH+T1DpJKX6F0iFR5nPxlytc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bJu0dcQOgYWlW9AbTE6coCUm5m8/eXmT+/Eq1C5OCcHMW4KkOVBbSGyR5aT2g6URv 7CqcEBJrKH8CkPrNaQ4C3PX26gaH05FPThpsbD5Z92MQpXxQMmLCgzZkb12KpZRSto /rgnC8V/30VnMrUhojytm/WYB73fawxcElr78d9o= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, "Masami Hiramatsu (Google)" , Sasha Levin Subject: [PATCH 6.1 0699/1059] kprobes: Switch ftrace ops when adding probe with post_handler Date: Fri, 4 Dec 2026 19:03:50 +0900 Message-ID: <20261204100702.083834040@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: patches@lists.linux.dev 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: Masami Hiramatsu (Google) [ Upstream commit f60f98881b54b0cf36b61f41c91fbd93b76a2d9c ] When an ftrace-based kprobe is registered without a post_handler, it is armed using kprobe_ftrace_ops. If a second probe with a post_handler is subsequently registered at the same address, register_aggr_kprobe() sets ap->post_handler to aggr_post_handler, but leaves the probe armed using kprobe_ftrace_ops. When the probe is later disarmed or disabled, disarm_kprobe_ftrace() checks whether p->post_handler is non-NULL and attempts to disarm from kprobe_ipmodify_ops. This fails with -ENOENT, decrements kprobe_ipmodify_enabled below zero, leaves the probe armed on kprobe_ftrace_ops, and aborts unregistration, leading to a use-after-free if the probe module is unloaded. Switch the armed probe from kprobe_ftrace_ops to kprobe_ipmodify_ops in register_aggr_kprobe() outside of text_mutex and cpus_read_lock to avoid lockdep and deadlock issues when modifying ftrace code. Defer adding the new probe onto ap->list until the ftrace ops switch completes so that the new probe's handlers are not published prematurely while ap is still on kprobe_ftrace_ops, and mark ap disabled if rollback re-arming fails. Also restore ftrace ops registration and counter if ftrace_set_filter_ip() fails in __disarm_kprobe_ftrace(). Link: https://lore.kernel.org/all/179440641120.328088.15542754095040659094.stgit@devnote2/ Fixes: 5dd7caf0bdc5 ("kprobes: Skip clearing aggrprobe's post_handler in kprobe-on-ftrace case") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) [ sashal: Adapted to the older code by replacing scoped_guard(cpus_read_lock) with explicit cpus_read_lock()/unlock calls and dropping the kprobe_unused(ap) check in add_new_kprobe(), with the ftrace ops switch placed after the unlock. ] Signed-off-by: Sasha Levin --- kernel/kprobes.c | 53 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index 03c7688fe2954..b74fb67fb0156 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1117,9 +1117,14 @@ static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops, (*cnt)--; ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0); - WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n", - p->addr, ret); - return ret; + if (WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (error %d)\n", + p->addr, ret)) { + if (*cnt == 0) + register_ftrace_function(ops); + (*cnt)++; + return ret; + } + return 0; } static int disarm_kprobe_ftrace(struct kprobe *p) @@ -1249,16 +1254,12 @@ void kprobe_busy_end(void) } /* Add the new probe to 'ap->list'. */ -static int add_new_kprobe(struct kprobe *ap, struct kprobe *p) +static void add_new_kprobe(struct kprobe *ap, struct kprobe *p) { if (p->post_handler) unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */ list_add_rcu(&p->list, &ap->list); - if (p->post_handler && !ap->post_handler) - ap->post_handler = aggr_post_handler; - - return 0; } /* @@ -1291,6 +1292,8 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) { int ret = 0; struct kprobe *ap = orig_p; + bool reset_post_handler = false; + bool ftrace_switch_ops = false; cpus_read_lock(); @@ -1340,15 +1343,45 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) | KPROBE_FLAG_DISABLED; } + if (p->post_handler && !ap->post_handler) { + reset_post_handler = true; + if (kprobe_ftrace(ap) && !kprobes_all_disarmed && + !kprobe_disabled(ap)) + ftrace_switch_ops = true; + else + ap->post_handler = aggr_post_handler; + } + /* Copy the insn slot of 'p' to 'ap'. */ copy_kprobe(ap, p); - ret = add_new_kprobe(ap, p); + if (!ftrace_switch_ops) + add_new_kprobe(ap, p); out: mutex_unlock(&text_mutex); jump_label_unlock(); cpus_read_unlock(); + if (ret == 0 && ftrace_switch_ops) { + /* + * Must switch ftrace ops outside of text_mutex and cpus_read_lock + * to prevent deadlocks when ftrace code modification acquires text_mutex. + */ + ret = disarm_kprobe_ftrace(ap); + if (ret) + return ret; + + ap->post_handler = aggr_post_handler; + ret = arm_kprobe_ftrace(ap); + if (ret) { + ap->post_handler = NULL; + if (arm_kprobe_ftrace(ap)) + ap->flags |= KPROBE_FLAG_DISABLED; + return ret; + } + add_new_kprobe(ap, p); + } + if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) { ap->flags &= ~KPROBE_FLAG_DISABLED; if (!kprobes_all_disarmed) { @@ -1356,6 +1389,8 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) ret = arm_kprobe(ap); if (ret) { ap->flags |= KPROBE_FLAG_DISABLED; + if (reset_post_handler) + ap->post_handler = NULL; list_del_rcu(&p->list); synchronize_rcu(); } -- 2.53.0