Skip to contentSkip to conversation

net: dsa: Fix use after free in dsa_switch_remove()

PATCH Applied Florian Fainelli 3 participants 3 messages Nov 3, 2019 – Nov 6, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: Fix use after free in dsa_switch_remove()

The order in which the ports are deleted from the list and freed and the call to dsa_tree_remove_switch() is done is reversed, which leads to an use after free condition. Reverse the two: first tear down the ports and switch from the fabric, then free the ports associated with that switch fabric.

Fixes 05f294a85235 ("net: dsa: allocate ports on touch")
---
 net/dsa/dsa2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/dsa/dsa2.c +2 -1
index ff2fa3950c62..9ef2caa13f27 100644
@@ -874,12 +874,13 @@ static void dsa_switch_remove(struct dsa_switch *ds)
 	struct dsa_switch_tree *dst = ds->dst;
 	struct dsa_port *dp, *next;
 
+	dsa_tree_teardown(dst);
+
 	list_for_each_entry_safe(dp, next, &dst->ports, list) {
 		list_del(&dp->list);
 		kfree(dp);
 	}
 
-	dsa_tree_teardown(dst);
 	dsa_tree_put(dst);
 }
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Florian Fainelli

The order in which the ports are deleted from the list and freed and the call to dsa_tree_remove_switch() is done is reversed, which leads to an

s/dsa_tree_remove_switch/dsa_switch_remove/

Quoted 6 lines

use after free condition. Reverse the two: first tear down the ports and switch from the fabric, then free the ports associated with that switch fabric.

Fixes 05f294a85235 ("net: dsa: allocate ports on touch")

Thanks for reporting and fixing this!

maintainer Applied
#2 lore raw
In reply to Florian Fainelli

From: Florian Fainelli <[email protected]>
Date: Sat, 2 Nov 2019 20:13:26 -0700

Quoted 8 lines

The order in which the ports are deleted from the list and freed and the call to dsa_tree_remove_switch() is done is reversed, which leads to an use after free condition. Reverse the two: first tear down the ports and switch from the fabric, then free the ports associated with that switch fabric.

Fixes 05f294a85235 ("net: dsa: allocate ports on touch")

Applied.