>Synopsis:      nvgre_set_parent() (sys/net/if_gre.c) rejects a requested
                parent only if it does not exist or is not IFF_MULTICAST.  An
                nvgre(4) is itself a multicast-capable Ethernet interface, so
                "ifconfig nvgreN parent nvgreN" is accepted, and so is the 
second
                half of a two-nvgre cycle ("nvgreA parent nvgreB; nvgreB parent
                nvgreA").  The nvgre is left using itself (or its peer in the
                loop) as its multicast underlay; it still comes up, and with
                net.inet.gre.allow=1 each frame it sends is handed back to the
                nvgre and encapsulated again, dozens of times, until it no 
longer
                fits the MTU.  No crash: nvgre has a fixed media and does not
                forward SIOCGIFMEDIA to its parent, so the media recursion that
                makes the same mistake fatal in vlan(4) and bpe(4) (reported
                separately) does not occur here.  nvgre(4) is a separate driver.
>Category:      kernel
>Severity:      non-critical
>Priority:      low
>Class:         sw-bug
>Release:       OpenBSD 8.0-current (source as of 2026-11-03, git d5b06e73;
                also present on master afb08c4d5)
>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_gre.c rev 1.192, openbsd/src as of
                      2026-11-03, git d5b06e73

>Description:

        nvgre_set_parent() (sys/net/if_gre.c):

            ifp0 = if_unit(parent);
            if (ifp0 == NULL)
                    return (EINVAL);
            if (!ISSET(ifp0->if_flags, IFF_MULTICAST)) {
                    if_put(ifp0);
                    return (EPROTONOSUPPORT);
            }
            ifsetlro(ifp0, 0);
            /* commit */
            sc->sc_ifp0 = ifp0->if_index;

        There is no identity or loop check at all.  An nvgre is IFF_MULTICAST,
        so the nvgre itself, or another nvgre whose parent is this one, is
        committed as the multicast underlay used by nvgre_up() (group join)
        and for transmit.

        Observed on the unpatched kernel, net.inet.gre.allow=1: the self- or
        loop-parented nvgre is accepted silently ("ifconfig nvgre200" shows
        "parent nvgre200"), comes up (RUNNING), and transmits far more than it
        is given: 36 output packets during bring-up, then +105 (self) / +70
        (two-nvgre loop) output packets after two pings, after which the
        counters stay flat.  The same steps on an nvgre with a real parent
        (vether) give 1 packet at bring-up and +2 for the two pings.  This is
        consistent with each frame being
        re-encapsulated through the nvgre until it exceeds the MTU (the tunnel
        sets DF).  No mbuf growth, no crash in 20 s of observation.  With the
        default net.inet.gre.allow=0 nvgre output is dropped anyway.  The
        configuration is meaningless and should be refused at SIOCSIFPARENT
        time.

>How-To-Repeat:

        As root (nvgre200, nvgre201 unused):

            ifconfig nvgre200 create
            ifconfig nvgre200 parent nvgre200      # should fail, succeeds
            ifconfig nvgre200 | grep encap
                    encap: vnetid 4096 parent nvgre200 txprio 0 rxprio packet

        Two-nvgre loop:

            ifconfig nvgre201 create
            ifconfig nvgre200 parent nvgre201
            ifconfig nvgre201 parent nvgre200      # closes the loop, succeeds

>Fix:

        Reject a parent that is the nvgre itself or that leads back to it
        along the nvgre parent chain (the same loop check proposed for
        vlan(4) and bpe(4)).  The walk is bounded in case a loop already
        exists.  An nvgre on top of another nvgre without a loop is still
        accepted.

--- sys/net/if_gre.c
+++ sys/net/if_gre.c
@@ -447,6 +447,7 @@
 static int     nvgre_up(struct nvgre_softc *);
 static int     nvgre_down(struct nvgre_softc *);
 static int     nvgre_set_parent(struct nvgre_softc *, const char *);
