]> git.hungrycats.org Git - linux/commitdiff
ipv4: validate checksum_start before completing checksum
authorMichael S. Tsirkin <mst@redhat.com>
Wed, 7 Oct 2026 22:42:49 +0000 (18:42 -0400)
committerJakub Kicinski <kuba@kernel.org>
Thu, 8 Oct 2026 18:03:47 +0000 (11:03 -0700)
If a packet with bad checksum metadata gets into the ipv4 stack,
skb_checksum_help can corrupt the network header and cause a bunch of
mischief.

This was discovered and reported by Paulos, and has been reporoduced
by others independently since.

We really shouldn't allow such packets in, but as a defence
in depth measure, let's also check before we complete the checksum.

A more complete validation at input is forthcoming, but needs more work.

Cc: stable@vger.kernel.org
Fixes: f43798c27684 ("tun: Allow GSO using virtio_net_hdr")
Fixes: bfd5f4a3d605 ("packet: Add GSO/csum offload support.")
Reported-by: Paulos Yibelo <habte.yibelo@gmail.com>
Closes: https://lore.kernel.org/netdev/20260922030310.8684-2-habte.yibelo@gmail.com/
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Link: https://patch.msgid.link/0a012b4923e189c4c593ef4f471e5ff0edbe9030.1791412497.git.mst@redhat.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/ip.h
net/bridge/netfilter/nf_conntrack_bridge.c
net/ipv4/ip_output.c
net/netfilter/nfnetlink_queue.c
net/netfilter/xt_CHECKSUM.c
net/xfrm/xfrm_output.c

index 6f602df72ee621ee4ee45e70beef0a1b5145367f..d07c2c5730240b80a2361c0a73694d22b22de8d8 100644 (file)
@@ -74,6 +74,13 @@ static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
        return ip_hdr(skb)->ihl * 4;
 }
 
+static inline int ip_check_csum_start(const struct sk_buff *skb)
+{
+       if (unlikely(skb->csum_start < skb->network_header + ip_hdrlen(skb)))
+               return -EINVAL;
+       return 0;
+}
+
 struct ipcm_cookie {
        struct sockcm_cookie    sockc;
        __be32                  addr;
index 7ecb8a26bfa3a7a5cd312be0ecba046bd4160722..b5444335b86f2bf1b6a882bcc56111e5209d2b8e 100644 (file)
@@ -39,9 +39,13 @@ static int nf_br_ip_fragment(struct net *net, struct sock *sk,
        int err = 0;
 
        /* for offloaded checksums cleanup checksum before fragmentation */
-       if (skb->ip_summed == CHECKSUM_PARTIAL &&
-           (err = skb_checksum_help(skb)))
-               goto blackhole;
+       if (skb->ip_summed == CHECKSUM_PARTIAL) {
+               err = ip_check_csum_start(skb);
+               if (!err)
+                       err = skb_checksum_help(skb);
+               if (err)
+                       goto blackhole;
+       }
 
        iph = ip_hdr(skb);
 
index a24cc8ee11d3ea3069bcc0d4d12e6867c2c475f7..eaa6fabda347e9181ffb687d3a7e6f37fb70cba6 100644 (file)
@@ -771,9 +771,13 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
        int err = 0;
 
        /* for offloaded checksums cleanup checksum before fragmentation */
-       if (skb->ip_summed == CHECKSUM_PARTIAL &&
-           (err = skb_checksum_help(skb)))
-               goto fail;
+       if (skb->ip_summed == CHECKSUM_PARTIAL) {
+               err = ip_check_csum_start(skb);
+               if (!err)
+                       err = skb_checksum_help(skb);
+               if (err)
+                       goto fail;
+       }
 
        /*
         *      Point into the IP datagram header.
index a3bc0028005109ce2736371ed321688231c03f28..477fc632657f4caaf3a920ec79a10a9c7ec2e842 100644 (file)
@@ -40,6 +40,7 @@
 #include <linux/udp.h>
 #include <net/gre.h>
 #include <net/gso.h>
+#include <net/ip.h>
 #include <net/sock.h>
 #include <net/tcp_states.h>
 #include <net/netfilter/nf_queue.h>
@@ -674,6 +675,12 @@ nla_put_failure:
 
 static int nf_queue_checksum_help(struct sk_buff *entskb)
 {
+       if (entskb->protocol == htons(ETH_P_IP)) {
+               if (!pskb_network_may_pull(entskb, sizeof(struct iphdr)) ||
+                   ip_check_csum_start(entskb))
+                       return -EINVAL;
+       }
+
        if (skb_csum_is_sctp(entskb))
                return skb_crc32c_csum_help(entskb);
 
index 9d99f5a3d1764bd84e3e04d2aca739840b0b51da..1fb0f8118404d6b4e44d11742aea591a90b18e45 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <linux/netfilter_ipv4/ip_tables.h>
 #include <linux/netfilter_ipv6/ip6_tables.h>
+#include <net/ip.h>
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Michael S. Tsirkin <mst@redhat.com>");
@@ -25,8 +26,11 @@ MODULE_ALIAS("ip6t_CHECKSUM");
 static unsigned int
 checksum_tg(struct sk_buff *skb, const struct xt_action_param *par)
 {
-       if (skb->ip_summed == CHECKSUM_PARTIAL && !skb_is_gso(skb))
+       if (skb->ip_summed == CHECKSUM_PARTIAL && !skb_is_gso(skb)) {
+               if (xt_family(par) == NFPROTO_IPV4 && ip_check_csum_start(skb))
+                       return NF_DROP;
                skb_checksum_help(skb);
+       }
 
        return XT_CONTINUE;
 }
index e305ba32e356b7db0efa8da74c6903498fff4357..f1f612cd7b438ddb760022645fed9997fbeeb534 100644 (file)
@@ -823,16 +823,22 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)
        }
 
        if (skb->ip_summed == CHECKSUM_PARTIAL) {
-               err = skb_checksum_help(skb);
-               if (err) {
-                       XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
-                       kfree_skb(skb);
-                       return err;
+               if (skb->protocol == htons(ETH_P_IP)) {
+                       err = ip_check_csum_start(skb);
+                       if (err)
+                               goto error;
                }
+               err = skb_checksum_help(skb);
+               if (err)
+                       goto error;
        }
 
 out:
        return xfrm_output2(net, sk, skb);
+error:
+       XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
+       kfree_skb(skb);
+       return err;
 }
 EXPORT_SYMBOL_GPL(xfrm_output);