hv_netvsc: Fix leaking of send/receive buffers after GPADL teardown error
cover hv_netvsc: Fix leaking of send/receive buffers after GPADL teardown error
When a Hyper-V netvsc device is shutdown, the GPADLs for the device's large send and receive buffers are torn down. If the teardown fails, Hyper-V retain access to the buffers and may continue to read or write them. Consequently the buffers should be leaked after such an error.
Leaking the buffers used to work correctly, but was broken by a commit applied in the 4.16 kernel. This patch series restores the correct leaking behavior.
Because the large buffers are allocated with wrapper functions that allow allocations larger than MAX_ORDER_NR_PAGES, leaking the memory is not as simple as setting the memory pointer to NULL so vfree() or kfree() does nothing. Patch 1 introduces a new vmbus_leak_buffer() function that causes vmbus_free_buffer() to properly leak the buffers. Then Patch 2 uses this function in the netvsc driver.
Michael Kelley (2):
Drivers: hv: Add vmbus_leak_buffer()
hv_netvsc: Leak send/recv buffers if GPADL teardown fails
drivers/hv/channel.c | 26 ++++++++++++++++++++++++++ drivers/net/hyperv/netvsc.c | 6 ++++++ include/linux/hyperv.h | 4 ++++ 3 files changed, 36 insertions(+)
Signature · 1 line
2.25.1
PATCH 1/2 Drivers: hv: Add vmbus_leak_buffer()
If an error case needs to leak the buffer memory allocated by vmbus_alloc_buffer(), doing so requires knowledge of how vmbus_free_buffer() works. In a CoCo VM buffers are allocated differently from a normal VM, and vmbus_free_buffer() handles the difference.
Encapsulate this knowledge in a new function, vmbus_leak_buffer(), that error paths can call. After calling vmbus_leak_buffer(), a subsequent call to vmbus_free_buffer() frees the additional resources used in the CoCo VM case but does not free the actual buffer memory. As such, vmbus_leak_buffer() is callable in a context where accesses to the buffer memory may be in flight.
--- drivers/hv/channel.c | 26 ++++++++++++++++++++++++++ include/linux/hyperv.h | 4 ++++ 2 files changed, 30 insertions(+)
drivers/hv/channel.c +26 -0
@@ -648,6 +648,32 @@ void vmbus_free_buffer(void *addr, struct page **chunks, u32 chunk_cnt) }EXPORT_SYMBOL_GPL (vmbus_free_buffer); +/** +* vmbus_leak_buffer - set up a buffer to be leaked by vmbus_free_buffer(). +* +* @addr: buffer address +* @chunks: chunks array from vmbus_alloc_buffer() +* @chunk_cnt: number of entries in @chunks +* +* When @chunks is NULL the buffer is a plain vzalloc() allocation and +* the buffer is leaked by setting @addr to NULL. Otherwise set +* @chunk_cnt to 0 so that vmbus_free_buffer() does not try to re-encrypt +* or free the buffer memory, but still releases the vmap address and +* the chunks memory. +* +* This function may be called in a context where the buffer is still +* being accessed. It must not remove any kernel virtual addresses of +* the buffer or change its encryption status. +*/ +void vmbus_leak_buffer (void **addr,struct page ***chunks,u32 *chunk_cnt) +{ +if (*chunks) + *chunk_cnt =0 ; +else + *addr =NULL ; +} +EXPORT_SYMBOL_GPL (vmbus_leak_buffer); +/** * vmbus_alloc_buffer - allocate a host-visible, virtually-contiguous buffer. *
include/linux/hyperv.h +4 -0
@@ -1220,6 +1220,10 @@ extern void *vmbus_alloc_buffer(struct vmbus_channel *channel,extern void vmbus_free_buffer (void *addr,struct page **chunks,u32 chunk_cnt); +extern void vmbus_leak_buffer (void **addr, +struct page ***chunks, +u32 *chunk_cnt); +void vmbus_reset_channel_cb (struct vmbus_channel *channel); extern int vmbus_recvpacket(struct vmbus_channel *channel,
Signature · 1 line
2.25.1
PATCH 2/2 hv_netvsc: Leak send/recv buffers if GPADL teardown fails
In reply to Michael KelleyIf GPADL teardown fails for the send or receive buffers, the Hyper-V host retains access to the buffers and might continue to access them. Per the code comments, the intent is to be safe by leaking the buffers instead of freeing them.
The intended behavior existed prior to commit 02400fcee254 ("hv_netvsc:
use RCU to fix concurrent rx and queue changes") because freeing the buffers was done in the same function as the GPADL teardown. The "return" statement in the error path effectively skipped freeing the memory. But commit 02400fcee254 moved the freeing to a separate function that is called later. It has no knowledge of the GPADL teardown error, and so frees the memory regardless.
Fix this by calling vmbus_leak_buffer() if the respective GPADL teardown fails. The later call to vmbus_free_buffer() then skips freeing of the actual buffer, including any re-encryption required in a CoCo VM.
--- drivers/net/hyperv/netvsc.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/hyperv/netvsc.c +6 -0
@@ -316,6 +316,9 @@ static void netvsc_teardown_recv_gpadl(struct hv_device *device,* rather than continue and a bugchk */ if (ret !=0 ) { +vmbus_leak_buffer (&net_device->recv_buf, + &net_device->recv_buf_chunks, + &net_device->recv_buf_chunk_cnt);netdev_err (ndev,"unable to teardown receive buffer's gpadl\n" );return ;
@@ -337,6 +340,9 @@ static void netvsc_teardown_send_gpadl(struct hv_device *device,* rather than continue and a bugchk */ if (ret !=0 ) { +vmbus_leak_buffer (&net_device->send_buf, + &net_device->send_buf_chunks, + &net_device->send_buf_chunk_cnt);netdev_err (ndev,"unable to teardown send buffer's gpadl\n" );return ;
Signature · 1 line
2.25.1
[email protected]
AI reviewRe: [PATCH net 2/2] hv_netvsc: Leak send/recv buffers if GPADL teardown fails
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] vmbus_teardown_gpadl silently overwrites its error code on failure, hiding GPADL teardown failures and defeating the memory leak safeguard. --
commit 67f15c255388157724c433ac1cc78395f8454d37
Author: Michael Kelley <[email protected]>
hv_netvsc: Leak send/recv buffers if GPADL teardown fails
This commit updates the receive and send buffer teardown paths to call vmbus_leak_buffer() when GPADL teardown fails. This ensures the buffers are intentionally leaked rather than freed while the Hyper-V host might still retain access to them.
drivers/net/hyperv/netvsc.c static void netvsc_teardown_recv_gpadl(struct hv_device *device, · line 318
Quoted 8 lines
drivers/net/hyperv/netvsc.c +0
-0@@ -316,6 +316,9 @@ static void netvsc_teardown_recv_gpadl(struct hv_device *device,* rather than continue and a bugchk */ if (ret !=0 ) {
[Severity: High]
This is a pre-existing issue, but this intended safeguard appears to be ineffective because vmbus_teardown_gpadl() silently overwrites its error code on failure.
If vmbus_post_msg() fails inside vmbus_teardown_gpadl(), the original error code in ret is unconditionally overwritten with 0 when gpadl->decrypted is false (the standard VM case):
drivers/hv/channel.c:vmbus_teardown_gpadl() {
...
post_msg_err:
if (gpadl->decrypted)
ret = set_memory_encrypted((unsigned long)gpadl->buffer,
PFN_UP(gpadl->size));
else
ret = 0;
...
return ret;
}Because vmbus_teardown_gpadl() returns success despite failing, the if (ret != 0) condition here will not evaluate to true, and the memory leak safeguard will be skipped. Will this allow the buffers to be freed while the Hyper-V host retains access, potentially causing memory corruption or a use-after-free?
Quoted 6 lines
drivers/net/hyperv/netvsc.c +3
-0+vmbus_leak_buffer (&net_device->recv_buf, + &net_device->recv_buf_chunks, + &net_device->recv_buf_chunk_cnt);netdev_err (ndev,"unable to teardown receive buffer's gpadl\n" );return ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
From: Michael Kelley <[email protected]> Sent: Thursday, September 3, 2026 9:07 AM
Quoted 22 lines
When a Hyper-V netvsc device is shutdown, the GPADLs for the device's large send and receive buffers are torn down. If the teardown fails, Hyper-V retain access to the buffers and may continue to read or write them. Consequently the buffers should be leaked after such an error.
Leaking the buffers used to work correctly, but was broken by a commit applied in the 4.16 kernel. This patch series restores the correct leaking behavior.
Because the large buffers are allocated with wrapper functions that allow allocations larger than
MAX_ORDER_NR_PAGES, leaking the memory is not as simple as setting the memory pointer to NULL sovfree()orkfree()does nothing. Patch 1 introduces a newvmbus_leak_buffer()function that causesvmbus_free_buffer()to properly leak the buffers. Then Patch 2 uses this function in the netvsc driver.Michael Kelley (2):
Drivers: hv: Addvmbus_leak_buffer()
hv_netvsc: Leak send/recv buffers if GPADL teardown fails
Quoted 8 lines
drivers/hv/channel.c | 26 ++++++++++++++++++++++++++ drivers/net/hyperv/netvsc.c | 6 ++++++ include/linux/hyperv.h | 4 ++++ 3 files changed, 36 insertions(+)--
2.25.1
I will be posting a v2 of this patch set that fixes an additional bug in vmbus_teardown_gpadl() that Sashiko pointed out. That fix is necessary for the full "leak on failure" scenario to work correctly.
Michael