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

From: Edward Adam Davis
Date: Sun Dec 31 2023 - 23:10:12 EST


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..ff49e5cf7d07 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -151,7 +151,10 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
struct udmabuf *ubuf = buf->priv;
struct device *dev = ubuf->device->this_device;
int ret = 0;
+ static DEFINE_MUTEX(lock);

+ mutex_lock(&lock);
+ buf->doing = true;
if (!ubuf->sg) {
ubuf->sg = get_sg_table(dev, buf, direction);
if (IS_ERR(ubuf->sg)) {
@@ -162,6 +165,8 @@ 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;
+ mutex_unlock(&lock);

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;
};

/**