Skip to contentSkip to conversation

fs: fix invalid-free in init_file()

PATCH Applied Amir Goldstein 2 participants 2 messages Jul 1, 2023 – Jul 2, 2023 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: fix invalid-free in init_file()

The use of file_free_rcu() in init_file() to free the struct that was allocated by the caller was hacky and we got what we desreved.

Let init_file() and its callers take care of cleaning up each after their own allocated resources on error.

Reported-by [email protected]
Fixes 62d53c4a1dfe ("fs: use backing_file container for internal files with "fake" f_path")
---
 fs/file_table.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
fs/file_table.c +10 -4
index e06c68e2d757..fc7d677ff5ad 100644
@@ -160,7 +160,7 @@ static int init_file(struct file *f, int flags, const struct cred *cred)
 	f->f_cred = get_cred(cred);
 	error = security_file_alloc(f);
 	if (unlikely(error)) {
-		file_free_rcu(&f->f_rcuhead);
+		put_cred(f->f_cred);
 		return error;
 	}
 
@@ -208,8 +208,10 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
 		return ERR_PTR(-ENOMEM);
 
 	error = init_file(f, flags, cred);
-	if (unlikely(error))
+	if (unlikely(error)) {
+		kmem_cache_free(filp_cachep, f);
 		return ERR_PTR(error);
+	}
 
 	percpu_counter_inc(&nr_files);
 
@@ -240,8 +242,10 @@ struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
 		return ERR_PTR(-ENOMEM);
 
 	error = init_file(f, flags, cred);
-	if (unlikely(error))
+	if (unlikely(error)) {
+		kmem_cache_free(filp_cachep, f);
 		return ERR_PTR(error);
+	}
 
 	f->f_mode |= FMODE_NOACCOUNT;
 
@@ -265,8 +269,10 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
 		return ERR_PTR(-ENOMEM);
 
 	error = init_file(&ff->file, flags, cred);
-	if (unlikely(error))
+	if (unlikely(error)) {
+		kfree(ff);
 		return ERR_PTR(error);
+	}
 
 	ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
 	return &ff->file;
Signature · 1 line
2.34.1
maintainer Applied
#1 lore raw
Quoted 8 lines · Amir Goldstein

The use of file_free_rcu() in init_file() to free the struct that was allocated by the caller was hacky and we got what we desreved.

Let init_file() and its callers take care of cleaning up each after their own allocated resources on error.

[...]

Thanks for the fix! I'll send a pr shortly.

---

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] fs: move cleanup from init_file() into its callers

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