Skip to contentSkip to conversation

net: bridge: flush FDB again after unregistering the RX handler

PATCH In review Cheng Yongkang 2 participants 3 messages Oct 22 – Oct 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: bridge: flush FDB again after unregistering the RX handler

del_nbp() flushes the port's FDB entries before it unregisters the RX handler. For a normal port, br_stp_disable_port() has already moved the port to BR_STATE_DISABLED, so no frame is learned in between.

That does not hold for an MRP ring port. br_set_state() ignores state changes while BR_MRP_AWARE_BIT is set, so br_stp_disable_port() leaves the port state alone, and br_mrp_port_del() then sets the port to BR_STATE_FORWARDING (when the bridge is up) before clearing the bit. A frame received between br_fdb_delete_by_port() and netdev_rx_handler_unregister() is learned with dst == p. Nothing removes that entry before destroy_nbp_rcu() frees the port, and a later frame to the learned MAC is forwarded to the freed port:

  BUG: KASAN: slab-use-after-free in br_forward+0x55/0x3c0
  Read of size 8 at addr ffff888079f4e038 by task br005-poc/9379
  Call Trace:
   <IRQ>
   br_forward+0x55/0x3c0
   br_handle_frame_finish+0x1300/0x1d40
   br_handle_frame+0x90a/0x15c0
   __netif_receive_skb_core+0x953/0x3040

Commit 8dca36978aa8 ("net: bridge: fix use-after-free due to MST port state bypass") fixed the same race for MST, which bypasses the port state in a similar way.

Flush the port's FDB entries again after netdev_rx_handler_unregister(). Once it returns, no frame received on the port can be learned, so no new entry can point to the port before it is freed.

MRP ring ports can be created when CONFIG_NET_SWITCHDEV is disabled, or through a switchdev driver that offloads MRP. The race was reproduced on a KASAN kernel with CONFIG_NET_SWITCHDEV=y, using a test module that accepts the MRP switchdev objects and holds del_nbp() between the FDB flush and the RX handler unregistration.

Fixes 6536993371fa ("bridge: mrp: Integrate MRP into the bridge")
Assisted-by LLM
---
 net/bridge/br_if.c | 7 +++++++
 1 file changed, 7 insertions(+)
net/bridge/br_if.c +7 -0
index c52613431f88..8e774329af01 100644
@@ -367,6 +367,13 @@ static void del_nbp(struct net_bridge_port *p)
 
 	netdev_rx_handler_unregister(dev);
 
+	/* The first flush can race with an MRP-aware port whose state was
+	 * restored to forwarding by br_mrp_port_del().  The handler
+	 * unregistration above waits for all active receive paths, so flush
+	 * once more before the port is queued for RCU free.
+	 */
+	br_fdb_delete_by_port(br, p, 0, 1);
+
 	br_multicast_del_port(p);
 
 	kobject_uevent(&p->kobj, KOBJ_REMOVE);

base-commit: f4377bf85887f57b59abb5479f7660a81ffe4449

Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 8 lines · Cheng Yongkang

del_nbp() flushes the port's FDB entries before it unregisters the RX handler. For a normal port, br_stp_disable_port() has already moved the port to BR_STATE_DISABLED, so no frame is learned in between.

That does not hold for an MRP ring port. br_set_state() ignores state changes while BR_MRP_AWARE_BIT is set, so br_stp_disable_port() leaves the port state alone, and br_mrp_port_del() then sets the port to BR_STATE_FORWARDING (when the bridge is up) before clearing the bit.

Same root cause as [1] where I suggested moving br_mrp_port_del() before br_stp_disable_port() so that the port is actually disabled. That would also fix this issue as the port would be disabled before the synchronize_net() in nbp_vlan_group_unpublish() and the FDB flush that follows it. Can you test it with your reproducer?

[1] https://lore.kernel.org/netdev/20261012123051.GA714215@shredder/

#2 lore raw

Hi Ido,

I'm sorry for not getting back to you on this patch sooner — I only found your reply when I went through my spam folder.

I tested your suggestion. I applied exactly the reorder from [1] (move br_mrp_port_del() and br_cfm_port_del() before br_stp_disable_port()), and ran the same reproducer: a diagnostic switchdev endpoint keeps del_nbp() between br_fdb_delete_by_port() and netdev_rx_handler_unregister() for 1.5 s while a flood keeps sending frames; the test then waits for destroy_nbp_rcu() and sends frames to the learned MACs.

On an unpatched v7.3 KASAN kernel the port stays in BR_STATE_FORWARDING, new entries with dst == the dying port are learned in that window, and the trigger reports:

  BUG: KASAN: slab-use-after-free in br_forward+0x55/0x3c0
  Read of size 8 at addr ffff8880200bf038 by task mrp-poc/9420
  ...
    br_forward
    br_handle_frame_finish
    br_handle_frame

With the reorder the same window is still exercised in all six cycles (the 1.5 s hold is reached every time), the port is disabled before the synchronize_net() and the FDB flush, and there is no KASAN report. A normal (non-MRP) bridge regression (two veth pairs in separate netns, ping, FDB learning, port detach and bridge deletion) also passes.

So the reorder is sufficient and the second flush is not needed. I have prepared a v2 implementing it and will send it as a separate patch.

Thanks,
Cheng Yongkang