Skip to content

Commit 28c7980

Browse files
committed
Merge tag 'v6.5/vfs.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
Pull vfs fix from Christian Brauner: "A fix for the backing file work from this cycle. When init_file() failed it would call file_free_rcu() on the file allocated by the caller of init_file(). It naively assumed that the correct cleanup operation would be called depending on whether it is a regular file or a backing file. However, that presupposes that the FMODE_BACKING flag would already be set which it won't be as that is done in the caller of init_file(). Fix that bug by moving the cleanup of the allocated file into the caller where it belongs in the first place. There's no good reason for init_file() to consume resources it didn't allocate. This is a mainline only fix and was reported by syzbot. The fix was validated by syzbot against the provided reproducer" * tag 'v6.5/vfs.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs: fs: move cleanup from init_file() into its callers
2 parents 5def00c + dff745c commit 28c7980

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

fs/file_table.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static int init_file(struct file *f, int flags, const struct cred *cred)
160160
f->f_cred = get_cred(cred);
161161
error = security_file_alloc(f);
162162
if (unlikely(error)) {
163-
file_free_rcu(&f->f_rcuhead);
163+
put_cred(f->f_cred);
164164
return error;
165165
}
166166

@@ -208,8 +208,10 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
208208
return ERR_PTR(-ENOMEM);
209209

210210
error = init_file(f, flags, cred);
211-
if (unlikely(error))
211+
if (unlikely(error)) {
212+
kmem_cache_free(filp_cachep, f);
212213
return ERR_PTR(error);
214+
}
213215

214216
percpu_counter_inc(&nr_files);
215217

@@ -240,8 +242,10 @@ struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
240242
return ERR_PTR(-ENOMEM);
241243

242244
error = init_file(f, flags, cred);
243-
if (unlikely(error))
245+
if (unlikely(error)) {
246+
kmem_cache_free(filp_cachep, f);
244247
return ERR_PTR(error);
248+
}
245249

246250
f->f_mode |= FMODE_NOACCOUNT;
247251

@@ -265,8 +269,10 @@ struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
265269
return ERR_PTR(-ENOMEM);
266270

267271
error = init_file(&ff->file, flags, cred);
268-
if (unlikely(error))
272+
if (unlikely(error)) {
273+
kfree(ff);
269274
return ERR_PTR(error);
275+
}
270276

271277
ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
272278
return &ff->file;

0 commit comments

Comments
 (0)