dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/virtio: fix memory leak of fence event on execbuffer failure
@ 2026-09-08 12:13 Peiyang He
  2026-09-08 12:34 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Peiyang He @ 2026-09-08 12:13 UTC (permalink / raw)
  To: airlied, kraxel, dmitry.osipenko, dri-devel
  Cc: linux-kernel, stable, virtualization, gurchetansingh, Peiyang He

virtio_gpu_execbuffer_ioctl() reserves a DRM event with
drm_event_reserve_init() when VIRTGPU_EXECBUF_RING_IDX selects a ring that
userspace has enabled polling for. virtio_gpu_init_submit() does this
before the BO handles, the command buffer, the syncobj arrays and the
in-fence are processed, so every later error path runs with the event
already pending, including plain argument validation failures such as an
invalid bo_handle or an in-syncobj that carries no fence.

On those paths, virtio_gpu_cleanup_submit() drops the out-fence without
cancelling the event. The fence is freed without ever having been emitted,
taking the only driver-side pointer to the event with it. Closing the DRM
file does not help. drm_events_release() unlinks pending events but
deliberately leaves the freeing to the driver's later drm_send_event(),
which never runs for an orphaned event, so the allocation is leaked
permanently.

Found when fuzzing the virtio driver with Syzkaller:

	BUG: memory leak
	unreferenced object 0xffff88802c176e80 (size 96):
	comm "syz.1.367", pid 10561, jiffies 4294960122
	hex dump (first 32 bytes):
		00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
		c8 6e 17 2c 80 88 ff ff 00 00 00 00 00 00 00 00  .n.,............
	backtrace (crc e1973c6b):
		kmemleak_alloc_recursive include/linux/kmemleak.h:44 [inline]
		slab_post_alloc_hook mm/slub.c:4597 [inline]
		slab_alloc_node mm/slub.c:4917 [inline]
		__kmalloc_cache_noprof+0x49d/0x6f0 mm/slub.c:5485
		_kmalloc_noprof include/linux/slab.h:988 [inline]
		_kzalloc_noprof include/linux/slab.h:1309 [inline]
		virtio_gpu_fence_event_create drivers/gpu/drm/virtio/virtgpu_submit.c:282 [inline]
		virtio_gpu_init_submit drivers/gpu/drm/virtio/virtgpu_submit.c:398 [inline]
		virtio_gpu_execbuffer_ioctl+0xbbf/0x1aa0 drivers/gpu/drm/virtio/virtgpu_submit.c:505
		drm_ioctl_kernel+0x1f4/0x3e0 drivers/gpu/drm/drm_ioctl.c:817
		drm_ioctl+0x5f4/0xc70 drivers/gpu/drm/drm_ioctl.c:914
		vfs_ioctl fs/ioctl.c:51 [inline]
		__do_sys_ioctl fs/ioctl.c:597 [inline]
		__se_sys_ioctl fs/ioctl.c:583 [inline]
		__x64_sys_ioctl+0x18e/0x210 fs/ioctl.c:583
		do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
		do_syscall_64+0x116/0x800 arch/x86/entry/syscall_64.c:94
		entry_SYSCALL_64_after_hwframe+0x77/0x7f

Fix by cancelling and freeing the DRM event on the execbuffer error path
before dropping the fence. Clear the fence's event pointer after
cancellation so it does not retain a dangling pointer.

Fixes: cd7f5ca33585 ("drm/virtio: implement context init: add virtio_gpu_fence_event")
Cc: stable@vger.kernel.org
Signed-off-by: Peiyang He <peiyang_he@smail.nju.edu.cn>
Assisted-by: Codex:gpt-5.6-luna
---
 drivers/gpu/drm/virtio/virtgpu_submit.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c
index 32cb1e4aa425..734b1e976a75 100644
--- a/drivers/gpu/drm/virtio/virtgpu_submit.c
+++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
@@ -538,6 +538,10 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
 	virtio_gpu_process_post_deps(&submit);
 	virtio_gpu_complete_submit(&submit);
 cleanup:
+	if (ret && submit.out_fence && submit.out_fence->e) {
+		drm_event_cancel_free(dev, &submit.out_fence->e->base);
+		submit.out_fence->e = NULL;
+	}
 	virtio_gpu_cleanup_submit(&submit);
 
 	return ret;
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/virtio: fix memory leak of fence event on execbuffer failure
  2026-09-08 12:13 [PATCH] drm/virtio: fix memory leak of fence event on execbuffer failure Peiyang He
@ 2026-09-08 12:34 ` sashiko-bot
  2026-09-09  3:42   ` Peiyang He
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-09-08 12:34 UTC (permalink / raw)
  To: Peiyang He; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The return value of virtio_gpu_fence_alloc() is not checked for NULL, leading to a NULL pointer dereference on memory allocation failure.
- [High] Silent command submission failures ignore error returns, bypassing error cleanup and leaking memory, GEM object references, and the fence event.
--

commit 93a0baf42622f4c32d5d66c718e042ee250da557
Author: Peiyang He <peiyang_he@smail.nju.edu.cn>
drm/virtio: fix memory leak of fence event on execbuffer failure

