Skip to content

Commit

Permalink
Merge tag 'v6.5/vfs.fixes' of git://git.kernel.org/pub/scm/linux/kern…
Browse files Browse the repository at this point in the history
…el/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
  • Loading branch information
Linus Torvalds committed Jul 2, 2023
2 parents 5def00c + dff745c commit 28c7980
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 28c7980

Please sign in to comment.