Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: ip6_gre: get ipv6hdr after skb_cow_head()

A KASAN:use-after-free bug was found related to ip6-erspan
while running selftests/net/ip6_gre_headroom.sh

It happens because of following sequence:
- ipv6hdr pointer is obtained from skb
- skb_cow_head() is called, skb->head memory is reallocated
- old data is accessed using ipv6hdr pointer

skb_cow_head() call was added in e41c7c68ea77 ("ip6erspan: make sure
enough headroom at xmit."), but looking at the history there was a
chance of similar bug because gre_handle_offloads() and pskb_trim()
can also reallocate skb->head memory. Fixes tag points to commit
which introduced possibility of this bug.

This patch moves ipv6hdr pointer assignment after skb_cow_head() call.

Fixes: 5a963eb61b7c ("ip6_gre: Add ERSPAN native tunnel support")
Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Reviewed-by: Greg Rose <gvrose8192@gmail.com>
Acked-by: William Tu <u9012063@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Prashant Bhole and committed by
David S. Miller
b7ed8794 6e8cfd6d

+2 -1
+2 -1
net/ipv6/ip6_gre.c
··· 927 927 static netdev_tx_t ip6erspan_tunnel_xmit(struct sk_buff *skb, 928 928 struct net_device *dev) 929 929 { 930 - struct ipv6hdr *ipv6h = ipv6_hdr(skb); 931 930 struct ip6_tnl *t = netdev_priv(dev); 932 931 struct dst_entry *dst = skb_dst(skb); 933 932 struct net_device_stats *stats; ··· 1009 1010 goto tx_err; 1010 1011 } 1011 1012 } else { 1013 + struct ipv6hdr *ipv6h = ipv6_hdr(skb); 1014 + 1012 1015 switch (skb->protocol) { 1013 1016 case htons(ETH_P_IP): 1014 1017 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));