Skip to contentSkip to conversation

hw/display/virtio-gpu: fix dmabuf_fd leak on remap failure

PATCH In review [email protected] 5 participants 5 messages Jul 13 – Jul 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/display/virtio-gpu: fix dmabuf_fd leak on remap failure

From: Marc-André Lureau <[email protected]>

When virtio_gpu_create_udmabuf() succeeds but virtio_gpu_remap_udmabuf() fails (mmap returns MAP_FAILED), virtio_gpu_init_udmabuf() returns early without closing the dmabuf fd. Since res->blob is never set in this path, later cleanup via virtio_gpu_cleanup_mapping() skips virtio_gpu_fini_udmabuf() entirely, leaking the file descriptor.

Call virtio_gpu_destroy_udmabuf() before the early return to close the fd. This function already handles partial state correctly: it skips the munmap when res->remapped is NULL and closes the fd when res->dmabuf_fd >= 0.

Fixes 9b60cdf98723 ("virtio-gpu: Add udmabuf helpers")
---
 hw/display/virtio-gpu-udmabuf.c | 1 +
 1 file changed, 1 insertion(+)
hw/display/virtio-gpu-udmabuf.c +1 -0
index d5ac1cfca0e..5f08c855dde 100644
@@ -143,6 +143,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
         }
         virtio_gpu_remap_udmabuf(res);
         if (!res->remapped) {
+            virtio_gpu_destroy_udmabuf(res);
             return;
         }
         pdata = res->remapped;
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · On 13/7/26 14:56, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

When virtio_gpu_create_udmabuf() succeeds but virtio_gpu_remap_udmabuf() fails (mmap returns MAP_FAILED), virtio_gpu_init_udmabuf() returns early without closing the dmabuf fd. Since res->blob is never set in this path, later cleanup via virtio_gpu_cleanup_mapping() skips virtio_gpu_fini_udmabuf() entirely, leaking the file descriptor.

Call virtio_gpu_destroy_udmabuf() before the early return to close the fd. This function already handles partial state correctly: it skips the munmap when res->remapped is NULL and closes the fd when res->dmabuf_fd >= 0.

Quoted 5 lines
Fixes 9b60cdf98723 ("virtio-gpu: Add udmabuf helpers")
---
  hw/display/virtio-gpu-udmabuf.c | 1 +
  1 file changed, 1 insertion(+)
Reviewed-by on v1 1/1
#2 lore raw
In reply to [email protected]
Quoted 24 lines · On 7/13/26 15:56, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

When virtio_gpu_create_udmabuf() succeeds but virtio_gpu_remap_udmabuf() fails (mmap returns MAP_FAILED), virtio_gpu_init_udmabuf() returns early without closing the dmabuf fd. Since res->blob is never set in this path, later cleanup via virtio_gpu_cleanup_mapping() skips virtio_gpu_fini_udmabuf() entirely, leaking the file descriptor.

Call virtio_gpu_destroy_udmabuf() before the early return to close the fd. This function already handles partial state correctly: it skips the munmap when res->remapped is NULL and closes the fd when res->dmabuf_fd >= 0.

Fixes 9b60cdf98723 ("virtio-gpu: Add udmabuf helpers")
---
 hw/display/virtio-gpu-udmabuf.c | 1 +
 1 file changed, 1 insertion(+)
hw/display/virtio-gpu-udmabuf.c +0 -0
index d5ac1cfca0e..5f08c855dde 100644

hw/display/virtio-gpu-udmabuf.c void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res) · line 149

Quoted 7 lines
hw/display/virtio-gpu-udmabuf.c +1 -0
@@ -143,6 +143,7 @@ void virtio_gpu_init_udmabuf(struct virtio_gpu_simple_resource *res)
         }
         virtio_gpu_remap_udmabuf(res);
         if (!res->remapped) {
+            virtio_gpu_destroy_udmabuf(res);
             return;
         }
         pdata = res->remapped;
Signature · 2 lines
Best regards,
Dmitry
Reviewed-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 8 lines · On 2026/07/13 21:56, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

When virtio_gpu_create_udmabuf() succeeds but virtio_gpu_remap_udmabuf() fails (mmap returns MAP_FAILED), virtio_gpu_init_udmabuf() returns early without closing the dmabuf fd. Since res->blob is never set in this path, later cleanup via virtio_gpu_cleanup_mapping() skips virtio_gpu_fini_udmabuf() entirely, leaking the file descriptor.

Quoted 7 lines

Call virtio_gpu_destroy_udmabuf() before the early return to close the fd. This function already handles partial state correctly: it skips the munmap when res->remapped is NULL and closes the fd when res->dmabuf_fd >= 0.

Fixes 9b60cdf98723 ("virtio-gpu: Add udmabuf helpers")
#4 lore raw
In reply to [email protected]
Quoted 8 lines · On 7/13/26 15:56, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

When virtio_gpu_create_udmabuf() succeeds but virtio_gpu_remap_udmabuf() fails (mmap returns MAP_FAILED), virtio_gpu_init_udmabuf() returns early without closing the dmabuf fd. Since res->blob is never set in this path, later cleanup via virtio_gpu_cleanup_mapping() skips virtio_gpu_fini_udmabuf() entirely, leaking the file descriptor.

Quoted 7 lines

Call virtio_gpu_destroy_udmabuf() before the early return to close the fd. This function already handles partial state correctly: it skips the munmap when res->remapped is NULL and closes the fd when res->dmabuf_fd >= 0.

Fixes 9b60cdf98723 ("virtio-gpu: Add udmabuf helpers")

This too smells like a qemu-stable material (the issue is minor though). I'm picking it up, please let me know if I should not.

Thanks,
/mjt