In dsa_conduit_setup(), dev->dsa_ptr is assigned to cpu_dp after
raising the conduit MTU and before dsa_conduit_ethtool_setup() and
sysfs_create_group(). However, the error path only tears down ethtool
ops and decrements promiscuity, leaving dev->dsa_ptr dangling at cpu_dp
and leaving the conduit MTU raised.
When dsa_tree_setup_conduit() propagates that error to
dsa_tree_setup(), it skips teardown_conduit and calls
dsa_switch_release_ports(), which frees cpu_dp while the conduit netdev
remains registered and active on RX (netdev_uses_dsa() /
cpu_dp->rcv()), causing a use-after-free on incoming packets.
Additionally, on multi-CPU port trees, if setting up a subsequent CPU
port fails inside dsa_tree_setup_conduit(), earlier CPU ports remain
attached without calling dsa_conduit_teardown().
Clear dev->dsa_ptr (followed by wmb() matching dsa_conduit_teardown())
and reset the conduit MTU on dsa_conduit_setup() failure, and tear down
previously configured CPU conduits on dsa_tree_setup_conduit() failure.
Found during a GLM-5.4-assisted code audit and verified manually against
mainline; compile-tested only (KCFLAGS="-Werror" on net/dsa/conduit.o
and net/dsa/dsa.o).
Fixes: 17a22fcfc84a ("net: dsa: setup and teardown master device")
Fixes: 0cfec588ec21 ("net: dsa: use ports list to setup multiple master devices")
Cc: stab…@vger.kernel.org
Signed-off-by: Hu Xinyao <huxi…@kylinsec.com.cn>
---
net/dsa/conduit.c | 6 ++++++
net/dsa/dsa.c | 19 +++++++++++++++++--
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/net/dsa/conduit.c b/net/dsa/conduit.c
index 1470885..4a56585 100644
--- a/net/dsa/conduit.c
+++ b/net/dsa/conduit.c
@@ -467,7 +467,13 @@ int dsa_conduit_setup(struct net_device *dev, struct dsa_port *cpu_dp)
out_err_ethtool_teardown:
dsa_conduit_ethtool_teardown(dev);
out_err_reset_promisc:
+ dsa_conduit_reset_mtu(dev);
dsa_conduit_set_promiscuity(dev, -1);
+ dev->dsa_ptr = NULL;
+ /* Pair with wmb() above so packets stop entering the DSA
+ * receive path before cpu_dp is freed by the caller.
+ */
+ wmb();
return ret;
}
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 2771c34..3622030 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -786,7 +786,7 @@ static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
static int dsa_tree_setup_conduit(struct dsa_switch_tree *dst)
{
- struct dsa_port *cpu_dp;
+ struct dsa_port *cpu_dp, *prev_dp;
int err = 0;
rtnl_lock();
@@ -798,7 +798,7 @@ static int dsa_tree_setup_conduit(struct dsa_switch_tree *dst)
err = dsa_conduit_setup(conduit, cpu_dp);
if (err)
- break;
+ goto teardown;
/* Replay conduit state event */
dsa_tree_conduit_admin_state_change(dst, conduit, admin_up);
@@ -808,6 +808,21 @@ static int dsa_tree_setup_conduit(struct dsa_switch_tree *dst)
rtnl_unlock();
+ return 0;
+
+teardown:
+ dsa_tree_for_each_cpu_port(prev_dp, dst) {
+ struct net_device *conduit = prev_dp->conduit;
+
+ if (prev_dp == cpu_dp)
+ break;
+
+ dsa_tree_conduit_admin_state_change(dst, conduit, false);
+ dsa_conduit_teardown(conduit);
+ }
+
+ rtnl_unlock();
+
return err;
}
--
2.43.0