net: fix underflow for checksum of invalid IPv4 packets
authorBruce Richardson <bruce.richardson@intel.com>
Mon, 17 Dec 2018 15:50:04 +0000 (15:50 +0000)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 21 Dec 2018 15:22:41 +0000 (16:22 +0100)
If we receive a packet with an invalid IP header, where the total packet
length is reported as less than the IP header length, we would end up
getting an underflow in the length subtraction.

This could cause us to checksum e.g. 4GB of data in the case where the
result of the subtraction was -1.

We fix this by having the function return 0 - an invalid sum - when
the length is less than the header length.

Fixes: af75078fece3 ("first public release")
Fixes: 6006818cfb26 ("net: new checksum functions")
Cc: stable@dpdk.org
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Hemant Agrawal <hemant.agrawal@nxp.com>
lib/librte_net/rte_ip.h

index f2a8904..f9b9090 100644 (file)
@@ -310,16 +310,20 @@ rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
  * @param l4_hdr
  *   The pointer to the beginning of the L4 header.
  * @return
- *   The complemented checksum to set in the IP packet.
+ *   The complemented checksum to set in the IP packet
+ *   or 0 on error
  */
 static inline uint16_t
 rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
 {
        uint32_t cksum;
-       uint32_t l4_len;
+       uint32_t l3_len, l4_len;
+
+       l3_len = rte_be_to_cpu_16(ipv4_hdr->total_length);
+       if (l3_len < sizeof(struct ipv4_hdr))
+               return 0;
 
-       l4_len = (uint32_t)(rte_be_to_cpu_16(ipv4_hdr->total_length) -
-               sizeof(struct ipv4_hdr));
+       l4_len = l3_len - sizeof(struct ipv4_hdr);
 
        cksum = rte_raw_cksum(l4_hdr, l4_len);
        cksum += rte_ipv4_phdr_cksum(ipv4_hdr, 0);