>Synopsis:      vlan(4) and svlan(4) interfaces can be stacked on each other
                without limit.  Transmit, SIOCGIFMEDIA and SIOCADDMULTI/
                SIOCDELMULTI recurse once per layer, so on amd64 a stack of a
                few dozen vlans (53 to 76 in the tests below, depending on the
                operation and the run) overflows the kernel stack (double
                fault) as soon as the top vlan is displayed, given an address
                or used.
>Category:      kernel
>Severity:      serious
>Priority:      low
>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); also GENERIC.MP + 
WITNESS
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guest, 2 and 4 vcpu)
        Source      : sys/net/if_vlan.c rev 1.224, openbsd/src as of 
2026-11-03, git d5b06e73

>Description:

        A vlan(4) is an IFT_ETHER interface, so vlan_set_parent() accepts
        another vlan (or svlan) as its parent; that is how QinQ and other
        multi-tag stacks are built.  Nothing limits how many vlans can be
        stacked this way.

        Several operations on a vlan call the same operation on its parent,
        so they recurse once per layer of the stack, on the kernel stack of
        the calling thread:

          - SIOCGIFMEDIA: vlan_ioctl() -> vlan_media_get() ->
            (*ifp0->if_ioctl)(ifp0, SIOCGIFMEDIA) -> vlan_ioctl() -> ...
          - SIOCADDMULTI/SIOCDELMULTI: vlan_ioctl() -> vlan_multi_add()/
            vlan_multi_del() -> (*ifp0->if_ioctl)(ifp0, ...) -> ...
            (issued when an address is added or removed, which joins or
            leaves multicast groups)
          - transmit: if_enqueue() -> vlan_enqueue() -> vlan_transmit() ->
            if_enqueue(ifp0) -> vlan_enqueue() -> ...

        (Receive does not recurse: if_vinput() queues each decapsulated
        packet on the netstack's input list.)

        Each layer costs about 160 bytes of stack for SIOCGIFMEDIA
        (vlan_ioctl 96 + vlan_media_get 64, from the function prologues of
        an amd64 GENERIC.MP kernel), about 190 for the multicast ioctls and
        about 215 for transmit (if_enqueue 56 + vlan_enqueue 64 +
        vlan_transmit 96).  The amd64 kernel stack has room for roughly
        12 KB below the system call frame, so a few dozen layers are enough
        to run off the end of it.  The result is "fatal double fault in
        supervisor mode" with cr2 just below the stack.

        Measured on amd64 with two kernels built from the same tree:
        GENERIC.MP as shipped, and GENERIC.MP with WITNESS enabled.  Stacks
        were built from vether0 upwards as in How-To-Repeat.  First, each
        trigger was run on the top vlan after every new layer (one run per
        trigger and kernel); depth of the first fault:

            trigger on the top vlan                    GENERIC.MP  +WITNESS
            ifconfig vlanN (SIOCGIFMEDIA)                  71         68
            add and delete an IPv4 address                 60         57
            add an IPv6 address, wait 4s, -inet6           53         56
            IPv4 address, static ARP entry, ping           59         64
            ping across a pair(4) link, a stack of
              N vlans on each end (reply sent by the
              network stack, not by a process)             56         54

        The GENERIC.MP backtrace of the IPv4 case shows the fault while
        the address was being deleted: in_purgeaddr() -> in_delmulti() ->
        SIOCDELMULTI recursing vlan_ioctl() -> vlan_multi_del() through
        all 60 layers.

        Second, the How-To-Repeat commands with a fresh stack of N vlans
        (three runs per N; runs that faulted / runs):

            N            67   68   69   70   71   72   76   100
            GENERIC.MP    -    -   0/3  1/3  2/3  1/3  3/3  3/3
            +WITNESS     0/3  0/3   -   1/3   -    -   2/3  3/3

        So there is no exact threshold: between about 70 and 76 layers it
        depends on how much stack the bottom of the recursion happens to
        need.  In the
        faulting GENERIC.MP run at N=70 the fault was inside mtx_enter()
        called from malloc() in vether0's ifmedia_ioctl(), one call deeper
        than in the 71-deep backtrace below.  Every run at 100 faulted on
        both kernels.

        The SIOCGIFMEDIA case on GENERIC.MP left a complete backtrace,
        71 vlan_ioctl/vlan_media_get pairs on top of the system call:

            fatal double fault in supervisor mode
            trap type 13 code 0 rip ffffffff820a7a81 cs 8 rflags 10246 cr2 
ffff8000454f7fa8 cpl 0 rsp ffff8000454f7fb0
            panic: trap type 13, code=0, pc=ffffffff820a7a81
            panic(ffffffff826f7c5f) at panic+0x170
            kerntrap(ffff800044ef3730) at kerntrap+0x15d
            Xcalltrap_specstk_untramp() at Xcalltrap_specstk_untramp+0xf
            malloc(8,7f,9) at malloc+0x51
            
ifmedia_ioctl(ffff80000044e800,ffff8000454fae10,ffff80000044ed58,c0406938) at 
ifmedia_ioctl+0x2f2
            vlan_media_get(ffff80000043b800,ffff8000454fae10) at 
vlan_media_get+0x52
            vlan_ioctl(ffff80000043b800,c0406938,ffff8000454fae10) at 
vlan_ioctl+0x1c8
            vlan_media_get(ffff8000003e4000,ffff8000454fae10) at 
vlan_media_get+0x52
            vlan_ioctl(ffff8000003e4000,c0406938,ffff8000454fae10) at 
vlan_ioctl+0x1c8
                ... [71 vlan_ioctl frames in total, each on a different vlan] 
...
            
ifioctl(ffff800000e17a10,c0406938,ffff8000454fae10,ffff80004560f238) at 
ifioctl+0xaa1
            sys_ioctl(ffff80004560f238,ffff8000454fafd0,ffff8000454faf50) at 
sys_ioctl+0x30e
            syscall(ffff8000454fafd0) at syscall+0x60d
            Xsyscall() at Xsyscall+0x128

        (ioctl 0xc0406938 == SIOCGIFMEDIA; the malloc() is the one in
        vether0's ifmedia_ioctl() at the bottom of the stack.)  The syscall
        frame (0xffff8000454fafd0) and the faulting address (cr2) are
        12328 bytes apart, i.e. about 170 bytes per layer.

        The other cases ended with "Bad frame pointer" or a fault inside
        the backtrace, so the recursion could not be read from them; their
        depths and fault addresses fit the same picture (the faulting
        instruction was in malloc, free, mtx_enter, _rb_root or a WITNESS
        function, i.e. whatever happened to be running when the stack ran
        out).

        In sys/net no other interface limits how deeply it can be nested;
        gif(4), gre(4), etherip(4) and vxlan(4) only drop a packet that
        passes through the same interface twice (PACKET_TAG_GRE), which does
        not apply to a stack of distinct vlans.

        Only root can configure this.

>How-To-Repeat:

        As root, in sh(1)/ksh(1):

        1. ifconfig vether0 create up
        2. p=vether0; for i in $(jot 100); do ifconfig vlan$i create; ifconfig 
vlan$i parent $p vnetid $i up; p=vlan$i; done
        3. ifconfig vlan100

        Step 2 builds vlan1 on vether0, vlan2 on vlan1, ..., vlan100 on
        vlan99 (each vlan has a different parent, so there is no loop).
        Step 3 issues SIOCGIFMEDIA on the top vlan, which recurses through
        all 100 layers and overflows the kernel stack.

>Fix:

        Limit the depth of a vlan stack in vlan_set_parent().  The new
        vlan_too_deep() counts the vlans that would be stacked on top of
        each other if the parent were set: the deepest chain of vlans
        already stacked above this vlan (found by walking every vlan's
        parent chain on ifnetlist, under the kernel lock that ifioctl()
        holds), the vlan itself, and the vlans from the proposed parent
        down.  If that is more than VLAN_MAXDEPTH (32) the parent is refused
        with EINVAL.  Counting both directions matters because a stack can
        also be built from the top down (set vlan2's parent to vlan1 before
        vlan1 has a parent) or by joining two shorter stacks.  The walks
        stop at the first interface that is not a vlan/svlan (if_ioctl !=
        vlan_ioctl) and after VLAN_MAXDEPTH steps, so they always terminate.

        Why 32: in the measurements above the vlan stack alone exhausted the
        roughly 12 KB available below the system call frame at about 53-76
        layers, at 160-215 bytes per layer, on an otherwise idle machine
        with the default pf ruleset loaded.  32 layers cost at most about
        6.9 KB (32 x 215 bytes, transmit), a bit over half of that room;
        the lowest depth that faulted in any test (53) is about 1.7 times
        the limit.  32 is far beyond any real tagging scheme (QinQ needs
        two layers) while still stopping the overflow.  The remaining
        room is what is left for what the tests did not put on the stack:
        larger pf rulesets, IPsec, bridge(4)/veb(4)/tpmr(4) forwarding,
        other tunnels below the vlans, and interrupt handlers (on amd64 an
        interrupt taken in the kernel is handled on the current kernel
        stack, see INTRENTRY in frameasm.h).  If that margin is thought too
        thin, a smaller VLAN_MAXDEPTH (e.g. 8, about 1.7 KB) works the same
        way; the diff was also tested with 8.


Index: sys/net/if_vlan.c
===================================================================
RCS file: /cvs/src/sys/net/if_vlan.c,v
retrieving revision 1.224
diff -u -p -r1.224 if_vlan.c
--- sys/net/if_vlan.c   18 Dec 2025 02:14:09 -0000      1.224
+++ sys/net/if_vlan.c   5 Nov 2026 20:02:33 -0000
@@ -110,6 +110,14 @@ SMR_SLIST_HEAD(vlan_list, vlan_softc);
 #define TAG_HASH_SIZE          (1 << TAG_HASH_BITS)
 #define TAG_HASH_MASK          (TAG_HASH_SIZE - 1)
 #define TAG_HASH(tag)          (tag & TAG_HASH_MASK)
+
+/*
+ * Transmit and some ioctls (media, multicast) recurse through every
+ * vlan(4) and svlan(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 VLAN_MAXDEPTH          32
+
 struct vlan_list *vlan_tagh, *svlan_tagh;
 struct rwlock vlan_tagh_lk = RWLOCK_INITIALIZER("vlantag");
 
@@ -133,6 +141,8 @@ int vlan_set_parent(struct vlan_softc *, const char *)
 int    vlan_del_parent(struct vlan_softc *);
 int    vlan_inuse(uint16_t, unsigned int, uint16_t);
 int    vlan_inuse_locked(uint16_t, unsigned int, uint16_t);
+unsigned int vlan_depth(struct ifnet *, struct ifnet *, int *);
+int    vlan_too_deep(struct ifnet *, struct ifnet *);
 
 int    vlan_multi_add(struct vlan_softc *, struct ifreq *);
 int    vlan_multi_del(struct vlan_softc *, struct ifreq *);
@@ -944,6 +954,11 @@ vlan_set_parent(struct vlan_softc *sc, const char *par
                goto put;
        }
 
+       if (vlan_too_deep(ifp, ifp0)) {
+               error = EINVAL;
+               goto put;
+       }
+
        error = vlan_inuse(sc->sc_type, ifp0->if_index, sc->sc_tag);
        if (error != 0)
                goto put;
@@ -975,6 +990,62 @@ vlan_del_parent(struct vlan_softc *sc)
                if_setlladdr(ifp, etheranyaddr);
 
        return (0);
+}
+
+/*
+ * Count the vlan interfaces from ifp down its parent chain, stopping at
+ * the first interface that is not a vlan, or at stop (not counted) in
+ * which case *found is set.  Counting stops past VLAN_MAXDEPTH.
+ */
+unsigned int
+vlan_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 == vlan_ioctl) {
+               if (p == stop) {
+                       *found = 1;
+                       break;
+               }
+               if (++depth > VLAN_MAXDEPTH)
+                       break;
+               next = if_get(((struct vlan_softc *)p->if_softc)->sc_ifidx0);
+               if_put(p);
+               p = next;
+       }
+       if_put(p);
+
+       return (depth);
+}
+
+/*
+ * Return non-zero if making ifp0 the parent of ifp would stack more than
+ * VLAN_MAXDEPTH vlan interfaces: the vlans already stacked on top of ifp,
+ * ifp itself, and the vlans from ifp0 down.
+ */
+int
+vlan_too_deep(struct ifnet *ifp, struct ifnet *ifp0)
+{
+       struct ifnet *ifv;
+       unsigned int above = 0, below, depth;
+       int found;
+
+       KERNEL_ASSERT_LOCKED();
+
+       TAILQ_FOREACH(ifv, &ifnetlist, if_list) {
+               if (ifv == ifp || ifv->if_ioctl != vlan_ioctl)
+                       continue;
+               depth = vlan_depth(ifv, ifp, &found);
+               if (found && depth > above)
+                       above = depth;
+       }
+
+       below = vlan_depth(ifp0, NULL, &found);
+
+       return (above + 1 + below > VLAN_MAXDEPTH);
 }
 
 int

