Skip to contentSkip to conversation

dma-buf: Fix memory leak in dma_buf_set_name

PATCH In review Bharath Vedartham 2 participants 3 messages Aug 16, 2019 – Aug 18, 2019

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

PATCH 1/1 dma-buf: Fix memory leak in dma_buf_set_name

This patch fixes a memory leak bug reported by syzbot. Link to the bug is given at [1].

A local variable name is used to hold the copied user buffer string using strndup_user. strndup_user allocates memory using kmalloc_track_caller in memdup_user. This kmalloc allocation needs to be followed by a kfree.

This patch has been tested by a compile test.

[1] https://syzkaller.appspot.com/bug?id=ce692a3aa13e00e335e090be7846c6eb60ddff7a

---
 drivers/dma-buf/dma-buf.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma-buf/dma-buf.c +1 -0
index f45bfb2..9798f6d 100644
@@ -342,6 +342,7 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
 	}
 	kfree(dmabuf->name);
 	dmabuf->name = name;
+	kfree(name);
 
 out_unlock:
 	mutex_unlock(&dmabuf->lock);
Signature · 1 line
2.7.4
#1 lore raw

Hi Bharath,

Thanks for taking the time to try to fix this report.

However, this doesn't look right.

Quoted 23 lines · Bharath Vedartham

This patch fixes a memory leak bug reported by syzbot. Link to the bug is given at [1].

A local variable name is used to hold the copied user buffer string using strndup_user. strndup_user allocates memory using kmalloc_track_caller in memdup_user. This kmalloc allocation needs to be followed by a kfree.

This patch has been tested by a compile test.

[1] https://syzkaller.appspot.com/bug?id=ce692a3aa13e00e335e090be7846c6eb60ddff7a

---
 drivers/dma-buf/dma-buf.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma-buf/dma-buf.c +0 -0
index f45bfb2..9798f6d 100644

drivers/dma-buf/dma-buf.c static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) · line 346

Quoted 5 lines
drivers/dma-buf/dma-buf.c +1 -0
@@ -342,6 +342,7 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
 	}
 	kfree(dmabuf->name);
 	dmabuf->name = name;
+	kfree(name);
 

Just by looking at this, you can deduce something is not right. You are assigning "name" to dmabuf->name, but then releasing "name"!

So now, dmabuf->name has free memory, which will lead to user-after-free issues.

Note also, that this function doesn't look leaky since the previous "name" is freed, before setting a new one.

Maybe the syzbot report is some kind of false positive?

Also, I _strongly_ suggest that in the future you don't compile-test only these kind of not trivial fixes. Since you are touching a crucial part of the kernel here, you should really be testing properly.

Specially since syzbot produces a reproducer.

Consider compile test as something you do when your changes are only cosmetic, and you are completely and absolutely sure things will be OK.

Thanks.
Ezequiel

#2 lore raw
Quoted 51 lines · Ezequiel Garcia

Hi Bharath,

Thanks for taking the time to try to fix this report.

However, this doesn't look right.

Quoted 23 lines · Bharath Vedartham

This patch fixes a memory leak bug reported by syzbot. Link to the bug is given at [1].

A local variable name is used to hold the copied user buffer string using strndup_user. strndup_user allocates memory using kmalloc_track_caller in memdup_user. This kmalloc allocation needs to be followed by a kfree.

This patch has been tested by a compile test.

[1] https://syzkaller.appspot.com/bug?id=ce692a3aa13e00e335e090be7846c6eb60ddff7a

---
 drivers/dma-buf/dma-buf.c | 1 +
 1 file changed, 1 insertion(+)
drivers/dma-buf/dma-buf.c +0 -0
index f45bfb2..9798f6d 100644

drivers/dma-buf/dma-buf.c static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) · line 346

Quoted 5 lines
drivers/dma-buf/dma-buf.c +1 -0
@@ -342,6 +342,7 @@ static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
 	}
 	kfree(dmabuf->name);
 	dmabuf->name = name;
+	kfree(name);
 

Just by looking at this, you can deduce something is not right. You are assigning "name" to dmabuf->name, but then releasing "name"!

So now, dmabuf->name has free memory, which will lead to user-after-free issues.

Note also, that this function doesn't look leaky since the previous "name" is freed, before setting a new one.

Maybe the syzbot report is some kind of false positive?

Also, I _strongly_ suggest that in the future you don't compile-test only these kind of not trivial fixes. Since you are touching a crucial part of the kernel here, you should really be testing properly.

Quoted 8 lines

Specially since syzbot produces a reproducer.

Consider compile test as something you do when your changes are only cosmetic, and you are completely and absolutely sure things will be OK.

Thanks.
Ezequiel

Hi Ezequiel,

Thank you for taking the time to review this.

I made a mistake here and thank you for notifying me of it.

Thank you for your comments, I ll keep them in mind before sending patches to the kernel :)

Thank you
Bharath