>Synopsis:      A bpe(4) interface can be configured with another bpe as its
                parent, and nothing limits how many bpes are stacked this way.
                SIOCGIFMEDIA on the top of a legal, loop-free stack recurses
                once per layer (bpe_media_get() -> parent's bpe_ioctl() ->
                bpe_media_get() ...), and a stack of about 61-64 or more bpes
                overflows the kernel stack (fatal double fault).  This is
                separate from the bpe self-/looped-parent bug I reported
                earlier ("bpe(4): self/looped parent accepted -> SIOCGIFMEDIA
                recurses -> kernel stack overflow"): that report's fix rejects
                loops only, and still accepts a loop-free chain of up to 64.
>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 as shipped: DIAGNOSTIC,
                      POOL_DEBUG)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 vcpu)
        Source      : sys/net/if_bpe.c rev 1.27, openbsd/src as of 2026-11-03,
                      git d5b06e73 (if_bpe.c is unchanged in master afb08c4d5)

>Description:

        bpe_set_parent() accepts any Ethernet interface as the parent of a
        bpe, and a bpe is itself IFT_ETHER, so a bpe can be the parent of
        another bpe.  There is no bound on the depth of such a stack.

        bpe_media_get() forwards SIOCGIFMEDIA to the parent:

            ifp0 = if_get(sc->sc_key.k_if);
            if (ifp0 != NULL)
                    error = (*ifp0->if_ioctl)(ifp0, SIOCGIFMEDIA, (caddr_t)ifr);

        so a media query on the top of a stack of N bpes nests N
        bpe_ioctl()/bpe_media_get() frames before it reaches the bottom
        Ethernet interface.  With enough layers this runs off the end of
        the kernel stack:

            fatal double fault in supervisor mode
            trap type 13 code 0 rip ffffffff819ca8e5 cs 8 rflags 10206
                cr2 ffff8000455c1ff8 cpl 4 rsp ffff8000455c2000

        (rsp on a page boundary, cr2 just below it), and the machine hangs.
        Every layer has a different parent, so this is not a parent loop.

        On GENERIC.MP / amd64 a stack of 60 survived a media query and a
        stack of 64 or more crashed (64: 1 of 3 runs crashed, so 64 is at
        the edge; 80 and 100 crashed every time).  Bringing a bpe whose
        parent is another bpe up fails with ENOTCONN, but that does not
        matter: the parent is set and the media query recurses regardless.

        vlan(4) has the same problem (reported separately, fixed there with
        a VLAN_MAXDEPTH of 32), but bpe(4) is a separate driver.

>How-To-Repeat:

        As root:

            ifconfig vether200 create up
            p=vether200
            for i in $(jot 100 201); do
                    ifconfig bpe$i create
                    ifconfig bpe$i parent $p vnetid $i
                    p=bpe$i
            done
            ifconfig bpe300

        The last command crashes the machine.

>Fix:

        Limit how many bpe interfaces can be stacked on top of each other,
        like VLAN_MAXDEPTH in the vlan report: when a parent is set, count
        the bpes already stacked above the interface, the interface itself,
        and the bpes from the new parent down, and refuse with EINVAL if
        that is more than BPE_MAXDEPTH (32).  The diff is independent of the
        loop fix from my earlier bpe report and applies with or without it
        (either order gives the same file); with both applied, loops and
        over-deep stacks are both refused.

Index: sys/net/if_bpe.c
===================================================================
--- sys/net/if_bpe.c
+++ sys/net/if_bpe.c
@@ -84,6 +84,13 @@ struct bpe_softc {
        struct etherbridge      sc_eb;
 };
 
+/*
+ * Several ioctls (media, multicast, up/down) recurse through every bpe(4)
+ * interface in a stack, so limit how many can be stacked on top of each
+ * other to keep the kernel stack from overflowing.
+ */
+#define BPE_MAXDEPTH           32
+
 void           bpeattach(int);
 
 static int     bpe_clone_create(struct if_clone *, int);
