Skip to contentSkip to conversation

net: check that seg is valid before dereference

PATCH Superseded Chas Williams 4 participants 8 messages Sep 28, 2020 – Oct 12, 2020 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: check that seg is valid before dereference

If the overall pkt_len and segment lengths are out of agreement, it is possible for the seg to be NULL after the loop. Add assert to check this condition in debug builds.

Fixes c442fed81bb9 ("net: add function to calculate checksum in mbuf")
---
 lib/librte_net/rte_ip.h | 1 +
 1 file changed, 1 insertion(+)
lib/librte_net/rte_ip.h +1 -0
index fcd1eb342..6b3e4cdda 100644
@@ -225,6 +225,7 @@ rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
 			break;
 		off -= seglen;
 	}
+	RTE_ASSERT(seg != NULL);
 	seglen -= off;
 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
 	if (seglen >= len) {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 23 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Chas Williams
Sent: Monday, September 28, 2020 11:32 PM
To: [email protected]
Cc: [email protected]; Chas Williams <[email protected]>
Subject: [dpdk-dev] [PATCH] net: check that seg is valid before dereference

If the overall pkt_len and segment lengths are out of agreement, it is possible for the seg to be NULL after the loop. Add assert to check this condition in debug builds.

Fixes c442fed81bb9 ("net: add function to calculate checksum in mbuf")
---
 lib/librte_net/rte_ip.h | 1 +
 1 file changed, 1 insertion(+)
lib/librte_net/rte_ip.h index renamed from lib/librte_net/rte_ip.h +0 -0

fcd1eb342..6b3e4cdda 100644

lib/librte_net/rte_ip.h rte_raw_cksum_mbuf(const struct rte_mbuf *m, · line 229

Quoted 5 lines
lib/librte_net/rte_ip.h +1 -0
@@ -225,6 +225,7 @@ rte_raw_cksum_mbuf(const struct rte_mbuf *m,
 uint32_t off, uint32_t len,
 			break;
 		off -= seglen;
 	}
+	RTE_ASSERT(seg != NULL);

Is it better to return an error code?

Quoted 5 lines
lib/librte_net/rte_ip.h +0 -0
 	seglen -= off;
 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);

if (seglen >= len) {
--
2.26.2

#2 lore raw
Quoted 18 lines · wangyunjian
Quoted 18 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Chas Williams
Sent: Monday, September 28, 2020 11:32 PM
To: [email protected]
Cc: [email protected]; Chas Williams <[email protected]>
Subject: [dpdk-dev] [PATCH] net: check that seg is valid before dereference

If the overall pkt_len and segment lengths are out of agreement, it is possible for the seg to be NULL after the loop. Add assert to check this condition in debug builds.

Fixes c442fed81bb9 ("net: add function to calculate checksum in mbuf")
---
  lib/librte_net/rte_ip.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 12 lines

lib/librte_net/rte_ip.h rte_raw_cksum_mbuf(const struct rte_mbuf *m, · line 229

Quoted 10 lines
lib/librte_net/rte_ip.h index renamed from lib/librte_net/rte_ip.h +0 -0

fcd1eb342..6b3e4cdda 100644

lib/librte_net/rte_ip.h +1 -0
@@ -225,6 +225,7 @@ rte_raw_cksum_mbuf(const struct rte_mbuf *m,
 uint32_t off, uint32_t len,
                     break;
             off -= seglen;
     }
+    RTE_ASSERT(seg != NULL);

Is it better to return an error code?

Maybe. However, to get into this state your mbuf chain is already badly broken. Personally, I would prefer an immediate failure in the application so I can track
down the source of this error.

No one is checking the return code now, so returning one wouldn't really help unless I also fix the callers. Lastly, would everyone be happy with an extra branch in the virtio RX path?

Quoted 7 lines
Quoted 6 lines
lib/librte_net/rte_ip.h +0 -0
     seglen -= off;
     buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
     if (seglen >= len) {

--
2.26.2

#3 lore raw

PATCH v2 1/2 net: check that seg is valid before dereference

In reply to Chas Williams

If the overall pkt_len and segment lengths are out of agreement, it is possible for the seg to be NULL after the loop. Add assert to check this condition in debug builds. Otherwise, return failure.

Fixes c442fed81bb9 ("net: add function to calculate checksum in mbuf")
---
 lib/librte_net/rte_ip.h | 3 +++
 1 file changed, 3 insertions(+)
lib/librte_net/rte_ip.h +3 -0
index fcd1eb342..b2bb5d7b9 100644
@@ -225,6 +225,9 @@ rte_raw_cksum_mbuf(const struct rte_mbuf *m, uint32_t off, uint32_t len,
 			break;
 		off -= seglen;
 	}
+	RTE_ASSERT(seg != NULL);
+	if (seg == NULL)
+		return -1;
 	seglen -= off;
 	buf = rte_pktmbuf_mtod_offset(seg, const char *, off);
 	if (seglen >= len) {
Signature · 1 line
2.26.2
#4 lore raw

PATCH v2 2/2 net/virtio: check return from rte_raw_cksum_mbuf

rte_raw_cksum_mbuf can fail, so we should check to see if it has. If so, return with an error.

Fixes 96cb6711939e ("net/virtio: support Rx checksum offload")
---
 drivers/net/virtio/virtio_rxtx.c            | 5 +++--
 drivers/net/virtio/virtio_rxtx_packed_avx.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
drivers/net/virtio/virtio_rxtx.c +3 -2
index 0ade35215..019187bf9 100644
@@ -910,9 +910,10 @@ virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
 			 */
 			uint16_t csum = 0, off;
 
-			rte_raw_cksum_mbuf(m, hdr->csum_start,
+			if (rte_raw_cksum_mbuf(m, hdr->csum_start,
 				rte_pktmbuf_pkt_len(m) - hdr->csum_start,
-				&csum);
+				&csum) < 0)
+				return -EINVAL;
 			if (likely(csum != 0xffff))
 				csum = ~csum;
 			off = hdr->csum_offset + hdr->csum_start;
drivers/net/virtio/virtio_rxtx_packed_avx.c +3 -2
index 6a8214725..67a55abce 100644
@@ -328,9 +328,10 @@ virtio_vec_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
 			 */
 			uint16_t csum = 0, off;
 
-			rte_raw_cksum_mbuf(m, hdr->csum_start,
+			if (rte_raw_cksum_mbuf(m, hdr->csum_start,
 				rte_pktmbuf_pkt_len(m) - hdr->csum_start,
-				&csum);
+				&csum) < 0)
+				return -1;
 			if (likely(csum != 0xffff))
 				csum = ~csum;
 			off = hdr->csum_offset + hdr->csum_start;
Signature · 1 line
2.26.2
#5 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/virtio: check return from rte_raw_cksum_mbuf

Quoted 4 lines

rte_raw_cksum_mbuf can fail, so we should check to see if it has. If so, return with an error.

Fixes 96cb6711939e ("net/virtio: support Rx checksum offload")

It is an old bug, so it should be backported, right?
Cc: [email protected]

Quoted 5 lines

---

 drivers/net/virtio/virtio_rxtx.c            | 5 +++--
 drivers/net/virtio/virtio_rxtx_packed_avx.c | 5 +++--

Please use --cc-cmd devtools/get-maintainer.sh
so the maintainers are aware.

Reviewed-by on v2 2/2
#6 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/virtio: check return from rte_raw_cksum_mbuf

In reply to Chas Williams
Quoted 11 lines · Chas Williams

rte_raw_cksum_mbuf can fail, so we should check to see if it has. If so, return with an error.

Fixes 96cb6711939e ("net/virtio: support Rx checksum offload")
---
 drivers/net/virtio/virtio_rxtx.c            | 5 +++--
 drivers/net/virtio/virtio_rxtx_packed_avx.c | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

Thanks,
Maxime

#7 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/virtio: check return from rte_raw_cksum_mbuf

09/10/2020 09:14, Maxime Coquelin:

Quoted 10 lines · Chas Williams
Quoted 6 lines

rte_raw_cksum_mbuf can fail, so we should check to see if it has. If so, return with an error.

Fixes 96cb6711939e ("net/virtio: support Rx checksum offload")

Series applied, thanks