Skip to contentSkip to conversation

net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

PATCH Superseded Chenyuan Yang 3 participants 6 messages Jul 23, 2025 – Jul 28, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to otx2_xdp_sq_append_pkt()
2. XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free xdp buffers")
---
 drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +7 -1
index 99ace381cc78..0c4c050b174a 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf,
 		qidx += pfvf->hw.tx_queues;
 		cq->pool_ptrs++;
 		xdpf = xdp_convert_buff_to_frame(&xdp);
+		if (unlikely(!xdpf))
+			return false;
+
 		return otx2_xdp_sq_append_pkt(pfvf, xdpf,
 					      cqe->sg.seg_addr,
 					      cqe->sg.seg_size,
@@ -1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf,
 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
 				    DMA_FROM_DEVICE);
 		xdpf = xdp_convert_buff_to_frame(&xdp);
-		xdp_return_frame(xdpf);
+		if (likely(xdpf))
+			xdp_return_frame(xdpf);
+		else
+			put_page(page);
 		break;
 	default:
 		bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 64 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Wednesday, July 23, 2025 6:03 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya Akula <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Chenyuan Yang <[email protected]>
Subject: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to otx2_xdp_sq_append_pkt() 2. XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free xdp

buffers")
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
index 99ace381cc78..0c4c050b174a 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		qidx += pfvf->hw.tx_queues;
 		cq->pool_ptrs++;
 		xdpf = xdp_convert_buff_to_frame(&xdp);
+		if (unlikely(!xdpf))
+			return false;
+
 		return otx2_xdp_sq_append_pkt(pfvf, xdpf,
 					      cqe->sg.seg_addr,
					      cqe->sg.seg_size,

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c static bool otx2_xdp_rcv_pkt_handler(struct · line 1568

Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +4 -1
@@ -1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
 				    DMA_FROM_DEVICE);
 		xdpf = xdp_convert_buff_to_frame(&xdp);
-		xdp_return_frame(xdpf);
+		if (likely(xdpf))
+			xdp_return_frame(xdpf);
+		else
+			put_page(page);

Thanks for the fix. Given that the page is already freed, returning true in this case makes sense.

Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +0 -0
 		break;
 	default:
		bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);

--
2.34.1

#2 lore raw
Quoted 88 lines

-----Original Message-----
From: Geethasowjanya Akula
Sent: Wednesday, July 23, 2025 8:59 AM
To: Chenyuan Yang <[email protected]>; Sunil Kovvuri Goutham <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 4 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Wednesday, July 23, 2025 6:03 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya

Akula

Quoted 3 lines

<[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected];

[email protected];

Quoted 9 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Chenyuan Yang <[email protected]>
Subject: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 8 lines

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to otx2_xdp_sq_append_pkt()

2.

Quoted 42 lines

XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call
xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free xdp

buffers")
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
index 99ace381cc78..0c4c050b174a 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		qidx += pfvf->hw.tx_queues;
 		cq->pool_ptrs++;
 		xdpf = xdp_convert_buff_to_frame(&xdp);
+		if (unlikely(!xdpf))
+			return false;
+
 		return otx2_xdp_sq_append_pkt(pfvf, xdpf,
 					      cqe->sg.seg_addr,
					      cqe->sg.seg_size,
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +0 -0
@@ -1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
 				    DMA_FROM_DEVICE);
Quoted 8 lines

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, · line 1567

Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +4 -1
 		xdpf = xdp_convert_buff_to_frame(&xdp);
-		xdp_return_frame(xdpf);
+		if (likely(xdpf))
+			xdp_return_frame(xdpf);
+		else
+			put_page(page);

Thanks for the fix. Given that the page is already freed, returning true in this case makes sense.

This change might not be directly related to the current patch, though. You can either include it here or we can submit a follow-up patch to address it.

Quoted 5 lines
Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +0 -0
 		break;
 	default:
		bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);

--
2.34.1

maintainer
#3 lore raw
Quoted 94 lines · Geethasowjanya Akula
Quoted 86 lines

-----Original Message-----
From: Geethasowjanya Akula
Sent: Wednesday, July 23, 2025 8:59 AM
To: Chenyuan Yang <[email protected]>; Sunil Kovvuri Goutham <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 4 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Wednesday, July 23, 2025 6:03 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya

Akula

Quoted 3 lines

<[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected];

[email protected];

Quoted 9 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Chenyuan Yang <[email protected]>
Subject: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 8 lines

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to otx2_xdp_sq_append_pkt()

2.

Quoted 42 lines

XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call
xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free xdp

buffers")
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
index 99ace381cc78..0c4c050b174a 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		qidx += pfvf->hw.tx_queues;
 		cq->pool_ptrs++;
 		xdpf = xdp_convert_buff_to_frame(&xdp);
+		if (unlikely(!xdpf))
+			return false;
+
 		return otx2_xdp_sq_append_pkt(pfvf, xdpf,
 					      cqe->sg.seg_addr,
					      cqe->sg.seg_size,
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +0 -0
@@ -1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 		otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
 				    DMA_FROM_DEVICE);
Quoted 8 lines

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, · line 1567

Quoted 6 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +4 -1
 		xdpf = xdp_convert_buff_to_frame(&xdp);
-		xdp_return_frame(xdpf);
+		if (likely(xdpf))
+			xdp_return_frame(xdpf);
+		else
+			put_page(page);

Thanks for the fix. Given that the page is already freed, returning true in this case makes sense.

Quoted 2 lines

This change might not be directly related to the current patch, though. You can either include it here or we can submit a follow-up patch to address it.

If I read correctly, returning false as the current patch is doing, will make the later code in otx2_rcv_pkt_handler() unconditionally use the just freed page.

I think returning true after put_page() is strictly necessary.

/P

#4 lore raw
Quoted 96 lines · Paolo Abeni
Quoted 94 lines · Geethasowjanya Akula
Quoted 94 lines

-----Original Message-----
From: Geethasowjanya Akula
Sent: Wednesday, July 23, 2025 8:59 AM
To: Chenyuan Yang <[email protected]>; Sunil Kovvuri Goutham <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 4 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Wednesday, July 23, 2025 6:03 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya

Akula

Quoted 3 lines

<[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected];

[email protected];

Quoted 9 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Chenyuan Yang <[email protected]>
Subject: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 8 lines

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom for skb_shared_info.

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to otx2_xdp_sq_append_pkt()

2.

Quoted 39 lines

XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call
xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free xdp

buffers")
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8 +++++++-

1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
index 99ace381cc78..0c4c050b174a 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
            qidx += pfvf->hw.tx_queues;
            cq->pool_ptrs++;
            xdpf = xdp_convert_buff_to_frame(&xdp);
