Skip to contentSkip to conversation

ip6_gre: return the full header length from ip6gre_header()

PATCH In review Kyuwon Cho 3 participants 4 messages Nov 25 – Nov 29

Subsystems (3)
lore
Share & tools
lore

Kyuwon Cho

author
#0 lore raw

PATCH 1/1 ip6_gre: return the full header length from ip6gre_header()

Since commit 76cc0d3282d4 ("ip6_gre: skb_push ipv6hdr before packing the header in ip6gre_header"), ip6gre_header() pushes the outer IPv6 header and the GRE header, t->hlen + sizeof(struct ipv6hdr) bytes, but it still returns t->hlen. packet_snd() copies the user's payload at the offset dev_hard_header() returns, so an AF_PACKET SOCK_DGRAM send on an ip6gre device with header_ops (remote any) writes the payload over the outer IPv6 header and leaves the last 40 bytes of the packet uninitialized. A packet tap on the device then copies those bytes to user space:

  BUG: KMSAN: kernel-infoleak in _copy_to_iter (lib/iov_iter.c:197)
   _copy_to_iter (lib/iov_iter.c:197)
   __skb_datagram_iter (net/core/datagram.c:402)
   packet_recvmsg (net/packet/af_packet.c:3573)
   sock_recvmsg (net/socket.c:1176)
   __sys_recvfrom (net/socket.c:2333)
  Uninit was created at:
   __alloc_skb (net/core/skbuff.c:716)
   alloc_skb_with_frags (net/core/skbuff.c:6940)
   sock_alloc_send_pskb (net/core/sock.c:3096)
   packet_sendmsg (net/packet/af_packet.c:3023 af_packet.c:3209)
  Bytes 68-107 of 108 are uninitialized

Return the length that was pushed, as ipgre_header() does. With the payload in place, the tap also shows that ip6gre_header() never sets payload_len, so set it as well.

Fixes 76cc0d3282d4 ("ip6_gre: skb_push ipv6hdr before packing the header in ip6gre_header")

---
The issue was found by an LLM-guided test generator that we are developing as a research prototype. A reproducer, which runs as any user; ip6gre-tap sends 64 bytes of 0xaa through an AF_PACKET SOCK_DGRAM socket and prints the first 48 bytes that a packet tap on the device sees:

$ cat ip6gre-tap.c
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>

/* send 64 bytes of 0xaa through argv[1] and print what a tap on it sees */ int main(int argc, char **argv)

{
	unsigned char sa[offsetof(struct sockaddr_ll, sll_addr) + 16] = {};
	struct sockaddr_ll *dst = (void *)sa, tap = { .sll_family = AF_PACKET };
	unsigned char payload[64], buf[256];
	int c = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	int s = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
	int n;

	tap.sll_protocol = htons(ETH_P_ALL);
	tap.sll_ifindex = if_nametoindex(argv[1]);
	bind(c, (void *)&tap, sizeof(tap));

	dst->sll_family = AF_PACKET;
	dst->sll_protocol = htons(ETH_P_IPV6);
	dst->sll_ifindex = tap.sll_ifindex;
	dst->sll_halen = 16;
	inet_pton(AF_INET6, "2001:db8::2", sa + offsetof(struct sockaddr_ll, sll_addr));
	memset(payload, 0xaa, sizeof(payload));
	sendto(s, payload, sizeof(payload), 0, (void *)sa, sizeof(sa));

	n = recv(c, buf, sizeof(buf), 0);
	printf("%d bytes:", n);
	for (int i = 0; i < 48; i++)
		printf(" %02x", buf[i]);
	printf("\n");
	return 0;
}
$ unshare -Urn sh -e <<'END'
ip link add g6 type ip6gre local 2001:db8::1
ip link set g6 up
./ip6gre-tap g6
END

Without the patch the outer IPv6 header is 0xaa from byte 4 on and KMSAN reports the infoleak above. With it:

108 bytes: 60 0c f6 22 00 44 2f 40 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 01 20 01 0d b8 00 00 00 00 00 00 00 00 00 00 00 02 00 00 86 dd aa aa aa aa

The patch was written with LLM assistance; we reviewed and tested it.

 net/ipv6/ip6_gre.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/ip6_gre.c +3 -2
index b5d6ed8f0c36..9b30fb2f4bcf 100644
@@ -1397,6 +1397,7 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
 						  t->fl.u.ip6.flowlabel,
 						  true, &t->fl.u.ip6));
+	ipv6h->payload_len = htons(t->hlen + len);
 	ipv6h->hop_limit = t->parms.hop_limit;
 	ipv6h->nexthdr = NEXTHDR_GRE;
 	ipv6h->saddr = t->parms.laddr;
@@ -1415,9 +1416,9 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	if (daddr)
 		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
 	if (!ipv6_addr_any(&ipv6h->daddr))
-		return t->hlen;
+		return needed;
 
-	return -t->hlen;
+	return -needed;
 }
 
 static const struct header_ops ip6gre_header_ops = {
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 13 lines · Kyuwon Cho
net/ipv6/ip6_gre.c +1 -0
index b5d6ed8f0c36..9b30fb2f4bcf 100644
@@ -1397,6 +1397,7 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
 						  t->fl.u.ip6.flowlabel,
 						  true, &t->fl.u.ip6));
+	ipv6h->payload_len = htons(t->hlen + len);
 	ipv6h->hop_limit = t->parms.hop_limit;
 	ipv6h->nexthdr = NEXTHDR_GRE;
 	ipv6h->saddr = t->parms.laddr;

net/ipv6/ip6_gre.c static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, · line 1422

Quoted 9 lines
net/ipv6/ip6_gre.c +2 -2
@@ -1415,9 +1416,9 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	if (daddr)
 		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
 	if (!ipv6_addr_any(&ipv6h->daddr))
-		return t->hlen;
+		return needed;
 
-	return -t->hlen;
+	return -needed;
 }

There's another info leak in this function. It only initializes the base GRE header. The optional parts (e.g., key, sequence number) are left uninitialized.

Before this patch the payload was likely written over these bytes, but now they remain uninitialized and leak. I suggest a memset(ipv6h, 0, needed) after the skb_push(). ipgre_header() has the same problem.

Please wait for the AI review before sending v2. As I see it, it should be three patches:

1. Add missing memset() in ipgre_header().
2. Add missing memset() in ip6gre_header().
3. The current patch.

And use the appropriate Fixes tag for each.

#2 lore raw
In reply to Kyuwon Cho
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 (1) · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix leaves part of the leak in place.
- [Low] The commit message understates the impact.

Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but the creator and consumer of the header disagree.

--

ip6_gre: return the full header length from ip6gre_header()

This commit changes ip6gre_header() to return the full number of bytes it pushes (t->hlen + sizeof(struct ipv6hdr)) instead of t->hlen. With that, packet_snd() copies the AF_PACKET payload after the outer IPv6 and GRE headers. It also sets payload_len in the outer IPv6 header.

Quoted 11 lines

ip6_gre: return the full header length from ip6gre_header()

Since commit 76cc0d3282d4 ("ip6_gre: skb_push ipv6hdr before packing the header in ip6gre_header"), ip6gre_header() pushes the outer IPv6 header and the GRE header, t->hlen + sizeof(struct ipv6hdr) bytes, but it still returns t->hlen. packet_snd() copies the user's payload at the offset dev_hard_header() returns, so an AF_PACKET SOCK_DGRAM send on an ip6gre device with header_ops (remote any) writes the payload over the outer IPv6 header and leaves the last 40 bytes of the packet uninitialized. A packet tap on the device then copies those bytes to user space:

[Severity: Low]
Is a local packet tap the only place these bytes end up?

The ip6gre xmit path never strips the header that ip6gre_header() builds. __gre6_xmit() only reads daddr from it:

net/ipv6/ip6_gre.c:__gre6_xmit() {
    ...
	if (dev->header_ops && dev->type == ARPHRD_IP6GRE)
		fl6->daddr = ((struct ipv6hdr *)skb->data)->daddr;
    ...
}

After that, gre_build_header() and ip6_tnl_xmit() push a new GRE header and a new IPv6 header in front of it.

The reproducer sends ETH_P_IPV6, so its packet is dropped. The NBMA branch of ip6_tnl_xmit() takes tx_err_link_failure because the AF_PACKET skb has no dst.

Any other sll_protocol (ETH_P_MPLS_UC, for example) goes through ip6gre_xmit_other()->__gre6_xmit()->ip6_tnl_xmit(). That path routes to the daddr from sll_addr, so the previously uninitialized bytes go out on the wire.

Also, before the fix, prepare_ip6gre_xmit_ipv6() parsed ipv6_hdr(skb). packet_snd() resets the network header before calling dev_hard_header(), so that pointer sits at data + needed and overlaps the uninitialized tail.

Could the commit message say that these bytes can leave the host, so stable and security triage see the full impact?

net/ipv6/ip6_gre.c static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, · line 1400

Quoted 9 lines
net/ipv6/ip6_gre.c +1 -0
index b5d6ed8f0c369..9b30fb2f4bcfc 100644
@@ -1397,6 +1397,7 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
 						  t->fl.u.ip6.flowlabel,
 						  true, &t->fl.u.ip6));
+	ipv6h->payload_len = htons(t->hlen + len);

[Severity: Medium]
This isn't a bug introduced by this patch, but is the header built here ever removed on the transmit side?

ip6gre_header() pushes t->hlen + 40 bytes. The ndo_start_xmit path ip6gre_tunnel_xmit()->ip6gre_xmit_{ipv4,ipv6,other}()->__gre6_xmit() never pulls them. gre_build_header() then calls skb_push(skb, hdr_len), and ip6_tnl_xmit() pushes another IPv6 header. The packet goes out as:

IPv6 | GRE | IPv6 | GRE | payload

The IPv4 version strips the header before it transmits:

net/ipv4/ip_gre.c:ipgre_xmit() {
    ...
	if (dev->header_ops) {
		int pull_len = tunnel->hlen + sizeof(struct iphdr);
    ...
		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
		skb_pull(skb, pull_len);
    ...
}

So the comparison with ipgre_header() in the commit message holds for the return value, but not for the transmit side.

Now that payload_len is valid, a Linux peer may decapsulate twice. The second set of headers also adds 40 + t->hlen bytes per packet that the MTU calculation doesn't count, so packets close to the MTU can fail with EMSGSIZE or PMTU errors.

net/ipv6/ip6_gre.c static int ip6gre_header(struct sk_buff *skb, struct net_device *dev, · line 1419

Quoted 9 lines
net/ipv6/ip6_gre.c +1 -1
 	ipv6h->hop_limit = t->parms.hop_limit;
 	ipv6h->nexthdr = NEXTHDR_GRE;
 	ipv6h->saddr = t->parms.laddr;
@@ -1415,9 +1416,9 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 	if (daddr)
 		memcpy(&ipv6h->daddr, daddr, sizeof(struct in6_addr));
 	if (!ipv6_addr_any(&ipv6h->daddr))
-		return t->hlen;
+		return needed;

[Severity: High]
With needed returned, packet_snd() now copies the payload after the full t->hlen + 40 bytes. Doesn't that leave the GRE option bytes uninitialized when okey, oseq or ocsum is set, or when a FOU/GUE encap is used?

ip6gre_header() writes the IPv6 header and only the 4-byte GRE base header:

	p = (__be16 *)(ipv6h + 1);
	p[0] = ip_tunnel_flags_to_be16(t->parms.o_flags);
	p[1] = htons(type);

However, t->hlen also counts the optional fields and encap_hlen:

net/ipv6/ip6_gre.c:ip6gre_calc_hlen() {
	tunnel->tun_hlen = gre_calc_hlen(tunnel->parms.o_flags);
	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen;
	...
}

Nothing writes the pushed bytes from offset 44 up to 40 + t->hlen. Before this patch, the payload copy at offset t->hlen happened to cover them. Now nothing does, yet p[0] still sets the key, sequence and checksum flag bits for that space.

ip6gre_tunnel_validate() rejects only GRE_VERSION and GRE_ROUTING, so these output flags are allowed on remote-any devices that use header_ops. Try the reproducer from the commit message against this device:

ip link add g6 type ip6gre local 2001:db8::1 okey 1

Bytes 44-47 of the captured frame should stay uninitialized. They reach the tap through dev_queue_xmit_nit(), and the same KMSAN kernel-infoleak should fire. __gre6_xmit() doesn't strip this header, so with a non-IP sll_protocol these bytes would also be transmitted.

Would it make sense for ip6gre_header() to zero the region after the GRE base header, or to fill in o_key and the sequence number?

The IPv4 ipgre_header() in net/ipv4/ip_gre.c, which the commit message uses as the model, also writes only greh->flags and greh->protocol. The same uninitialized option bytes can reach a tap there before ipgre_xmit() pulls the header. That one is pre-existing.

Quoted 6 lines
net/ipv6/ip6_gre.c +1 -1
-	return -t->hlen;
+	return -needed;
 }
 
 static const struct header_ops ip6gre_header_ops = {
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/179553307366.1670514.6802805102726706751%40gmail.com
maintainer
#3 lore raw
Quoted 2 lines · On Sun, Nov 29, 2026 at 02:26:52AM +0000, [email protected] wrote:

[Severity: Low]
Is a local packet tap the only place these bytes end up?

Yes. These NBMA tunnels are completely broken. In this mode, ip6gre_tunnel_xmit() passes the ANY address as the remote address to ip6_tnl_xmit_ctl(), which causes it to return 0 and the packet is dropped. It never makes it to the wire.

[...]

Quoted 3 lines

[Severity: Medium]
This isn't a bug introduced by this patch, but is the header built here ever removed on the transmit side?

See above. The packet never makes it to the peer.

[...]

Quoted 4 lines

[Severity: High]
With needed returned, packet_snd() now copies the payload after the full t->hlen + 40 bytes. Doesn't that leave the GRE option bytes uninitialized when okey, oseq or ocsum is set, or when a FOU/GUE encap is used?

Already mentioned.