>Synopsis:      vlan_set_parent() does not reject a parent that would create a
                loop: the vlan itself ("ifconfig vlanN parent vlanN") or another
                vlan whose parent chain leads back to it ("vlan100 parent 
vlan10;
                vlan10 parent vlan100").  A vlan is IFT_ETHER, so both are
                accepted.  Thereafter SIOCGIFMEDIA on any vlan in the loop 
recurses
                vlan_media_get() -> vlan_ioctl() -> vlan_media_get() without 
bound
                and overflows the kernel stack.  svlan(4) shares the driver and 
is
                affected identically.
>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; backtrace from 
GENERIC.MP)
        Architecture: OpenBSD.amd64
        Machine     : amd64 (QEMU/KVM guests, 2 and 4 vcpu)
        Source      : sys/net/if_vlan.c rev 1.224, openbsd/src as of 
2026-11-03, git d5b06e73

>Description:

        vlan(4) interfaces are ethernet-like (ether_ifattach() sets
        if_type = IFT_ETHER), and a vlan may be stacked on another vlan.
        vlan_set_parent() (sys/net/if_vlan.c) validates the proposed parent
        only as follows:

            ifp0 = if_unit(parent);
            if (ifp0 == NULL) return (EINVAL);
            if (ifp0->if_type != IFT_ETHER) { error = EPROTONOSUPPORT; goto 
put; }
            if (sc->sc_ifidx0 == ifp0->if_index) { /* nop */ goto put; }
            if (ISSET(ifp->if_flags, IFF_RUNNING)) { error = EBUSY; goto put; }
            error = vlan_inuse(sc->sc_type, ifp0->if_index, sc->sc_tag);
            ...
            sc->sc_ifidx0 = ifp0->if_index;   /* commit */

        A vlan is IFT_ETHER, so a vlan passes the type check.  Nothing checks
        whether the proposed parent is the vlan itself, or whether following
        the parent's own parent chain leads back to it.  (The only
        self-related test, sc_ifidx0 == ifp0->if_index, means "parent
        unchanged", not "parent is me".)  So both of these are accepted:

            vlanN parent vlanN                             (self, 1-cycle)
            vlan100 parent vlan10; vlan10 parent vlan100   (2-cycle; and so on)

        Once a loop exists, any media query recurses.  vlan_ioctl()'s
        SIOCGIFMEDIA case calls vlan_media_get():

            case SIOCGIFMEDIA:
                error = vlan_media_get(sc, ifr);

        and vlan_media_get() forwards the same ioctl to the parent's if_ioctl:

            ifp0 = if_get(sc->sc_ifidx0);                    /* next in loop  */
            error = (ifp0 == NULL) ? ENOTTY :
                (*ifp0->if_ioctl)(ifp0, SIOCGIFMEDIA, ifr);  /* == vlan_ioctl */

        Every member of the loop is a vlan, so the forwarding never reaches a
        real ethernet driver:

            vlan_ioctl -> vlan_media_get -> vlan_ioctl -> vlan_media_get -> ...

        recurses with no termination and overflows the kernel stack.  Any
        "ifconfig vlanN" issues SIOCGIFMEDIA, so merely displaying the
        interface (or running a bare "ifconfig") is enough to trigger it.
        On GENERIC.MP as shipped (no WITNESS), case 1 below left a complete
        backtrace: 87 levels of the recursion, all on the same vlan, on top
        of the ioctl system call:

            fatal double fault in supervisor mode
            trap type 13 code 0 rip ffffffff81a49be5 cs 8 rflags 10282 cr2 
ffff8000454cfff8 cpl 0 rsp ffff8000454d0000
            panic: trap type 13, code=0, pc=ffffffff81a49be5
            panic(ffffffff826f7c5f) at panic+0x170
            kerntrap(ffff800044ef3730) at kerntrap+0x15d
            Xcalltrap_specstk_untramp() at Xcalltrap_specstk_untramp+0xf
            vlan_media_get(ffff800000513800,ffff8000454d3700) at 
vlan_media_get+0x15
            vlan_ioctl(ffff800000513800,c0406938,ffff8000454d3700) at 
vlan_ioctl+0x1c8
            vlan_media_get(ffff800000513800,ffff8000454d3700) at 
vlan_media_get+0x52
            vlan_ioctl(ffff800000513800,c0406938,ffff8000454d3700) at 
vlan_ioctl+0x1c8
                ... [87 vlan_ioctl frames in total] ...
            
ifioctl(ffff800012fdaf48,c0406938,ffff8000454d3700,ffff8000454ca7f0) at 
ifioctl+0xaa1
            sys_ioctl(ffff8000454ca7f0,ffff8000454d38c0,ffff8000454d3840) at 
sys_ioctl+0x30e
            syscall(ffff8000454d38c0) at syscall+0x60d
            Xsyscall() at Xsyscall+0x128

        (ioctl 0xc0406938 == SIOCGIFMEDIA.)  With WITNESS both cases end in
        the same "fatal double fault in supervisor mode" with cr2 just below
        the kernel stack, but the backtrace is lost ("Bad frame pointer").
        With ddb.panic=0 the machine then hung in "syncing disks..." instead
        of rebooting (every run, both kernels); a reset was needed.

        The operation is available to root via ioctl(SIOCSIFPARENT) +
        ioctl(SIOCGIFMEDIA); no special hardware is involved.  svlan(4) uses
        the same vlan_set_parent()/vlan_media_get() and is affected identically.

