drm/virtio: Fix NULL pointer deref in virtgpu_dma_buf_free_obj()
Subsystems (3)
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().
--- 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
@@ -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
@@ -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
Dmitry Osipenko
maintainerQuoted 38 lines · Vivek Kasireddy
There is a chance that
obj->dma_bufwould be NULL by the timevirtgpu_dma_buf_free_obj()is called. This can happen for imported prime objects, whendrm_gem_object_exported_dma_buf_free()gets called on them beforedrm_gem_object_free(). This is becausedrm_gem_object_exported_dma_buf_free()explicitly setsobj->dma_bufto NULL.Therefore, fix this issue by storing the
dma_bufpointer in thevirtio_gpu_objectinstance and using it invirtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it invirtgpu_gem_prime_import().Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object instance")Cc Dmitry Osipenko <[email protected]>Cc Thomas Zimmermann <[email protected]>Signed-off-by Vivek Kasireddy <[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@@ -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
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
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_bufwould be NULL by the timevirtgpu_dma_buf_free_obj()is called. This can happen for imported prime objects, whendrm_gem_object_exported_dma_buf_free()getscalled
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 becausedrm_gem_object_exported_dma_buf_free()explicitly setsobj->dma_bufto NULL.Therefore, fix this issue by storing the
dma_bufpointer in thevirtio_gpu_objectinstance and using it invirtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it invirtgpu_gem_prime_import().Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM objectinstance")
Cc Dmitry Osipenko <[email protected]>Cc Thomas Zimmermann <[email protected]>Signed-off-by Vivek Kasireddy <[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@@ -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@@ -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
-1struct 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 whetherobj->dma_bufis NULL, hencedrm_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
Dmitry Osipenko
maintainerQuoted 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_bufwould be NULL by the timevirtgpu_dma_buf_free_obj()is called. This can happen for imported prime objects, whendrm_gem_object_exported_dma_buf_free()getscalled
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 becausedrm_gem_object_exported_dma_buf_free()explicitly setsobj->dma_bufto NULL.Therefore, fix this issue by storing the
dma_bufpointer in thevirtio_gpu_objectinstance and using it invirtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it invirtgpu_gem_prime_import().Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM objectinstance")
Cc Dmitry Osipenko <[email protected]>Cc Thomas Zimmermann <[email protected]>Signed-off-by Vivek Kasireddy <[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@@ -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@@ -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
-1struct 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 whetherobj->dma_bufis NULL, hencedrm_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 checkobj->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.
Signature · 2 lines
Best regards, Dmitry
Quoted 47 lines · Vivek Kasireddy
There is a chance that
obj->dma_bufwould be NULL by the timevirtgpu_dma_buf_free_obj()is called. This can happen for imported prime objects, whendrm_gem_object_exported_dma_buf_free()gets called on them beforedrm_gem_object_free(). This is becausedrm_gem_object_exported_dma_buf_free()explicitly setsobj->dma_bufto NULL.Therefore, fix this issue by storing the
dma_bufpointer in thevirtio_gpu_objectinstance and using it invirtgpu_dma_buf_free_obj. This stored pointer is guaranteed to be valid until the object is freed as we took a reference on it invirtgpu_gem_prime_import().Fixes 415cb45895f4 ("drm/virtio: Use dma_buf from GEM object instance")Cc Dmitry Osipenko <[email protected]>Cc Thomas Zimmermann <[email protected]>Signed-off-by Vivek Kasireddy <[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@@ -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@@ -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