Skip to contentSkip to conversation

drm/virtio: Fix Smatch tool warnings in virtgpu_dma_buf_free_obj()

PATCH Applied Vivek Kasireddy 2 participants 2 messages Dec 12, 2024 – Jan 9, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/virtio: Fix Smatch tool warnings in virtgpu_dma_buf_free_obj()

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 2885e575abc7 ("drm/virtio: Add helpers to initialize and free the imported object")
Fixes ca77f27a2665 ("drm/virtio: Import prime buffers from other devices as guest blobs")
Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_prime.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/virtio/virtgpu_prime.c +6 -5
index b3664c12843d..f92133a01195 100644
@@ -189,10 +189,11 @@ 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);
 
@@ -200,10 +201,10 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
 			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) {
Signature · 1 line
2.47.1
maintainer Applied
#1 lore raw
Quoted 19 lines · Vivek Kasireddy

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 2885e575abc7 ("drm/virtio: Add helpers to initialize and free the imported object")
Fixes ca77f27a2665 ("drm/virtio: Import prime buffers from other devices as guest blobs")
Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
Quoted 3 lines
---
 drivers/gpu/drm/virtio/virtgpu_prime.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Applied to misc-next with edited commit title, thanks!

P.S.: This patch may also be needed applied to next-fixes, but that branch is locked at the moment. I'll keep an eye on whether patch will be missed in 6.14-rc1 and I'll need to re-add it to the -fixes.

Signature · 2 lines
Best regards,
Dmitry