Skip to content

Commit

Permalink
btrfs: fix fallocate to use file_modified to update permissions consi…
Browse files Browse the repository at this point in the history
…stently

Since the initial introduction of (posix) fallocate back at the turn of
the century, it has been possible to use this syscall to change the
user-visible contents of files.  This can happen by extending the file
size during a preallocation, or through any of the newer modes (punch,
zero range).  Because the call can be used to change file contents, we
should treat it like we do any other modification to a file -- update
the mtime, and drop set[ug]id privileges/capabilities.

The VFS function file_modified() does all this for us if pass it a
locked inode, so let's make fallocate drop permissions correctly.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Darrick J. Wong authored and David Sterba committed Mar 24, 2022
1 parent bbac586 commit 05fd956
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions fs/btrfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -2957,8 +2957,9 @@ int btrfs_replace_file_extents(struct btrfs_inode *inode,
return ret;
}

static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
static int btrfs_punch_hole(struct file *file, loff_t offset, loff_t len)
{
struct inode *inode = file_inode(file);
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
struct btrfs_root *root = BTRFS_I(inode)->root;
struct extent_state *cached_state = NULL;
Expand Down Expand Up @@ -2990,6 +2991,10 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
goto out_only_mutex;
}

ret = file_modified(file);
if (ret)
goto out_only_mutex;

lockstart = round_up(offset, btrfs_inode_sectorsize(BTRFS_I(inode)));
lockend = round_down(offset + len,
btrfs_inode_sectorsize(BTRFS_I(inode))) - 1;
Expand Down Expand Up @@ -3430,7 +3435,7 @@ static long btrfs_fallocate(struct file *file, int mode,
return -EOPNOTSUPP;

if (mode & FALLOC_FL_PUNCH_HOLE)
return btrfs_punch_hole(inode, offset, len);
return btrfs_punch_hole(file, offset, len);

/*
* Only trigger disk allocation, don't trigger qgroup reserve
Expand All @@ -3452,6 +3457,10 @@ static long btrfs_fallocate(struct file *file, int mode,
goto out;
}

ret = file_modified(file);
if (ret)
goto out;

/*
* TODO: Move these two operations after we have checked
* accurate reserved space, or fallocate can still fail but
Expand Down

0 comments on commit 05fd956

Please sign in to comment.