>Synopsis:      tpmr_add_port(), veb_add_port() and the bridge(4) SIOCBRDGADD
                handler reject an interface that is already a bridge-family
                port (ether_brport_isset) but not one that is already an
                aggr(4) port (ac_trport).  aggr_add_port() checks both.  The
                missing check lets an interface be an aggr(4) member and a
                tpmr(4)/veb(4) port at once; both save and restore the
                member's if_ioctl, and after the aggr is destroyed the
                tpmr/veb teardown re-enters the stale aggr_p_ioctl() ->
                KASSERT "ep != NULL" (a NULL dereference without DIAGNOSTIC).
>Category:      kernel
>Severity:      serious
>Priority:      medium
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (source as of 2026-11-03)
>Environment:
        System      : OpenBSD 8.0-current
        Details     : OpenBSD 8.0-current (GENERIC.MP), WITNESS + DIAGNOSTIC
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
        Source      : sys/net/if_tpmr.c rev 1.45, sys/net/if_veb.c rev 1.74, 
sys/net/if_bridge.c rev 1.382, openbsd/src as of 2026-11-03, git d5b06e73

>Description:

        aggr(4) protects against an interface being enslaved to two
        incompatible subsystems: aggr_add_port() (if_aggr.c) rejects a
        candidate port that is already a bridge-family port, via

            error = ether_brport_isset(ifp0);   /* != 0 (EBUSY) if ac_brport 
set */

        and one that is already a trunk(4)/aggr(4) port, via

            if (SMR_PTR_GET_LOCKED(&ac0->ac_trport) != NULL)
                    return (EBUSY);

        The three bridge-family port-add paths only do half of this.  They
        check ether_brport_isset() (so two bridge-family devices can't share a
        port) and if_type == IFT_ETHER, but they never test ac_trport:

            tpmr_add_port()           if_tpmr.c
            veb_add_port()            if_veb.c
            bridge SIOCBRDGADD        if_bridge.c (bridge_ioctl)

        A trunk(4) member is refused anyway, because trunk_port_create()
        changes the member's if_type to IFT_IEEE8023ADLAG.  aggr(4) sets
        IFT_IEEE8023ADLAG only on the aggr interface itself, so an aggr
        member keeps IFT_ETHER and is accepted.

        Both aggr(4) and tpmr(4)/veb(4) interpose the member's if_ioctl
        by saving the previous handler and installing their own (aggr:
        if_ioctl = aggr_p_ioctl, saving p_ioctl; tpmr/veb likewise).  When the
        same ifnet is a member of both, the two saved/restored chains overlap.
        On teardown they clobber each other: e.g. removing the aggr port
        restores the member's if_ioctl to aggr's saved original and clears
        ac_trport, while tpmr/veb still hold p_ioctl == aggr_p_ioctl.  When the
        bridge-family port is then destroyed, *_p_dtor() restores that stale
        aggr_p_ioctl and (tpmr) calls ifpromisc(), re-entering aggr_p_ioctl
        with ac_trport == NULL:

            const struct ether_port *ep = SMR_PTR_GET_LOCKED(&ac0->ac_trport);
            KASSERTMSG(ep != NULL, "%s: %s called without an ether_port set", 
...)

            panic: kernel diagnostic assertion "ep != NULL" failed:
                file ".../sys/net/if_aggr.c", line 1470
                vether0: aggr_p_ioctl called without an ether_port set

        On a GENERIC (non-DIAGNOSTIC) kernel the KASSERT is compiled out and
        the following "p = ep->ep_port" is a NULL dereference.

        With the How-To-Repeat below, GENERIC.MP + WITNESS + DIAGNOSTIC
        panics in the "tpmr0 destroy" step:

            panic: kernel diagnostic assertion "ep != NULL" failed: file
                ".../sys/net/if_aggr.c", line 1470 vether0: aggr_p_ioctl called
                without an ether_port set
            panic() at panic+0x170
            aggr_p_ioctl(...,80206910,...)          (80206910 = SIOCSIFFLAGS)

        (the rest of the backtrace was lost in the console output).

        bridge(4) accepts the aggr member as well, but it does not interpose
        the member's if_ioctl, so there the result is only the double
        membership, not the panic.

        All operations are available to root via SIOCBRDGADD / SIOCSTRUNKPORT /
        SIOCIFDESTROY; no special hardware is involved.

>How-To-Repeat:

        As root (tpmr shown; veb behaves the same):

            ifconfig aggr0 create
            ifconfig vether0 create up
            ifconfig aggr0 trunkport vether0
            ifconfig tpmr0 create
            ifconfig tpmr0 add vether0     # should fail EBUSY, but succeeds
            ifconfig aggr0 destroy         # clears ac_trport; tpmr keeps stale 
p_ioctl
            ifconfig tpmr0 destroy         # tpmr_p_dtor -> ifpromisc -> 
aggr_p_ioctl -> panic

        After the "add", "ifconfig vether0" shows "trunk: trunkdev aggr0"
        while vether0 is also a tpmr0 port.  The member has to be UP:
        ifpromisc() only calls the (stale) if_ioctl on an UP interface, so
        with vether0 down the sequence leaves the stale pointer behind
        without faulting.

