Netdev List
 help / color / mirror / Atom feed
From: Julius Bairaktaris <julius@bairaktaris.de>
To: Andrew Lunn <andrew@lunn.ch>, Vladimir Oltean <olteanv@gmail.com>
Cc: netdev@vger.kernel.org, bridge@lists.linux.dev,
	"David S . Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Willem de Bruijn" <willemb@google.com>,
	"Simon Horman" <horms@kernel.org>,
	"Nikolay Aleksandrov" <razor@blackwall.org>,
	"Ido Schimmel" <idosch@nvidia.com>,
	"Alvin Šipraga" <alvin.sipraga@analog.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Tobias Waldekranz" <tobias@waldekranz.com>,
	"Linus Walleij" <linusw@kernel.org>,
	"Pawel Dembicki" <paweldembicki@gmail.com>
Subject: [PATCH net v3 2/3] net: dsa: hold the device a queued FDB event is reported to
Date: Fri,  4 Dec 2026 23:20:44 +0100	[thread overview]
Message-ID: <20261204222045.214014-3-julius@bairaktaris.de> (raw)
In-Reply-To: <20261204222045.214014-1-julius@bairaktaris.de>

dsa_user_fdb_event() queues an FDB event on the DSA workqueue together
with the device the event was emitted for (orig_dev), and
dsa_user_switchdev_event_work() reports an added entry to that device
with SWITCHDEV_FDB_OFFLOADED once the switch is programmed. No
reference is held on orig_dev. It is a DSA user port only when the
entry is on one: for an entry on another port of the bridge, which DSA
installs as a host address, it is that port. DSA flushes its workqueue
before one of its own ports leaves the bridge, but nothing waits for
the queue when a foreign bridge port is unregistered, and a work that
runs after the device was freed reads it in br_switchdev_event():

  BUG: KASAN: slab-use-after-free in br_switchdev_event+0x41b/0x550
  Read of size 8 at addr ffff888004d00000 by task kworker/u16:0/13
  Workqueue: dsa_ordered dsa_user_switchdev_event_work
  Call Trace:
   br_switchdev_event+0x41b/0x550
   notifier_call_chain+0x9a/0x2a0
   atomic_notifier_call_chain+0x5a/0x110
   dsa_user_switchdev_event_work+0x32c/0x4c0
   process_one_work+0x8e1/0x1640
  Freed by task 139:
   kfree+0x1b7/0x550
   device_release+0xca/0x250

Hold a reference on orig_dev while the event is queued, as mlxsw,
prestera, sparx5 and cpsw do for the device of their deferred FDB
events. Unregistering the device then waits for the work, which does
not take RTNL.

DSA held the device until commit 425d19cedef8 ("net: dsa: stop calling
dev_hold in dsa_slave_fdb_event"), when the report still went to the
DSA user port.

Fixes: 93c798230af5 ("net: dsa: call SWITCHDEV_FDB_OFFLOADED for the orig_dev")
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
---
Found by an LLM review (Claude) of a DSA FDB change of mine. In QEMU
with KASAN, with dsa_loop given FDB operations, assisted learning on
the CPU port and a 500 ms delay in its FDB add (test-only changes),
deleting a veth bridge port right after an address was learned on it
gives the report above in 3 of 3 boots, and nothing with this change
in 3 of 3 boots.

v3:
- No change; rebased on net e9d78fc6741a.
v2:
- Rebased on patch 1, which changes the same exit of
  dsa_user_switchdev_event_work(): its out: label is above the
  netdev_put() added here, so an event it drops still releases the
  device. NIPA could not apply v1 after patch 1.
- The message says how the bug was found.
v2: https://lore.kernel.org/all/20261128133503.3887987-3-julius@bairaktaris.de/
v1: https://lore.kernel.org/all/20261122173347.791590-1-julius@bairaktaris.de/

 net/dsa/user.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/dsa/user.c b/net/dsa/user.c
index 81f0d3901209..2073129b9f87 100644
--- a/net/dsa/user.c
+++ b/net/dsa/user.c
@@ -35,6 +35,7 @@
 struct dsa_switchdev_event_work {
 	struct net_device *dev;
 	struct net_device *orig_dev;
+	netdevice_tracker orig_dev_tracker;
 	struct work_struct work;
 	unsigned long event;
 	/* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
@@ -3697,6 +3698,7 @@ static void dsa_user_switchdev_event_work(struct work_struct *work)
 	}
 
 out:
+	netdev_put(switchdev_work->orig_dev, &switchdev_work->orig_dev_tracker);
 	kfree(switchdev_work);
 }
 
@@ -3775,6 +3777,10 @@ static int dsa_user_fdb_event(struct net_device *dev,
 	switchdev_work->event = event;
 	switchdev_work->dev = dev;
 	switchdev_work->orig_dev = orig_dev;
+	/* The work reports to orig_dev, which can be a foreign bridge port
+	 * that is unregistered before the work runs
+	 */
+	netdev_hold(orig_dev, &switchdev_work->orig_dev_tracker, GFP_ATOMIC);
 
 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
 	switchdev_work->vid = fdb_info->vid;
-- 
2.53.0


  parent reply	other threads:[~2026-12-04 22:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 22:20 [PATCH net v3 0/3] net: dsa: FDB events and tagging after a port left its bridge Julius Bairaktaris
2026-12-04 22:20 ` [PATCH net v3 1/3] net: dsa: do not run queued FDB events after the port left the bridge Julius Bairaktaris
2026-12-04 22:20 ` Julius Bairaktaris [this message]
2026-12-04 22:20 ` [PATCH net v3 3/3] net: dsa: forward a marked frame only while its port is in a bridge Julius Bairaktaris
2026-12-05 22:44   ` netdev-bot+sashiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204222045.214014-3-julius@bairaktaris.de \
    --to=julius@bairaktaris.de \
    --cc=alvin.sipraga@analog.com \
    --cc=andrew@lunn.ch \
    --cc=bridge@lists.linux.dev \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linusw@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=paweldembicki@gmail.com \
    --cc=razor@blackwall.org \
    --cc=tobias@waldekranz.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox