Skip to contentSkip to conversation

net/netvsc: fix event buffer leak on realloc failure

PATCH Superseded Chen Zhao 2 participants 6 messages Oct 25 – Oct 31 Network drivers

lore
Share & tools
lore

Chen Zhao

author
#0 lore raw

PATCH 1/1 net/netvsc: fix event buffer leak on realloc failure

rte_realloc() failure overwrites event_buf and leaks the old allocation. Save the old pointer and free it on failure before resetting event_sz.

Fixes 1f2766b7ee02 ("net/netvsc: resize event buffer as needed")
---
 .mailmap                     | 1 +
 drivers/net/netvsc/hn_rxtx.c | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
.mailmap +1 -0
index 6d9e262cfb..deae6514dd 100644
@@ -267,6 +267,7 @@ Charles Myers <[email protected]>
 Charles Stoll <[email protected]>
 Chas Williams <[email protected]> <[email protected]>
 Chas Williams <[email protected]> <[email protected]>
+Chen Zhao <[email protected]>
 Chenbo Xia <[email protected]> <[email protected]>
 Cheng Jiang <[email protected]> <[email protected]>
 Cheng Liu <[email protected]>
drivers/net/netvsc/hn_rxtx.c +6 -2
index 6f536610f2..4db6c933c9 100644
@@ -1131,15 +1131,19 @@ uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
 
 		if (unlikely(ret == -ENOBUFS)) {
 			/* event buffer not large enough to read ring */
+			void *old_buf = rxq->event_buf;
 
 			PMD_DRV_LOG(DEBUG,
 				    "event buffer expansion (need %u)", len);
 			rxq->event_sz = len + len / 4;
-			rxq->event_buf = rte_realloc(rxq->event_buf, rxq->event_sz,
+			rxq->event_buf = rte_realloc(old_buf, rxq->event_sz,
 						     RTE_CACHE_LINE_SIZE);
 			if (rxq->event_buf)
 				goto retry;
-			/* out of memory, no more events now */
+			/*
+			 * out of memory, free the old buffer, no more events now
+			 */
+			rte_free(old_buf);
 			rxq->event_sz = 0;
 			break;
 		}
Signature · 1 line
2.43.0
author
#1 lore raw

Hi,

The AI review reports a double-free and a dangling event_buf:
https://mails.dpdk.org/archives/test-report/2026-October/1069492.html

Both findings are false positives:

1. On successful reallocation, goto retry skips rte_free(old_buf). The retry label is outside the ENOBUFS block. Each subsequent entry into that block reinitializes old_buf from the current event_buf, so a later allocation failure frees the current allocation, not an address retained from a previous successful reallocation.

2. On failure, rte_realloc() returns NULL without freeing the old buffer. That NULL return value has already been assigned to rxq->event_buf before rte_free(old_buf) is reached. Thus event_buf is already NULL; no additional NULL assignment is needed.

The patch preserves the existing reset-and-retry behavior while freeing the old allocation on failure.

Thanks,
Chen

#2 lore raw
Quoted 18 lines · On Sun, 25 Oct 2026 02:18:02 +0000 Chen Zhao <[email protected]> wrote:

Hi,

The AI review reports a double-free and a dangling event_buf:
https://mails.dpdk.org/archives/test-report/2026-October/1069492.html

Both findings are false positives:

1. On successful reallocation, goto retry skips rte_free(old_buf). The retry label is outside the ENOBUFS block. Each subsequent entry into that block reinitializes old_buf from the current event_buf, so a later allocation failure frees the current allocation, not an address retained from a previous successful reallocation.

2. On failure, rte_realloc() returns NULL without freeing the old buffer. That NULL return value has already been assigned to rxq->event_buf before rte_free(old_buf) is reached. Thus event_buf is already NULL; no additional NULL assignment is needed.

Quoted 5 lines

The patch preserves the existing reset-and-retry behavior while freeing the old allocation on failure.

Thanks,
Chen

The CI AI model is still using to weak a model and I never believe anything it says

#3 lore raw
In reply to Chen Zhao
Quoted 8 lines · On Sun, 25 Oct 2026 01:46:33 +0000 Chen Zhao <[email protected]> wrote:

rte_realloc() failure overwrites event_buf and leaks the old allocation. Save the old pointer and free it on failure before resetting event_sz.

Fixes 1f2766b7ee02 ("net/netvsc: resize event buffer as needed")

---

AI review says "good but it could be better"

Quoted 2 lines · On Sun, 25 Oct 2026 01:46:33 +0000 Chen Zhao <[email protected]> wrote:

rte_realloc() failure overwrites event_buf and leaks the old allocation. Save the old pointer and free it on failure before resetting event_sz.

The leak is real, but freeing the old buffer is the wrong recovery. The old buffer is still valid and usable for smaller events; there is no reason to throw it away. Also event_sz is updated before the allocation succeeds.

Use a temporary and only commit on success:

		if (unlikely(ret == -ENOBUFS)) {
			/* event buffer not large enough to read ring */
			uint32_t new_sz = len + len / 4;
			void *new_buf;

			PMD_DRV_LOG(DEBUG,
				    "event buffer expansion (need %u)", len);
			new_buf = rte_realloc(rxq->event_buf, new_sz,
					      RTE_CACHE_LINE_SIZE);
			if (new_buf == NULL)
				break;	/* out of memory, no more events now */

			rxq->event_buf = new_buf;
			rxq->event_sz = new_sz;
			goto retry;
		}

Drop the Cc to [email protected], that address is dead.

author
#4 lore raw

PATCH v2 1/1 net/netvsc: fix event buffer leak on realloc failure

In reply to Chen Zhao

rte_realloc() failure overwrites event_buf and leaks the old allocation. Use a temporary pointer and update event_buf and event_sz only on success, preserving the existing buffer and size on failure.

Fixes 1f2766b7ee02 ("net/netvsc: resize event buffer as needed")

---
v2:
- Preserve the existing buffer and size on allocation failure.
- Update the buffer pointer and size only on success.
- Remove the obsolete Microsoft email address.

 .mailmap                     |  1 +
 drivers/net/netvsc/hn_rxtx.c | 18 ++++++++++--------
 2 files changed, 11 insertions(+), 8 deletions(-)
.mailmap +1 -0
index 6d9e262cfb..deae6514dd 100644
@@ -267,6 +267,7 @@ Charles Myers <[email protected]>
 Charles Stoll <[email protected]>
 Chas Williams <[email protected]> <[email protected]>
 Chas Williams <[email protected]> <[email protected]>
+Chen Zhao <[email protected]>
 Chenbo Xia <[email protected]> <[email protected]>
 Cheng Jiang <[email protected]> <[email protected]>
 Cheng Liu <[email protected]>
drivers/net/netvsc/hn_rxtx.c +10 -8
index 6f536610f2..f38fc33f7c 100644
@@ -1131,17 +1131,19 @@ uint32_t hn_process_events(struct hn_data *hv, uint16_t queue_id,
 
 		if (unlikely(ret == -ENOBUFS)) {
 			/* event buffer not large enough to read ring */
+			uint32_t new_sz = len + len / 4;
+			void *new_buf;
 
 			PMD_DRV_LOG(DEBUG,
 				    "event buffer expansion (need %u)", len);
-			rxq->event_sz = len + len / 4;
-			rxq->event_buf = rte_realloc(rxq->event_buf, rxq->event_sz,
-						     RTE_CACHE_LINE_SIZE);
-			if (rxq->event_buf)
-				goto retry;
-			/* out of memory, no more events now */
-			rxq->event_sz = 0;
-			break;
+			new_buf = rte_realloc(rxq->event_buf, new_sz,
+					      RTE_CACHE_LINE_SIZE);
+			if (new_buf == NULL)
+				break;	/* out of memory, no more events now */
+
+			rxq->event_buf = new_buf;
+			rxq->event_sz = new_sz;
+			goto retry;
 		}
 
 		if (unlikely(ret <= 0)) {
Signature · 1 line
2.43.0
#5 lore raw
Quoted 8 lines · On Thu, 29 Oct 2026 16:54:52 +0000 Chen Zhao <[email protected]> wrote:

rte_realloc() failure overwrites event_buf and leaks the old allocation. Use a temporary pointer and update event_buf and event_sz only on success, preserving the existing buffer and size on failure.

Fixes 1f2766b7ee02 ("net/netvsc: resize event buffer as needed")

---

Updated the version in next-net.

Note, this is a real corner case. If rte_realloc() failed the application is going to be failing anyway. It means malloc pool (ie hugepages) exhausted.