Skip to content

Commit

Permalink
vfs: create a generic checking and prep function for FS_IOC_SETFLAGS
Browse files Browse the repository at this point in the history
Create a generic function to check incoming FS_IOC_SETFLAGS flag values
and later prepare the inode for updates so that we can standardize the
implementations that follow ext4's flag values.

Note that the efivarfs implementation no longer fails a no-op SETFLAGS
without CAP_LINUX_IMMUTABLE since that's the behavior in ext*.

Signed-off-by: Darrick J. Wong <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
Reviewed-by: Christoph Hellwig <[email protected]>
Acked-by: David Sterba <[email protected]>
Reviewed-by: Bob Peterson <[email protected]>
  • Loading branch information
djwong committed Jul 1, 2019
1 parent d1fdb6d commit 5aca284
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 118 deletions.
13 changes: 5 additions & 8 deletions fs/btrfs/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
struct btrfs_inode *binode = BTRFS_I(inode);
struct btrfs_root *root = binode->root;
struct btrfs_trans_handle *trans;
unsigned int fsflags;
unsigned int fsflags, old_fsflags;
int ret;
const char *comp = NULL;
u32 binode_flags = binode->flags;
Expand All @@ -212,13 +212,10 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
inode_lock(inode);

fsflags = btrfs_mask_fsflags_for_type(inode, fsflags);
if ((fsflags ^ btrfs_inode_flags_to_fsflags(binode->flags)) &
(FS_APPEND_FL | FS_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
ret = -EPERM;
goto out_unlock;
}
}
old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags);
ret = vfs_ioc_setflags_prepare(inode, old_fsflags, fsflags);
if (ret)
goto out_unlock;

if (fsflags & FS_SYNC_FL)
binode_flags |= BTRFS_INODE_SYNC;
Expand Down
26 changes: 17 additions & 9 deletions fs/efivarfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,22 @@ static ssize_t efivarfs_file_read(struct file *file, char __user *userbuf,
return size;
}

static int
efivarfs_ioc_getxflags(struct file *file, void __user *arg)
static inline unsigned int efivarfs_getflags(struct inode *inode)
{
struct inode *inode = file->f_mapping->host;
unsigned int i_flags;
unsigned int flags = 0;

i_flags = inode->i_flags;
if (i_flags & S_IMMUTABLE)
flags |= FS_IMMUTABLE_FL;
return flags;
}

static int
efivarfs_ioc_getxflags(struct file *file, void __user *arg)
{
struct inode *inode = file->f_mapping->host;
unsigned int flags = efivarfs_getflags(inode);

if (copy_to_user(arg, &flags, sizeof(flags)))
return -EFAULT;
Expand All @@ -132,6 +138,7 @@ efivarfs_ioc_setxflags(struct file *file, void __user *arg)
struct inode *inode = file->f_mapping->host;
unsigned int flags;
unsigned int i_flags = 0;
unsigned int oldflags = efivarfs_getflags(inode);
int error;

if (!inode_owner_or_capable(inode))
Expand All @@ -143,9 +150,6 @@ efivarfs_ioc_setxflags(struct file *file, void __user *arg)
if (flags & ~FS_IMMUTABLE_FL)
return -EOPNOTSUPP;

if (!capable(CAP_LINUX_IMMUTABLE))
return -EPERM;

if (flags & FS_IMMUTABLE_FL)
i_flags |= S_IMMUTABLE;

Expand All @@ -155,12 +159,16 @@ efivarfs_ioc_setxflags(struct file *file, void __user *arg)
return error;

inode_lock(inode);

error = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (error)
goto out;

inode_set_flags(inode, i_flags, S_IMMUTABLE);
out:
inode_unlock(inode);

mnt_drop_write_file(file);

return 0;
return error;
}

static long
Expand Down
16 changes: 4 additions & 12 deletions fs/ext2/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,10 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
}
oldflags = ei->i_flags;

