On 20-11-2026 00:56, [email protected] wrote: > >> @@ -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).
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
