Skip to contentSkip to conversation

net/mlx5: fix double free in vectorized Rx recovery

PATCH In review Borys Tsyrulnikov 3 participants 3 messages Jun 17 – Jun 24 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mlx5: fix double free in vectorized Rx recovery

During Rx queue error recovery, the vectorized path in mlx5_rx_err_handle() reallocates an mbuf for every queue element. When rte_mbuf_raw_alloc() fails (for example, the mempool is exhausted), the rollback loop frees the mbufs allocated so far, but masks the element ring index with "& elts_n" instead of "& (elts_n - 1)".

elts_n is a power-of-two element count, so "x & elts_n" isolates a single bit and can only evaluate to 0 or elts_n, regardless of the loop counter. The rollback therefore never frees the mbufs just allocated in this pass (they are leaked); instead it repeatedly frees elts[0], a live mbuf still posted to the NIC (use-after-free / double free), and elts[elts_n], the fake_mbuf padding entry used by the vector datapath.

Mask with the existing e_mask (elts_n - 1), as already done in the matching forward allocation loop just above.

Fixes 0f20acbf5eda ("net/mlx5: implement vectorized MPRQ burst")
---
 .mailmap                   | 1 +
 drivers/net/mlx5/mlx5_rx.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
.mailmap +1 -0
index 4001e5fb0e..0b09243c45 100644
@@ -222,6 +222,7 @@ Boleslav Stankevich <[email protected]>
 Boon Ang <[email protected]> <[email protected]>
 Boris Ouretskey <[email protected]>
 Boris Pismenny <[email protected]>
+Borys Tsyrulnikov <[email protected]>
 Brad Larson <[email protected]>
 Brandon Lo <[email protected]>
 Brendan Ryan <[email protected]>
drivers/net/mlx5/mlx5_rx.c +1 -1
index ce50087b70..c0ad8d6701 100644
@@ -662,7 +662,7 @@ mlx5_rx_err_handle(struct mlx5_rxq_data *rxq, uint8_t vec,
 					if (!*elt) {
 						for (i--; i >= 0; --i) {
 							elt_idx = (elts_ci +
-								   i) & elts_n;
+								   i) & e_mask;
 							elt = &(*rxq->elts)
 								[elt_idx];
 							rte_pktmbuf_free_seg
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Borys Tsyrulnikov

During Rx queue error recovery, the vectorized path in mlx5_rx_err_handle() reallocates an mbuf for every queue element. When rte_mbuf_raw_alloc() fails (for example, the mempool is exhausted), the rollback loop frees the mbufs allocated so far, but masks the element ring index with "& elts_n" instead of "& (elts_n - 1)".

elts_n is a power-of-two element count, so "x & elts_n" isolates a single bit and can only evaluate to 0 or elts_n, regardless of the loop counter. The rollback therefore never frees the mbufs just allocated in this pass (they are leaked); instead it repeatedly frees elts[0], a live mbuf still posted to the NIC (use-after-free / double free), and elts[elts_n], the fake_mbuf padding entry used by the vector datapath.

Quoted 7 lines

Mask with the existing e_mask (elts_n - 1), as already done in the matching forward allocation loop just above.

Fixes 0f20acbf5eda ("net/mlx5: implement vectorized MPRQ burst")
#2 lore raw
In reply to Borys Tsyrulnikov

Hi,

Quoted 13 lines · Borys Tsyrulnikov

During Rx queue error recovery, the vectorized path in mlx5_rx_err_handle() reallocates an mbuf for every queue element. When rte_mbuf_raw_alloc() fails (for example, the mempool is exhausted), the rollback loop frees the mbufs allocated so far, but masks the element ring index with "& elts_n" instead of "& (elts_n - 1)".

elts_n is a power-of-two element count, so "x & elts_n" isolates a single bit and can only evaluate to 0 or elts_n, regardless of the loop counter. The rollback therefore never frees the mbufs just allocated in this pass (they are leaked); instead it repeatedly frees elts[0], a live mbuf still posted to the NIC (use-after-free / double free), and elts[elts_n], the fake_mbuf padding entry used by the vector datapath.

Quoted 7 lines

Mask with the existing e_mask (elts_n - 1), as already done in the matching forward allocation loop just above.

Fixes 0f20acbf5eda ("net/mlx5: implement vectorized MPRQ burst")

Patch applied to next-net-mlx,

Kindest regards
Raslan Darawsheh