/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*
* This test looks nicer. Thanks to Pauline Middelink
*/
if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
inode_unlock(inode);
ret = -EPERM;
goto setflags_out;
}
ret = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (ret) {
inode_unlock(inode);
goto setflags_out;
}

flags = flags & EXT2_FL_USER_MODIFIABLE;
Expand Down
13 changes: 3 additions & 10 deletions fs/ext4/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,9 @@ static int ext4_ioctl_setflags(struct inode *inode,
/* The JOURNAL_DATA flag is modifiable only by root */
jflag = flags & EXT4_JOURNAL_DATA_FL;

/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*
* This test looks nicer. Thanks to Pauline Middelink
*/
if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE))
goto flags_out;
}
err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (err)
goto flags_out;

/*
* The JOURNAL_DATA flag can only be changed by
Expand Down
42 changes: 29 additions & 13 deletions fs/gfs2/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,36 @@ static struct {
{FS_JOURNAL_DATA_FL, GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA},
};

static inline u32 gfs2_gfsflags_to_fsflags(struct inode *inode, u32 gfsflags)
{
int i;
u32 fsflags = 0;

if (S_ISDIR(inode->i_mode))
gfsflags &= ~GFS2_DIF_JDATA;
else
gfsflags &= ~GFS2_DIF_INHERIT_JDATA;

for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
if (gfsflags & fsflag_gfs2flag[i].gfsflag)
fsflags |= fsflag_gfs2flag[i].fsflag;
return fsflags;
}

static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
{
struct inode *inode = file_inode(filp);
struct gfs2_inode *ip = GFS2_I(inode);
struct gfs2_holder gh;
int i, error;
u32 gfsflags, fsflags = 0;
int error;
u32 fsflags;

gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
error = gfs2_glock_nq(&gh);
if (error)
goto out_uninit;

gfsflags = ip->i_diskflags;
if (S_ISDIR(inode->i_mode))
gfsflags &= ~GFS2_DIF_JDATA;
else
gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
if (gfsflags & fsflag_gfs2flag[i].gfsflag)
fsflags |= fsflag_gfs2flag[i].fsflag;
fsflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);

if (put_user(fsflags, ptr))
error = -EFAULT;
Expand Down Expand Up @@ -200,17 +209,19 @@ void gfs2_set_inode_flags(struct inode *inode)
* @filp: file pointer
* @reqflags: The flags to set
* @mask: Indicates which flags are valid
* @fsflags: The FS_* inode flags passed in
*
*/
static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask,
const u32 fsflags)
{
struct inode *inode = file_inode(filp);
struct gfs2_inode *ip = GFS2_I(inode);
struct gfs2_sbd *sdp = GFS2_SB(inode);
struct buffer_head *bh;
struct gfs2_holder gh;
int error;
u32 new_flags, flags;
u32 new_flags, flags, oldflags;

error = mnt_want_write_file(filp);
if (error)
Expand All @@ -220,6 +231,11 @@ static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
if (error)
goto out_drop_write;

oldflags = gfs2_gfsflags_to_fsflags(inode, ip->i_diskflags);
error = vfs_ioc_setflags_prepare(inode, oldflags, fsflags);
if (error)
goto out;

error = -EACCES;
if (!inode_owner_or_capable(inode))
goto out;
Expand Down Expand Up @@ -308,7 +324,7 @@ static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
}

return do_gfs2_set_flags(filp, gfsflags, mask);
return do_gfs2_set_flags(filp, gfsflags, mask, fsflags);
}

static int gfs2_getlabel(struct file *filp, char __user *label)
Expand Down
21 changes: 12 additions & 9 deletions fs/hfsplus/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ static int hfsplus_ioctl_bless(struct file *file, int __user *user_flags)
return 0;
}

static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
static inline unsigned int hfsplus_getflags(struct inode *inode)
{
struct inode *inode = file_inode(file);
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
unsigned int flags = 0;

Expand All @@ -69,6 +68,13 @@ static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
flags |= FS_APPEND_FL;
if (hip->userflags & HFSPLUS_FLG_NODUMP)
flags |= FS_NODUMP_FL;
return flags;
}

