net: sparx5: Fix use after free inside sparx5_del_mact_entry
authorHoratiu Vultur <horatiu.vultur@microchip.com>
Fri, 1 Mar 2024 08:06:08 +0000 (09:06 +0100)
committerJakub Kicinski <kuba@kernel.org>
Tue, 5 Mar 2024 04:38:22 +0000 (20:38 -0800)
Based on the static analyzis of the code it looks like when an entry
from the MAC table was removed, the entry was still used after being
freed. More precise the vid of the mac_entry was used after calling
devm_kfree on the mac_entry.
The fix consists in first using the vid of the mac_entry to delete the
entry from the HW and after that to free it.

Fixes: b37a1bae742f ("net: sparx5: add mactable support")
Signed-off-by: Horatiu Vultur <horatiu.vultur@microchip.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://lore.kernel.org/r/20240301080608.3053468-1-horatiu.vultur@microchip.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/microchip/sparx5/sparx5_mactable.c

index 4af285918ea2a45cae16ca01ec1ab8fd3f5370c9..75868b3f548ec40549c02ad1ac2a3c5c388eade4 100644 (file)
@@ -347,10 +347,10 @@ int sparx5_del_mact_entry(struct sparx5 *sparx5,
                                 list) {
                if ((vid == 0 || mact_entry->vid == vid) &&
                    ether_addr_equal(addr, mact_entry->mac)) {
+                       sparx5_mact_forget(sparx5, addr, mact_entry->vid);
+
                        list_del(&mact_entry->list);
                        devm_kfree(sparx5->dev, mact_entry);
-
-                       sparx5_mact_forget(sparx5, addr, mact_entry->vid);
                }
        }
        mutex_unlock(&sparx5->mact_lock);