This patch fixes a memory leak in the virtio GPU driver by cancelling
and freeing the DRM event on the execbuffer error path before dropping
the out-fence. It ensures the fence's event pointer is cleared after
cancellation to prevent dangling pointers.

> diff --git a/drivers/gpu/drm/virtio/virtgpu_submit.c b/drivers/gpu/drm/virtio/virtgpu_submit.c
> index 32cb1e4aa4258..734b1e976a75c 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_submit.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_submit.c
> @@ -538,6 +538,10 @@ int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
>  	virtio_gpu_process_post_deps(&submit);
>  	virtio_gpu_complete_submit(&submit);

[Severity: High]
This isn't a bug introduced by this patch, but does this code silently absorb
submission errors and leak resources?

In the current code just above this hunk, we have:

    virtio_gpu_submit(&submit);
    ...
    virtio_gpu_complete_submit(&submit);
cleanup:

If the underlying queue functions fail (e.g., vmalloc_to_sgt() returning
-ENOMEM in virtio_gpu_queue_fenced_ctrl_buffer()), they return an error code:

drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_queue_fenced_ctrl_buffer() {
    ...
        sgt = vmalloc_to_sgt(vbuf->data_buf, vbuf->data_size, &sg_ents);
        if (!sgt) {
            if (fence && vbuf->objs)
                virtio_gpu_array_unlock_resv(vbuf->objs);
            return -ENOMEM;
        }
    ...
}

However, the wrapper functions like virtio_gpu_cmd_submit() return void,
dropping this error:

drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_cmd_submit() {
    ...
    virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
}

Because the error is dropped, virtio_gpu_execbuffer_ioctl() proceeds as if
successful, calls virtio_gpu_complete_submit() to clear pointers, and
skips the error cleanup path.

Does this permanently leak GEM object references, memory buffers, and the
fence event when command submission fails under memory pressure?


[Severity: High]
This is another pre-existing issue, but is it possible for
virtio_gpu_fence_alloc() to fail and cause a NULL pointer dereference
earlier in the submission path?

Looking at virtio_gpu_init_submit() called earlier in this ioctl, it does
not check the return value of virtio_gpu_fence_alloc():

drivers/gpu/drm/virtio/virtgpu_submit.c:virtio_gpu_init_submit() {
    ...
        out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);
    else
        out_fence = NULL;

    if (drm_fence_event) {
        err = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
    ...
}

And in virtio_gpu_fence_event_create():

drivers/gpu/drm/virtio/virtgpu_submit.c:virtio_gpu_fence_event_create() {
    ...
    fence->e = e;
    ...
}

If kzalloc_obj() inside virtio_gpu_fence_alloc() fails due to memory pressure,
it returns NULL, which is then unconditionally dereferenced.

Can unprivileged userspace trigger this by invoking the execbuffer ioctl
under constrained memory conditions?

>  cleanup:
> +	if (ret && submit.out_fence && submit.out_fence->e) {
> +		drm_event_cancel_free(dev, &submit.out_fence->e->base);
> +		submit.out_fence->e = NULL;
> +	}
>  	virtio_gpu_cleanup_submit(&submit);
>  
>  	return ret;

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/D320EAB5680C1411+20260908121323.2405044-1-peiyang_he@smail.nju.edu.cn?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/virtio: fix memory leak of fence event on execbuffer failure
  2026-09-08 12:34 ` sashiko-bot
@ 2026-09-09  3:42   ` Peiyang He
  0 siblings, 0 replies; 3+ messages in thread
From: Peiyang He @ 2026-09-09  3:42 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel

On 2026/9/8 20:34, sashiko-bot@kernel.org wrote:
> [Severity: High]
> This is another pre-existing issue, but is it possible for
> virtio_gpu_fence_alloc() to fail and cause a NULL pointer dereference
> earlier in the submission path?
> 
> Looking at virtio_gpu_init_submit() called earlier in this ioctl, it does
> not check the return value of virtio_gpu_fence_alloc():
> 
> drivers/gpu/drm/virtio/virtgpu_submit.c:virtio_gpu_init_submit() {
>     ...
>         out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);
>     else
>         out_fence = NULL;
> 
>     if (drm_fence_event) {
>         err = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
>     ...
> }
> 
> And in virtio_gpu_fence_event_create():
> 
> drivers/gpu/drm/virtio/virtgpu_submit.c:virtio_gpu_fence_event_create() {
>     ...
>     fence->e = e;
>     ...
> }
> 
> If kzalloc_obj() inside virtio_gpu_fence_alloc() fails due to memory pressure,
> it returns NULL, which is then unconditionally dereferenced.
> 
> Can unprivileged userspace trigger this by invoking the execbuffer ioctl
> under constrained memory conditions?
> 

Our fuzzing results confirm this NULL pointer deref path is valid.
Will send a patch soon.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-09-09  6:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-08 12:13 [PATCH] drm/virtio: fix memory leak of fence event on execbuffer failure Peiyang He
2026-09-08 12:34 ` sashiko-bot
2026-09-09  3:42   ` Peiyang He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox