Skip to contentSkip to conversation

media: videobuf2-dma-sg: Fix buf->vb NULL pointer dereference

PATCH In review Hans de Goede 3 participants 4 messages Nov 1, 2021 – Nov 15, 2021

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

PATCH 1/1 media: videobuf2-dma-sg: Fix buf->vb NULL pointer dereference

Commit a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API") added a new vb member to struct vb2_dma_sg_buf, but it only added code setting this to the vb2_dma_sg_alloc() function and not to the vb2_dma_sg_get_userptr() and vb2_dma_sg_attach_dmabuf() which also create vb2_dma_sg_buf objects.

This is causing a crash due to a NULL pointer deref when using libcamera on devices with an Intel IPU3 (qcam app).

Fix these crashes by assigning buf->vb in the other 2 functions too, note libcamera tests the vb2_dma_sg_get_userptr() path, the change to the vb2_dma_sg_attach_dmabuf() path is untested.

Fixes a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API")
Cc Sergey Senozhatsky <[email protected]>
---
 drivers/media/common/videobuf2/videobuf2-dma-sg.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/common/videobuf2/videobuf2-dma-sg.c +2 -0
index 33ee63a99139..0452ed9fac95 100644
@@ -241,6 +241,7 @@ static void *vb2_dma_sg_get_userptr(struct vb2_buffer *vb, struct device *dev,
 	buf->offset = vaddr & ~PAGE_MASK;
 	buf->size = size;
 	buf->dma_sgt = &buf->sg_table;
+	buf->vb = vb;
 	vec = vb2_create_framevec(vaddr, size);
 	if (IS_ERR(vec))
 		goto userptr_fail_pfnvec;
@@ -642,6 +643,7 @@ static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
 	buf->dma_dir = vb->vb2_queue->dma_dir;
 	buf->size = size;
 	buf->db_attach = dba;
+	buf->vb = vb;
 
 	return buf;
 }
Signature · 1 line
2.31.1
#1 lore raw

Hi,

Quoted 9 lines · Hans de Goede

Commit a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API") added a new vb member to struct vb2_dma_sg_buf, but it only added code setting this to the vb2_dma_sg_alloc() function and not to the vb2_dma_sg_get_userptr() and vb2_dma_sg_attach_dmabuf() which also create vb2_dma_sg_buf objects.

This is causing a crash due to a NULL pointer deref when using libcamera on devices with an Intel IPU3 (qcam app).

Quoted 7 lines

Fix these crashes by assigning buf->vb in the other 2 functions too, note libcamera tests the vb2_dma_sg_get_userptr() path, the change to the vb2_dma_sg_attach_dmabuf() path is untested.

Fixes a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API")
Cc Sergey Senozhatsky <[email protected]>

Ping ? This is still an issue in the current media-staging tree.

Regards,

Hans

Quoted 25 lines
---
 drivers/media/common/videobuf2/videobuf2-dma-sg.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/common/videobuf2/videobuf2-dma-sg.c +2 -0
index 33ee63a99139..0452ed9fac95 100644
@@ -241,6 +241,7 @@ static void *vb2_dma_sg_get_userptr(struct vb2_buffer *vb, struct device *dev,
 	buf->offset = vaddr & ~PAGE_MASK;
 	buf->size = size;
 	buf->dma_sgt = &buf->sg_table;
+	buf->vb = vb;
 	vec = vb2_create_framevec(vaddr, size);
 	if (IS_ERR(vec))
 		goto userptr_fail_pfnvec;
@@ -642,6 +643,7 @@ static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
 	buf->dma_dir = vb->vb2_queue->dma_dir;
 	buf->size = size;
 	buf->db_attach = dba;
+	buf->vb = vb;
 
 	return buf;
 }
maintainer Acked-by
#2 lore raw

Hi Hans,

Quoted 17 lines · Hans de Goede

Hi,

Quoted 13 lines · Hans de Goede

Commit a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API") added a new vb member to struct vb2_dma_sg_buf, but it only added code setting this to the vb2_dma_sg_alloc() function and not to the vb2_dma_sg_get_userptr() and vb2_dma_sg_attach_dmabuf() which also create vb2_dma_sg_buf objects.

This is causing a crash due to a NULL pointer deref when using libcamera on devices with an Intel IPU3 (qcam app).

Fix these crashes by assigning buf->vb in the other 2 functions too, note libcamera tests the vb2_dma_sg_get_userptr() path, the change to the vb2_dma_sg_attach_dmabuf() path is untested.

Quoted 5 lines
Quoted 3 lines
Fixes a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API")
Cc Sergey Senozhatsky <[email protected]>

Ping ? This is still an issue in the current media-staging tree.

Uh, sorry, I thought this was already fixed by [1], but that was only for the dma-contig allocator. Thanks for the patch.

[1] https://patchwork.kernel.org/project/linux-media/patch/[email protected]/

Hans (V.), would you pick this fix, please?

Best regards,
Tomasz

Quoted 32 lines

Regards,

Hans

Quoted 25 lines
---
 drivers/media/common/videobuf2/videobuf2-dma-sg.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/common/videobuf2/videobuf2-dma-sg.c +2 -0
index 33ee63a99139..0452ed9fac95 100644
@@ -241,6 +241,7 @@ static void *vb2_dma_sg_get_userptr(struct vb2_buffer *vb, struct device *dev,
      buf->offset = vaddr & ~PAGE_MASK;
      buf->size = size;
      buf->dma_sgt = &buf->sg_table;
+     buf->vb = vb;
      vec = vb2_create_framevec(vaddr, size);
      if (IS_ERR(vec))
              goto userptr_fail_pfnvec;
@@ -642,6 +643,7 @@ static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
      buf->dma_dir = vb->vb2_queue->dma_dir;
      buf->size = size;
      buf->db_attach = dba;
+     buf->vb = vb;
 
      return buf;
 }
#3 lore raw
Quoted 26 lines · Tomasz Figa

Hi Hans,

Quoted 17 lines · Hans de Goede

Hi,

Quoted 13 lines · Hans de Goede

Commit a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API") added a new vb member to struct vb2_dma_sg_buf, but it only added code setting this to the vb2_dma_sg_alloc() function and not to the vb2_dma_sg_get_userptr() and vb2_dma_sg_attach_dmabuf() which also create vb2_dma_sg_buf objects.

This is causing a crash due to a NULL pointer deref when using libcamera on devices with an Intel IPU3 (qcam app).

Fix these crashes by assigning buf->vb in the other 2 functions too, note libcamera tests the vb2_dma_sg_get_userptr() path, the change to the vb2_dma_sg_attach_dmabuf() path is untested.

Quoted 5 lines
Quoted 3 lines
Fixes a4b83deb3e76 ("media: videobuf2: rework vb2_mem_ops API")
Cc Sergey Senozhatsky <[email protected]>

Ping ? This is still an issue in the current media-staging tree.

Quoted 8 lines

Uh, sorry, I thought this was already fixed by [1], but that was only for the dma-contig allocator. Thanks for the patch.

[1] https://patchwork.kernel.org/project/linux-media/patch/[email protected]/

Hans (V.), would you pick this fix, please?

Yes, it's in a PR I posted for 5.16.

Regards,

Hans

Quoted 36 lines

Best regards,
Tomasz

Quoted 32 lines

Regards,

Hans

Quoted 25 lines
---
 drivers/media/common/videobuf2/videobuf2-dma-sg.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/common/videobuf2/videobuf2-dma-sg.c +2 -0
index 33ee63a99139..0452ed9fac95 100644
@@ -241,6 +241,7 @@ static void *vb2_dma_sg_get_userptr(struct vb2_buffer *vb, struct device *dev,
      buf->offset = vaddr & ~PAGE_MASK;
      buf->size = size;
      buf->dma_sgt = &buf->sg_table;
+     buf->vb = vb;
      vec = vb2_create_framevec(vaddr, size);
      if (IS_ERR(vec))
              goto userptr_fail_pfnvec;
@@ -642,6 +643,7 @@ static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
      buf->dma_dir = vb->vb2_queue->dma_dir;
      buf->size = size;
      buf->db_attach = dba;
+     buf->vb = vb;
 
      return buf;
 }