@@ -102,6 +109,9 @@ static int  bpe_get_parent(struct bpe_sof
 static int     bpe_del_parent(struct bpe_softc *);
 static int     bpe_add_addr(struct bpe_softc *, const struct ifbareq *);
 static int     bpe_del_addr(struct bpe_softc *, const struct ifbareq *);
+static unsigned int
+               bpe_depth(struct ifnet *, struct ifnet *, int *);
+static int     bpe_too_deep(struct ifnet *, struct ifnet *);
 
 static void    bpe_link_hook(void *);
 static void    bpe_link_state(struct bpe_softc *, u_char, uint64_t);
@@ -630,6 +640,11 @@ bpe_set_parent(struct bpe_softc *sc, con
        if (ifp0->if_index == sc->sc_key.k_if)
                goto put;
 
+       if (bpe_too_deep(ifp, ifp0)) {
+               error = EINVAL;
+               goto put;
+       }
+
        if (ISSET(ifp->if_flags, IFF_RUNNING)) {
                error = EBUSY;
                goto put;
@@ -646,6 +661,62 @@ put:
        return (error);
 }
 
+/*
+ * Count the bpe interfaces from ifp down its parent chain, stopping at
+ * the first interface that is not a bpe, or at stop (not counted) in
+ * which case *found is set.  Counting stops past BPE_MAXDEPTH.
+ */
+static unsigned int
+bpe_depth(struct ifnet *ifp, struct ifnet *stop, int *found)
+{
+       struct ifnet *p, *next;
+       unsigned int depth = 0;
+
+       *found = 0;
+       p = if_ref(ifp);
+       while (p != NULL && p->if_ioctl == bpe_ioctl) {
+               if (p == stop) {
+                       *found = 1;
+                       break;
+               }
+               if (++depth > BPE_MAXDEPTH)
+                       break;
+               next = if_get(((struct bpe_softc *)p->if_softc)->sc_key.k_if);
+               if_put(p);
+               p = next;
+       }
+       if_put(p);
+
+       return (depth);
+}
+
+/*
+ * Return non-zero if making ifp0 the parent of ifp would stack more than
+ * BPE_MAXDEPTH bpe interfaces: the bpes already stacked on top of ifp,
+ * ifp itself, and the bpes from ifp0 down.
+ */
+static int
+bpe_too_deep(struct ifnet *ifp, struct ifnet *ifp0)
+{
+       struct ifnet *ifb;
+       unsigned int above = 0, below, depth;
+       int found;
+
+       KERNEL_ASSERT_LOCKED();
+
+       TAILQ_FOREACH(ifb, &ifnetlist, if_list) {
+               if (ifb == ifp || ifb->if_ioctl != bpe_ioctl)
+                       continue;
+               depth = bpe_depth(ifb, ifp, &found);
+               if (found && depth > above)
+                       above = depth;
+       }
+
+       below = bpe_depth(ifp0, NULL, &found);
+
+       return (above + 1 + below > BPE_MAXDEPTH);
+}
+
 static int
 bpe_get_parent(struct bpe_softc *sc, struct if_parent *p)
 {

>Verification:

        The diff above (md5 3a9d1e3faeaea5af25131411ee265e5f) applies with
        patch -p0 -F0 to sys/net/if_bpe.c r1.27 (git d5b06e73), on its own
        and before or after the bpe-parent-loop.diff from my earlier bpe
        report (both orders give the same if_bpe.c); if_bpe.c is identical
        in master afb08c4d5.  Three kernels, built from one clean export of
        d5b06e73, GENERIC.MP as shipped, amd64, no warnings:
          A  unpatched
          B  + this diff
          C  + the earlier bpe-parent-loop.diff + this diff
        Every run below was on a freshly booted kernel (2-vcpu guest).

          100-deep stack (How-To-Repeat):
            A  3/3 fatal double fault (trap type 13, rsp on a page boundary,
               cr2 just below it), machine hung
            B  3/3 the 33rd "parent" refused ("SIOCSIFPARENT: Invalid
               argument"), 32 layers built, media on the top returns
            C  1/1 the same as B
          How-To-Repeat, run exactly as printed above:
            A  1/1 fatal double fault
            B  1/1 survived: every 33rd "parent" is refused, so the loop
               builds separate stacks of at most 32 and "ifconfig bpe300"
               returns
          64-deep stack:
            A  1/3 fatal double fault (2/3 survived: 64 is at the edge)
            B  3/3 and C 3/3 refused at the 33rd layer, media returns
          exactly at the limit (B 3/3, C 1/1, 6 checks each): a 32-deep
            stack is accepted and its media query returns; a 33rd layer is
            refused; a 16-deep stack joined under the bottom of a separate
            16-deep stack (32 total) is accepted and media on its top
            returns; a 17-deep stack joined under a 16-deep stack (33) is
            refused.  On A the 33rd layer and the 33-deep join are accepted.
          self-parent and two-bpe cycle (C, 3/3 each): refused with EINVAL,
            media returns (the earlier fix still works with this one).

        Normal use, 8 checks each, B 3/3, C 1/1, A 1/1, all passed with no
        kernel messages: a bpe on each end of a pair(4) link between two
        rdomains, IPv4 and IPv6 ping across, media, down/up and ping
        again, and a legal two-deep stack with media.  (Unrelated to this
        bug and the same on all three kernels: the first IPv6 address added
        to a bpe fails with "SIOCAIFADDR: Network dropped connection on
        reset"; adding it again works.  The test repeats the add.)

Reply via email to