[PATCH v2] efivarfs: Fix memory leak of efivarfs_fs_info in fs_context error paths

Breno Leitao posted 1 patch 2 months, 3 weeks ago
fs/efivarfs/super.c | 6 ++++++
1 file changed, 6 insertions(+)
[PATCH v2] efivarfs: Fix memory leak of efivarfs_fs_info in fs_context error paths
Posted by Breno Leitao 2 months, 3 weeks ago
When processing mount options, efivarfs allocates efivarfs_fs_info (sfi)
early in fs_context initialization. However, sfi is associated with the
superblock and typically freed when the superblock is destroyed. If the
fs_context is released (final put) before fill_super is called—such as
on error paths or during reconfiguration—the sfi structure would leak,
as ownership never transfers to the superblock.

Implement the .free callback in efivarfs_context_ops to ensure any
allocated sfi is properly freed if the fs_context is torn down before
fill_super, preventing this memory leak.

Suggested-by: James Bottomley <James.Bottomley@HansenPartnership.com>
Fixes: 5329aa5101f73c ("efivarfs: Add uid/gid mount options")
Signed-off-by: Breno Leitao <leitao@debian.org>
---
Changes in v2:
- instead of silenting the warning, just fix the problem.
- Link to v1: https://lore.kernel.org/r/20250715-kmemleak_efi-v1-1-c07e68c76ae8@debian.org
---
 fs/efivarfs/super.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
index c900d98bf4945..284d6dbba2ece 100644
--- a/fs/efivarfs/super.c
+++ b/fs/efivarfs/super.c
@@ -390,10 +390,16 @@ static int efivarfs_reconfigure(struct fs_context *fc)
 	return 0;
 }
 
+static void efivarfs_free(struct fs_context *fc)
+{
+	kfree(fc->s_fs_info);
+}
+
 static const struct fs_context_operations efivarfs_context_ops = {
 	.get_tree	= efivarfs_get_tree,
 	.parse_param	= efivarfs_parse_param,
 	.reconfigure	= efivarfs_reconfigure,
+	.free		= efivarfs_free,
 };
 
 static int efivarfs_check_missing(efi_char16_t *name16, efi_guid_t vendor,

---
base-commit: 8c2e52ebbe885c7eeaabd3b7ddcdc1246fc400d2
change-id: 20250714-kmemleak_efi-bedfe48a304d

Best regards,
--  
Breno Leitao <leitao@debian.org>

Re: [PATCH v2] efivarfs: Fix memory leak of efivarfs_fs_info in fs_context error paths
Posted by Ard Biesheuvel 2 months, 2 weeks ago
On Thu, 17 Jul 2025 at 01:23, Breno Leitao <leitao@debian.org> wrote:
>
> When processing mount options, efivarfs allocates efivarfs_fs_info (sfi)
> early in fs_context initialization. However, sfi is associated with the
> superblock and typically freed when the superblock is destroyed. If the
> fs_context is released (final put) before fill_super is called—such as
> on error paths or during reconfiguration—the sfi structure would leak,
> as ownership never transfers to the superblock.
>
> Implement the .free callback in efivarfs_context_ops to ensure any
> allocated sfi is properly freed if the fs_context is torn down before
> fill_super, preventing this memory leak.
>
> Suggested-by: James Bottomley <James.Bottomley@HansenPartnership.com>
> Fixes: 5329aa5101f73c ("efivarfs: Add uid/gid mount options")
> Signed-off-by: Breno Leitao <leitao@debian.org>
> ---
> Changes in v2:
> - instead of silenting the warning, just fix the problem.
> - Link to v1: https://lore.kernel.org/r/20250715-kmemleak_efi-v1-1-c07e68c76ae8@debian.org
> ---
>  fs/efivarfs/super.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>

Queued as a fix - thanks.



> diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
> index c900d98bf4945..284d6dbba2ece 100644
> --- a/fs/efivarfs/super.c
> +++ b/fs/efivarfs/super.c
> @@ -390,10 +390,16 @@ static int efivarfs_reconfigure(struct fs_context *fc)
>         return 0;
>  }
>
> +static void efivarfs_free(struct fs_context *fc)
> +{
> +       kfree(fc->s_fs_info);
> +}
> +
>  static const struct fs_context_operations efivarfs_context_ops = {
>         .get_tree       = efivarfs_get_tree,
>         .parse_param    = efivarfs_parse_param,
>         .reconfigure    = efivarfs_reconfigure,
> +       .free           = efivarfs_free,
>  };
>
>  static int efivarfs_check_missing(efi_char16_t *name16, efi_guid_t vendor,
>
> ---
> base-commit: 8c2e52ebbe885c7eeaabd3b7ddcdc1246fc400d2
> change-id: 20250714-kmemleak_efi-bedfe48a304d
>
> Best regards,
> --
> Breno Leitao <leitao@debian.org>
>