public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] kprobes: Rollback the aggrprobe post_handler on failed arm_kprobe()
@ 2022-06-19  4:50 Chuang W
  2022-06-21 15:51 ` Masami Hiramatsu
  0 siblings, 1 reply; 2+ messages in thread
From: Chuang W @ 2022-06-19  4:50 UTC (permalink / raw)
  Cc: Chuang W, stable, Naveen N. Rao, Anil S Keshavamurthy,
	David S. Miller, Masami Hiramatsu, Ingo Molnar, Jessica Yu,
	linux-kernel

In a scenario where livepatch and aggrprobe coexist on the same function
entry, and if this aggrprobe has a post_handler, arm_kprobe() always
fails as both livepatch and aggrprobe with post_handler will use
FTRACE_OPS_FL_IPMODIFY.
Since register_aggr_kprobe() doesn't roll back the post_handler on
failed arm_kprobe(), this aggrprobe will no longer be available even if
all kprobes on this aggrprobe don't have the post_handler.

Fix to roll back the aggrprobe post_handler for this case.
With this patch, if a kprobe that has the post_handler is removed from
this aggrprobe (since arm_kprobe() failed), it will be available again.

Fixes: 12310e343755 ("kprobes: Propagate error from arm_kprobe_ftrace()")
Signed-off-by: Chuang W <nashuiliang@gmail.com>
Cc: <stable@vger.kernel.org>
---
v1 -> v2:
- Add commit details

 kernel/kprobes.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index f214f8c088ed..0610b02a3a05 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1300,6 +1300,7 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
 {
 	int ret = 0;
 	struct kprobe *ap = orig_p;
+	kprobe_post_handler_t old_post_handler = NULL;
 
 	cpus_read_lock();
 
@@ -1351,6 +1352,9 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
 
 	/* Copy the insn slot of 'p' to 'ap'. */
 	copy_kprobe(ap, p);
+
+	/* save the old post_handler */
+	old_post_handler = ap->post_handler;
 	ret = add_new_kprobe(ap, p);
 
 out:
@@ -1365,6 +1369,7 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
 			ret = arm_kprobe(ap);
 			if (ret) {
 				ap->flags |= KPROBE_FLAG_DISABLED;
+				ap->post_handler = old_post_handler;
 				list_del_rcu(&p->list);
 				synchronize_rcu();
 			}
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] kprobes: Rollback the aggrprobe post_handler on failed arm_kprobe()
  2022-06-19  4:50 [PATCH v2] kprobes: Rollback the aggrprobe post_handler on failed arm_kprobe() Chuang W
@ 2022-06-21 15:51 ` Masami Hiramatsu
  0 siblings, 0 replies; 2+ messages in thread
From: Masami Hiramatsu @ 2022-06-21 15:51 UTC (permalink / raw)
  To: Chuang W, Steven Rostedt
  Cc: stable, Naveen N. Rao, Anil S Keshavamurthy, David S. Miller,
	Masami Hiramatsu, Ingo Molnar, Jessica Yu, linux-kernel

On Sun, 19 Jun 2022 12:50:27 +0800
Chuang W <nashuiliang@gmail.com> wrote:

> In a scenario where livepatch and aggrprobe coexist on the same function
> entry, and if this aggrprobe has a post_handler, arm_kprobe() always
> fails as both livepatch and aggrprobe with post_handler will use
> FTRACE_OPS_FL_IPMODIFY.
> Since register_aggr_kprobe() doesn't roll back the post_handler on
> failed arm_kprobe(), this aggrprobe will no longer be available even if
> all kprobes on this aggrprobe don't have the post_handler.
> 
> Fix to roll back the aggrprobe post_handler for this case.
> With this patch, if a kprobe that has the post_handler is removed from
> this aggrprobe (since arm_kprobe() failed), it will be available again.
> 

This looks good to me.

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thank you!

> Fixes: 12310e343755 ("kprobes: Propagate error from arm_kprobe_ftrace()")
> Signed-off-by: Chuang W <nashuiliang@gmail.com>
> Cc: <stable@vger.kernel.org>
> ---
> v1 -> v2:
> - Add commit details
> 
>  kernel/kprobes.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index f214f8c088ed..0610b02a3a05 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1300,6 +1300,7 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
>  {
>  	int ret = 0;
>  	struct kprobe *ap = orig_p;
> +	kprobe_post_handler_t old_post_handler = NULL;
>  
>  	cpus_read_lock();
>  
> @@ -1351,6 +1352,9 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
>  
>  	/* Copy the insn slot of 'p' to 'ap'. */
>  	copy_kprobe(ap, p);
> +
> +	/* save the old post_handler */
> +	old_post_handler = ap->post_handler;
>  	ret = add_new_kprobe(ap, p);
>  
>  out:
> @@ -1365,6 +1369,7 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
>  			ret = arm_kprobe(ap);
>  			if (ret) {
>  				ap->flags |= KPROBE_FLAG_DISABLED;
> +				ap->post_handler = old_post_handler;
>  				list_del_rcu(&p->list);
>  				synchronize_rcu();
>  			}
> -- 
> 2.34.1
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-21 15:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-19  4:50 [PATCH v2] kprobes: Rollback the aggrprobe post_handler on failed arm_kprobe() Chuang W
2022-06-21 15:51 ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox