Skip to contentSkip to conversation

gre: fix the headers that ipgre_header() and ip6gre_header() build

PATCH New Kyuwon Cho 1 participant 4 messages Nov 30

Subsystems (3)
lore
Share & tools
lore

Kyuwon Cho

author
#0 lore raw

v2 cover gre: fix the headers that ipgre_header() and ip6gre_header() build

ipgre_header() and ip6gre_header() build the outer IP header and the GRE header for AF_PACKET SOCK_DGRAM sends on a GRE device with header_ops (remote any). Neither clears the optional GRE fields, so a packet socket bound to the device reads the key and the sequence number from uninitialized memory (patches 1 and 2, from Ido's review of v1). ip6gre_header() also returns 40 bytes too little, so the payload lands on the outer IPv6 header (patch 3, which is v1).

v2:
- Add patches 1 and 2; v1 is now patch 3 (Ido).
v1: https://lore.kernel.org/netdev/[email protected]/

Tested on a KMSAN kernel as any user under "unshare -Urn", with the program below on three devices, each set up first:

ip link add g4 type gre local 10.0.0.1 okey 7 oseq # gre-tap g4 4
ip link add g6 type ip6gre local 2001:db8::1 okey 7 oseq # gre-tap g6 6
ip link add g6p type ip6gre local 2001:db8::3 # gre-tap g6p 6

Without the series, KMSAN reports bytes 24-31 of the g4 packet (the key and the sequence number) and bytes 68-107 of the g6p packet as uninitialized. With patch 3 alone, it reports bytes 44-51 of the g6 packet. With the series, it reports nothing, the optional fields are zero and the IPv6 headers are intact.

#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>

/* gre-tap DEV 4|6: send 64 bytes of 0xaa through DEV 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 };
	int v6 = argc > 2 && argv[2][0] == '6';
	int proto = htons(v6 ? ETH_P_IPV6 : ETH_P_IP);
	int c = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	int s = socket(AF_PACKET, SOCK_DGRAM, proto);
	unsigned char payload[64], buf[256];
	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 = proto;
	dst->sll_ifindex = tap.sll_ifindex;
	dst->sll_halen = v6 ? 16 : 4;
	inet_pton(v6 ? AF_INET6 : AF_INET, v6 ? "2001:db8::2" : "10.0.0.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 < 64 && i < n; i++)
		printf(" %02x", buf[i]);
	printf("\n");
	return 0;
}

The issue in patch 3 was found by an LLM-guided test generator that we are developing as a research prototype.

Kyuwon Cho (3):
ip_gre: clear the optional GRE fields in ipgre_header()
ip6_gre: clear the optional GRE fields in ip6gre_header()
ip6_gre: return the full header length from ip6gre_header()

 net/ipv4/ip_gre.c  | 1 +
 net/ipv6/ip6_gre.c | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/3 ip_gre: clear the optional GRE fields in ipgre_header()

ipgre_header() pushes the outer IPv4 header and t->hlen bytes of GRE header, but fills in only the IPv4 header and the base GRE header. When the tunnel sends a checksum, a key or a sequence number, those fields keep whatever was in the skb's headroom, and a packet socket bound to the device copies them to user space.

Clear the whole header before filling it in.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by Claude:claude-opus-5-5
---
 net/ipv4/ip_gre.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/ip_gre.c +1 -0
index 5485586fa0e6..84dde2e16f18 100644
@@ -907,6 +907,7 @@ static int ipgre_header(struct sk_buff *skb, struct net_device *dev,
 		return -needed;
 
 	iph = skb_push(skb, needed);
+	memset(iph, 0, needed);
 	greh = (struct gre_base_hdr *)(iph+1);
 	greh->flags = gre_tnl_flags_to_gre_flags(t->parms.o_flags);
 	greh->protocol = htons(type);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v2 2/3 ip6_gre: clear the optional GRE fields in ip6gre_header()

In reply to Kyuwon Cho

ip6gre_header() pushes the outer IPv6 header and t->hlen bytes of GRE header, but fills in only the IPv6 header and the base GRE header. When the tunnel sends a checksum, a key or a sequence number, those fields keep whatever was in the skb's headroom, and a packet socket bound to the device copies them to user space.

Clear the whole header before filling it in.

Fixes c12b395a4664 ("gre: Support GRE over IPv6")
Assisted-by Claude:claude-opus-5-5
---
 net/ipv6/ip6_gre.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv6/ip6_gre.c +1 -0
index b5d6ed8f0c36..9816c62c0c50 100644
@@ -1394,6 +1394,7 @@ static int ip6gre_header(struct sk_buff *skb, struct net_device *dev,
 		return -needed;
 
 	ipv6h = skb_push(skb, needed);
+	memset(ipv6h, 0, needed);
 	ip6_flow_hdr(ipv6h, 0, ip6_make_flowlabel(dev_net(dev), skb,
 						  t->fl.u.ip6.flowlabel,
 						  true, &t->fl.u.ip6));
Signature · 1 line
2.43.0
author
#3 lore raw

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

In reply to Kyuwon Cho

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")
Assisted-by Claude:claude-opus-5-5
---
 net/ipv6/ip6_gre.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/ip6_gre.c +3 -2
index 9816c62c0c50..d824238eb598 100644
@@ -1398,6 +1398,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;
@@ -1416,9 +1417,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