Re: [syzbot] [dri?] [media?] memory leak in get_sg_table

Edward Adam Davis
2024-01-01T02:49:09Z

please test memory leak in get_sg_table

#syz test https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git fbafc3e621c3

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 21916bba77d5..709260211546 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -471,8 +471,11 @@ static long dma_buf_ioctl(struct file *file,

if (sync.flags & DMA_BUF_SYNC_END)
ret = dma_buf_end_cpu_access(dmabuf, direction);
- else
+ else {
+ if (dmabuf->doing)
+ return -EBUSY;
ret = dma_buf_begin_cpu_access(dmabuf, direction);
+ }

return ret;

diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index c40645999648..ed98c0319e03 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -152,6 +152,7 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
struct device *dev = ubuf->device->this_device;
int ret = 0;

+ buf->doing = true;
if (!ubuf->sg) {
ubuf->sg = get_sg_table(dev, buf, direction);
if (IS_ERR(ubuf->sg)) {
@@ -162,6 +163,7 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
dma_sync_sg_for_cpu(dev, ubuf->sg->sgl, ubuf->sg->nents,
direction);
}
+ buf->doing = false;

return ret;
}
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 3f31baa3293f..4316ad0e6155 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -446,6 +446,7 @@ struct dma_buf {
struct dma_buf *dmabuf;
} *sysfs_entry;
#endif
+ bool doing;
};

/**