>Synopsis: bridge_clone_destroy() empties the member and span lists, then
sleeps in if_detach(); a concurrent SIOCBRDGADD/SIOCBRDGADDS
can insert a port after the lists were emptied, and the softc
is freed with that port still attached
>Category: kernel
>Severity: serious
>Class: sw-bug
>Release: OpenBSD 8.0-current (src as of 2026-11-03)
>Environment:
System      : OpenBSD 8.0-current, GENERIC.MP as shipped (DIAGNOSTIC,
     POOL_DEBUG)
Architecture: OpenBSD.amd64
Machine     : amd64 (QEMU/KVM guest), 2 CPUs
Source      : sys/net/if_bridge.c rev 1.382, sys/net/if.c as of
     2026-11-03 (line numbers below are from these)

>Description:

bridge_clone_destroy() (if_bridge.c:221) removes the ports first and
detaches the interface afterwards:

   bridge_stop(sc);                                    /* :233 */
   bridge_rtflush(sc, IFBF_FLUSHALL);
   while (... sc_iflist ...)   bridge_ifremove(bif);   /* :236 */
   while (... sc_spanlist ...) bridge_spanremove(bif); /* :238 */
   bstp_destroy(sc->sc_stp);
   if_detach(ifp);                                     /* :242 */
   free(sc, M_DEVBUF, sizeof *sc);                     /* :244 */

Each bridge_ifremove()/bridge_spanremove() sleeps in smr_barrier()
(and bridge_ifremove() in NET_LOCK()), and if_detach() sleeps in
NET_LOCK(), net_tq_barriers() and refcnt_finalize().  Whenever the
destroy path sleeps, the KERNEL_LOCK is released.  The bridge is
still reachable by ioctls until if_remove() (if.c:1198) takes it off
the interface list; ifioctl() looks it up with if_unit() (if.c:2180)
and holds that reference until if_put() (if.c:2578).

bridge_ioctl() drops the NET_LOCK on entry (if_bridge.c:270).  Its
SIOCBRDGADD case checks the new member (if_bridgeidx, brport, span
list; :289), sleeps in NET_LOCK()/ifpromisc() (:323) and
NET_LOCK()/ifsetlro() (:339), and then inserts the port into
sc_iflist, sets ifs->if_bridgeidx, the ether brport and a detach hook
on the member (:341-351).  SIOCBRDGADDS does the same for sc_spanlist
(:402-412).  Nothing in either path, or in the destroy path, notices
that the bridge is being destroyed.

So an "ifconfig bridgeN add ifX" that runs while destroy sleeps after
its member loop -- e.g. in smr_barrier() for a span port, or early in
if_detach() -- inserts ifX after sc_iflist was emptied.  if_detach()
waits in refcnt_finalize() for that ioctl if it is still running,
and free() releases the softc while ifX still has the bridge's
brport, if_bridgeidx and a detach hook pointing at a bridge_iflist
whose bridge_sc is freed memory.  The member stays promiscuous and
"busy" (any later SIOCBRDGADD of it fails with EBUSY), and destroying
it runs bridge_ifdetach() -> bridge_ifremove(), which reads
sc->sc_iflist of the freed softc.

struct bridge_softc is 9664 bytes on amd64 GENERIC.MP (9872 with
WITNESS), more than MAXALLOCSAVE, so free() hands it to km_free() and
the pages are unmapped at once.  sc_iflist is at offset 0x5a0 (0x648
with WITNESS).  Destroying the left-over member then faults
immediately:

 uvm_fault(0xffffffff82c52298, 0xffff800000e125a0, 0, 1) -> e
 fatal page fault in supervisor mode
 trap type 6 code 0 rip ffffffff81819604 cs 8 rflags 10286
     cr2 ffff800000e125a0 cpl 0 rsp ffff800045586f60
 panic: trap type 6, code=0, pc=ffffffff81819604
 bridge_ifremove(ffff800000e17080) at bridge_ifremove+0x24
 bridge_ifdetach(ffff800000e17080) at bridge_ifdetach+0x2d
 if_hooks_run(ffff8000004490a0) at if_hooks_run+0xdb
 if_deactivate(ffff800000449000) at if_deactivate+0x34
 ether_ifdetach(ffff800000449000) at ether_ifdetach+0x21
 pair_clone_destroy(ffff800000449000) at pair_clone_destroy+0xd0
 if_clone_destroy(ffff800045587200) at if_clone_destroy+0x13f
 ifioctl(...) at ifioctl+0x1f5
 sys_ioctl(...) at sys_ioctl+0x30e

The same trace (fault at <freed softc>+0x648 on a WITNESS kernel)
also turned up under a random concurrent ifconfig workload that
creates, fills and destroys bridges.

Without the fix the member is visibly left behind.  Running the
How-To-Repeat workload below but stopping after the first bridge
destroy, without destroying the member, showed no bridge left, the
member still PROMISC, and:

 # ifconfig bridge260 create; ifconfig bridge260 add pair240
 ifconfig: bridge260: pair240: Device busy

