>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.

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 18:00:00 -0000
@@ -230,6 +230,12 @@ bridge_clone_destroy(struct ifnet *ifp)
         */
        NET_ASSERT_UNLOCKED();
 
+       /*
+        * The removal loops below sleep.  Refuse new ports from here on,
+        * or a concurrent SIOCBRDGADD or SIOCBRDGADDS could keep them going.
+        */
+       sc->sc_dying = 1;
+
        bridge_stop(sc);
        bridge_rtflush(sc, IFBF_FLUSHALL);
        while ((bif = SMR_SLIST_FIRST_LOCKED(&sc->sc_iflist)) != NULL)
@@ -275,6 +281,10 @@ bridge_ioctl(struct ifnet *ifp, u_long c
        case SIOCBRDGADDL:
                if ((error = suser(curproc)) != 0)
                        break;
+               if (sc->sc_dying) {
+                       error = ENXIO;
+                       break;
+               }
 
                ifs = if_unit(req->ifbr_ifsname);
                if (ifs == NULL) {                      /* no such interface */
@@ -364,6 +374,10 @@ bridge_ioctl(struct ifnet *ifp, u_long c
        case SIOCBRDGADDS:
                if ((error = suser(curproc)) != 0)
                        break;
+               if (sc->sc_dying) {
+                       error = ENXIO;
+                       break;
+               }
                ifs = if_unit(req->ifbr_ifsname);
                if (ifs == NULL) {                      /* no such interface */
                        error = ENOENT;
Index: sys/net/if_bridge.h
===================================================================
RCS file: /cvs/src/sys/net/if_bridge.h,v
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 5 Nov 2026 18:00:00 -0000
@@ -526,6 +526,7 @@ struct bridge_softc {
        struct bstp_state               *sc_stp;        /* stp state */
        SMR_SLIST_HEAD(, bridge_iflist) sc_iflist;      /* [k] interface list */
        SMR_SLIST_HEAD(, bridge_iflist) sc_spanlist;    /* [k] span ports */
+       int                             sc_dying;       /* [k] being destroyed 
*/
        struct mutex                    sc_mtx;         /* mutex */
        LIST_HEAD(, bridge_rtnode)      sc_rts[BRIDGE_RTABLE_SIZE];     /* [m] 
hash table */
 };

>Verification:

        amd64 KVM guest, 2 CPUs.  Pristine -current src (2026-11-03,
        if_bridge.c 1.382, if_bridge.h 1.78), 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 repeats: create vether240 and bridge240, add vether240
        as a span port, start a loop re-adding it ("ifconfig bridge240
        addspan vether240"), destroy bridge240, and 2 s later create
        vether241 from another process.  A run counts as a livelock if the
        destroy has not returned 20 s after it started while the loop is
        still running; the loop is then stopped and the run ends.

          unpatched   6 of 6 runs livelocked in their first destroy (five
                      60 s runs, one 300 s run): destroy in state D, wchan
                      "smrbar", the vether241 create in state D, wchan
                      "clonelk", for 18 s; both returned 1 s after the
                      loop was stopped.  In one extra run with a 280 s
                      limit, the create stayed blocked for 278 s, again
                      until the loop was stopped.
          patched     6 of 6 runs completed (five 60 s, one 300 s), 203
                      destroys, each returned within 3 s (the check has
                      1 s resolution and starts 2 s after the destroy)
                      while the loop kept running; the loop's adds were
                      refused with ENXIO 11210 times in total.  No panic
                      or fault in dmesg.

        The same test with the port added as a member ("add" instead of
        "addspan"), 60 s: no livelock on either kernel (21 and 20
        destroys); on the patched kernel 1055 adds were refused with
        ENXIO during destroy.

        The How-To-Repeat commands exactly as shown above: on the unpatched
        kernel steps 7 and 8 showed the destroy in "smrbar" (32 s) and the
        create in "clonelk" (30 s); on the patched kernel both had already
        returned (steps 7 and 8 printed only the ps header).

        Normal use on the patched kernel: add, delete and re-add a member
        and a span port, STP on two members with the bridge up, destroy a
        member, add a new member, destroy the bridge (1 s), reuse the
        former member and span port in a new bridge: 19 of 19 checks
        passed, no panic or fault in dmesg.

Reply via email to