Skip to contentSkip to conversation

configfs: fix memleak in configfs_release_bin_file

PATCH In review Chung-Chiang Cheng 2 participants 4 messages Jun 18, 2021 – Jun 22, 2021

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

PATCH 1/1 configfs: fix memleak in configfs_release_bin_file

When reading binary attributes in progress, buffer->bin_buffer is setup in configfs_read_bin_file() but never freed.

Fixes 03607ace807b4 ("configfs: implement binary attributes")
---
 fs/configfs/file.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/configfs/file.c +7 -3
index e26060dae70a..cdd23f4a51c8 100644
@@ -466,9 +466,13 @@ static int configfs_release_bin_file(struct inode *inode, struct file *file)
 {
 	struct configfs_buffer *buffer = file->private_data;
 
-	buffer->read_in_progress = false;
-
-	if (buffer->write_in_progress) {
+	if (buffer->read_in_progress) {
+		buffer->read_in_progress = false;
+		vfree(buffer->bin_buffer);
+		buffer->bin_buffer = NULL;
+		buffer->bin_buffer_size = 0;
+		buffer->needs_read_fill = 1;
+	} else if (buffer->write_in_progress) {
 		struct configfs_fragment *frag = to_frag(file);
 		buffer->write_in_progress = false;
 
Signature · 1 line
2.25.1
#1 lore raw

Hmm. The issue looks real, but I think we should just call the vfree unconditionally given that the buffer structure is zeroed on allocation and freed just after, and also remove the pointless clearing of all the flags. Does something like this work for you?

fs/configfs/file.c +1 -8
index 53913b84383a..1ab6afb84f04 100644
@@ -393,11 +393,8 @@ static int configfs_release_bin_file(struct inode *inode, struct file *file)
 {
 	struct configfs_buffer *buffer = file->private_data;
 
-	buffer->read_in_progress = false;
-
 	if (buffer->write_in_progress) {
 		struct configfs_fragment *frag = to_frag(file);
-		buffer->write_in_progress = false;
 
 		down_read(&frag->frag_sem);
 		if (!frag->frag_dead) {
@@ -407,13 +404,9 @@ static int configfs_release_bin_file(struct inode *inode, struct file *file)
 					buffer->bin_buffer_size);
 		}
 		up_read(&frag->frag_sem);
-		/* vfree on NULL is safe */
-		vfree(buffer->bin_buffer);
-		buffer->bin_buffer = NULL;
-		buffer->bin_buffer_size = 0;
-		buffer->needs_read_fill = 1;
 	}
 
+	vfree(buffer->bin_buffer);
 	configfs_release(inode, file);
 	return 0;
 }
#2 lore raw

It works for me. I've verified it with ACPI configfs that is the only one using configfs binary attributes so far, and the memleak issue is solved.

Quoted 37 lines · Christoph Hellwig

Hmm. The issue looks real, but I think we should just call the vfree unconditionally given that the buffer structure is zeroed on allocation and freed just after, and also remove the pointless clearing of all the flags. Does something like this work for you?

fs/configfs/file.c +1 -8
index 53913b84383a..1ab6afb84f04 100644
@@ -393,11 +393,8 @@ static int configfs_release_bin_file(struct inode *inode, struct file *file)
 {
        struct configfs_buffer *buffer = file->private_data;
 
-       buffer->read_in_progress = false;
-
        if (buffer->write_in_progress) {
                struct configfs_fragment *frag = to_frag(file);
-               buffer->write_in_progress = false;
 
                down_read(&frag->frag_sem);
                if (!frag->frag_dead) {
@@ -407,13 +404,9 @@ static int configfs_release_bin_file(struct inode *inode, struct file *file)
                                        buffer->bin_buffer_size);
                }
                up_read(&frag->frag_sem);
-               /* vfree on NULL is safe */
-               vfree(buffer->bin_buffer);
-               buffer->bin_buffer = NULL;
-               buffer->bin_buffer_size = 0;
-               buffer->needs_read_fill = 1;
        }
 
+       vfree(buffer->bin_buffer);
        configfs_release(inode, file);
        return 0;
 }