Skip to contentSkip to conversation

fix a potential memleak in rethook_alloc()

PATCH In review Yi Yang 2 participants 2 messages Nov 10, 2022 – Nov 11, 2022 Tracing

lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH 1/1 fix a potential memleak in rethook_alloc()

In rethook_alloc(), the variable rh is not freed or passed out if handler is NULL, which could lead to a memleak, fix it.

Fixes 54ecbe6f1ed5 ("rethook: Add a generic return hook")
---
 kernel/trace/rethook.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/rethook.c +3 -1
index 01df98db2fbe..f50aebc7698e 100644
@@ -91,8 +91,10 @@ struct rethook *rethook_alloc(void *data, rethook_handler_t handler, gfp_t gfp,
 {
 	struct rethook *rh = kzalloc(sizeof(struct rethook), GFP_KERNEL);
 
-	if (!rh || !handler)
+	if (!rh || !handler) {
+		kfree(rh);
 		return NULL;
+	}
 
 	rh->data = data;
 	rh->handler = handler;
Signature · 1 line
2.17.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · On Thu, 10 Nov 2022 18:44:38 +0800 Yi Yang <[email protected]> wrote:

In rethook_alloc(), the variable rh is not freed or passed out if handler is NULL, which could lead to a memleak, fix it.

Oops, good catch!

Thank you!

Quoted 25 lines
Fixes 54ecbe6f1ed5 ("rethook: Add a generic return hook")
---
 kernel/trace/rethook.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/rethook.c +3 -1
index 01df98db2fbe..f50aebc7698e 100644
@@ -91,8 +91,10 @@ struct rethook *rethook_alloc(void *data, rethook_handler_t handler, gfp_t gfp,
 {
 	struct rethook *rh = kzalloc(sizeof(struct rethook), GFP_KERNEL);
 
-	if (!rh || !handler)
+	if (!rh || !handler) {
+		kfree(rh);
 		return NULL;
+	}
 
 	rh->data = data;
 	rh->handler = handler;
Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>