Skip to contentSkip to conversation

prevent out of bounds read with checksum

PATCH In review Bruce Richardson 4 participants 10 messages Dec 17, 2018 – Dec 20, 2018 Network drivers

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 net: fix underflow for checksum of invalid IPv4 packets

In reply to Bruce Richardson

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")
---
 lib/librte_net/rte_ip.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
lib/librte_net/rte_ip.h +8 -4
index f2a8904a2..f9b909090 100644
@@ -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);
Signature · 1 line
2.19.2
#4 lore raw

PATCH 2/2 net/tap: add buffer overflow checks before checksum

In reply to Bruce Richardson

The checksum calculation APIs take only the packet headers pointers as parameters, so they assume that the lengths reported in those headers are correct. However, a malicious packet could claim to be far larger than it is, so we need to check the header lengths in the driver before calling the checksum API.

A better fix would be to allow the lengths to be passed into the API function, but that would be an API break, so fixing in TAP driver for now.

Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")
---
 drivers/net/tap/rte_eth_tap.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/net/tap/rte_eth_tap.c +14 -0
index 49afd38dd..0ec030bef 100644
@@ -281,13 +281,27 @@ tap_verify_csum(struct rte_mbuf *mbuf)
 		l3_len = 4 * (iph->version_ihl & 0xf);
 		if (unlikely(l2_len + l3_len > rte_pktmbuf_data_len(mbuf)))
 			return;
+		/* check that the total length reported by header is not
+		 * greater than the total received size
+		 */
+		if (l2_len + rte_be_to_cpu_16(iph->total_length) >
+				rte_pktmbuf_data_len(mbuf))
+			return;
 
 		cksum = ~rte_raw_cksum(iph, l3_len);
 		mbuf->ol_flags |= cksum ?
 			PKT_RX_IP_CKSUM_BAD :
 			PKT_RX_IP_CKSUM_GOOD;
 	} else if (l3 == RTE_PTYPE_L3_IPV6) {
+		struct ipv6_hdr *iph = l3_hdr;
+
 		l3_len = sizeof(struct ipv6_hdr);
+		/* check that the total length reported by header is not
+		 * greater than the total received size
+		 */
+		if (l2_len + l3_len + rte_be_to_cpu_16(iph->payload_len) >
+				rte_pktmbuf_data_len(mbuf))
+			return;
 	} else {
 		/* IPv6 extensions are not supported */
 		return;
Signature · 1 line
2.19.2
#0 lore raw

cover prevent out of bounds read with checksum

The functions for checksumming the packet payload don't perform bounds checks, and are used by the TAP driver which does not do any bounds checks on the incoming packet either. This means a packet received with an incorrect IP header can read beyond the end of the mbuf.

In the worst case, where the length is specified as being smaller than the IPv4 header, 32-bit wrap-around on subtraction occurs, meaning that approx 4GB of memory will be read.

To fix this, we can introduce a sanity check into the ipv4 function to ensure that underflow does not occur. Since the checksum function does not take the mbuf length as a parameter, we cannot check for overflow there, so we instead perform the checks in the TAP driver directly.

Ideally, in a future release, all checksum functions should be modified to take a max buffer length parameter to fix this issue globally.

NOTE: It appears that the dpaa driver also uses these functions, but from what I can see there, they are only used on TX, which means that there should be less need for parameter length checking, as the data does not come from an untrusted source. Perhaps maintainers, Hemant and Shreyansh, can confirm?

CC Hemant Agrawal <[email protected]>
CC Shreyansh Jain <[email protected]>

Bruce Richardson (2):
net: fix underflow for checksum of invalid IPv4 packets net/tap: add buffer overflow checks before checksum

 drivers/net/tap/rte_eth_tap.c | 14 ++++++++++++++
 lib/librte_net/rte_ip.h       | 12 ++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)
Signature · 1 line
2.19.2
#7 lore raw

HI Bruce,

Quoted 14 lines · Bruce Richardson

The functions for checksumming the packet payload don't perform bounds checks, and are used by the TAP driver which does not do any bounds checks on the incoming packet either. This means a packet received with an incorrect IP header can read beyond the end of the mbuf.

In the worst case, where the length is specified as being smaller than the IPv4 header, 32-bit wrap-around on subtraction occurs, meaning that approx 4GB of memory will be read.

To fix this, we can introduce a sanity check into the ipv4 function to ensure that underflow does not occur. Since the checksum function does not take the mbuf length as a parameter, we cannot check for overflow there, so we instead perform the checks in the TAP driver directly.

Quoted 8 lines

Ideally, in a future release, all checksum functions should be modified to take a max buffer length parameter to fix this issue globally.

NOTE: It appears that the dpaa driver also uses these functions, but from what I can see there, they are only used on TX, which means that there should be less need for parameter length checking, as the data does not come from an untrusted source. Perhaps maintainers, Hemant and Shreyansh, can confirm?

In DPAA, we are using software based checksum calculation for self generated packets largely.

They are mostly trust worthy unless someone is deliberately or mistakenly trying to send a corrupt packet.

