Skip to contentSkip to conversation

adfs: fix memory leak in sb->s_fs_info

PATCH Superseded Ahmet Eray Karadag 3 participants 9 messages Dec 13, 2025 – Aug 31 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 adfs: fix memory leak in sb->s_fs_info

Syzbot reported a memory leak in adfs during the mount process. The issue arises because the ownership of the allocated (struct adfs_sb_info) is transferred from the filesystem context to the superblock via sget_fc(). This function sets fc->s_fs_info to NULL after the transfer.

The ADFS filesystem previously used the default kill_block_super for superblock destruction. This helper performs generic cleanup but does not free the private sb->s_fs_info data. Since fc->s_fs_info is set to NULL during the transfer, the standard context cleanup (adfs_free_fc) also skips freeing this memory. As a result, if the superblock is destroyed, the allocated struct adfs_sb_info is leaked.

Fix this by implementing a custom .kill_sb callback (adfs_kill_sb) that explicitly frees sb->s_fs_info before invoking the generic kill_block_super.

---
 fs/adfs/super.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
fs/adfs/super.c +10 -1
index fdccdbbfc213..afcd9f6ef350 100644
@@ -462,10 +462,19 @@ static int adfs_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
+static void adfs_kill_sb(struct super_block *sb)
+{
+	struct adfs_sb_info *asb = ADFS_SB(sb);
+
+	kill_block_super(sb);
+
+	kfree(asb);
+}
+
 static struct file_system_type adfs_fs_type = {
 	.owner		= THIS_MODULE,
 	.name		= "adfs",
-	.kill_sb	= kill_block_super,
+	.kill_sb	= adfs_kill_sb,
 	.fs_flags	= FS_REQUIRES_DEV,
 	.init_fs_context = adfs_init_fs_context,
 	.parameters	= adfs_param_spec,
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 12 lines · Ahmet Eray Karadag

Syzbot reported a memory leak in adfs during the mount process. The issue arises because the ownership of the allocated (struct adfs_sb_info) is transferred from the filesystem context to the superblock via sget_fc(). This function sets fc->s_fs_info to NULL after the transfer.

The ADFS filesystem previously used the default kill_block_super for superblock destruction. This helper performs generic cleanup but does not free the private sb->s_fs_info data. Since fc->s_fs_info is set to NULL during the transfer, the standard context cleanup (adfs_free_fc) also skips freeing this memory. As a result, if the superblock is destroyed, the allocated struct adfs_sb_info is leaked.

Quoted 3 lines

Fix this by implementing a custom .kill_sb callback (adfs_kill_sb) that explicitly frees sb->s_fs_info before invoking the generic kill_block_super.

I hate dealing with humans in the way one would deal with a chatbot, but...

Question: if that thing is leaking all the time, why hadn't that been caught earlier?

Question: does it really leak all the time? How would one check that?

Question: if it does not leak in each and every case, presumably the damn thing does get freed at some point; where would that be?

Question: would we, by any chance, run into a double-free with that "fix"?

Please, do yourself a favour and find answers to the questions above. They are fairly trivial and it is the kind of exercise one has to do every time when dealing with something of that sort.

maintainer
#2 lore raw
Quoted 8 lines · Al Viro

Question: if that thing is leaking all the time, why hadn't that been caught earlier?

Question: does it really leak all the time? How would one check that?

Question: if it does not leak in each and every case, presumably the damn thing does get freed at some point; where would that be?

Quoted 6 lines

Question: would we, by any chance, run into a double-free with that "fix"?

Please, do yourself a favour and find answers to the questions above. They are fairly trivial and it is the kind of exercise one has to do every time when dealing with something of that sort.

<spoiler alert>

A trivial experiment would be to mount a valid image, unmount it and see if anything has leaked. Finding a valid image is not hard - the second hit when googling for "ADFS image acorn" is a github-hosted project and right in there there's a directory called "ADFS Test Images", with expected contents. Whether it's legitimate or not, mounting it in a kernel that runs under unpriveleged qemu ought to be safe enough.

And no, it doesn't leak on mount + umount

Looking for places where it could be freed in normal operation is also not terribly hard - looking for kfree() in fs/adfs/super.c catches three hits:

1) in adfs_put_super() we see

	struct adfs_sb_info *asb = ADFS_SB(sb);
	adfs_free_map(sb);
	kfree_rcu(asb, rcu);

2) in the end of adfs_fill_super() there's
error:

        sb->s_fs_info = NULL;
        kfree(asb);
        return ret;

3) in adfs_free_fc() we have

        struct adfs_context *asb = fc->s_fs_info;
        kfree(asb);