>How-To-Repeat:

        As root.  Either sequence crashes the machine.

        1. vlan as its own parent:

            ifconfig vlan100 create
            ifconfig vlan100 parent vlan100
            ifconfig vlan100

        2. Indirect loop through a second vlan:

            ifconfig vlan100 create
            ifconfig vlan10 create
            ifconfig vlan100 parent vlan10
            ifconfig vlan10 parent vlan100
            ifconfig

        In case 1 the second command should fail; in case 2 the fourth
        command (the one closing the loop) should fail.  The last command
        issues SIOCGIFMEDIA and overflows the kernel stack.

>Fix:

        A check for "parent == self" alone is not enough: it would still
        accept case 2, which recurses the same way.

        Instead, vlan_set_parent() calls a new vlan_parent_loop(ifp, ifp0).
        It starts at the proposed parent and follows the vlan parent chain
        (identified by if_ioctl == vlan_ioctl, which vlan and svlan share).
        It returns EINVAL if it reaches the vlan being configured, and stops
        at the first non-vlan interface (the bottom of the stack).

        Only interface identity is compared, never the vlan tag (vnetid).
        So vlan-on-vlan and QinQ (svlan over vlan) stacks, including ones
        where both layers use the same tag, are still allowed.  The walk
        holds references with if_ref()/if_get()/if_put().  The walk is
        bounded at 64 hops so that it always terminates.

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 17:24:54 -0000
@@ -130,6 +130,7 @@ void        vlan_link_state(struct vlan_softc *, u_char, 
uint
 
 int    vlan_set_vnetid(struct vlan_softc *, uint16_t);
 int    vlan_set_parent(struct vlan_softc *, const char *);
+int    vlan_parent_loop(struct ifnet *, struct ifnet *);
 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);
@@ -918,7 +919,36 @@ unlock:
        return (error);
 }
 
+/*
+ * Return non-zero if making ifp0 the parent of ifp would create a loop,
+ * i.e. ifp is ifp0 or is reachable by following the vlan parent chain
+ * from ifp0.  The walk is bounded in case a loop already exists.
+ */
 int
+vlan_parent_loop(struct ifnet *ifp, struct ifnet *ifp0)
+{
+       struct ifnet *p, *next;
+       unsigned int depth = 0;
+       int loop = 0;
+
+       p = if_ref(ifp0);
+       while (p != NULL) {
+               if (p == ifp || ++depth > 64) {
+                       loop = 1;
+                       break;
+               }
+               if (p->if_ioctl != vlan_ioctl)
+                       break;
+               next = if_get(((struct vlan_softc *)p->if_softc)->sc_ifidx0);
+               if_put(p);
+               p = next;
+       }
+       if_put(p);
+
+       return (loop);
+}
+
+int
 vlan_set_parent(struct vlan_softc *sc, const char *parent)
 {
        struct ifnet *ifp = &sc->sc_if;
@@ -931,6 +961,12 @@ vlan_set_parent(struct vlan_softc *sc, const char *par
 
        if (ifp0->if_type != IFT_ETHER) {
                error = EPROTONOSUPPORT;
+               goto put;
+       }
+
+       if (vlan_parent_loop(ifp, ifp0)) {
+               /* a vlan cannot be (indirectly) its own parent */
+               error = EINVAL;
                goto put;
        }
 

>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; kernel GENERIC.MP + WITNESS (GENERIC
        already has DIAGNOSTIC), amd64, built without warnings.  Unpatched
        = the same tree without the diff, same config.  (ifconfig only warns
        when SIOCSIFPARENT fails and exits 0, so refusals were judged by
        "SIOCSIFPARENT: Invalid argument" and "parent none" on the vlan
        whose parent was refused.)  How-To-Repeat run three times per case
        on each kernel:

          case 1 (self):       unpatched 3/3 "fatal double fault in
                               supervisor mode", cr2 just below the kernel
                               stack, machine hung; patched 3/3 refused,
                               "ifconfig vlan100" fine.
          case 2 (two vlans):  unpatched 3/3 double fault; patched 3/3 the
                               loop-closing "vlan10 parent vlan100" refused,
                               the first (legal) one accepted, "ifconfig" fine.

        Patched kernel, three runs each:
          - other loop shapes: svlan100 as its own parent; vlan20 on vlan21
            on vlan22 with vlan22 then on vlan20 (3 vlans); svlan30 on vlan30
            with vlan30 then on svlan30 (mixed vlan/svlan): in every run
            exactly the loop-closing command was refused and the following
            media query was fine.
          - QinQ with the same tag on two layers: two pair(4) interfaces
            patched together across rdomains, each with vlan10 on the pair,
            svlan20 on vlan10 and a vlan with tag 20 on svlan20; all parents
            accepted, ping across the stacks succeeded 3/3.
          - a legal stack of 8 vlans on each end of a pair(4) link (vlan1 on
            pair0, vlan2 on vlan1, ... vlan8 on vlan7, the same on pair1):
            all 16 parents accepted, media fine on every vlan, inet and
            inet6 addresses on the top vlans, ping and ping6 across
            succeeded 3/3.

Reply via email to