dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
@ 2026-11-19 19:07 Jayapal Ashwin Nair
  2026-11-19 19:26 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jayapal Ashwin Nair @ 2026-11-19 19:07 UTC (permalink / raw)
  To: dri-devel, zack.rusin
  Cc: linux-kernel, bcm-kernel-feedback-list, maaz.mombasawala, mripard,
	maarten.lankhorst, tzimmermann, airlied, simona,
	jayapalashwin.nair

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.

Fixes: f42c09e614f1 ("drm/vmwgfx: Fix dumb buffer leak")
Signed-off-by: Jayapal Ashwin Nair <jayapalashwin.nair@amd.com>
---
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/sashiko-outbox-188756@kernel.org

v1: https://lore.kernel.org/7a822adb-ddb5-4338-8949-3ccb0942e0a0@amd.com

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(-)

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
index bd0563741e89..942d2f39332b 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
@@ -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
-- 
2.53.0


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

* Re: [PATCH v2] drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
  2026-11-19 19:07 [PATCH v2] drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref Jayapal Ashwin Nair
@ 2026-11-19 19:26 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-19 19:26 UTC (permalink / raw)
  To: Jayapal Ashwin Nair, dri-devel

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 <jayapalashwin.nair@amd.com>

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.

> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
> index bd0563741e89d..942d2f39332b5 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
> +++ b/drivers/gpu/drm/vmwgfx_surface.c

[ ... ]

> @@ -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.

>  	drm_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                              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_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.

> -	/*
> -	 * 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                              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

When 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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119190705.11305-1-jayapalashwin.nair@amd.com?part=1

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

end of thread, other threads:[~2026-11-19 19:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:07 [PATCH v2] drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref Jayapal Ashwin Nair
2026-11-19 19:26 ` sashiko-bot

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