]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
timekeeping: Fix resource leak in tk_aux_sysfs_init() error paths
authorMalaya Kumar Rout <mrout@redhat.com>
Thu, 20 Nov 2025 15:02:13 +0000 (20:32 +0530)
committerThomas Gleixner <tglx@linutronix.de>
Thu, 20 Nov 2025 15:40:48 +0000 (16:40 +0100)
tk_aux_sysfs_init() 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.

Fixes: 7b95663a3d96 ("timekeeping: Provide interface to control auxiliary clocks")
Signed-off-by: Malaya Kumar Rout <mrout@redhat.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://patch.msgid.link/20251120150213.246777-1-mrout@redhat.com
kernel/time/timekeeping.c

index 3a4d3b2e3f74092b2cd36b9e2b7fe2fce970aed6..08e0943b54da6f5e30b4a4b5c410c589ca86c9a4 100644 (file)
@@ -3060,29 +3060,32 @@ 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 = -ENOMEM;
 
        if (!tko)
-               return -ENOMEM;
+               return ret;
 
        auxo = kobject_create_and_add("aux_clocks", tko);
-       if (!auxo) {
-               kobject_put(tko);
-               return -ENOMEM;
-       }
+       if (!auxo)
+               goto err_clean;
 
        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);
+                       goto err_clean;
 
+               ret = sysfs_create_group(clk, &aux_clock_enable_attr_group);
                if (ret)
-                       return ret;
+                       goto err_clean;
        }
        return 0;
+
+err_clean:
+       kobject_put(auxo);
+       kobject_put(tko);
+       return ret;
 }
 late_initcall(tk_aux_sysfs_init);