[PATCH] fs: fix memory leak in automount_add()

Ahmad Fatoum a.fatoum at barebox.org
Thu May 7 04:54:07 PDT 2026


The struct automount is allocated before validating the path, but both
error paths (filename_lookup failure and !d_is_dir check) skip freeing
it. Move the allocation after validation succeeds.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 fs/fs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/fs.c b/fs/fs.c
index 5ca98ec12704..dc6c30802d89 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -3450,7 +3450,7 @@ EXPORT_SYMBOL(automount_remove);
 
 int automount_add(const char *pathname, const char *cmd)
 {
-	struct automount *am = xzalloc(sizeof(*am));
+	struct automount *am;
 	struct path path;
 	int ret;
 
@@ -3463,6 +3463,7 @@ int automount_add(const char *pathname, const char *cmd)
 		goto out;
 	}
 
+	am = xzalloc(sizeof(*am));
 	am->path = dpath(path.dentry, d_root);
 	am->dentry = dget(path.dentry);
 	am->dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
-- 
2.47.3




More information about the barebox mailing list