+static int     nvgre_parent_loop(struct ifnet *, struct ifnet *);
 static void    nvgre_link_change(void *);
 static void    nvgre_detach(void *);
 
@@ -3624,6 +3625,7 @@
 static int
 nvgre_set_parent(struct nvgre_softc *sc, const char *parent)
 {
+       struct ifnet *ifp = &sc->sc_ac.ac_if;
        struct ifnet *ifp0;
 
        ifp0 = if_unit(parent);
@@ -3635,6 +3637,12 @@
                return (EPROTONOSUPPORT);
        }
 
+       if (nvgre_parent_loop(ifp, ifp0)) {
+               /* an nvgre cannot be (indirectly) its own parent */
+               if_put(ifp0);
+               return (EINVAL);
+       }
+
        ifsetlro(ifp0, 0);
 
        /* commit */
@@ -3644,6 +3652,35 @@
        return (0);
 }
 
+/*
+ * 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 nvgre parent chain
+ * from ifp0.  The walk is bounded in case a loop already exists.
+ */
+static int
+nvgre_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 != nvgre_ioctl)
+                       break;
+               next = if_get(((struct nvgre_softc *)p->if_softc)->sc_ifp0);
+               if_put(p);
+               p = next;
+       }
+       if_put(p);
+
+       return (loop);
+}
+
 static int
 nvgre_add_addr(struct nvgre_softc *sc, const struct ifbareq *ifba)
 {

>Verification:
        Diff applied exactly as shown with patch -p0 to sys/net/if_gre.c
        r1.192 of a tree exported with git archive from openbsd/src d5b06e73
        (2026-11-03), no fuzz or
        offset; kernel GENERIC.MP, amd64, built with -Werror, no warnings.
        Unpatched = the same tree without the diff, same config (the patched
        kernel is an incremental rebuild of that tree; the diff was reverted
        afterwards and the source md5 checked).  The diff also applies to
        master afb08c4d5 (offset -3 lines, no fuzz).  VM with 2 vcpu; every
        check below ran on a freshly booted kernel.

        Unpatched (3 fresh boots, 8 runs):
          "ifconfig nvgre200 parent nvgre200" accepted 8/8, no diagnostic,
            "parent nvgre200" shown;
          two-nvgre loop ("nvgre200 parent nvgre201; nvgre201 parent
            nvgre200") accepted 8/8;
          self-parent and loop, brought up (group, local tunnel source on a
            vether, net.inet.gre.allow=1): RUNNING; two pings produce +105
            (self) / +70 (loop) output packets versus +2 with a real parent,
            then stop; no mbuf growth, no crash (1 run each).

        Patched (3 fresh boots, 5 runs):
          self-parent refused 5/5 with "SIOCSIFPARENT: Invalid argument",
            the nvgre keeps "parent none";
          the loop-closing "nvgre201 parent nvgre200" refused 5/5 with the
            same error; "nvgre200 parent nvgre201" (nvgre on nvgre, no loop)
            is still accepted;
          vxlan(4) on the same kernel still accepts its own self-parent 1/1
            (the diff touches nvgre only).

        Normal use, patched 5/5 and unpatched 5/5, identical, with
        net.inet.gre.allow=1 (nvgre output is dropped at the default 0): a
        real multicast underlay = pair(4) link between rdomain 200 (pair290,
        192.0.2.1/24) and rdomain 201 (pair291, 192.0.2.2/24); nvgre200
        (tunneldomain 200, tunnel 192.0.2.1 239.0.0.11, parent pair290,
        vnetid 111, rdomain 202, 198.51.100.1/24) and nvgre201 (tunneldomain
        201, tunnel 192.0.2.2 239.0.0.11, parent pair291, vnetid 111,
        rdomain 203, 198.51.100.2/24): both accept the parent and come up
        RUNNING, "ping -V 202 198.51.100.2" through the tunnel is answered,
        both destroy cleanly.

Reply via email to