#2 and #3 are obviously irrelevant for the case of normal mount + umount - adfs_fill_super() has already run at mount time (and did not hit error:) and so did adfs_free_fc().

So we have #1 to look into. adfs_put_super() is never called directly and it's only reached as a member of struct super_operations adfs_sops - something called 'put_super'. Where would that method be called?

grep and you shall find it:
void generic_shutdown_super(struct super_block *sb)

{
        const struct super_operations *sop = sb->s_op;
 
        if (sb->s_root) {
		...
                if (sop->put_super)
                        sop->put_super(sb);

So it is called by generic_shutdown_super() in case if ->s_root had not been NULL. Looking for callers of generic_shutdown_super() immediately catches
void kill_block_super(struct super_block *sb)

{
        struct block_device *bdev = sb->s_bdev;
 
        generic_shutdown_super(sb);
        if (bdev) {
                sync_blockdev(bdev);
                bdev_fput(sb->s_bdev_file);
        }
}

so it is called by adfs ->kill_sb() - both the current mainline and with that patch.

IOW, there's our double-free. For extra fun, it's not just kfree() + kfree(), it's kfree_rcu() + kfree().

maintainer
#3 lore raw
Quoted 2 lines · Al Viro

IOW, there's our double-free. For extra fun, it's not just kfree() + kfree(), it's kfree_rcu() + kfree().

[sorry, accidentally sent halfway through writing a reply; continued below]

So after successful mount, it gets freed (RCU-delayed) from ->kill_sb() called at fs shutdown.

On adfs_fill_super() failure (hit #2) it is freed on failure exit - with non-delayed kfree().

In case we never got to superblock allocation, the thing gets freed by adfs_free_fc() (also non-delayed).

The gap is between a successful call of sget_fc() and call of adfs_fill_super() (in get_tree_bdev(), which is where adfs_fill_super() is passed as a callback). If setup_bdev_super() fails, we will
* transfer it from fs_context to super_block, so the fs_context destruction
won't have anything to free
* won't free it in never-called adfs_fill_super()
* won't free it in ->kill_sb(), since ->s_root remains NULL and ->put_super()
is never called.

A leak is real, IOW.

Getting ->kill_sb() to do freeing unconditionally would cover the gap. However, to do that, we need to _move_ freeing (RCU-delayed) from adfs_put_super() to adfs_kill_sb(), not just add kfree() in the latter.

What's more, that allows to simplify adfs_fill_super() failure exit: we can leave freeing asb (and clearing ->s_fs_info, of course) to ->kill_sb() - the latter is called on any superblock destruction, including that after failing fill_super() callback. Almost the first thing done by deactivate_locked_super() called in that case is

                fs->kill_sb(s);

So if we go with "have it freed in ->kill_sb()" approach, the solution would be

1) adfs_kill_sb() calling kfree_rcu(asb, rcu) instead of kfree(asb)
2) call of kfree_rcu() removed from adfs_put_super()
3) all goto error; in adfs_fill_super() becoming return ret; (and error:
getting removed, that is)

#4 lore raw

Hi Al,

I apologize for overlooking the double-free scenario in the success path. I focused too narrowly on the failure case provided by the reproducer and failed to verify the fix against the normal lifecycle.

As a newcomer to kernel development, I truly appreciate you taking the time to guide me through this analysis. It is a valuable lesson for me on looking at the broader lifecycle rather than just the immediate bug.

I will implement the changes you suggested in v2.

Thank you for your patience and the detailed explanation.

Best regards,
Ahmet Eray

Quoted 45 lines · Al Viro
Quoted 2 lines · Al Viro

IOW, there's our double-free. For extra fun, it's not just kfree() + kfree(), it's kfree_rcu() + kfree().

[sorry, accidentally sent halfway through writing a reply; continued below]

So after successful mount, it gets freed (RCU-delayed) from ->kill_sb() called at fs shutdown.

On adfs_fill_super() failure (hit #2) it is freed on failure exit - with non-delayed kfree().

In case we never got to superblock allocation, the thing gets freed by adfs_free_fc() (also non-delayed).

The gap is between a successful call of sget_fc() and call of adfs_fill_super() (in get_tree_bdev(), which is where adfs_fill_super() is passed as a callback). If setup_bdev_super() fails, we will
* transfer it from fs_context to super_block, so the fs_context destruction
won't have anything to free
* won't free it in never-called adfs_fill_super()
* won't free it in ->kill_sb(), since ->s_root remains NULL and ->put_super()
is never called.

A leak is real, IOW.

Getting ->kill_sb() to do freeing unconditionally would cover the gap. However, to do that, we need to _move_ freeing (RCU-delayed) from adfs_put_super() to adfs_kill_sb(), not just add kfree() in the latter.

What's more, that allows to simplify adfs_fill_super() failure exit: we can leave freeing asb (and clearing ->s_fs_info, of course) to ->kill_sb() - the latter is called on any superblock destruction, including that after failing fill_super() callback. Almost the first thing done by deactivate_locked_super() called in that case is

                fs->kill_sb(s);

So if we go with "have it freed in ->kill_sb()" approach, the solution would be

1) adfs_kill_sb() calling kfree_rcu(asb, rcu) instead of kfree(asb)
2) call of kfree_rcu() removed from adfs_put_super()
3) all goto error; in adfs_fill_super() becoming return ret; (and error:
getting removed, that is)