>Fix:

        Make the three bridge-family port-add paths reject an interface that
        is already a trunk(4)/aggr(4) port (ac_trport set), mirroring
        aggr_add_port().  tpmr(4) and veb(4) check again after the steps
        that can sleep; in veb(4) that re-check runs after
        veb_ports_insert() has built the new port map, so its error path
        frees that unpublished map.  The diff is against if_tpmr.c r1.45,
        if_veb.c r1.74 and if_bridge.c r1.382.

Index: sys/net/if_tpmr.c
===================================================================
--- sys/net/if_tpmr.c
+++ sys/net/if_tpmr.c
@@ -519,6 +519,12 @@ tpmr_add_port(struct tpmr_softc *sc, const struct ifbr
        if (error != 0)
                goto put;
 
+       /* Reject a port already enslaved to a trunk(4)/aggr(4) */
+       if (SMR_PTR_GET_LOCKED(&((struct arpcom *)ifp0)->ac_trport) != NULL) {
+               error = EBUSY;
+               goto put;
+       }
+
        /* let's try */
 
        p = malloc(sizeof(*p), M_DEVBUF, M_WAITOK|M_ZERO|M_CANFAIL);
@@ -551,6 +557,11 @@ tpmr_add_port(struct tpmr_softc *sc, const struct ifbr
        error = ether_brport_isset(ifp0);
        if (error != 0)
                goto unpromisc;
+
+       if (SMR_PTR_GET_LOCKED(&((struct arpcom *)ifp0)->ac_trport) != NULL) {
+               error = EBUSY;
+               goto unpromisc;
+       }
 
        task_set(&p->p_ltask, tpmr_p_linkch, p);
        if_linkstatehook_add(ifp0, &p->p_ltask);
Index: sys/net/if_veb.c
===================================================================
--- sys/net/if_veb.c
+++ sys/net/if_veb.c
@@ -1765,6 +1765,12 @@ veb_add_port(struct veb_softc *sc, const struct ifbreq
        if (error != 0)
                goto put;
 
+       /* Reject a port already enslaved to a trunk(4)/aggr(4) */
+       if (SMR_PTR_GET_LOCKED(&((struct arpcom *)ifp0)->ac_trport) != NULL) {
+               error = EBUSY;
+               goto put;
+       }
+
        /* let's try */
 
        p = malloc(sizeof(*p), M_DEVBUF, M_WAITOK|M_ZERO|M_CANFAIL);
@@ -1830,6 +1836,13 @@ veb_add_port(struct veb_softc *sc, const struct ifbreq
        error = ether_brport_isset(ifp0);
        if (error != 0)
                goto unpromisc;
+
+       if (SMR_PTR_GET_LOCKED(&((struct arpcom *)ifp0)->ac_trport) != NULL) {
+               /* nm was never published: drop its port refs and free it */
+               veb_ports_destroy(nm);
+               error = EBUSY;
+               goto unpromisc;
+       }
 
        task_set(&p->p_ltask, veb_p_linkch, p);
        if_linkstatehook_add(ifp0, &p->p_ltask);
Index: sys/net/if_bridge.c
===================================================================
--- sys/net/if_bridge.c
+++ sys/net/if_bridge.c
@@ -301,6 +301,13 @@ bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t da
                        break;
                }
 
+               /* Reject a port already enslaved to a trunk(4)/aggr(4) */
+               if (SMR_PTR_GET_LOCKED(&((struct arpcom *)ifs)->ac_trport) != 
NULL) {
+                       if_put(ifs);
+                       error = EBUSY;
+                       break;
+               }
+
                /* If it's in the span list, it can't be a member. */
                SMR_SLIST_FOREACH_LOCKED(bif, &sc->sc_spanlist, bif_next) {
                        if (bif->ifp == ifs)

>Verification:
        Diff applied as shown with patch -p0 to the revisions listed under
        Environment (openbsd/src as of 2026-11-03), no fuzz or offset;
        kernel GENERIC.MP + WITNESS + DIAGNOSTIC, amd64, 2 vcpu, built
        without warnings.  Unpatched = the same tree without the diff, same
        config.  How-To-Repeat run three times per driver (tpmr0, veb0 or
        bridge0 in place of tpmr0) on each kernel:

          unpatched: tpmr 3/3 and veb 3/3 the "add" succeeded and the
                     destroy panicked with a kernel diagnostic assertion
                     (where the console message is readable: "ep != NULL",
                     if_aggr.c line 1470, aggr_p_ioctl); bridge 1/1 the "add"
                     succeeded and vether0 was in aggr0 and bridge0 at once,
                     no panic.
          patched:   tpmr 3/3, veb 3/3, bridge 3/3 the "add" was refused
                     with "Device busy" and the teardown was clean.

        With vether0 left down (the sequence without "up"), the unpatched
        kernel accepted the add but did not panic (tpmr 3/3, veb 3/3,
        bridge 1/1).  A trunk(4) member was refused by all three drivers on
        both kernels (tpmr/veb "Protocol not supported", bridge EINVAL), as
        trunk(4) changes the member's if_type.
        The re-checks after the steps that can sleep (tpmr, veb) only fire
        under a concurrent aggr(4)/trunk(4) add and were not exercised.
        A normal-use run on the patched kernel (tpmr, veb and bridge: add two
        ports, delete and re-add one, destroy a member, destroy the bridge;
        aggr and trunk port add/delete/re-add/destroy) gave the same results
        as on the unpatched kernel.

Reply via email to