static int hfsplus_ioctl_getflags(struct file *file, int __user *user_flags)
{
struct inode *inode = file_inode(file);
unsigned int flags = hfsplus_getflags(inode);

return put_user(flags, user_flags);
}
Expand All @@ -78,6 +84,7 @@ static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)
struct inode *inode = file_inode(file);
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
unsigned int flags, new_fl = 0;
unsigned int oldflags = hfsplus_getflags(inode);
int err = 0;

err = mnt_want_write_file(file);
Expand All @@ -96,13 +103,9 @@ static int hfsplus_ioctl_setflags(struct file *file, int __user *user_flags)

inode_lock(inode);

if ((flags & (FS_IMMUTABLE_FL|FS_APPEND_FL)) ||
inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
err = -EPERM;
goto out_unlock_inode;
}
}
err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (err)
goto out_unlock_inode;

/* don't silently ignore unsupported ext2 flags */
if (flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) {
Expand Down
24 changes: 24 additions & 0 deletions fs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2170,3 +2170,27 @@ struct timespec64 current_time(struct inode *inode)
return timespec64_trunc(now, inode->i_sb->s_time_gran);
}
EXPORT_SYMBOL(current_time);

/*
* Generic function to check FS_IOC_SETFLAGS values and reject any invalid
* configurations.
*
* Note: the caller should be holding i_mutex, or else be sure that they have
* exclusive access to the inode structure.
*/
int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
unsigned int flags)
{
/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*
* This test looks nicer. Thanks to Pauline Middelink
*/
if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL) &&
!capable(CAP_LINUX_IMMUTABLE))
return -EPERM;

return 0;
}
EXPORT_SYMBOL(vfs_ioc_setflags_prepare);
22 changes: 7 additions & 15 deletions fs/jfs/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,16 @@ long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
/* Lock against other parallel changes of flags */
inode_lock(inode);

oldflags = jfs_inode->mode2;

/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*/
if ((oldflags & JFS_IMMUTABLE_FL) ||
((flags ^ oldflags) &
(JFS_APPEND_FL | JFS_IMMUTABLE_FL))) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
inode_unlock(inode);
err = -EPERM;
goto setflags_out;
}
oldflags = jfs_map_ext2(jfs_inode->mode2 & JFS_FL_USER_VISIBLE,
0);
err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (err) {
inode_unlock(inode);
goto setflags_out;
}

flags = flags & JFS_FL_USER_MODIFIABLE;
flags |= oldflags & ~JFS_FL_USER_MODIFIABLE;
flags |= jfs_inode->mode2 & ~JFS_FL_USER_MODIFIABLE;
jfs_inode->mode2 = flags;

jfs_set_inode_flags(inode);
Expand Down
9 changes: 2 additions & 7 deletions fs/nilfs2/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,8 @@ static int nilfs_ioctl_setflags(struct inode *inode, struct file *filp,

oldflags = NILFS_I(inode)->i_flags;

/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by the
* relevant capability.
*/
ret = -EPERM;
if (((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) &&
!capable(CAP_LINUX_IMMUTABLE))
ret = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (ret)
goto out;

ret = nilfs_transaction_begin(inode->i_sb, &ti, 0);
Expand Down
13 changes: 3 additions & 10 deletions fs/ocfs2/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,9 @@ static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
flags = flags & mask;
flags |= oldflags & ~mask;

/*
* The IMMUTABLE and APPEND_ONLY flags can only be changed by
* the relevant capability.
*/
status = -EPERM;
if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
(OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
if (!capable(CAP_LINUX_IMMUTABLE))
goto bail_unlock;
}
status = vfs_ioc_setflags_prepare(inode, oldflags, flags);
if (status)
goto bail_unlock;

handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
if (IS_ERR(handle)) {
Expand Down
Loading

0 comments on commit 5aca284

Please sign in to comment.