#5 lore raw

PATCH v2 1/1 adfs: fix memory leak in sb->s_fs_info

In reply to Ahmet Eray Karadag

Syzbot reported a memory leak in adfs during the mount process. The issue arises because the ownership of the allocated (struct adfs_sb_info) is transferred from the filesystem context to the superblock via sget_fc(). This function sets fc->s_fs_info to NULL after the transfer.

The ADFS filesystem previously used the default kill_block_super for superblock destruction. This helper performs generic cleanup but does not free the private sb->s_fs_info data. Since fc->s_fs_info is set to NULL during the transfer, the standard context cleanup (adfs_free_fc) also skips freeing this memory. As a result, if the superblock is destroyed, the allocated struct adfs_sb_info is leaked.

Fix this by implementing a custom .kill_sb callback (adfs_kill_sb) that explicitly frees sb->s_fs_info before invoking the generic kill_block_super.

---
v2:
- Remove adfs_put_super
- Remove error label in adfs_fill_super
- Use kfree_rcu instead kfree
- Free map in adfs_kill_sb
- Tested with ADFS test images

---
 fs/adfs/super.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)
fs/adfs/super.c +14 -18
index fdccdbbfc213..96855f1086cd 100644
@@ -90,14 +90,6 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
 	return 0;
 }
 
-static void adfs_put_super(struct super_block *sb)
-{
-	struct adfs_sb_info *asb = ADFS_SB(sb);
-
-	adfs_free_map(sb);
-	kfree_rcu(asb, rcu);
-}
-
 static int adfs_show_options(struct seq_file *seq, struct dentry *root)
 {
 	struct adfs_sb_info *asb = ADFS_SB(root->d_sb);
@@ -246,7 +238,6 @@ static const struct super_operations adfs_sops = {
 	.free_inode	= adfs_free_inode,
 	.drop_inode	= adfs_drop_inode,
 	.write_inode	= adfs_write_inode,
-	.put_super	= adfs_put_super,
 	.statfs		= adfs_statfs,
 	.show_options	= adfs_show_options,
 };
@@ -362,7 +353,7 @@ static int adfs_fill_super(struct super_block *sb, struct fs_context *fc)
 		ret = -EINVAL;
 	}
 	if (ret)
-		goto error;
+		return ret;
 
 	/* set up enough so that we can read an inode */
 	sb->s_op = &adfs_sops;
@@ -403,15 +394,9 @@ static int adfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (!sb->s_root) {
 		adfs_free_map(sb);
 		adfs_error(sb, "get root inode failed\n");
-		ret = -EIO;
-		goto error;
+		return -EIO;
 	}
 	return 0;
-
-error:
-	sb->s_fs_info = NULL;
-	kfree(asb);
-	return ret;
 }
 
 static int adfs_get_tree(struct fs_context *fc)
