[PATCH net v2 2/2] net: dsa: tag_8021q: clear the context when dsa_tag_8021q_register() fails
From: Julius Bairaktaris
Date: Sun Dec 06 2026 - 03:32:05 EST
dsa_tag_8021q_register() points ds->tag_8021q_ctx at the new context
before it sets the ports up, and on a failure frees the context but
leaves the pointer. dsa_switch_tag_8021q_vlan_add() and
dsa_switch_tag_8021q_vlan_del() read a non-NULL ds->tag_8021q_ctx as
"this switch uses tag_8021q", so a later tag_8021q VLAN notification
walks the freed ctx->vlans and can add an entry to it.
felix keeps running after such a failure: when the change to
ocelot-8021q fails, it stays on its NPI tagger. A bridge join or leave
on an sja1105 or vsc73xx port in another tree then broadcasts a
tag_8021q VLAN, which reaches the freed context through felix's CPU
port.
The failure also leaves the ports set up before the failing one with
their standalone VLAN on the user port, the CPU port and the conduit's
RX filter. This also happens when sja1105, vsc73xx or mt7628 fail to
set up tag_8021q at probe.
Tear down the ports that were set up when one fails, and clear
ds->tag_8021q_ctx before freeing the context. The failing port itself
leaves nothing behind, since dsa_switch_tag_8021q_vlan_add() undoes its
partial work. Free what is still in ctx->vlans: entries the rollback
could not delete, because a deletion failed on this switch or the
notification stopped at an earlier one.
Fixes: c64b9c05045a ("net: dsa: tag_8021q: add proper cross-chip notifier support")
Fixes: e095493091e8 ("net: dsa: tag_8021q: avoid leaking ctx on dsa_tag_8021q_register() error path")
Signed-off-by: Julius Bairaktaris <julius@xxxxxxxxxxxxxx>
---
v2:
- The failing port's own partial add is undone by patch 1. The purge
in err_free stays for entries the rollback could not delete, and
its comment says so (Sashiko, Willem de Bruijn).
- Name mt7628 among the drivers that set up tag_8021q at probe
(Sashiko).
- Shorter notes; rebased on net 6902a2bb708.
Reproduced in QEMU (x86_64, KASAN) with dsa_loop given the tag_8021q
ops, a change_tag_protocol that registers tag_8021q, a teardown that
unregisters it while ds->tag_8021q_ctx is set, and a
tag_8021q_vlan_add() that fails for port 2 (test-only): on net
6902a2bb708, writing vsc73xx-8021q to dsa/tagging oopses with a KASAN
general protection fault in dsa_switch_tag_8021q_vlan_del() before the
write returns; with the series the write fails with -EIO and the
following rmmod is clean (one run each).
net/dsa/tag_8021q.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/net/dsa/tag_8021q.c b/net/dsa/tag_8021q.c
index 78c9fe735e8..e7a4a637ad5 100644
--- a/net/dsa/tag_8021q.c
+++ b/net/dsa/tag_8021q.c
@@ -405,11 +405,17 @@ static int dsa_tag_8021q_setup(struct dsa_switch *ds)
dev_err(ds->dev,
"Failed to setup VLAN tagging for port %d: %pe\n",
port, ERR_PTR(err));
- return err;
+ goto rollback;
}
}
return 0;
+
+rollback:
+ while (port-- > 0)
+ dsa_tag_8021q_port_teardown(ds, port);
+
+ return err;
}
static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
@@ -424,6 +430,7 @@ static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
{
+ struct dsa_tag_8021q_vlan *v, *n;
struct dsa_8021q_context *ctx;
int err;
@@ -445,6 +452,13 @@ int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
return 0;
err_free:
+ /* Entries the rollback could not delete */
+ list_for_each_entry_safe(v, n, &ctx->vlans, list) {
+ list_del(&v->list);
+ kfree(v);
+ }
+
+ ds->tag_8021q_ctx = NULL;
kfree(ctx);
return err;
}
--
2.53.0