* [PATCH net] net: dsa: clean up conduit state on setup failure
@ 2026-12-02 3:30 Hu Xinyao
2026-12-03 3:31 ` netdev-bot+sashiko
0 siblings, 1 reply; 2+ messages in thread
From: Hu Xinyao @ 2026-12-02 3:30 UTC (permalink / raw)
To: Andrew Lunn, Vladimir Oltean
Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Willem de Bruijn, Simon Horman, netdev, linux-kernel, Hu Xinyao
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: stable@vger.kernel.org
Signed-off-by: Hu Xinyao <huxinyao@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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH net] net: dsa: clean up conduit state on setup failure
2026-12-02 3:30 [PATCH net] net: dsa: clean up conduit state on setup failure Hu Xinyao
@ 2026-12-03 3:31 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-03 3:31 UTC (permalink / raw)
To: huxinyao
Cc: andrew, olteanv, davem, edumazet, kuba, pabeni, willemb, horms,
netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [High] On failure, dsa_conduit_setup() (net/dsa/conduit.c:470-476) now
always resets the conduit MTU to ETH_DATA_LEN and writes `dev->dsa_ptr…
- [Low] The new error-path comment in dsa_conduit_setup()
(net/dsa/conduit.c:473-475) is inaccurate and claims more than the…
--
net: dsa: clean up conduit state on setup failure
This patch clears dev->dsa_ptr and resets the conduit MTU on the
dsa_conduit_setup() error path. It also adds a teardown loop in
dsa_tree_setup_conduit(), so CPU conduits that were already set up are
torn down when a later one fails. The goal is to avoid a use-after-free
of cpu_dp once dsa_switch_release_ports() has freed it.
> diff --git a/net/dsa/conduit.c b/net/dsa/conduit.c
> index 1470885d5a5ee..4a56585531bd4 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;
[Severity: High]
Can this wipe out state that belongs to another CPU port?
The error path now always resets the MTU to ETH_DATA_LEN and writes NULL
to dev->dsa_ptr. It does not restore the value dsa_ptr had before
dsa_conduit_setup() was called.
dsa_port_parse_cpu() does not seem to reject a conduit that another CPU
port already uses, whether in the same tree or in a different one. In
that setup the second dsa_conduit_setup() always fails, because the "dsa"
sysfs group already exists on the conduit:
ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
if (ret)
goto out_err_ethtool_teardown;
sysfs_create_group() returns -EEXIST, and this error path then sets the
shared conduit's dsa_ptr to NULL. Back in dsa_tree_setup_conduit(), the
new teardown loop runs for the earlier CPU port on the same conduit:
dsa_tree_conduit_admin_state_change(dst, conduit, false);
dsa_conduit_teardown(conduit);
dsa_tree_conduit_admin_state_change() then does:
struct dsa_port *cpu_dp = conduit->dsa_ptr;
...
if ((dsa_port_conduit_is_operational(cpu_dp)) !=
(up && cpu_dp->conduit_oper_up))
Wouldn't this dereference NULL through
dsa_port_conduit_is_operational()->dsa_port_is_cpu(), with rtnl_lock and
dsa2_mutex held?
Even if that call were skipped, dsa_conduit_teardown() would hit the same
NULL in dsa_conduit_ethtool_teardown() and dsa_conduit_set_promiscuity().
The cross-tree case has a similar problem. If tree B fails to probe on a
conduit that tree A is using, tree A's dsa_ptr is cleared and its MTU is
dropped, so tree A's tagged traffic goes through the normal receive path.
Tree A's later dsa_tree_teardown_conduit() would then hit the same NULL
dereference.
Before this patch, this setup left a dangling dsa_ptr. With the patch it
crashes every time inside the new teardown loop.
Could the previous dsa_ptr and MTU be saved and restored here? Another
option is to reject a conduit that already has dsa_ptr set before this
point.
> + /* Pair with wmb() above so packets stop entering the DSA
> + * receive path before cpu_dp is freed by the caller.
> + */
> + wmb();
[Severity: Low]
Is this comment accurate?
The wmb() above, before dev->dsa_ptr = cpu_dp, is also a write barrier
on the same CPU, so the two cannot pair with each other. A barrier pairs
with a read barrier or address dependency on another CPU.
Also, a wmb() after the NULL store does not wait for receive paths that
have already loaded the old pointer. dsa_switch_rcv() uses a plain load:
struct dsa_port *cpu_dp = dev->dsa_ptr;
and netdev_uses_dsa() does:
return dev->dsa_ptr && dev->dsa_ptr->rcv;
Either one may still be using cpu_dp when the caller frees it in
dsa_switch_release_ports():
list_del(&dp->list);
kfree(dp);
The matching barrier in dsa_conduit_teardown() only claims that later
packets take the regular receive path. Could this comment be reworded to
say the same thing? Protecting readers that are already in flight would
need something else, such as an RCU grace period.
> return ret;
> }
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/6323744BEA8D6888%2B1796182219.53640-1-huxinyao%40kylinsec.com.cn
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 3:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02 3:30 [PATCH net] net: dsa: clean up conduit state on setup failure Hu Xinyao
2026-12-03 3:31 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®