Skip to content

Commit

Permalink
drm/virtio: Fix UAF in virtgpu_dma_buf_free_obj()
Browse files Browse the repository at this point in the history
Fix the following issues identified by Smatch static checker:
- The call to dma_buf_put(attach->dmabuf) after dma_buf_detach()
  leads to a UAF bug as dma_buf_detach() frees the attach object.
  Fix this by extracting the dmabuf object from attach and using
  that in the call to dma_buf_put().

- The resv object is extracted from attach before checking to see
  if attach is valid (that is !NULL) or not. Although, attach would
  very likely be valid, fix this by making sure that the resv object
  is used only after ensuring that attach is valid.

Fixes: 2885e57 ("drm/virtio: Add helpers to initialize and free the imported object")
Fixes: ca77f27 ("drm/virtio: Import prime buffers from other devices as guest blobs")
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Cc: Gurchetan Singh <gurchetansingh@chromium.org>
Cc: Chia-I Wu <olvaffe@gmail.com>
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20241212055421.775759-1-vivek.kasireddy@intel.com
Reviewed-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Tested-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
[dmitry.osipenko@collabora.com: Edited commit title]
  • Loading branch information
vivekkreddy authored and digetx committed Jan 19, 2025
1 parent bea6afc commit db8b2c0
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions drivers/gpu/drm/virtio/virtgpu_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,22 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
struct virtio_gpu_device *vgdev = obj->dev->dev_private;
struct dma_buf_attachment *attach = obj->import_attach;
struct dma_resv *resv = attach->dmabuf->resv;

if (attach) {
dma_resv_lock(resv, NULL);
struct dma_buf *dmabuf = attach->dmabuf;

dma_resv_lock(dmabuf->resv, NULL);

virtio_gpu_detach_object_fenced(bo);

if (bo->sgt)
dma_buf_unmap_attachment(attach, bo->sgt,
DMA_BIDIRECTIONAL);

dma_resv_unlock(resv);
dma_resv_unlock(dmabuf->resv);

dma_buf_detach(attach->dmabuf, attach);
dma_buf_put(attach->dmabuf);
dma_buf_detach(dmabuf, attach);
dma_buf_put(dmabuf);
}

if (bo->created) {
Expand Down

0 comments on commit db8b2c0

Please sign in to comment.