To confirm the window, a debug-only printf was added to the second
removal loop of the fix below.  That loop only runs after
if_detach(), so anything it finds was inserted after the first
loops.  Under the How-To-Repeat workload it found the concurrently
added member once per destroy cycle (308 times in 308 cycles),
printing (after a debug tag):

 bridge240: member pair240 added during destroy

Both SIOCIFDESTROY and SIOCBRDGADD need root; no hardware is
involved.

>How-To-Repeat:

As root (pair(4) members as in the trace; the three span ports make
the destroy path sleep after its member loop) I have manually verified this
and it  is easily repeatible
Thanks folks

#!/bin/ksh
# usage: race.sh SECS   (uses bridge240-241, pair240-241, vether240-245)
T=${1:-60}; end=$(( $(date +%s) + T ))
for w in 0 1; do for s in 0 1 2; do
ifconfig vether$((240+w*3+s)) create up; done; done
worker() {
b=bridge$((240+$1)); m=pair$((240+$1))
while [ $(date +%s) -lt $end ]; do
ifconfig $m create up
ifconfig $b create
for s in 0 1 2; do ifconfig $b addspan vether$((240+$1*3+s)); done
( while :; do ifconfig $b add $m 2>/dev/null; done ) &
ifconfig $b destroy
kill $!; wait $! 2>/dev/null
ifconfig $b destroy 2>/dev/null
ifconfig $m destroy # faults if $m was left on the freed bridge
done
}
worker 0 & worker 1 &
wait; echo survived

Each worker creates a fresh pair(4) interface and a bridge with three
span ports, starts a loop that keeps running "ifconfig bridgeN add
pairN", and destroys the bridge.  It then stops the loop, destroys the
bridge again in case the loop re-created it, and destroys the pair.
On the unpatched kernel the member is normally left behind in the
very first cycle, and destroying it panics.

>Fix:

After if_detach() no ioctl can reach the bridge any more: if_remove()
has taken it off the interface list and waited for every ioctl that
still held a reference.  So run the member and span removal loops
once more after if_detach(); they remove whatever a concurrent
SIOCBRDGADD/SIOCBRDGADDS inserted while the destroy path slept.  In
the normal case the lists are already empty and nothing changes.
bstp_destroy() moves after this second pass, because removing an
STP-enabled member calls bstp_delete() on the bridge's bstp state
(bstp_destroy() panics "bstp still active" if a port is left).

Index: sys/net/if_bridge.c
===================================================================
RCS file: /cvs/src/sys/net/if_bridge.c,v
diff -u -p -r1.382 if_bridge.c
--- sys/net/if_bridge.c 2 Dec 2025 03:24:19 -0000 1.382
+++ sys/net/if_bridge.c 5 Nov 2026 15:00:00 -0000
@@ -237,10 +237,21 @@ bridge_clone_destroy(struct ifnet *ifp)
  while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_spanlist)) != NULL)
  bridge_spanremove(bif);

- bstp_destroy(sc->sc_stp);
-
  if_detach(ifp);

+ /*
+ * bridge_ioctl() runs without the NET_LOCK() and the teardown above
+ * sleeps, so a concurrent SIOCBRDGADD or SIOCBRDGADDS may have added
+ * a port after it was emptied.  if_detach() waited for those ioctls
+ * and no new one can reach the bridge, so remove such ports now.
+ */
+ while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_iflist)) != NULL)
+ bridge_ifremove(bif);
+ while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_spanlist)) != NULL)
+ bridge_spanremove(bif);
+
+ bstp_destroy(sc->sc_stp);
+
  free(sc, M_DEVBUF, sizeof *sc);
  return (0);
 }

>Verification:

amd64 KVM guest, 2 CPUs.  Pristine -current src (2026-11-03,
if_bridge.c 1.382), GENERIC.MP as shipped (DIAGNOSTIC + POOL_DEBUG).
Two kernels: without the diff, and with exactly the diff above
applied with patch -p0 to a fresh export of the same tree (nothing
else changed; the patched source tree was checked file by file
against the pristine one).  Each run is the How-To-Repeat race with
2 workers, plus a check after every cycle that adds the member to a
fresh bridge (EBUSY = member left behind); the box was checked for a
reboot or hang afterwards.

 unpatched   6 of 6 runs crashed (five 60 s runs, one 300 s run):
               page fault at <freed softc>+0x5a0 in
               bridge_ifremove() <- bridge_ifdetach() <-
               pair_clone_destroy()                             4
               fatal page fault, console output interleaved
               with another CPU (no trace), box hung in ddb
               (reset)                                          2
 patched     6 of 6 runs completed (five 60 s, one 300 s), 516
             create/addspan/add/destroy cycles under the concurrent
             "add", no member left behind (the per-cycle check
             never saw EBUSY), no panic or fault in dmesg

The How-To-Repeat script exactly as shown above: crashed the
unpatched kernel in a 60 s run and ran 60 s clean on the patched one.

With the debug printf described above added to the patched kernel,
the second removal loop found and removed a late member in 308 of
308 destroy cycles (60 s + 300 s runs), without any crash.

Reply via email to