Commit e441d54d authored by Chris Mason's avatar Chris Mason

Btrfs: add permission checks to the ioctls

Only root can add/remove devices
Only root can defrag subtrees
Only files open for writing can be defragged
Only files open for writing can be the destination for a clone
Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent b34b086c
...@@ -453,6 +453,9 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg) ...@@ -453,6 +453,9 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
if (root->fs_info->sb->s_flags & MS_RDONLY) if (root->fs_info->sb->s_flags & MS_RDONLY)
return -EROFS; return -EROFS;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
if (!vol_args) if (!vol_args)
...@@ -638,16 +641,24 @@ static int btrfs_ioctl_defrag(struct file *file) ...@@ -638,16 +641,24 @@ static int btrfs_ioctl_defrag(struct file *file)
switch (inode->i_mode & S_IFMT) { switch (inode->i_mode & S_IFMT) {
case S_IFDIR: case S_IFDIR:
if (!capable(CAP_SYS_ADMIN)) {
ret = -EPERM;
goto out;
}
btrfs_defrag_root(root, 0); btrfs_defrag_root(root, 0);
btrfs_defrag_root(root->fs_info->extent_root, 0); btrfs_defrag_root(root->fs_info->extent_root, 0);
break; break;
case S_IFREG: case S_IFREG:
if (!(file->f_mode & FMODE_WRITE)) {
ret = -EINVAL;
goto out;
}
btrfs_defrag_file(file); btrfs_defrag_file(file);
break; break;
} }
out:
mnt_drop_write(file->f_path.mnt); mnt_drop_write(file->f_path.mnt);
return 0; return ret;
} }
static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
...@@ -655,6 +666,9 @@ static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) ...@@ -655,6 +666,9 @@ static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
struct btrfs_ioctl_vol_args *vol_args; struct btrfs_ioctl_vol_args *vol_args;
int ret; int ret;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS); vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
if (!vol_args) if (!vol_args)
...@@ -677,6 +691,9 @@ static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) ...@@ -677,6 +691,9 @@ static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
struct btrfs_ioctl_vol_args *vol_args; struct btrfs_ioctl_vol_args *vol_args;
int ret; int ret;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
if (root->fs_info->sb->s_flags & MS_RDONLY) if (root->fs_info->sb->s_flags & MS_RDONLY)
return -EROFS; return -EROFS;
...@@ -726,6 +743,10 @@ static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, ...@@ -726,6 +743,10 @@ static long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
* they don't overlap)? * they don't overlap)?
*/ */
/* the destination must be opened for writing */
if (!(file->f_mode & FMODE_WRITE))
return -EINVAL;
ret = mnt_want_write(file->f_path.mnt); ret = mnt_want_write(file->f_path.mnt);
if (ret) if (ret)
return ret; return ret;
......
...@@ -589,6 +589,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, ...@@ -589,6 +589,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
int ret = 0; int ret = 0;
int len; int len;
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
vol = kmalloc(sizeof(*vol), GFP_KERNEL); vol = kmalloc(sizeof(*vol), GFP_KERNEL);
if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) { if (copy_from_user(vol, (void __user *)arg, sizeof(*vol))) {
ret = -EFAULT; ret = -EFAULT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment