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 leaks the CPU and DSA port entries of the context, and the ports set up before the failing one keep their standalone VLAN on the user port, the CPU port and the conduit's RX filter. This also happens when sja1105 or vsc73xx fail to probe on a bus error. Tear down the ports that were set up when one fails, free the CPU and DSA port entries the failing port left, and clear ds->tag_8021q_ctx before freeing the context. Found by an LLM review (Claude), and reproduced in QEMU with dsa_loop. 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 --- How it is triggered: a local administrator changes a felix switch to ocelot-8021q through dsa/tagging in sysfs and an allocation fails during the change; the use-after-free then needs a bridge join or leave on an sja1105 or vsc73xx switch in another DSA tree of the same system. The leaks also follow an SPI or MDIO error while sja1105 or vsc73xx probe. Reachability: felix for the use-after-free, sja1105 and vsc73xx at probe for the leaks. Impact: use-after-free read and write of struct dsa_8021q_context; otherwise leaked memory and conduit VLAN filter entries. CVSS 3.1: 4.1 AV:L/AC:H/PR:H/UI:N/S:U/C:N/I:N/A:H. Workaround: none short of the fix. 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 (a test-only change): on net 28a371228, writing vsc73xx-8021q to dsa/tagging fails with -EIO and the following rmmod oopses with a KASAN "general protection fault" in dsa_switch_tag_8021q_vlan_del(); with the fix the write fails the same way and rmmod is clean (one run). I used Claude (Anthropic) to help write these patches. 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 f91646e6024..68a82a545eb 100644 --- a/net/dsa/tag_8021q.c +++ b/net/dsa/tag_8021q.c @@ -398,11 +398,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) @@ -417,6 +423,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; @@ -438,6 +445,13 @@ int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto) return 0; err_free: + /* The CPU and DSA port entries of the port that failed */ + 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; } base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f -- 2.53.0