Skip to contentSkip to conversation

dma-buf: acquire name lock before read/write dma_buf.name

PATCH Superseded [email protected] 2 participants 7 messages Oct 8, 2021 – Oct 29, 2021

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

PATCH 1/1 dma-buf: acquire name lock before read/write dma_buf.name

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

---
 drivers/dma-buf/dma-buf.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/dma-buf/dma-buf.c +5 -0
index 511fe0d217a0..aebb51b3ff52 100644
@@ -80,7 +80,9 @@ static void dma_buf_release(struct dentry *dentry)
 		dma_resv_fini(dmabuf->resv);
 
 	module_put(dmabuf->owner);
+	spin_lock(&dmabuf->name_lock);
 	kfree(dmabuf->name);
+	spin_unlock(&dmabuf->name_lock);
 	kfree(dmabuf);
 }
 
@@ -1372,6 +1374,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		if (ret)
 			goto error_unlock;
 
+
+		spin_lock(&dmabuf->name_lock);
 		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
 				buf_obj->size,
 				buf_obj->file->f_flags, buf_obj->file->f_mode,
@@ -1379,6 +1383,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 				buf_obj->exp_name,
 				file_inode(buf_obj->file)->i_ino,
 				buf_obj->name ?: "");
+		spin_unlock(&dmabuf->name_lock);
 
 		robj = buf_obj->resv;
 		fence = dma_resv_excl_fence(robj);
Signature · 1 line
2.17.1
maintainer
#1 lore raw

Am 08.10.21 um 08:29 schrieb [email protected]:

Quoted 16 lines

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

---
  drivers/dma-buf/dma-buf.c | 5 +++++
  1 file changed, 5 insertions(+)
drivers/dma-buf/dma-buf.c +0 -0
index 511fe0d217a0..aebb51b3ff52 100644

drivers/dma-buf/dma-buf.c static void dma_buf_release(struct dentry *dentry) · line 85

Quoted 6 lines
drivers/dma-buf/dma-buf.c +2 -0
@@ -80,7 +80,9 @@ static void dma_buf_release(struct dentry *dentry)
  		dma_resv_fini(dmabuf->resv);
 
  	module_put(dmabuf->owner);
+	spin_lock(&dmabuf->name_lock);
  	kfree(dmabuf->name);
+	spin_unlock(&dmabuf->name_lock);

That here is certainly a NAK. This is the release function if somebody is changing the name on a released DMA-buf we have much bigger problems.

Quoted 13 lines
drivers/dma-buf/dma-buf.c +2 -0
  	kfree(dmabuf);
  }
 
@@ -1372,6 +1374,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  		if (ret)
  			goto error_unlock;
 
+
+		spin_lock(&dmabuf->name_lock);
  		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
  				buf_obj->size,
  				buf_obj->file->f_flags, buf_obj->file->f_mode,

drivers/dma-buf/dma-buf.c static int dma_buf_debug_show(struct seq_file *s, void *unused) · line 1386

Quoted 4 lines
drivers/dma-buf/dma-buf.c +1 -0
@@ -1379,6 +1383,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  				buf_obj->exp_name,
  				file_inode(buf_obj->file)->i_ino,
  				buf_obj->name ?: "");
+		spin_unlock(&dmabuf->name_lock);

Yeah, that part looks like a good idea to me as well.

Christian.

Quoted 3 lines
drivers/dma-buf/dma-buf.c +0 -0
  		robj = buf_obj->resv;
  		fence = dma_resv_excl_fence(robj);
#2 lore raw

PATCH v2 1/1 dma-buf: acquire name lock before read/write dma_buf.name

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

---
 drivers/dma-buf/dma-buf.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +3 -0
index 511fe0d217a0..a7f6fd13a635 100644
@@ -1372,6 +1372,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		if (ret)
 			goto error_unlock;
 
+
+		spin_lock(&buf_obj->name_lock);
 		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
 				buf_obj->size,
 				buf_obj->file->f_flags, buf_obj->file->f_mode,
@@ -1379,6 +1381,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 				buf_obj->exp_name,
 				file_inode(buf_obj->file)->i_ino,
 				buf_obj->name ?: "");
+		spin_unlock(&buf_obj->name_lock);
 
 		robj = buf_obj->resv;
 		fence = dma_resv_excl_fence(robj);
Signature · 1 line
2.17.1
maintainer Reviewed-by on v2 1/1
#3 lore raw

Am 08.10.21 um 09:54 schrieb [email protected]:

Quoted 7 lines

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

Going to push that upstream if nobody else objects.

Thanks,
Christian.

Quoted 25 lines
---
  drivers/dma-buf/dma-buf.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +3 -0
index 511fe0d217a0..a7f6fd13a635 100644
@@ -1372,6 +1372,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  		if (ret)
  			goto error_unlock;
 
+
+		spin_lock(&buf_obj->name_lock);
  		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
  				buf_obj->size,
  				buf_obj->file->f_flags, buf_obj->file->f_mode,
@@ -1379,6 +1381,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  				buf_obj->exp_name,
  				file_inode(buf_obj->file)->i_ino,
  				buf_obj->name ?: "");
