dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jayapal Ashwin Nair" <jayapalashwin.nair@amd.com>,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2] drm/vmwgfx: Fix UAF race between dumb buffer creation and surface unref
Date: Thu, 19 Nov 2026 19:26:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-223878@kernel.org> (raw)
In-Reply-To: <20261119190705.11305-1-jayapalashwin.nair@amd.com>

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

      reply	other threads:[~2026-11-19 19:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-223878@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jayapalashwin.nair@amd.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox