Attaching to a zero-sized dma-buf does not make sense at all: there is
nothing for an importer to map.

So reject the attach, using WARN_ON() because a zero-sized dma-buf is
an exporter bug that userspace cannot trigger on its own.

Adding the check to dma_buf_export() instead was considered, which
would prevent zero-sized DMA-bufs from being created in the first
place.  It is not done here because drivers may be using zero-sized
DMA-bufs for driver internal buffers, and refusing them at creation
time risks regressing those users.

Fixes: d15bd7ee445d ("dma-buf: Introduce dma buffer sharing mechanism")
Signed-off-by: Lizhi Hou <[email protected]>
---
 drivers/dma-buf/dma-buf.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 5c6957f8839a..8be7b6023e4a 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1050,6 +1050,9 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct 
device *dev,
        if (WARN_ON(!dmabuf || !dev))
                return ERR_PTR(-EINVAL);
 
+       if (WARN_ON(!dmabuf->size))
+               return ERR_PTR(-EINVAL);
+
        attach = kzalloc_obj(*attach);
        if (!attach)
                return ERR_PTR(-ENOMEM);
-- 
2.34.1

Reply via email to