batman-adv: Improve exception handling in batadv_throw_uevent()
authorMarkus Elfring <elfring@users.sourceforge.net>
Tue, 2 Jan 2024 06:52:21 +0000 (07:52 +0100)
committerSimon Wunderlich <sw@simonwunderlich.de>
Sat, 27 Jan 2024 08:13:59 +0000 (09:13 +0100)
The kfree() function was called in up to three cases by
the batadv_throw_uevent() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Thus adjust jump targets.

* Reorder kfree() calls at the end.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
Acked-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
net/batman-adv/main.c

index 5fc754b0b3f7f2ba84fdbc8195be65528c0e5d04..75119f1ffcccf31262c545fb083731c6e50ccbe8 100644 (file)
@@ -691,29 +691,31 @@ int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
                                  "%s%s", BATADV_UEV_TYPE_VAR,
                                  batadv_uev_type_str[type]);
        if (!uevent_env[0])
-               goto out;
+               goto report_error;
 
        uevent_env[1] = kasprintf(GFP_ATOMIC,
                                  "%s%s", BATADV_UEV_ACTION_VAR,
                                  batadv_uev_action_str[action]);
        if (!uevent_env[1])
-               goto out;
+               goto free_first_env;
 
        /* If the event is DEL, ignore the data field */
        if (action != BATADV_UEV_DEL) {
                uevent_env[2] = kasprintf(GFP_ATOMIC,
                                          "%s%s", BATADV_UEV_DATA_VAR, data);
                if (!uevent_env[2])
-                       goto out;
+                       goto free_second_env;
        }
 
        ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
-out:
-       kfree(uevent_env[0]);
-       kfree(uevent_env[1]);
        kfree(uevent_env[2]);
+free_second_env:
+       kfree(uevent_env[1]);
+free_first_env:
+       kfree(uevent_env[0]);
 
        if (ret)
+report_error:
                batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
                           "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
                           batadv_uev_type_str[type],