Skip to contentSkip to conversation

fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free()

PATCH In review Masami Hiramatsu (Google) 2 participants 3 messages Jul 7, 2023 – Jul 11, 2023

Subsystems (2)
lore
Share & tools
lore

Masami Hiramatsu (Google)

author maintainer
#0 lore raw

PATCH v2 1/1 fprobe: Ensure running fprobe_exit_handler() finished before calling rethook_free()

From: Masami Hiramatsu (Google) <[email protected]>

Ensure running fprobe_exit_handler() has finished before calling rethook_free() in the unregister_fprobe() so that caller can free the fprobe right after unregister_fprobe().

unregister_fprobe() ensured that all running fprobe_entry/exit_handler() have finished by calling unregister_ftrace_function() which synchronizes RCU. But commit 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") changed to call rethook_free() after unregister_ftrace_function(). So call rethook_stop() to make rethook disabled before unregister_ftrace_function() and ensure it again.

Here is the possible code flow that can call the exit handler after unregister_fprobe().

------
CPU1 CPU2
call unregister_fprobe(fp)
...
__fprobe_handler() rethook_hook() on probed function
unregister_ftrace_function()
return from probed function rethook hooks
find rh->handler == fprobe_exit_handler call fprobe_exit_handler()
rethook_free():
set rh->handler = NULL;

 return from unreigster_fprobe;
                                   call fp->exit_handler() <- (*)

------

(*) At this point, the exit handler is called after returning from unregister_fprobe().

This fixes it as following;
------
CPU1 CPU2
call unregister_fprobe()
...
rethook_stop():
set rh->handler = NULL;
__fprobe_handler() rethook_hook() on probed function
unregister_ftrace_function()
return from probed function rethook hooks
find rh->handler == NULL return from rethook
rethook_free()
return from unreigster_fprobe;
------

Fixes 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered")

---
Changes in v2:
- Update changelog to add a problematic code flow.

---
 include/linux/rethook.h |    1 +
 kernel/trace/fprobe.c   |    3 +++
 kernel/trace/rethook.c  |   13 +++++++++++++
 3 files changed, 17 insertions(+)
include/linux/rethook.h +1 -0
index c8ac1e5afcd1..bdbe6717f45a 100644
@@ -59,6 +59,7 @@ struct rethook_node {
 };
 
 struct rethook *rethook_alloc(void *data, rethook_handler_t handler);
+void rethook_stop(struct rethook *rh);
 void rethook_free(struct rethook *rh);
 void rethook_add_node(struct rethook *rh, struct rethook_node *node);
 struct rethook_node *rethook_try_get(struct rethook *rh);
kernel/trace/fprobe.c +3 -0
index 0121e8c0d54e..75517667b54f 100644
@@ -364,6 +364,9 @@ int unregister_fprobe(struct fprobe *fp)
 		    fp->ops.saved_func != fprobe_kprobe_handler))
 		return -EINVAL;
 
+	if (fp->rethook)
+		rethook_stop(fp->rethook);
+
 	ret = unregister_ftrace_function(&fp->ops);
 	if (ret < 0)
 		return ret;
kernel/trace/rethook.c +13 -0
index 60f6cb2b486b..468006cce7ca 100644
@@ -53,6 +53,19 @@ static void rethook_free_rcu(struct rcu_head *head)
 		kfree(rh);
 }
 