+		spin_unlock(&buf_obj->name_lock);
 
  		robj = buf_obj->resv;
  		fence = dma_resv_excl_fence(robj);
#4 lore raw

From: Guangming Cao <[email protected]>

Quoted 7 lines

Am 08.10.21 um 09:54 schrieb [email protected]:

Quoted 6 lines

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

Quoted 8 lines
Quoted 1 line

Going to push that upstream if nobody else objects.

Thanks,
Christian.

I'm sorry to disturb you, actually I need this patch to solve our issues. Is there any question about it? seems it hasn't been merged into mainline.

Thanks,
Guangming.

Quoted 26 lines
Quoted 26 lines
---
  drivers/dma-buf/dma-buf.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +3 -0
index 511fe0d217a0..a7f6fd13a635 100644
@@ -1372,6 +1372,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  		if (ret)
  			goto error_unlock;
 
+
+		spin_lock(&buf_obj->name_lock);
  		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
  				buf_obj->size,
  				buf_obj->file->f_flags, buf_obj->file->f_mode,
@@ -1379,6 +1381,7 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
  				buf_obj->exp_name,
  				file_inode(buf_obj->file)->i_ino,
  				buf_obj->name ?: "");
+		spin_unlock(&buf_obj->name_lock);
 
  		robj = buf_obj->resv;
  		fence = dma_resv_excl_fence(robj);
#5 lore raw
In reply to Christian König

From: Guangming Cao <[email protected]>

Quoted 7 lines · Christian König

Am 08.10.21 um 09:54 schrieb [email protected]:

Quoted 6 lines

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

Quoted 8 lines
Quoted 1 line

Going to push that upstream if nobody else objects.

Thanks,
Christian.

Just a gentle ping for this patch, please kindly let me know how is it going.

Quoted 36 lines
Quoted 29 lines
---
  drivers/dma-buf/dma-buf.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +2 -0
index 511fe0d217a0..a7f6fd13a635 100644
@@ -1372,6 +1372,8 @@ static int dma_buf_debug_show(struct seq_file
 *s, void *unused)
  		if (ret)
  			goto error_unlock;
 
+
+		spin_lock(&buf_obj->name_lock);
  		seq_printf(s,
 "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",

buf_obj->size, buf_obj->file->f_flags, buf_obj->file-

Quoted 1 line

f_mode,

drivers/dma-buf/dma-buf.c +1 -0
@@ -1379,6 +1381,7 @@ static int dma_buf_debug_show(struct seq_file
 *s, void *unused)
  				buf_obj->exp_name,
  				file_inode(buf_obj->file)->i_ino,
  				buf_obj->name ?: "");
+		spin_unlock(&buf_obj->name_lock);
 
  		robj = buf_obj->resv;
Quoted 1 line
  		fence = dma_resv_excl_fence(robj);

_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek

maintainer
#6 lore raw

Am 29.10.21 um 04:15 schrieb [email protected]:

Quoted 10 lines

From: Guangming Cao <[email protected]>

Quoted 7 lines · Christian König

Am 08.10.21 um 09:54 schrieb [email protected]:

Quoted 6 lines

From: Guangming Cao <[email protected]>

Because dma-buf.name can be freed in func: "dma_buf_set_name", so, we need to acquire lock first before we read/write dma_buf.name to prevent Use After Free(UAF) issue.

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

Going to push that upstream if nobody else objects.

Thanks,
Christian.

Just a gentle ping for this patch, please kindly let me know how is it going.

Ah, yes. Thanks for the reminder.

I've just pushed this to drm-misc-fixes.

Christian.

Quoted 35 lines
Quoted 35 lines
Quoted 29 lines
---
   drivers/dma-buf/dma-buf.c | 3 +++
   1 file changed, 3 insertions(+)
drivers/dma-buf/dma-buf.c +2 -0
index 511fe0d217a0..a7f6fd13a635 100644
@@ -1372,6 +1372,8 @@ static int dma_buf_debug_show(struct seq_file
 *s, void *unused)
   		if (ret)
   			goto error_unlock;
 
+
+		spin_lock(&buf_obj->name_lock);
   		seq_printf(s,
 "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
   				buf_obj->size,
   				buf_obj->file->f_flags, buf_obj->file-
Quoted 1 line

f_mode,

drivers/dma-buf/dma-buf.c +1 -0
@@ -1379,6 +1381,7 @@ static int dma_buf_debug_show(struct seq_file
 *s, void *unused)
   				buf_obj->exp_name,
   				file_inode(buf_obj->file)->i_ino,
   				buf_obj->name ?: "");
+		spin_unlock(&buf_obj->name_lock);
 
   		robj = buf_obj->resv;
Quoted 1 line
   		fence = dma_resv_excl_fence(robj);

_______________________________________________
Linux-mediatek mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.infradead.org%2Fmailman%2Flistinfo%2Flinux-mediatek&amp;data=04%7C01%7Cchristian.koenig%40amd.com%7C9e95ae08d63d440fc4d108d99a8200c1%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637710705542841586%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=HdiD8%2FX853nQ1vD8n0Qsfv93NaHCCIJF6Pb2rOd%2FLOQ%3D&amp;reserved=0