drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
Subsystems (3)
PATCH v2 1/1 drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
vmw_dumb_create() hands the surface's TTM base reference to vbo->dumb_surface by clearing refcount_release, but the surface handle is already visible to userspace. A concurrent DRM_IOCTL_VMW_UNREF_SURFACE can release the TTM base first, and its destructor drops the reference the vbo was meant to inherit:
CPU0 (MODE_CREATE_DUMB) CPU1 (VMW_UNREF_SURFACE)
vmw_dumb_create()
vmw_gb_surface_define_ext_ioctl() vmw_user_resource_lookup_handle()
vmw_user_surface_base_release()
vmw_resource_unreference()
refcount_release = NULL
vmw_resource_unreference(&res)
vmw_resource_release()
vmw_user_bo_unref() /* drops the GEM handle's ref, vbo freed */
The dumb buffer's GEM handle is left pointing at the freed vbo, and DRM_IOCTL_GEM_CLOSE on it is a use-after-free.
Take a counted reference for vbo->dumb_surface and leave the base destructor armed, so the base and the vbo each drop only their own reference. Drop the WARN_ON() in vmw_user_surface_base_release(), which no longer holds.
---
v2:
- Rework: take a counted surface reference for vbo->dumb_surface instead of only reordering the GEM put.
- The GEM put is still the last access to the vbo in vmw_dumb_create(), so the v1 GEM_CLOSE race stays fixed. The ttm_base_object use-after-free Sashiko reported on the GEM_CLOSE path is also closed, since the base now keeps its own surface reference:
https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]
I plan to send a separate IGT test later for the unref and GEM_CLOSE races, thanks for review and suggestion!
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +2 -15
@@ -666,13 +666,6 @@ static void vmw_user_surface_base_release(struct ttm_base_object **p_base)struct vmw_resource *res = &user_srf->srf.res; *p_base =NULL ; - -/* -* Dumb buffers own the resource and they'll unref the -* resource themselves -*/ -WARN_ON (res && res->guest_memory_bo && res->guest_memory_bo->is_dumb); -vmw_resource_unreference (&res); }
@@ -2331,15 +2324,9 @@ int vmw_dumb_create(struct drm_file *file_priv, vbo = res->guest_memory_bo; vbo->is_dumb = true; - vbo->dumb_surface =vmw_res_to_srf (res); +/* Owned by the GEM handle, dropped in vmw_bo_free() */ + vbo->dumb_surface =vmw_surface_reference (vmw_res_to_srf (res));drm_gem_object_put (&vbo->tbo.base); -/* -* Unset the user surface dtor since this in not actually exposed -* to userspace. The suface is owned via the dumb_buffer's GEM handle -*/ -struct vmw_user_surface *usurf =container_of (vbo->dumb_surface, - struct vmw_user_surface, srf); - usurf->prime.base.refcount_release =NULL ; err:if (res)vmw_resource_unreference (&res);
base-commit: 07940daf7b231273866376cc9657c09e47e7cef6
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/vmwgfx: race before lookup in vmw_dumb_create() leaks BO and causes UAF - [High] drm/vmwgfx…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] drm/vmwgfx: race before lookup in vmw_dumb_create() leaks BO and causes UAF
- [High] drm/vmwgfx: uncounted guest_memory_bo in vmw_dumb_create() allows BO UAF
- [Medium] drm/vmwgfx: race on vbo->is_dumb init and early handle in vmw_dumb_create()
--
commit 54040d43ddc198064c68982ed3a1423e38eed16b
Author: Jayapal Ashwin Nair <[email protected]>
drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
This commit takes a counted surface reference for vbo->dumb_surface in vmw_dumb_create() and leaves the surface TTM base object destructor set so each owner drops its own reference.
Quoted 4 lines
drivers/gpu/drm/vmwgfx_surface.c renamed from drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-0
[ ... ]
drivers/gpu/drm/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2328
Quoted 7 lines
drivers/gpu/drm/vmwgfx_surface.c renamed from drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +2
-1@@ -2331,15 +2324,9 @@ int vmw_dumb_create(struct drm_file *file_priv, vbo = res->guest_memory_bo; vbo->is_dumb = true; - vbo->dumb_surface =vmw_res_to_srf (res); +/* Owned by the GEM handle, dropped in vmw_bo_free() */ + vbo->dumb_surface =vmw_surface_reference (vmw_res_to_srf (res));
[Severity: Medium]
Can concurrent ioctls observe vbo before vbo->is_dumb and vbo->dumb_surface are initialized?
vmw_gb_surface_define_ext_ioctl() publishes both arg.rep.buffer_handle and arg.rep.handle before returning to vmw_dumb_create(). A concurrent DRM_IOCTL_MODE_ADDFB2 on arg.rep.buffer_handle can enter vmw_user_object_lookup() while vbo->is_dumb is still false (converting the lookup to a surface and dropping uo->buffer) or after vbo->is_dumb is set to true while vbo->dumb_surface is still NULL.
Similarly, if another thread retains arg.rep.handle via DRM_IOCTL_VMW_GB_SURFACE_REF_EXT and the GEM handle is later closed while vbo is busy, ttm_bo_release() queues vbo for delayed deletion with refcount 0 before vmw_bo_free() clears res->guest_memory_bo. A concurrent vmw_gb_surface_reference_internal() can then pass &srf->res.guest_memory_bo->tbo.base to drm_gem_handle_create() after its refcount has reached 0.
drivers/gpu/drm/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2329
Quoted 1 line
drivers/gpu/drm/vmwgfx_surface.c renamed from drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-0drm_gem_object_put (&vbo->tbo.base);
[Severity: High]
Does dropping res->guest_memory_bo's GEM reference here allow a use-after-free on vbo if userspace retains the surface handle and switches its backup buffer?
Because arg.rep.handle is published as a shareable surface before ttm_ref_object_base_unref() removes it below, another thread can retain the surface handle and submit SVGA_3D_CMD_BIND_GB_SURFACE:
CPU0 CPU1vmw_dumb_create()
vmw_gb_surface_define_ext_ioctl()
vmw_surface_handle_reference()
ttm_ref_object_add(tfile, ...)
drm_gem_object_put(&vbo->tbo.base)
...
ttm_ref_object_base_unref(...)
vmw_execbuf_process()
vmw_cmd_bind_gb_surface()
...
vmw_resource_unreserve()
vmw_user_bo_unref(
&res->guest_memory_bo)
<-- drops GEM handle's ref,
freeing vbores->guest_memory_bo still points to vbo without a counted reference after drm_gem_object_put(), so switching the surface backup drops the last reference belonging to args->handle while the GEM handle remains installed in file_priv->object_idr.
drivers/gpu/drm/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2332
Quoted 10 lines
drivers/gpu/drm/vmwgfx_surface.c renamed from drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-7-/* -* Unset the user surface dtor since this in not actually exposed -* to userspace. The suface is owned via the dumb_buffer's GEM handle -*/ -struct vmw_user_surface *usurf =container_of (vbo->dumb_surface, - struct vmw_user_surface, srf); - usurf->prime.base.refcount_release =NULL ; err:if (res)vmw_resource_unreference (&res);
[Severity: High]
What happens if DRM_IOCTL_VMW_UNREF_SURFACE destroys arg.rep.handle before vmw_user_resource_lookup_handle() runs in vmw_dumb_create()?
CPU0 CPU1vmw_dumb_create()
vmw_gb_surface_define_ext_ioctl()
...
vmw_bo_add_detached_resource(vbo, res)
ttm_prime_object_init(...)
vmw_resource_unreference(&res)
ttm_ref_object_base_unref()
vmw_user_surface_base_release()
vmw_resource_unreference(&res)
vmw_resource_release()
vmw_user_bo_unref(...)
vmw_user_surface_free()
kfree(user_srf)
vmw_user_resource_lookup_handle()
/* returns -EINVAL, jumps to err */
vmw_kms_fb_create()
...
vmw_bo_surface(vbo)
res->func->res_type
<-- use-after-freeWhen vmw_user_resource_lookup_handle() fails, the err path does not call drm_gem_handle_delete(file_priv, arg.rep.buffer_handle). That leaks arg.rep.buffer_handle in file_priv->object_idr with vbo->is_dumb still false and a dangling res pointer in vbo->detached_resources, which vmw_bo_surface() dereferences when the leaked GEM handle is later passed to DRM_IOCTL_MODE_ADDFB2 or DRM_IOCTL_PRIME_HANDLE_TO_FD.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 98 lines · On 20-11-2026 00:56, [email protected] wrote:
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2328
Quoted 8 lines
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +2
-1@@ -2331,15 +2324,9 @@ int vmw_dumb_create(struct drm_file *file_priv, vbo = res->guest_memory_bo; vbo->is_dumb = true; - vbo->dumb_surface =vmw_res_to_srf (res); +/* Owned by the GEM handle, dropped in vmw_bo_free() */ + vbo->dumb_surface =vmw_surface_reference (vmw_res_to_srf (res));[Severity: Medium]
Can concurrent ioctls observe vbo beforevbo->is_dumbandvbo->dumb_surfaceare initialized?
vmw_gb_surface_define_ext_ioctl()publishes both arg.rep.buffer_handle and arg.rep.handle before returning tovmw_dumb_create(). A concurrentDRM_IOCTL_MODE_ADDFB2on arg.rep.buffer_handle can entervmw_user_object_lookup()whilevbo->is_dumbis still false (converting the lookup to a surface and droppinguo->buffer) or aftervbo->is_dumbis set to true whilevbo->dumb_surfaceis still NULL.Similarly, if another thread retains arg.rep.handle via
DRM_IOCTL_VMW_GB_SURFACE_REF_EXTand the GEM handle is later closed while vbo is busy,ttm_bo_release()queues vbo for delayed deletion with refcount 0 beforevmw_bo_free()clearsres->guest_memory_bo. A concurrentvmw_gb_surface_reference_internal()can then pass&srf->res.guest_memory_bo->tbo.basetodrm_gem_handle_create()after its refcount has reached 0.drivers/gpu/drm/vmwgfx/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2329
Quoted 1 line
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-0drm_gem_object_put (&vbo->tbo.base);[Severity: High]
Does dropping res->guest_memory_bo's GEM reference here allow a use-after-free on vbo if userspace retains the surface handle and switches its backup buffer?Because arg.rep.handle is published as a shareable surface before
ttm_ref_object_base_unref()removes it below, another thread can retain the surface handle and submitSVGA_3D_CMD_BIND_GB_SURFACE:CPU0 CPU1
vmw_dumb_create()
vmw_gb_surface_define_ext_ioctl()vmw_surface_handle_reference() ttm_ref_object_add(tfile, ...) drm_gem_object_put(&vbo->tbo.base) ... ttm_ref_object_base_unref(...) vmw_execbuf_process() vmw_cmd_bind_gb_surface() ... vmw_resource_unreserve() vmw_user_bo_unref( &res->guest_memory_bo) <-- drops GEM handle's ref, freeing vbo
res->guest_memory_bostill points to vbo without a counted reference afterdrm_gem_object_put(), so switching the surface backup drops the last reference belonging toargs->handlewhile the GEM handle remains installed infile_priv->object_idr.drivers/gpu/drm/vmwgfx/vmwgfx_surface.c int vmw_dumb_create(struct drm_file *file_priv, · line 2332
Quoted 10 lines
drivers/gpu/drm/vmwgfx/vmwgfx_surface.c +0
-7-/* -* Unset the user surface dtor since this in not actually exposed -* to userspace. The suface is owned via the dumb_buffer's GEM handle -*/ -struct vmw_user_surface *usurf =container_of (vbo->dumb_surface, - struct vmw_user_surface, srf); - usurf->prime.base.refcount_release =NULL ; err:if (res)vmw_resource_unreference (&res);[Severity: High]
What happens ifDRM_IOCTL_VMW_UNREF_SURFACEdestroys arg.rep.handle beforevmw_user_resource_lookup_handle()runs invmw_dumb_create()?CPU0 CPU1
vmw_dumb_create()
vmw_gb_surface_define_ext_ioctl()... vmw_bo_add_detached_resource(vbo, res) ttm_prime_object_init(...) vmw_resource_unreference(&res) ttm_ref_object_base_unref() vmw_user_surface_base_release() vmw_resource_unreference(&res) vmw_resource_release() vmw_user_bo_unref(...) vmw_user_surface_free() kfree(user_srf) vmw_user_resource_lookup_handle() /* returns -EINVAL, jumps to err */ vmw_kms_fb_create() ... vmw_bo_surface(vbo) res->func->res_type <-- use-after-free
Quoted 2 lines
When
vmw_user_resource_lookup_handle()fails, the err path does not calldrm_gem_handle_delete(file_priv, arg.rep.buffer_handle).
It would only narrow the race with unref, as unref can still be called earlier and needs to be paired with other ioctl calls before we call drm_gem_handle_delete().
But it doesn't harm to have the deletion.
These races seem to come up due to handles being installed before the object being fully initialized, especially the ttm handle is causing unref ioctl race.
- Ashwin