Skip to contentSkip to conversation

drm/virtio: Fix NULL pointer deref in virtgpu_dma_buf_free_obj()

PATCH Applied Vivek Kasireddy 2 participants 5 messages May 1, 2025 – May 12, 2025

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

PATCH 1/1 drm/virtio: Fix NULL pointer deref in virtgpu_dma_buf_free_obj()

There is a chance that obj->dma_buf would be NULL by the time virtgpu_dma_buf_free_obj() is called. This can happen for imported prime objects, when drm_gem_object_exported_dma_buf_free() gets called on them before drm_gem_object_free(). This is because drm_gem_object_exported_dma_buf_free() explicitly sets obj->dma_buf to NULL.

Therefore, fix this issue by storing the dma_buf pointer in the virtio_gpu_object instance and using it in virtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it in virtgpu_gem_prime_import().

Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object instance")
Cc Dmitry Osipenko <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_prime.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..f7def8b42068 100644
@@ -88,6 +88,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct dma_buf *dma_buf;
 	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
drivers/gpu/drm/virtio/virtgpu_prime.c +2 -1
index 1118a0250279..722cde5e2d86 100644
@@ -206,7 +206,7 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
 	struct virtio_gpu_device *vgdev = obj->dev->dev_private;
 
 	if (drm_gem_is_imported(obj)) {
-		struct dma_buf *dmabuf = obj->dma_buf;
+		struct dma_buf *dmabuf = bo->dma_buf;
 
 		dma_resv_lock(dmabuf->resv, NULL);
 		virtgpu_dma_buf_unmap(bo);
@@ -332,6 +332,7 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
 
 	obj->import_attach = attach;
 	get_dma_buf(buf);
+	bo->dma_buf = buf;
 
 	ret = virtgpu_dma_buf_init_obj(dev, bo, attach);
 	if (ret < 0)
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 38 lines · Vivek Kasireddy

There is a chance that obj->dma_buf would be NULL by the time virtgpu_dma_buf_free_obj() is called. This can happen for imported prime objects, when drm_gem_object_exported_dma_buf_free() gets called on them before drm_gem_object_free(). This is because drm_gem_object_exported_dma_buf_free() explicitly sets obj->dma_buf to NULL.

Therefore, fix this issue by storing the dma_buf pointer in the virtio_gpu_object instance and using it in virtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it in virtgpu_gem_prime_import().

Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object instance")
Cc Dmitry Osipenko <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_prime.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..f7def8b42068 100644
@@ -88,6 +88,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct dma_buf *dma_buf;
 	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
drivers/gpu/drm/virtio/virtgpu_prime.c +0 -0
index 1118a0250279..722cde5e2d86 100644

drivers/gpu/drm/virtio/virtgpu_prime.c static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj) · line 209

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +1 -1
@@ -206,7 +206,7 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
 	struct virtio_gpu_device *vgdev = obj->dev->dev_private;
 
 	if (drm_gem_is_imported(obj)) {
-		struct dma_buf *dmabuf = obj->dma_buf;
+		struct dma_buf *dmabuf = bo->dma_buf;

drm_gem_is_imported() checks whether obj->dma_buf is NULL, hence drm_gem_is_imported() can't be used here too?

Signature · 2 lines
Best regards,
Dmitry
#2 lore raw

Hi Dmitry,

Quoted 47 lines

Subject: Re: [PATCH] drm/virtio: Fix NULL pointer deref in virtgpu_dma_buf_free_obj()

Quoted 3 lines · Vivek Kasireddy

There is a chance that obj->dma_buf would be NULL by the time virtgpu_dma_buf_free_obj() is called. This can happen for imported prime objects, when drm_gem_object_exported_dma_buf_free() gets

called

drivers/gpu/drm/virtio/virtgpu_prime.c static void virtgpu_dma_buf_free_obj(struct

Quoted 38 lines

on them before drm_gem_object_free(). This is because drm_gem_object_exported_dma_buf_free() explicitly sets obj->dma_buf to NULL.

Therefore, fix this issue by storing the dma_buf pointer in the virtio_gpu_object instance and using it in virtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it in virtgpu_gem_prime_import().

Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object

instance")

Cc Dmitry Osipenko <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_prime.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..f7def8b42068 100644
@@ -88,6 +88,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct dma_buf *dma_buf;
 	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
drivers/gpu/drm/virtio/virtgpu_prime.c +0 -0
index 1118a0250279..722cde5e2d86 100644
@@ -206,7 +206,7 @@ static void virtgpu_dma_buf_free_obj(struct

drm_gem_object *obj)

Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj) · line 209

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +1 -1
 	struct virtio_gpu_device *vgdev = obj->dev->dev_private;
 
 	if (drm_gem_is_imported(obj)) {
-		struct dma_buf *dmabuf = obj->dma_buf;
+		struct dma_buf *dmabuf = bo->dma_buf;

drm_gem_is_imported() checks whether obj->dma_buf is NULL, hence drm_gem_is_imported() can't be used here too?

Unless I am missing something, it looks like drm_gem_is_imported() does not seem to check obj->dma_buf:

static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)

{
        return !!obj->import_attach;
}

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#3 lore raw
Quoted 57 lines · Vivek

Hi Dmitry,

Quoted 47 lines

Subject: Re: [PATCH] drm/virtio: Fix NULL pointer deref in virtgpu_dma_buf_free_obj()

Quoted 3 lines · Vivek Kasireddy

There is a chance that obj->dma_buf would be NULL by the time virtgpu_dma_buf_free_obj() is called. This can happen for imported prime objects, when drm_gem_object_exported_dma_buf_free() gets

called

drivers/gpu/drm/virtio/virtgpu_prime.c static void virtgpu_dma_buf_free_obj(struct

Quoted 38 lines

on them before drm_gem_object_free(). This is because drm_gem_object_exported_dma_buf_free() explicitly sets obj->dma_buf to NULL.

Therefore, fix this issue by storing the dma_buf pointer in the virtio_gpu_object instance and using it in virtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it in virtgpu_gem_prime_import().

Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object

instance")

Cc Dmitry Osipenko <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_prime.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..f7def8b42068 100644
@@ -88,6 +88,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct dma_buf *dma_buf;
 	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
drivers/gpu/drm/virtio/virtgpu_prime.c +0 -0
index 1118a0250279..722cde5e2d86 100644
@@ -206,7 +206,7 @@ static void virtgpu_dma_buf_free_obj(struct

drm_gem_object *obj)

Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj) · line 209

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +1 -1
 	struct virtio_gpu_device *vgdev = obj->dev->dev_private;
 
 	if (drm_gem_is_imported(obj)) {
-		struct dma_buf *dmabuf = obj->dma_buf;
+		struct dma_buf *dmabuf = bo->dma_buf;

drm_gem_is_imported() checks whether obj->dma_buf is NULL, hence drm_gem_is_imported() can't be used here too?

Quoted 7 lines

Unless I am missing something, it looks like drm_gem_is_imported() does not seem to check obj->dma_buf:

static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)

{
        return !!obj->import_attach;
}

Indeed, it was changed a month ago by [1]. Then the patch looks okay, thanks. Will merge soon.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8260731ccad0451207b45844bb66eb161a209218

Signature · 2 lines
Best regards,
Dmitry
maintainer Reviewed-by on v1 1/1 Applied
#4 lore raw
In reply to Vivek Kasireddy
Quoted 47 lines · Vivek Kasireddy

There is a chance that obj->dma_buf would be NULL by the time virtgpu_dma_buf_free_obj() is called. This can happen for imported prime objects, when drm_gem_object_exported_dma_buf_free() gets called on them before drm_gem_object_free(). This is because drm_gem_object_exported_dma_buf_free() explicitly sets obj->dma_buf to NULL.

Therefore, fix this issue by storing the dma_buf pointer in the virtio_gpu_object instance and using it in virtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it in virtgpu_gem_prime_import().

Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object instance")
Cc Dmitry Osipenko <[email protected]>
Cc Thomas Zimmermann <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_prime.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_drv.h +1 -0
index f17660a71a3e..f7def8b42068 100644
@@ -88,6 +88,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct dma_buf *dma_buf;
 	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
drivers/gpu/drm/virtio/virtgpu_prime.c +1 -1
index 1118a0250279..722cde5e2d86 100644
@@ -206,7 +206,7 @@ static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
 	struct virtio_gpu_device *vgdev = obj->dev->dev_private;
 
 	if (drm_gem_is_imported(obj)) {
-		struct dma_buf *dmabuf = obj->dma_buf;
+		struct dma_buf *dmabuf = bo->dma_buf;
 
 		dma_resv_lock(dmabuf->resv, NULL);
 		virtgpu_dma_buf_unmap(bo);

drivers/gpu/drm/virtio/virtgpu_prime.c struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev, · line 338

Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +1 -0
@@ -332,6 +332,7 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
 
 	obj->import_attach = attach;
 	get_dma_buf(buf);
+	bo->dma_buf = buf;
 
 	ret = virtgpu_dma_buf_init_obj(dev, bo, attach);
 	if (ret < 0)

Applied to misc-next, thanks!

Signature · 2 lines
Best regards,
Dmitry