We will check, if we can also add some checks in DPAA driver in these legs without making performance impact for self generated packets.

Regards,

Hemant

Quoted 12 lines
CC Hemant Agrawal <[email protected]>
CC Shreyansh Jain <[email protected]>

Bruce Richardson (2):
net: fix underflow for checksum of invalid IPv4 packets net/tap: add buffer overflow checks before checksum

  drivers/net/tap/rte_eth_tap.c | 14 ++++++++++++++
  lib/librte_net/rte_ip.h       | 12 ++++++++----
  2 files changed, 22 insertions(+), 4 deletions(-)
#8 lore raw
Quoted 39 lines

-----Original Message-----
From: Hemant Agrawal [mailto:[email protected]]
Sent: Tuesday, December 18, 2018 12:50 PM
To: Richardson, Bruce <[email protected]>; Olivier Matz <[email protected]>; Wiles, Keith <[email protected]>
Cc: [email protected]; Shreyansh Jain <[email protected]>
Subject: Re: [dpdk-dev] [PATCH 0/2] prevent out of bounds read with checksum

HI Bruce,

Quoted 10 lines · Bruce Richardson

The functions for checksumming the packet payload don't perform bounds checks, and are used by the TAP driver which does not do any bounds checks on the incoming packet either. This means a packet received with an incorrect IP header can read beyond the end of the mbuf.

In the worst case, where the length is specified as being smaller than the
IPv4 header, 32-bit wrap-around on subtraction occurs, meaning that approx 4GB of memory will be read.

Quoted 7 lines

To fix this, we can introduce a sanity check into the ipv4 function to ensure that underflow does not occur. Since the checksum function does not take the mbuf length as a parameter, we cannot check for overflow there, so we instead perform the checks in the TAP driver directly.

Ideally, in a future release, all checksum functions should be modified to take a max buffer length parameter to fix this issue

globally.

Quoted 6 lines

NOTE: It appears that the dpaa driver also uses these functions, but from what I can see there, they are only used on TX, which means that there should be less need for parameter length checking, as the data does not come from an untrusted source. Perhaps maintainers, Hemant and Shreyansh, can confirm?

Quoted 9 lines

In DPAA, we are using software based checksum calculation for self generated packets largely.

They are mostly trust worthy unless someone is deliberately or mistakenly trying to send a corrupt packet.

We will check, if we can also add some checks in DPAA driver in these legs without making performance impact for self generated packets.

Right. Thanks for confirming it's not on RX path which would be the main risk. I would assume that data coming from the app should be trusted, unless the app is deliberately trying to crash itself. :-) (I didn't look to try and fix this in DPAA because of that assumption, but glad you are looking into it.)

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] net/tap: add buffer overflow checks before checksum

In reply to Bruce Richardson
Quoted 6 lines · Bruce Richardson

The checksum calculation APIs take only the packet headers pointers as parameters, so they assume that the lengths reported in those headers are correct. However, a malicious packet could claim to be far larger than it is, so we need to check the header lengths in the driver before calling the checksum API.

Quoted 8 lines

A better fix would be to allow the lengths to be passed into the API function, but that would be an API break, so fixing in TAP driver for now.

Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")
#9 lore raw
In reply to Bruce Richardson
Quoted 23 lines · Bruce Richardson

The functions for checksumming the packet payload don't perform bounds checks, and are used by the TAP driver which does not do any bounds checks on the incoming packet either. This means a packet received with an incorrect IP header can read beyond the end of the mbuf.

In the worst case, where the length is specified as being smaller than the IPv4 header, 32-bit wrap-around on subtraction occurs, meaning that approx 4GB of memory will be read.

To fix this, we can introduce a sanity check into the ipv4 function to ensure that underflow does not occur. Since the checksum function does not take the mbuf length as a parameter, we cannot check for overflow there, so we instead perform the checks in the TAP driver directly.

Ideally, in a future release, all checksum functions should be modified to take a max buffer length parameter to fix this issue globally.

NOTE: It appears that the dpaa driver also uses these functions, but from what I can see there, they are only used on TX, which means that there should be less need for parameter length checking, as the data does not come from an untrusted source. Perhaps maintainers, Hemant and Shreyansh, can confirm?

Quoted 6 lines
CC Hemant Agrawal <[email protected]>
CC Shreyansh Jain <[email protected]>

Bruce Richardson (2):
net: fix underflow for checksum of invalid IPv4 packets net/tap: add buffer overflow checks before checksum

Series applied to dpdk-next-net/master, thanks.

Acked-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] net/tap: add buffer overflow checks before checksum

In reply to Bruce Richardson
Quoted 14 lines · Bruce

The checksum calculation APIs take only the packet headers pointers as parameters, so they assume that the lengths reported in those headers are correct. However, a malicious packet could claim to be far larger than it is, so we need to check the header lengths in the driver before calling the checksum API.

A better fix would be to allow the lengths to be passed into the API function, but that would be an API break, so fixing in TAP driver for now.

Quoted 5 lines
Fixes 8ae3023387e9 ("net/tap: add Rx/Tx checksum offload support")

—

Regards,
Keith