btrfs: ioctl: Fix memory leak on duplicated memory
PATCH 1/1 btrfs: ioctl: Fix memory leak on duplicated memory
On 'btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.
--- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
fs/btrfs/ioctl.c +2 -1
@@ -3740,7 +3740,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) prealloc =kzalloc (sizeof (*prealloc),GFP_KERNEL );if (!prealloc) { ret = -ENOMEM ; - goto drop_write; + goto out_sa_drop_write; } }
@@ -3775,6 +3775,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) out:kfree (prealloc); +out_sa_drop_write:kfree (sa); drop_write:mnt_drop_write_file (file);
Signature · 1 line
2.51.0
Quoted 8 lines · Miquel Sabaté Solà
On '
btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.Signed-off-by Miquel Sabaté Solà <[email protected]>
LGTM, thanks for the fix!
One thing though: I don't like the label names. I think with multiple cleanups the best way is to name each label with the cleanup it is for. Once you have some named ones, "out" feels unspecific, and encoding every single action like "out_sa_drop_write" doesn't scale as you add more cleanups, so it's just not a useful pattern. It's already quite clunky with just two.
If you fixup the names, you can add:
Quoted 18 lines
--- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/ioctl.c +1
-1@@ -3740,7 +3740,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) prealloc =kzalloc (sizeof (*prealloc),GFP_KERNEL );if (!prealloc) { ret = -ENOMEM ; - goto drop_write; + goto out_sa_drop_write; } }
fs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3776
Quoted 2 lines
fs/btrfs/ioctl.c +0
-0@@ -3775,6 +3775,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) out:
call this free_prealloc
fs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3778
Quoted 2 lines
fs/btrfs/ioctl.c +1
-0kfree (prealloc); +out_sa_drop_write:
and this one free_args
Quoted 6 lines
fs/btrfs/ioctl.c +0
-0kfree (sa); drop_write:mnt_drop_write_file (file);Signature · 1 line
2.51.0
Quoted 10 lines · Boris Burkov
Quoted 8 lines · Miquel Sabaté Solà
On '
btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.Signed-off-by Miquel Sabaté Solà <[email protected]>
Quoted 8 lines
LGTM, thanks for the fix!
One thing though: I don't like the label names. I think with multiple cleanups the best way is to name each label with the cleanup it is for. Once you have some named ones, "out" feels unspecific, and encoding every single action like "
out_sa_drop_write" doesn't scale as you add more cleanups, so it's just not a useful pattern. It's already quite clunky with just two.
The patch is adding a new label and it follows the pattern we use elsewhere, with "out_<what>" pattern. The standalone 'out' is there and I agree it should be named like 'out_free_prealloc' or such but it's in the original code and it's been there for a long time. Cleaning that up is for another patch.
Quoted 43 lines · Boris Burkov
Quoted 8 lines · Miquel Sabaté Solà
On '
btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.Signed-off-by Miquel Sabaté Solà <[email protected]>LGTM, thanks for the fix!
One thing though: I don't like the label names. I think with multiple cleanups the best way is to name each label with the cleanup it is for. Once you have some named ones, "out" feels unspecific, and encoding every single action like "
out_sa_drop_write" doesn't scale as you add more cleanups, so it's just not a useful pattern. It's already quite clunky with just two.If you fixup the names, you can add:
Reviewed-by Boris Burkov <[email protected]>Quoted 19 lines
--- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/ioctl.c +1
-1@@ -3740,7 +3740,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) prealloc =kzalloc (sizeof (*prealloc),GFP_KERNEL );if (!prealloc) { ret = -ENOMEM ; - goto drop_write; + goto out_sa_drop_write; } }@@ -3775,6 +3775,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
Quoted 8 lines
fs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3776
Quoted 1 line
fs/btrfs/ioctl.c +0
-0out:call this
free_preallocfs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3778
Quoted 2 lines
fs/btrfs/ioctl.c +1
-0kfree (prealloc); +out_sa_drop_write:and this one
free_args
Rather than adding yet one more label, which over time has proven error prone, I'd rather have a single label.
Just the existing 'out' label and then the fix would be to replace the
goto drop_write;
with
goto out;
kfree() against a NULL pointer is safe.
Also, missing a Fixes tag which should be:
a relation")
Thanks.
Quoted 8 lines
Quoted 7 lines
fs/btrfs/ioctl.c +0
-1kfree (sa); drop_write:mnt_drop_write_file (file); --2.51.0
Filipe Manana @ 2025-09-25 18:48 +01:
Quoted 58 lines · Boris Burkov
Quoted 43 lines · Miquel Sabaté Solà
Quoted 8 lines
On '
btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.Signed-off-by Miquel Sabaté Solà <[email protected]>LGTM, thanks for the fix!
One thing though: I don't like the label names. I think with multiple cleanups the best way is to name each label with the cleanup it is for. Once you have some named ones, "out" feels unspecific, and encoding every single action like "
out_sa_drop_write" doesn't scale as you add more cleanups, so it's just not a useful pattern. It's already quite clunky with just two.If you fixup the names, you can add:
Reviewed-by Boris Burkov <[email protected]>Quoted 19 lines
--- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/ioctl.c +1
-1@@ -3740,7 +3740,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) prealloc =kzalloc (sizeof (*prealloc),GFP_KERNEL );if (!prealloc) { ret = -ENOMEM ; - goto drop_write; + goto out_sa_drop_write; } }@@ -3775,6 +3775,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)Quoted 8 lines
fs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3776
Quoted 1 line
fs/btrfs/ioctl.c +0
-0out:call this
free_preallocfs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3778
Quoted 2 lines
fs/btrfs/ioctl.c +1
-0kfree (prealloc); +out_sa_drop_write:and this one
free_argsRather than adding yet one more label, which over time has proven error prone, I'd rather have a single label.
Just the existing 'out' label and then the fix would be to replace the
Quoted 7 lines
goto
drop_write;with
goto out;
kfree()against a NULL pointer is safe.
I wanted to keep it simple and just fix the issue at hand. Actually I found out about this as part of a larger refactoring involving cleanup functions [1], which would fix the amount of labels as well.
Hence, as David mentions on another email, I would handle cleaning up the amount of labels as part of another series.
Quoted 5 lines
Also, missing a Fixes tag which should be:
Fixes 4addc1ffd67a ("btrfs: qgroup: preallocate memory before addinga relation")
I will add it as part of v2, thanks!
Quoted 11 lines
Thanks.
Quoted 8 lines
Quoted 7 lines
fs/btrfs/ioctl.c +0
-1kfree (sa); drop_write:mnt_drop_write_file (file); --2.51.0
Thanks for the review,
Miquel
Miquel Sabaté Solà @ 2025-09-25 20:26 +02:
Quoted 64 lines
Filipe Manana @ 2025-09-25 18:48 +01:
Quoted 62 lines · Boris Burkov
Quoted 43 lines · Miquel Sabaté Solà
Quoted 8 lines
On '
btrfs_ioctl_qgroup_assign' we first duplicate the argument as provided by the user, which is kfree'd in the end. But this was not the case when allocating memory for 'prealloc'. In this case, if it somehow failed, then the previous code would go directly into calling 'mnt_drop_write_file', without freeing the string duplicated from the user space.Signed-off-by Miquel Sabaté Solà <[email protected]>LGTM, thanks for the fix!
One thing though: I don't like the label names. I think with multiple cleanups the best way is to name each label with the cleanup it is for. Once you have some named ones, "out" feels unspecific, and encoding every single action like "
out_sa_drop_write" doesn't scale as you add more cleanups, so it's just not a useful pattern. It's already quite clunky with just two.If you fixup the names, you can add:
Reviewed-by Boris Burkov <[email protected]>Quoted 19 lines
--- fs/btrfs/ioctl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)fs/btrfs/ioctl.c +1
-1@@ -3740,7 +3740,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) prealloc =kzalloc (sizeof (*prealloc),GFP_KERNEL );if (!prealloc) { ret = -ENOMEM ; - goto drop_write; + goto out_sa_drop_write; } }@@ -3775,6 +3775,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)Quoted 8 lines
fs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3776
Quoted 1 line
fs/btrfs/ioctl.c +0
-0out:call this
free_preallocfs/btrfs/ioctl.c static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) · line 3778
Quoted 2 lines
fs/btrfs/ioctl.c +1
-0kfree (prealloc); +out_sa_drop_write:and this one
free_argsRather than adding yet one more label, which over time has proven error prone, I'd rather have a single label.
Just the existing 'out' label and then the fix would be to replace thegoto
drop_write;with
Quoted 7 lines
Quoted 3 lines
goto out;
kfree()against a NULL pointer is safe.I wanted to keep it simple and just fix the issue at hand. Actually I found out about this as part of a larger refactoring involving cleanup functions [1], which would fix the amount of labels as well.
I clearly read too fast here. I applied your suggestion for v2. Sorry for the noise!
Quoted 28 lines
Hence, as David mentions on another email, I would handle cleaning up the amount of labels as part of another series.
Quoted 5 lines
Also, missing a Fixes tag which should be:
Fixes 4addc1ffd67a ("btrfs: qgroup: preallocate memory before addinga relation")
I will add it as part of v2, thanks!
Quoted 11 lines
Thanks.
Quoted 8 lines
Quoted 7 lines
fs/btrfs/ioctl.c +0
-1kfree (sa); drop_write:mnt_drop_write_file (file); --2.51.0
Thanks for the review,
Miquel