@@ -462,10 +447,21 @@ static int adfs_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
+static void adfs_kill_sb(struct super_block *sb)
+{
+	struct adfs_sb_info *asb = ADFS_SB(sb);
+
+	kill_block_super(sb);
+
+	adfs_free_map(sb);
+
+	kfree_rcu(asb, rcu);
+}
+
 static struct file_system_type adfs_fs_type = {
 	.owner		= THIS_MODULE,
 	.name		= "adfs",
-	.kill_sb	= kill_block_super,
+	.kill_sb	= adfs_kill_sb,
 	.fs_flags	= FS_REQUIRES_DEV,
 	.init_fs_context = adfs_init_fs_context,
 	.parameters	= adfs_param_spec,
Signature · 1 line
2.43.0
maintainer
#6 lore raw

fs/adfs/super.c static int adfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 397

Quoted 7 lines · Ahmet Eray Karadag
fs/adfs/super.c +1 -2
@@ -403,15 +394,9 @@ static int adfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (!sb->s_root) {
 		adfs_free_map(sb);
 		adfs_error(sb, "get root inode failed\n");
-		ret = -EIO;
-		goto error;
+		return -EIO;

Double-free again, this time of asb->s_map - adfs_free_map() is called twice in that case, once here and once in adfs_kill_sb()...

fs/adfs/super.c static int adfs_init_fs_context(struct fs_context *fc) · line 456

Quoted 7 lines
fs/adfs/super.c +7 -0
+static void adfs_kill_sb(struct super_block *sb)
+{
+	struct adfs_sb_info *asb = ADFS_SB(sb);
+
+	kill_block_super(sb);
+
+	adfs_free_map(sb);

... and calling adfs_map_relse(), which calls brelse() after the block device has been closed. IOW, unlike freeing the ->s_fs_info this can't be done after kill_block_super(). And with this one we don't have that kind of gap - it doesn't exist until adfs_fill_super(), so there's no leak to be had anyway. Yes, it would be possible to get rid of more of cleanup on failure exit there, but it would require expanding kill_block_super() and inserting adfs_free_map() call before the call sync_blockdev() in there - more headache than you win in adfs_fill_super(), IMO.

Leave adfs_free_map() in adfs_put_super() (or make _it_ ->put_super(), for that matter).

#7 lore raw

PATCH v3 1/1 adfs: fix memory leak in sb->s_fs_info

In reply to Ahmet Eray Karadag

Syzbot reported a memory leak in adfs during the mount process. The issue arises because the ownership of the allocated (struct adfs_sb_info) is transferred from the filesystem context to the superblock via sget_fc(). This function sets fc->s_fs_info to NULL after the transfer.

The ADFS filesystem previously used the default kill_block_super for superblock destruction. This helper performs generic cleanup but does not free the private sb->s_fs_info data. Since fc->s_fs_info is set to NULL during the transfer, the standard context cleanup (adfs_free_fc) also skips freeing this memory. As a result, if the superblock is destroyed, the allocated struct adfs_sb_info is leaked.

Fix this by implementing a custom .kill_sb callback (adfs_kill_sb) that explicitly frees sb->s_fs_info before invoking the generic kill_block_super.

---
v2:
- Remove adfs_put_super
- Remove error label in adfs_fill_super
- Use kfree_rcu instead kfree
- Free map in adfs_kill_sb
- Tested with ADFS test images
---
v3:
- Restore adfs_put_super() to handle map cleanup
- Moving map cleanup to kill_sb() caused a double-free

---
 fs/adfs/super.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
fs/adfs/super.c +12 -12
index fdccdbbfc213..51bf4652422d 100644
@@ -92,10 +92,7 @@ static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
 
 static void adfs_put_super(struct super_block *sb)
 {
-	struct adfs_sb_info *asb = ADFS_SB(sb);
-
 	adfs_free_map(sb);
-	kfree_rcu(asb, rcu);
 }
 
 static int adfs_show_options(struct seq_file *seq, struct dentry *root)
@@ -362,7 +359,7 @@ static int adfs_fill_super(struct super_block *sb, struct fs_context *fc)
 		ret = -EINVAL;
 	}
 	if (ret)
-		goto error;
+		return ret;
 
 	/* set up enough so that we can read an inode */
 	sb->s_op = &adfs_sops;
@@ -403,15 +400,9 @@ static int adfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (!sb->s_root) {
 		adfs_free_map(sb);
 		adfs_error(sb, "get root inode failed\n");
-		ret = -EIO;
-		goto error;
+		return -EIO;
 	}
 	return 0;
-
-error:
-	sb->s_fs_info = NULL;
-	kfree(asb);
-	return ret;
 }
 
 static int adfs_get_tree(struct fs_context *fc)
@@ -462,10 +453,19 @@ static int adfs_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
+static void adfs_kill_sb(struct super_block *sb)
+{
+	struct adfs_sb_info *asb = ADFS_SB(sb);
+
+	kill_block_super(sb);
+
+	kfree_rcu(asb, rcu);
+}
+
 static struct file_system_type adfs_fs_type = {
 	.owner		= THIS_MODULE,
 	.name		= "adfs",
-	.kill_sb	= kill_block_super,
+	.kill_sb	= adfs_kill_sb,
 	.fs_flags	= FS_REQUIRES_DEV,
 	.init_fs_context = adfs_init_fs_context,
 	.parameters	= adfs_param_spec,
Signature · 1 line
2.43.0
maintainer Applied
#8 lore raw
Quoted 1 line · Ahmet Eray Karadag

adfs: fix memory leak in sb->s_fs_info

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] adfs: fix memory leak in sb->s_fs_info

      https://git.kernel.org/vfs/vfs/c/b12e20c6ac15