+/**
+ * rethook_stop() - Stop using a rethook.
+ * @rh: the struct rethook to stop.
+ *
+ * Stop using a rethook to prepare for freeing it. If you want to wait for
+ * all running rethook handler before calling rethook_free(), you need to
+ * call this first and wait RCU, and call rethook_free().
+ */
+void rethook_stop(struct rethook *rh)
+{
+	WRITE_ONCE(rh->handler, NULL);
+}
+
 /**
  * rethook_free() - Free struct rethook.
  * @rh: the struct rethook to be freed.
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 55 lines · On Fri, 7 Jul 2023 23:03:19 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Ensure running fprobe_exit_handler() has finished before calling rethook_free() in the unregister_fprobe() so that caller can free the fprobe right after unregister_fprobe().

unregister_fprobe() ensured that all running fprobe_entry/exit_handler() have finished by calling unregister_ftrace_function() which synchronizes RCU. But commit 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") changed to call rethook_free() after unregister_ftrace_function(). So call rethook_stop() to make rethook disabled before unregister_ftrace_function() and ensure it again.

Here is the possible code flow that can call the exit handler after unregister_fprobe().

------
CPU1 CPU2
call unregister_fprobe(fp)
...
__fprobe_handler() rethook_hook() on probed function
unregister_ftrace_function()
return from probed function rethook hooks
find rh->handler == fprobe_exit_handler call fprobe_exit_handler()
rethook_free():

   set rh->handler = NULL;
 return from unreigster_fprobe;
                                   call fp->exit_handler() <- (*)

------

(*) At this point, the exit handler is called after returning from unregister_fprobe().

This fixes it as following;
------
CPU1 CPU2
call unregister_fprobe()
...
rethook_stop():

   set rh->handler = NULL;
                                   __fprobe_handler()
                                   rethook_hook() on probed function

unregister_ftrace_function()
return from probed function rethook hooks find rh->handler == NULL return from rethook
rethook_free()
return from unreigster_fprobe;
------

Quoted 3 lines
Fixes 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered")

Looks good.

Quoted 3 lines

---
Changes in v2:
- Update changelog to add a problematic code flow.

Nit, for making forensic analysis easier in the future, I now always add a link to the previous version. That is:

Changes since v1: https://lore.kernel.org/linux-trace-kernel/168796344232.46347.7947681068822514750.stgit@devnote2/
- Update changelog to add a problematic code flow.

-- Steve

Quoted 6 lines
---
 include/linux/rethook.h |    1 +
 kernel/trace/fprobe.c   |    3 +++
 kernel/trace/rethook.c  |   13 +++++++++++++
 3 files changed, 17 insertions(+)
author maintainer
#2 lore raw
Quoted 58 lines · On Mon, 10 Jul 2023 18:04:22 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 55 lines · On Fri, 7 Jul 2023 23:03:19 +0900 "Masami Hiramatsu (Google)"…

From: Masami Hiramatsu (Google) <[email protected]>

Ensure running fprobe_exit_handler() has finished before calling rethook_free() in the unregister_fprobe() so that caller can free the fprobe right after unregister_fprobe().

unregister_fprobe() ensured that all running fprobe_entry/exit_handler() have finished by calling unregister_ftrace_function() which synchronizes RCU. But commit 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered") changed to call rethook_free() after unregister_ftrace_function(). So call rethook_stop() to make rethook disabled before unregister_ftrace_function() and ensure it again.

Here is the possible code flow that can call the exit handler after unregister_fprobe().

------
CPU1 CPU2
call unregister_fprobe(fp)
...
__fprobe_handler() rethook_hook() on probed function
unregister_ftrace_function()
return from probed function rethook hooks
find rh->handler == fprobe_exit_handler call fprobe_exit_handler()
rethook_free():

   set rh->handler = NULL;
 return from unreigster_fprobe;
                                   call fp->exit_handler() <- (*)

------

(*) At this point, the exit handler is called after returning from unregister_fprobe().

This fixes it as following;
------
CPU1 CPU2
call unregister_fprobe()
...
rethook_stop():

   set rh->handler = NULL;
                                   __fprobe_handler()
                                   rethook_hook() on probed function

unregister_ftrace_function()
return from probed function rethook hooks find rh->handler == NULL return from rethook
rethook_free()
return from unreigster_fprobe;
------

Quoted 7 lines
Quoted 3 lines
Fixes 5f81018753df ("fprobe: Release rethook after the ftrace_ops is unregistered")

Looks good.

Thank you :)

Quoted 11 lines
Quoted 5 lines

---
Changes in v2:
- Update changelog to add a problematic code flow.

Nit, for making forensic analysis easier in the future, I now always add a link to the previous version. That is:

Changes since v1: https://lore.kernel.org/linux-trace-kernel/168796344232.46347.7947681068822514750.stgit@devnote2/
- Update changelog to add a problematic code flow.

OK, I'll add it for an isolated patch too.

Thanks!

Quoted 10 lines

-- Steve

Quoted 6 lines
---
 include/linux/rethook.h |    1 +
 kernel/trace/fprobe.c   |    3 +++
 kernel/trace/rethook.c  |   13 +++++++++++++
 3 files changed, 17 insertions(+)
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>