On Sat, Nov 07, 2026 at 12:55:45AM +0000, [email protected] wrote:
> >Synopsis:    bridge_clone_destroy() empties the span list one port at a
>               time and sleeps in smr_barrier() for each; a concurrent
>               SIOCBRDGADDS puts the port back during that sleep, so the loop
>               does not end while someone keeps adding, and if_cloners_lock
>               is held the whole time
> >Category:    kernel
> >Severity:    non-critical
> >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, if_bridge.h rev 1.78,
>                     sys/net/if.c as of 2026-11-03 (line numbers below
>                     are from these)
> 
> >Description:
> 
>       if_clone_destroy() takes if_cloners_lock for writing (if.c:1445) and
>       calls bridge_clone_destroy() (if_bridge.c:221), which empties the
>       member and span lists like this:
> 
>           while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_iflist)) != NULL)
>               bridge_ifremove(bif);                           /* :235 */
>           while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_spanlist)) != NULL)
>               bridge_spanremove(bif);                         /* :237 */
> 
>       bridge_spanremove() unlinks the port and then sleeps in
>       smr_barrier() (:585), which releases the KERNEL_LOCK.  The bridge
>       is still on the interface list, so ioctls still reach
>       bridge_ioctl(), which drops the NET_LOCK on entry (:270).
>       SIOCBRDGADDS (:364) then checks only that the port is an Ethernet
>       interface, that its if_bridgeidx is 0 (a span port never sets it)
>       and that it is not already in sc_spanlist (:385; it was just
>       unlinked), and inserts it again (:412).  Nothing in SIOCBRDGADDS
>       knows that the bridge is being destroyed.
> 
>       When smr_barrier() returns, the loop reads the head of sc_spanlist
>       again, finds the re-added port, removes it and sleeps again.  In
>       testing, each sleep was long enough for another "ifconfig bridgeN
>       addspan ifX" to get in, so as long as a process kept re-adding the
>       port the loop did not end.  The destroying process sits in
>       "smrbar", and since it holds if_cloners_lock, every other interface
>       create or destroy on the system (any driver) blocks in "clonelk"
>       behind it:
> 
>         PID STAT   WCHAN        ELAPSED COMMAND
>        2888 DU     smrbar         00:20 ifconfig bridge240 destroy
>       78813 DU     clonelk        00:18 ifconfig vether241 create
> 
>       Destroy completes about a second after the adding process is
>       stopped.  The member loop has the same shape (bridge_ifremove()
>       sleeps in smr_barrier() at :555 and in NET_LOCK() at :563, and
>       if_bridgeidx is cleared at :562 before the second sleep), but a
>       member loop that does not end was not seen in testing: during the
>       smr_barrier() sleep the member's if_bridgeidx still refuses the
>       add with EEXIST.
> 
>       Both SIOCIFDESTROY and SIOCBRDGADDS need root; no hardware is
>       involved.
> 
> >How-To-Repeat:
> 
>       As root, in one ksh session (bridge9, vether8 and vether9 must not
>       exist yet):
> 
>       1.  ifconfig vether9 create up
>       2.  ifconfig bridge9 create
>       3.  ifconfig bridge9 addspan vether9
>       4.  while :; do ifconfig bridge9 addspan vether9 2>/dev/null; done &
>       5.  A=$!; sleep 1; ifconfig bridge9 destroy & D=$!
>       6.  sleep 2; ifconfig vether8 create & C=$!
>       7.  sleep 30; ps -o pid,stat,wchan,etime,command -p $D
>       8.  ps -o pid,stat,wchan,etime,command -p $C
>       9.  kill $A; wait $D $C
>       10. ifconfig bridge9 destroy; ifconfig vether9 destroy
>       11. ifconfig vether8 destroy
> 
>       Step 4 keeps adding vether9 as a span port of bridge9, step 5
>       destroys bridge9 and step 6 creates an unrelated interface from
>       another process.  On the unpatched kernel, step 7 still shows the
>       destroy in state D, wchan "smrbar", and step 8 shows the create in
>       state D, wchan "clonelk", both still waiting after 30 s.  The
>       destroy and the create return as soon as step 9 stops the loop, and
>       the "ifconfig bridge9 destroy" in step 10 then fails because bridge9
>       is gone.  On the patched kernel both have returned before step 7;
>       the loop, which keeps running until step 9, may create bridge9
>       again (ifconfig creates a missing cloned interface), and step 10
>       removes it.
> 
> >Fix:
> 
>       Mark the bridge as dying at the start of bridge_clone_destroy() and
>       refuse SIOCBRDGADD and SIOCBRDGADDS on a dying bridge.  Both the
>       flag and the check run under the KERNEL_LOCK, like the list
>       updates.  Once the flag is set, only an add that was already past
>       the check can still insert a port, and each such ioctl can insert
>       at most one, so the removal loops end.
> 

I prefer to exit early, at the beginning of bridge_ioctl().

Index: sys/net/if_bridge.c
===================================================================
RCS file: /cvs/src/sys/net/if_bridge.c,v
retrieving revision 1.382
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 7 Nov 2026 15:02:01 -0000
@@ -223,6 +223,8 @@ bridge_clone_destroy(struct ifnet *ifp)
        struct bridge_softc *sc = ifp->if_softc;
        struct bridge_iflist *bif;
 
+       sc->sc_dying = 1;
+
        /*
         * bridge(4) detach hook doesn't need the NET_LOCK(), worst the
         * use of smr_barrier() while holding the lock might lead to a
@@ -256,6 +258,9 @@ bridge_ioctl(struct ifnet *ifp, u_long c
        struct bstp_port *bp;
        struct bstp_state *bs = sc->sc_stp;
        int error = 0;
+
+       if (sc->sc_dying)
+               return (ENXIO);
 
        /*
         * bridge(4) data structure aren't protected by the NET_LOCK().
Index: sys/net/if_bridge.h
===================================================================
RCS file: /cvs/src/sys/net/if_bridge.h,v
retrieving revision 1.78
diff -u -p -r1.78 if_bridge.h
--- sys/net/if_bridge.h 9 Oct 2026 16:36:24 -0000       1.78
+++ sys/net/if_bridge.h 7 Nov 2026 15:02:01 -0000
@@ -518,6 +518,7 @@ struct bridge_rtnode {
  */
 struct bridge_softc {
        struct ifnet                    sc_if;  /* the interface */
+       int                             sc_dying;       /* [k] */
        uint32_t                        sc_brtmax;      /* [m] max # addresses 
*/
        uint32_t                        sc_brtcnt;      /* [m] current # addrs 
*/
        int                             sc_brttimeout;  /* timeout ticks */

Reply via email to