mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH objtool/urgent] klp-build: Fix memory leak when enabling the generated livepatch fails
@ 2026-10-16 12:23 Yafang Shao
  2026-11-25  8:40 ` [tip: objtool/core] klp-build: Fix memory leak in livepatch module init error path tip-bot2 for Yafang Shao
  0 siblings, 1 reply; 2+ messages in thread
From: Yafang Shao @ 2026-10-16 12:23 UTC (permalink / raw)
  To: jpoimboe, jikos, mbenes, pmladek, joe.lawrence, song
  Cc: live-patching, linux-kernel, Yafang Shao, sashiko-bot

The klp-build generated livepatch module allocates the patch structure,
the array of objects, and the per-object arrays of functions with
kzalloc() in livepatch_mod_init().

The result of klp_enable_patch() is returned directly. It bypasses
the err_free_objs cleanup path when the call fails. As a result, all the
allocated structures leak.

Save the return value and go into the error path on failure.

The per-object arrays of functions must be freed in the shared
err_free_objs path as well. The cleanup loop in the funcs allocation
failure path is removed instead. Otherwise, it would cause a double
free when the new error path jumps into the shared cleanup.

Fixes: 59adee07b568 ("livepatch/klp-build: Add stub init code for livepatch modules")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-173837@kernel.org/
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 scripts/livepatch/init.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

---

It is based on tip's objtool/urgent branch.

diff --git a/scripts/livepatch/init.c b/scripts/livepatch/init.c
index 16aff8f..a1f6993 100644
--- a/scripts/livepatch/init.c
+++ b/scripts/livepatch/init.c
@@ -50,8 +50,6 @@ static int __init livepatch_mod_init(void)
 		funcs = kzalloc(sizeof(struct klp_func) * (nr_funcs + 1), GFP_KERNEL);
 		if (!funcs) {
 			ret = -ENOMEM;
-			for (int j = 0; j < i; j++)
-				kfree(objs[j].funcs);
 			goto err_free_objs;
 		}
 
@@ -78,9 +76,14 @@ static int __init livepatch_mod_init(void)
 	patch->replace = true;
 #endif
 
-	return klp_enable_patch(patch);
+	ret = klp_enable_patch(patch);
+	if (ret)
+		goto err_free_objs;
+	return 0;
 
 err_free_objs:
+	for (int i = 0; i < nr_objs; i++)
+		kfree(objs[i].funcs);
 	kfree(objs);
 err_free_patch:
 	kfree(patch);
-- 
1.8.3.1


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

* [tip: objtool/core] klp-build: Fix memory leak in livepatch module init error path
  2026-10-16 12:23 [PATCH objtool/urgent] klp-build: Fix memory leak when enabling the generated livepatch fails Yafang Shao
@ 2026-11-25  8:40 ` tip-bot2 for Yafang Shao
  0 siblings, 0 replies; 2+ messages in thread
From: tip-bot2 for Yafang Shao @ 2026-11-25  8:40 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: sashiko-bot, Yafang Shao, Josh Poimboeuf, x86, linux-kernel

The following commit has been merged into the objtool/core branch of tip:

Commit-ID:     b99a5337da3c847db216476d645b69acd714c3ba
Gitweb:        https://git.kernel.org/tip/b99a5337da3c847db216476d645b69acd714c3ba
Author:        Yafang Shao <laoar.shao@gmail.com>
AuthorDate:    Fri, 16 Oct 2026 20:23:14 +08:00
Committer:     Josh Poimboeuf <jpoimboe@kernel.org>
CommitterDate: Sat, 21 Nov 2026 23:38:33 -08:00

klp-build: Fix memory leak in livepatch module init error path

The klp-build generated livepatch module allocates the patch structure,
the array of objects, and the per-object arrays of functions with
kzalloc() in livepatch_mod_init().

The result of klp_enable_patch() is returned directly. It bypasses
the err_free_objs cleanup path when the call fails. As a result, all the
allocated structures leak.

Save the return value and go into the error path on failure.

The per-object arrays of functions must be freed in the shared
err_free_objs path as well. The cleanup loop in the funcs allocation
failure path is removed instead. Otherwise, it would cause a double
free when the new error path jumps into the shared cleanup.

Fixes: 59adee07b568 ("livepatch/klp-build: Add stub init code for livepatch modules")
Reported-by: sashiko-bot <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-173837@kernel.org/
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Link: https://patch.msgid.link/20261016122314.81476-1-laoar.shao@gmail.com
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 scripts/livepatch/init.c |  9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/scripts/livepatch/init.c b/scripts/livepatch/init.c
index df041fe..097c11f 100644
--- a/scripts/livepatch/init.c
+++ b/scripts/livepatch/init.c
@@ -50,8 +50,6 @@ static int __init livepatch_mod_init(void)
 		funcs = kzalloc_objs(*funcs, nr_funcs + 1);
 		if (!funcs) {
 			ret = -ENOMEM;
-			for (int j = 0; j < i; j++)
-				kfree(objs[j].funcs);
 			goto err_free_objs;
 		}
 
@@ -78,9 +76,14 @@ static int __init livepatch_mod_init(void)
 	patch->replace = true;
 #endif
 
-	return klp_enable_patch(patch);
+	ret = klp_enable_patch(patch);
+	if (ret)
+		goto err_free_objs;
+	return 0;
 
 err_free_objs:
+	for (int i = 0; i < nr_objs; i++)
+		kfree(objs[i].funcs);
 	kfree(objs);
 err_free_patch:
 	kfree(patch);

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

end of thread, other threads:[~2026-11-25  8:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-16 12:23 [PATCH objtool/urgent] klp-build: Fix memory leak when enabling the generated livepatch fails Yafang Shao
2026-11-25  8:40 ` [tip: objtool/core] klp-build: Fix memory leak in livepatch module init error path tip-bot2 for Yafang Shao

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

all inboxes | Powered by JetHome®