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

Pre-existing issues:
- [Critical] TOCTOU race on `srf->res.guest_memory_bo` leading to a NULL 
pointer dereference in `drm_gem_handle_create`.
- [Critical] Lockless accesses to `guest_memory_bo` after releasing 
`cmdbuf_mutex` can lead to a Use-After-Free or NULL pointer dereference.
- [High] Leaked DRM handle on error paths in 
`vmw_gb_surface_reference_internal` when processing a PRIME file descriptor.
- [High] GEM handle leak in `vmw_gb_surface_define_internal` when 
initialization fails after handle creation.
- [Medium] The function `vmw_gb_surface_reference_internal` fails to populate 
the `buffer_byte_stride` field in the extended reply structure.
--

--- Patch [1]: [PATCH] drm/vmwgfx: return an error when a shared GB surface has 
no backup buffer ---
commit 6bbaf49225562d900a468ed3b35d8995cea03c99
Author: Aldo Ariel Panzardo <[email protected]>

drm/vmwgfx: return an error when a shared GB surface has no backup buffer

This commit fixes a missing error assignment in
vmw_gb_surface_reference_internal() when a shared GB surface lacks a backup
buffer. By returning -EINVAL instead of 0, it prevents the ioctl from
returning uninitialized stack memory to userspace.

> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c 
> b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
> index bd0563741e89d..014f674744ac3 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_surface.c
> @@ -1720,6 +1720,7 @@ vmw_gb_surface_reference_internal(struct drm_device 
> *dev,
>       srf = &user_srf->srf;
>       if (!srf->res.guest_memory_bo) {
>               DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
> +             ret = -EINVAL;
>               goto out_bad_resource;
>       }

[Severity: Critical]
This is a pre-existing issue, but does this lockless check create a TOCTOU
race?

If a concurrent thread detaches the backup buffer and sets guest_memory_bo to
NULL under cmdbuf_mutex, the lockless check here might pass, but a NULL pointer
would then be dereferenced in drm_gem_handle_create():

drivers/gpu/drm/vmwgfx/vmwgfx_surface.c:vmw_gb_surface_reference_internal() {
...
        mutex_lock(&dev_priv->cmdbuf_mutex);
        ret = drm_gem_handle_create(file_priv,
                                    &srf->res.guest_memory_bo->tbo.base,
                                    &backup_handle);
        mutex_unlock(&dev_priv->cmdbuf_mutex);
...
}

[Severity: High]
This isn't a bug introduced by this patch, but does taking this error path leak
a DRM handle?

When handle_type == DRM_VMW_HANDLE_PRIME, vmw_surface_handle_reference() adds a
reference to the file's handle table. The cleanup path at out_bad_resource
drops the object reference but doesn't remove the newly created handle from the
file:

drivers/gpu/drm/vmwgfx/vmwgfx_surface.c:vmw_gb_surface_reference_internal() {
...
out_bad_resource:
        ttm_base_object_unref(&base);

        return ret;
}

>       metadata = &srf->metadata;

[Severity: Critical]
This is a pre-existing issue, but are there unsafe lockless accesses to
guest_memory_bo later in this function?

After releasing cmdbuf_mutex, the function populates mapping handles and sizes
by locklessly dereferencing srf->res.guest_memory_bo:

drivers/gpu/drm/vmwgfx/vmwgfx_surface.c:vmw_gb_surface_reference_internal() {
...
        rep->crep.buffer_map_handle =
                drm_vma_node_offset_addr(
                        &srf->res.guest_memory_bo->tbo.base.vma_node);
        rep->crep.buffer_size = srf->res.guest_memory_bo->tbo.base.size;
...
}

Could this lead to a NULL pointer dereference or Use-After-Free if another
thread detaches or frees the backup buffer concurrently?

[Severity: Medium]
This isn't a bug introduced by this patch, but does this function fail to
populate the buffer_byte_stride field in the extended reply structure?

drivers/gpu/drm/vmwgfx/vmwgfx_surface.c:vmw_gb_surface_reference_internal() {
...
        rep->creq.version = drm_vmw_gb_surface_v1;
        rep->creq.svga3d_flags_upper_32_bits =
                SVGA3D_FLAGS_UPPER_32(metadata->flags);
        rep->creq.multisample_pattern = metadata->multisample_pattern;
        rep->creq.quality_level = metadata->quality_level;
        rep->creq.must_be_zero = 0;
...
}

[Severity: High]
This is a pre-existing issue, but does vmw_gb_surface_define_internal() leak a
GEM handle when initialization fails?

If any initialization fails after vmw_gem_object_create_with_handle()
successfully allocates a GEM buffer object and registers backup_handle, the
cleanup path fails to call drm_gem_handle_delete():

drivers/gpu/drm/vmwgfx/vmwgfx_surface.c:vmw_gb_surface_define_internal() {
...
                ret = vmw_gem_object_create_with_handle(dev_priv, file_priv,
                                                        res->guest_memory_size,
                                                        &backup_handle,
                                                        &res->guest_memory_bo);
        }

        if (unlikely(ret != 0)) {
                vmw_resource_unreference(&res);
                goto out_unlock;
        }
...
out_unlock:
        vmw_resource_unreference(&tmp);
        vmw_resource_unreference(&res);
        return ret;
}

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to