+           if (unlikely(!xdpf))
+                   return false;
+
            return otx2_xdp_sq_append_pkt(pfvf, xdpf,
                                          cqe->sg.seg_addr,
                                          cqe->sg.seg_size,

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c static bool otx2_xdp_rcv_pkt_handler(struct · line 1568

Quoted 9 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +4 -1
@@ -1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
            otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
                                DMA_FROM_DEVICE);
            xdpf = xdp_convert_buff_to_frame(&xdp);
-           xdp_return_frame(xdpf);
+           if (likely(xdpf))
+                   xdp_return_frame(xdpf);
+           else
+                   put_page(page);

Thanks for the fix. Given that the page is already freed, returning true in this case makes sense.

Quoted 8 lines
Quoted 2 lines

This change might not be directly related to the current patch, though. You can either include it here or we can submit a follow-up patch to address it.

If I read correctly, returning false as the current patch is doing, will make the later code in otx2_rcv_pkt_handler() unconditionally use the just freed page.

I think returning true after put_page() is strictly necessary.

Thanks for the review and for catching that issue. You're right, returning false would cause a use-after-free, as the caller would proceed to use the already freed page.

I've updated the patch to return true in the XDP_TX failure case. I also adjusted the XDP_REDIRECT error path to do the same after calling put_page(), preventing a fall-through.

Does the updated patch below look correct? If so, I'll send out a formal v2.

---
 drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c index 99ace381cc78..4e1b9a3f6e51 100644

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +0 -0
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct

otx2_nic *pfvf,
qidx += pfvf->hw.tx_queues;
cq->pool_ptrs++;
xdpf = xdp_convert_buff_to_frame(&xdp);
+ if (unlikely(!xdpf))
+ return true;
+
return otx2_xdp_sq_append_pkt(pfvf, xdpf,

        cqe->sg.seg_addr,
        cqe->sg.seg_size,
@@ -1558,7 +1561,12 @@ static bool otx2_xdp_rcv_pkt_handler(struct
otx2_nic *pfvf,
  otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
      DMA_FROM_DEVICE);

xdpf = xdp_convert_buff_to_frame(&xdp);
- xdp_return_frame(xdpf);
+ if (likely(xdpf)) {
+ xdp_return_frame(xdpf);
+ } else {
+ put_page(page);
+ return true;
+ }
break;
default:
bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);
---

Quoted 2 lines

/P

#5 lore raw
Quoted 129 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Sunday, July 27, 2025 1:51 AM
To: Paolo Abeni <[email protected]>
Cc: Geethasowjanya Akula <[email protected]>; Sunil Kovvuri Goutham <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 37 lines · Paolo Abeni
Quoted 35 lines · Geethasowjanya Akula
Quoted 35 lines

-----Original Message-----
From: Geethasowjanya Akula
Sent: Wednesday, July 23, 2025 8:59 AM
To: Chenyuan Yang <[email protected]>; Sunil Kovvuri Goutham <[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 4 lines

-----Original Message-----
From: Chenyuan Yang <[email protected]>
Sent: Wednesday, July 23, 2025 6:03 AM
To: Sunil Kovvuri Goutham <[email protected]>; Geethasowjanya

Akula

Quoted 3 lines

<[email protected]>; Subbaraya Sundeep Bhatta <[email protected]>; Hariprasad Kelam <[email protected]>; Bharat Bhushan <[email protected]>; [email protected];

[email protected];

Quoted 9 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Suman Ghosh <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; Chenyuan Yang <[email protected]>
Subject: [EXTERNAL] [PATCH] net: otx2: handle NULL returned by xdp_convert_buff_to_frame()

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

The xdp_convert_buff_to_frame() function can return NULL when there is insufficient headroom in the buffer to store the xdp_frame structure or when the driver didn't reserve enough tailroom

for skb_shared_info.

Quoted 61 lines
Quoted 61 lines
Quoted 61 lines
Quoted 6 lines

Currently, the otx2 driver does not check for this NULL return value in two critical paths within otx2_xdp_rcv_pkt_handler():

1. XDP_TX case: Passes potentially NULL xdpf to
otx2_xdp_sq_append_pkt()

2.

Quoted 47 lines

XDP_REDIRECT error path: Calls xdp_return_frame() with potentially NULL

This can lead to kernel crashes due to NULL pointer dereference.

Fix by adding proper NULL checks in both paths. For XDP_TX, return false to indicate packet should be dropped. For XDP_REDIRECT error path, only call
xdp_return_frame() if conversion succeeded, otherwise manually free the page.

Please correct me if any error path is incorrect.

This is similar to the commit cc3628dcd851
("xen-netfront: handle NULL returned by xdp_convert_buff_to_frame()").

Fixes 94c80f748873 ("octeontx2-pf: use xdp_return_frame() to free

xdp
buffers")
---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 8
+++++++-

1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c index 99ace381cc78..0c4c050b174a 100644

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
            qidx += pfvf->hw.tx_queues;
            cq->pool_ptrs++;
            xdpf = xdp_convert_buff_to_frame(&xdp);
+           if (unlikely(!xdpf))
+                   return false;
+
            return otx2_xdp_sq_append_pkt(pfvf, xdpf,
                                          cqe->sg.seg_addr,
                                          cqe->sg.seg_size, @@
-1558,7 +1561,10 @@ static bool otx2_xdp_rcv_pkt_handler(struct
otx2_nic *pfvf,
            otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
                                DMA_FROM_DEVICE);
            xdpf = xdp_convert_buff_to_frame(&xdp);

drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c

Quoted 5 lines
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +4 -1
-           xdp_return_frame(xdpf);
+           if (likely(xdpf))
+                   xdp_return_frame(xdpf);
+           else
+                   put_page(page);

Thanks for the fix. Given that the page is already freed, returning true in this case makes sense.

Quoted 2 lines
Quoted 2 lines

This change might not be directly related to the current patch, though. You can either include it here or we can submit a follow-up patch

to address it.

Quoted 5 lines

If I read correctly, returning false as the current patch is doing, will make the later code in otx2_rcv_pkt_handler() unconditionally use the just freed page.

Quoted 7 lines
Quoted 1 line

I think returning true after put_page() is strictly necessary.

Thanks for the review and for catching that issue. You're right, returning false would cause a use-after-free, as the caller would proceed to use the already freed page.

I've updated the patch to return true in the XDP_TX failure case. I also adjusted

Returning true on XDP_TX failure requires the page to be freed. Otherwise, it should return false so the packet can be handled by otx2_rcv_pkt_handler.

Quoted 45 lines

the XDP_REDIRECT error path to do the same after calling put_page(), preventing a fall-through.

Does the updated patch below look correct? If so, I'll send out a formal v2.

---
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c | 10 +++++++++-

1 file changed, 9 insertions(+), 1 deletion(-)
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +3 -0
index 99ace381cc78..4e1b9a3f6e51 100644
@@ -1534,6 +1534,9 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 qidx += pfvf->hw.tx_queues;
 cq->pool_ptrs++;
 xdpf = xdp_convert_buff_to_frame(&xdp);
+ if (unlikely(!xdpf))
+ return true;
+
 return otx2_xdp_sq_append_pkt(pfvf, xdpf,
       cqe->sg.seg_addr,
       cqe->sg.seg_size,
drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +6 -1
@@ -1558,7 +1561,12 @@ static bool otx2_xdp_rcv_pkt_handler(struct
 otx2_nic *pfvf,
 otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize,
     DMA_FROM_DEVICE);
 xdpf = xdp_convert_buff_to_frame(&xdp);
- xdp_return_frame(xdpf);
+ if (likely(xdpf)) {
+ xdp_return_frame(xdpf);
+ } else {
+ put_page(page);
+ return true;
+ }
 break;
 default:

bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act);
---

Quoted 2 lines

/P