The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH net] net: dsa: tag_8021q: clear the context when dsa_tag_8021q_register() fails
@ 2026-11-28 18:55 Julius Bairaktaris
  2026-12-06  2:55 ` willemb
  0 siblings, 1 reply; 2+ messages in thread
From: Julius Bairaktaris @ 2026-11-28 18:55 UTC (permalink / raw)
  To: Andrew Lunn, Vladimir Oltean, netdev
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Pawel Dembicki, linux-kernel

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 <julius@bairaktaris.de>
---
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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net] net: dsa: tag_8021q: clear the context when dsa_tag_8021q_register() fails
  2026-11-28 18:55 [PATCH net] net: dsa: tag_8021q: clear the context when dsa_tag_8021q_register() fails Julius Bairaktaris
@ 2026-12-06  2:55 ` willemb
  0 siblings, 0 replies; 2+ messages in thread
From: willemb @ 2026-12-06  2:55 UTC (permalink / raw)
  To: Julius Bairaktaris, Andrew Lunn, Vladimir Oltean, netdev
  Cc: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Pawel Dembicki, linux-kernel

Julius Bairaktaris wrote:
> 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 <julius@bairaktaris.de>
> ---
> 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;

LLM feedback (obv.):

The rollback added here is incomplete: it tears down ports 0..(port - 1) and
frees ctx->vlans nodes in software, but never unwinds the ports that already
succeeded inside dsa_switch_tag_8021q_vlan_add() during the failing port's  
own
dsa_port_tag_8021q_vlan_add() notification.

1. User port succeeded before a later CPU/DSA port failed (the standard DT  
port
    order on felix/LS1028A, where user ports 0..3 precede CPU ports 4..5 in
    dst->ports):
    In dsa_switch_tag_8021q_vlan_add(), dp == info->dp runs first and invokes
    felix_tag_8021q_vlan_add(), allocating and installing VCAP ES0/IS1/IS2
    filters into hardware and ocelot->block[...].rules. User ports are not
    tracked in ctx->vlans. If the subsequent CPU port kzalloc_obj(*v) in
    dsa_port_do_tag_8021q_vlan_add() returns -ENOMEM,
    dsa_switch_tag_8021q_vlan_add() returns without deleting the user port's
    filters, and rollback: skips the failing port. Because felix keeps  
running on
    NPI after failure, those VCAP filters leak in memory and remain live in
    hardware. Worse, if the administrator later retries switching to
    ocelot-8021q and succeeds, felix_tag_8021q_vlan_add() inserts duplicate
    filters with identical id.cookie into ocelot->block[...].rules  
(incrementing
    block->count). On subsequent teardown or switch back to NPI,
    ocelot_vcap_filter_del() -> ocelot_vcap_block_remove_filter()
    (drivers/net/ethernet/mscc/ocelot_vcap.c:1229-1238) deletes both  
duplicates
    in list_for_each_entry_safe() while decrementing block->count-- only  
once;
    the immediately following compaction loop then calls
    ocelot_vcap_block_find_filter_by_index() for the out-of-range tail index,
    obtains NULL, and dereferences filter->block_id in vcap_entry_get()
    (ocelot_vcap.c:855) — a deterministic NULL pointer dereference.

2. CPU/DSA port succeeded before a later user (or second CPU/DSA) port  
failed:
    err_free only unlinks and kfree()s each struct dsa_tag_8021q_vlan from
    ctx->vlans without calling ds->ops->tag_8021q_vlan_del(ds, v->port,  
v->vid),
    leaving the hardware/driver VLAN entries installed on the CPU/DSA port(s)
    (and similarly on earlier switches in a multi-switch DSA tree).

Fix this at the source by unwinding already-added matching ports inside
dsa_switch_tag_8021q_vlan_add() on error using
dsa_switch_for_each_port_continue_reverse(dp, ds) and calling
dsa_port_do_tag_8021q_vlan_del(dp, info->vid) (which also closes the same
partial-notification gap for dsa_tag_8021q_bridge_join()). Keep the
list_for_each_entry_safe() cleanup in dsa_tag_8021q_register()'s err_free  
path
only as a fallback for entries whose tag_8021q_vlan_del() failed during
rollback (e.g. persistent SPI/MDIO bus errors), and update the comment there
accordingly.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06  2:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 18:55 [PATCH net] net: dsa: tag_8021q: clear the context when dsa_tag_8021q_register() fails Julius Bairaktaris
2026-12-06  2:55 ` willemb

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox