accel/amdxdna: Reject a zero sized buffer in amdxdna_gem_prime_import()
Lizhi Hou
maintainerPATCH 1/1 accel/amdxdna: Reject a zero sized buffer in amdxdna_gem_prime_import()
Zero sized buffer is not supported. Check the buffer size and return -EINVAL if the size is zero.
--- drivers/accel/amdxdna/amdxdna_gem.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/accel/amdxdna/amdxdna_gem.c +3 -0
@@ -1133,6 +1133,9 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)struct sg_table *sgt;int ret; +if (!dma_buf->size) +return ERR_PTR (-EINVAL ); +get_dma_buf (dma_buf); attach =dma_buf_attach (dma_buf, dev->dev);
Signature · 1 line
2.34.1
Quoted 2 lines · Lizhi Hou
Zero sized buffer is not supported. Check the buffer size and return
-EINVALif the size is zero.
Oh that is surprising a zero sized buffer shouldn't be supported by DMA-buf in the first place.
Doesn't DMA-buf check that somewhere?
Regards,
Christian.
Quoted 21 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/amdxdna_gem.c | 3 +++ 1 file changed, 3 insertions(+)drivers/accel/amdxdna/amdxdna_gem.c +3
-0@@ -1133,6 +1133,9 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)struct sg_table *sgt;int ret; +if (!dma_buf->size) +return ERR_PTR (-EINVAL ); +get_dma_buf (dma_buf); attach =dma_buf_attach (dma_buf, dev->dev);
Lizhi Hou
maintainerQuoted 6 lines · Christian König
Quoted 2 lines · Lizhi Hou
Zero sized buffer is not supported. Check the buffer size and return
-EINVALif the size is zero.Oh that is surprising a zero sized buffer shouldn't be supported by DMA-buf in the first place.
Doesn't DMA-buf check that somewhere?
No. I built a local test to reproduce. The test driver calls dma_buf_export() with exp_info.size=0, and it successes. Then, import that buffer and get the stack.
It looks other drm drivers check the size as well, e.g. drivers/accel/qaic/qaic_data.c, qaic_gem_prime_import().
Maybe it is better to add the check to dma_buf_attach()? Or I can use the check in amdxdna for now?
[89432.394897] RIP: 0010:amdxdna_gem_prime_import+0xb/0x120 [amdxdna] [89432.394905] Code: a2 29 c1 5d 31 f6 31 ff c3 cc cc cc cc 66 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 48 83 3e 00 75 14 <0f> 0b 48 c7 c0 ea ff ff ff 31 d2 31 f6 31 ff c3 cc cc cc cc 55 b8 [89432.394908] RSP: 0018:ffffd04344fd79d8 EFLAGS: 00010246 [89432.394911] RAX: ffffffffc10b4bb0 RBX: ffff8ddb05863a00 RCX: 0000000000000000 [89432.394912] RDX: 0000000000000000 RSI: ffff8ddb084ebc00 RDI: ffff8dd94b17f000 [89432.394914] RBP: ffffd04344fd7a10 R08: 0000000000000000 R09: 0000000000000000 [89432.394915] R10: 000000000000000c R11: 0000000000000000 R12: ffff8dd94b17f000 [89432.394916] R13: ffffd04344fd7ad0 R14: ffff8ddb05863b20 R15: ffff8ddb084ebc00 [89432.394917] FS: 00007f8216e08740(0000) GS:ffff8de0c09bd000(0000) knlGS:0000000000000000 [89432.394919] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [89432.394921] CR2: 000055c3644b51e9 CR3: 000000011237f000 CR4: 0000000000750ef0 [89432.394923] PKRU: 55555554 [89432.394924] Call Trace: [89432.394926] <TASK> [89432.394929] ? drm_gem_prime_fd_to_handle+0xdb/0x220 [drm] [89432.394974] ? __pfx_drm_prime_fd_to_handle_ioctl+0x10/0x10 [drm] [89432.395003] drm_prime_fd_to_handle_ioctl+0x37/0x50 [drm] [89432.395029] drm_ioctl_kernel+0xb0/0x110 [drm] [89432.395069] drm_ioctl+0x2d6/0x580 [drm]
Thanks,
Lizhi
Quoted 24 lines
Regards,
Christian.Quoted 20 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/amdxdna_gem.c | 3 +++ 1 file changed, 3 insertions(+)drivers/accel/amdxdna/amdxdna_gem.c +3
-0@@ -1133,6 +1133,9 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)struct sg_table *sgt;int ret; +if (!dma_buf->size) +return ERR_PTR (-EINVAL ); +get_dma_buf (dma_buf); attach =dma_buf_attach (dma_buf, dev->dev);
Quoted 7 lines · Lizhi Hou
Quoted 5 lines · Christian König
Quoted 2 lines · Lizhi Hou
Zero sized buffer is not supported. Check the buffer size and return
-EINVALif the size is zero.Oh that is surprising a zero sized buffer shouldn't be supported by DMA-buf in the first place.
Quoted 7 lines
Quoted 1 line
Doesn't DMA-buf check that somewhere?
No. I built a local test to reproduce. The test driver calls
dma_buf_export()with exp_info.size=0, and it successes. Then, import that buffer and get the stack.It looks other drm drivers check the size as well, e.g.
drivers/accel/qaic/qaic_data.c,qaic_gem_prime_import().Maybe it is better to add the check to
dma_buf_attach()? Or I can use the check in amdxdna for now?
Yeah, I think attaching to a zero sized DMA-buf doesn't make any sense at all.
Alternatively the the check could be in dma_buf_export() prevent even creation of zero sized DMA-bufs, but it is remotely possible that somebody uses this for driver internal buffers so I'm not 100% sure if that doesn't regress anything.
But the check should definitely be in DMA-buf, otherwise you can do very naughty things with drivers who doesn't check that.
Regards,
Christian.
Quoted 48 lines
[89432.394897] RIP: 0010:amdxdna_gem_prime_import+0xb/0x120 [amdxdna] [89432.394905] Code: a2 29 c1 5d 31 f6 31 ff c3 cc cc cc cc 66 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 48 83 3e 00 75 14 <0f> 0b 48 c7 c0 ea ff ff ff 31 d2 31 f6 31 ff c3 cc cc cc cc 55 b8 [89432.394908] RSP: 0018:ffffd04344fd79d8 EFLAGS: 00010246 [89432.394911] RAX: ffffffffc10b4bb0 RBX: ffff8ddb05863a00 RCX: 0000000000000000 [89432.394912] RDX: 0000000000000000 RSI: ffff8ddb084ebc00 RDI: ffff8dd94b17f000 [89432.394914] RBP: ffffd04344fd7a10 R08: 0000000000000000 R09: 0000000000000000 [89432.394915] R10: 000000000000000c R11: 0000000000000000 R12: ffff8dd94b17f000 [89432.394916] R13: ffffd04344fd7ad0 R14: ffff8ddb05863b20 R15: ffff8ddb084ebc00 [89432.394917] FS: 00007f8216e08740(0000) GS:ffff8de0c09bd000(0000) knlGS:0000000000000000 [89432.394919] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [89432.394921] CR2: 000055c3644b51e9 CR3: 000000011237f000 CR4: 0000000000750ef0 [89432.394923] PKRU: 55555554 [89432.394924] Call Trace: [89432.394926] <TASK> [89432.394929] ? drm_gem_prime_fd_to_handle+0xdb/0x220 [drm] [89432.394974] ? __pfx_drm_prime_fd_to_handle_ioctl+0x10/0x10 [drm] [89432.395003] drm_prime_fd_to_handle_ioctl+0x37/0x50 [drm] [89432.395029] drm_ioctl_kernel+0xb0/0x110 [drm] [89432.395069] drm_ioctl+0x2d6/0x580 [drm]Thanks,
Lizhi
Quoted 22 lines
Regards,
Christian.Quoted 18 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>---
drivers/accel/amdxdna/amdxdna_gem.c| 3 +++1 file changed, 3 insertions(+)drivers/accel/amdxdna/amdxdna_gem.c +2
-0@@ -1133,6 +1133,9 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf) struct sg_table *sgt; int ret; + if (!dma_buf->size) + return ERR_PTR(-EINVAL); + get_dma_buf(dma_buf); attach = dma_buf_attach(dma_buf, dev->dev);
Lizhi Hou
maintainerQuoted 10 lines · Christian König
Quoted 9 lines · Lizhi Hou
Quoted 6 lines · Christian König
Quoted 2 lines · Lizhi Hou
Zero sized buffer is not supported. Check the buffer size and return
-EINVALif the size is zero.Oh that is surprising a zero sized buffer shouldn't be supported by DMA-buf in the first place.
Doesn't DMA-buf check that somewhere?
No. I built a local test to reproduce. The test driver calls
dma_buf_export()with exp_info.size=0, and it successes. Then, import that buffer and get the stack.
Quoted 8 lines
Quoted 3 lines
It looks other drm drivers check the size as well, e.g.
drivers/accel/qaic/qaic_data.c,qaic_gem_prime_import().Maybe it is better to add the check to
dma_buf_attach()? Or I can use the check in amdxdna for now?Yeah, I think attaching to a zero sized DMA-buf doesn't make any sense at all.
Alternatively the the check could be in
dma_buf_export()prevent even creation of zero sized DMA-bufs, but it is remotely possible that somebody uses this for driver internal buffers so I'm not 100% sure if that doesn't regress anything.But the check should definitely be in DMA-buf, otherwise you can do very naughty things with drivers who doesn't check that.
Thanks for your guidance. I have submitted a patch to check in dma_buf_attach.
https://lore.kernel.org/all/[email protected]/
Lizhi
Quoted 50 lines
Regards,
Christian.Quoted 46 lines
[89432.394897] RIP: 0010:amdxdna_gem_prime_import+0xb/0x120 [amdxdna] [89432.394905] Code: a2 29 c1 5d 31 f6 31 ff c3 cc cc cc cc 66 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 48 83 3e 00 75 14 <0f> 0b 48 c7 c0 ea ff ff ff 31 d2 31 f6 31 ff c3 cc cc cc cc 55 b8 [89432.394908] RSP: 0018:ffffd04344fd79d8 EFLAGS: 00010246 [89432.394911] RAX: ffffffffc10b4bb0 RBX: ffff8ddb05863a00 RCX: 0000000000000000 [89432.394912] RDX: 0000000000000000 RSI: ffff8ddb084ebc00 RDI: ffff8dd94b17f000 [89432.394914] RBP: ffffd04344fd7a10 R08: 0000000000000000 R09: 0000000000000000 [89432.394915] R10: 000000000000000c R11: 0000000000000000 R12: ffff8dd94b17f000 [89432.394916] R13: ffffd04344fd7ad0 R14: ffff8ddb05863b20 R15: ffff8ddb084ebc00 [89432.394917] FS: 00007f8216e08740(0000) GS:ffff8de0c09bd000(0000) knlGS:0000000000000000 [89432.394919] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [89432.394921] CR2: 000055c3644b51e9 CR3: 000000011237f000 CR4: 0000000000750ef0 [89432.394923] PKRU: 55555554 [89432.394924] Call Trace: [89432.394926] <TASK> [89432.394929] ? drm_gem_prime_fd_to_handle+0xdb/0x220 [drm] [89432.394974] ? __pfx_drm_prime_fd_to_handle_ioctl+0x10/0x10 [drm] [89432.395003] drm_prime_fd_to_handle_ioctl+0x37/0x50 [drm] [89432.395029] drm_ioctl_kernel+0xb0/0x110 [drm] [89432.395069] drm_ioctl+0x2d6/0x580 [drm]Thanks,
Lizhi
Quoted 21 lines
Regards,
Christian.Quoted 18 lines
Fixes e486147c912f ("accel/amdxdna: Add BO import and export")Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/amdxdna_gem.c | 3 +++ 1 file changed, 3 insertions(+)drivers/accel/amdxdna/amdxdna_gem.c +2
-0@@ -1133,6 +1133,9 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf) struct sg_table *sgt; int ret; + if (!dma_buf->size) + return ERR_PTR(-EINVAL); + get_dma_buf(dma_buf); attach = dma_buf_attach(dma_buf, dev->dev);