public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths
@ 2025-11-10  7:00 Malaya Kumar Rout
  2025-11-14 18:41 ` Thomas Gleixner
  0 siblings, 1 reply; 6+ messages in thread
From: Malaya Kumar Rout @ 2025-11-10  7:00 UTC (permalink / raw)
  To: linux-kernel
  Cc: mrout, lyude, malayarout91, John Stultz, Thomas Gleixner,
	Stephen Boyd

The tk_aux_sysfs_init() function returns immediately on error during
the auxiliary clock initialization loop without cleaning up previously
allocated kobjects and sysfs groups.

If kobject_create_and_add() or sysfs_create_group() fails during loop
iteration, the parent kobjects (tko and auxo) and any previously created
child kobjects are leaked.

Fix this by adding proper error handling with goto labels to ensure all
allocated resources are cleaned up on failure. kobject_put() on the
parent kobjects will handle cleanup of their children.

Signed-off-by: Malaya Kumar Rout <mrout@redhat.com>
---
 kernel/time/timekeeping.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 3a4d3b2e3f74..79019b25f188 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -3060,29 +3060,37 @@ static const struct attribute_group aux_clock_enable_attr_group = {
 static int __init tk_aux_sysfs_init(void)
 {
 	struct kobject *auxo, *tko = kobject_create_and_add("time", kernel_kobj);
+	int ret;
 
 	if (!tko)
 		return -ENOMEM;
 
 	auxo = kobject_create_and_add("aux_clocks", tko);
 	if (!auxo) {
-		kobject_put(tko);
-		return -ENOMEM;
+		ret = -ENOMEM;
+		goto err_put_tko;
 	}
 
 	for (int i = 0; i < MAX_AUX_CLOCKS; i++) {
 		char id[2] = { [0] = '0' + i, };
 		struct kobject *clk = kobject_create_and_add(id, auxo);
 
-		if (!clk)
-			return -ENOMEM;
-
-		int ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
+		if (!clk) {
+			ret = -ENOMEM;
+			goto err_put_auxo;
+		}
 
+		ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
 		if (ret)
-			return ret;
+			goto err_put_auxo;
 	}
 	return 0;
+
+err_put_auxo:
+	kobject_put(auxo);
+err_put_tko:
+	kobject_put(tko);
+	return ret;
 }
 late_initcall(tk_aux_sysfs_init);
 
-- 
2.51.0


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

end of thread, other threads:[~2025-11-20 15:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-10  7:00 [PATCH] timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths Malaya Kumar Rout
2025-11-14 18:41 ` Thomas Gleixner
2025-11-16 15:49   ` Malaya Kumar Rout
2025-11-16 17:14     ` Thomas Gleixner
2025-11-20 15:02       ` [PATCH v2] " Malaya Kumar Rout
2025-11-20 15:44         ` [tip: timers/urgent] " tip-bot2 for Malaya Kumar Rout

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