>Verification:
        Diff applied as shown with patch -p0 -F0 to a fresh export of
        openbsd/src as of 2026-11-03 (git d5b06e73, sys/net/if_vlan.c
        r1.224), no fuzz or offset; amd64 kernels GENERIC.MP as shipped and
        GENERIC.MP + WITNESS (GENERIC already has DIAGNOSTIC), both built
        without warnings.  Unpatched = the same tree without the diff, same
        config.  Three runs of each test on each of the four kernels:

          How-To-Repeat (N=100):
            unpatched GENERIC.MP 3/3 and +WITNESS 3/3 "fatal double fault in
            supervisor mode" with cr2 just below the kernel stack; the
            machine hung in "syncing disks..." (ddb.panic=0), reset needed.
            patched (both configs) 3/3 no fault: "ifconfig vlan33 parent
            vlan32" and every 32nd parent after it (vlan65, vlan97) was
            refused with "SIOCSIFPARENT: Invalid argument", so the commands
            produce stacks of at most 32 vlans; vlan100 ended up 4 vlans deep
            and "ifconfig vlan100" was fine.

          Limit, patched only (both configs, 3/3 each):
            - bottom-up: vlan1 on vether0 ... vlan32 on vlan31 accepted,
              vlan33 on vlan32 refused;
            - top-down: of the parent links vlan233 -> vlan232, ...,
              vlan202 -> vlan201 (set first, top to bottom) and finally
              vlan201 -> vether0, exactly one was refused;
            - joining two stacks: 28 vlans onto a stack of 5 refused, 27
              vlans onto a stack of 5 (32 in total) accepted;
            - an svlan on top of 32 vlans refused.

          A stack at the limit, patched (both configs, 3/3 each): 32 vlans
            on each end of a pair(4) link (side B in rdomain 1): all 64
            parents accepted and up, media fine on every vlan, inet and inet6
            addresses on both top vlans, ping and ping6 across succeeded,
            the stacks were destroyed top-down without trouble.

          QinQ, patched (both configs, 3/3 each): vlan10 on a pair(4)
            interface, svlan20 on vlan10, a vlan with tag 20 on svlan20, on
            both ends; all parents accepted, ping across succeeded.

Reply via email to