Skip to content

Commit

Permalink
[PATCH] r/o bind mounts: elevate write count for ioctls()
Browse files Browse the repository at this point in the history
Some ioctl()s can cause writes to the filesystem.  Take these, and make them
use mnt_want/drop_write() instead.

[AV: updated]

Acked-by: Al Viro <viro@ZenIV.linux.org.uk>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Dave Hansen <haveblue@us.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
hansendc authored and Al Viro committed Apr 19, 2008
1 parent 20ddee2 commit 42a74f2
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 155 deletions.
57 changes: 37 additions & 20 deletions fs/ext2/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <linux/time.h>
#include <linux/sched.h>
#include <linux/compat.h>
#include <linux/mount.h>
#include <linux/smp_lock.h>
#include <asm/current.h>
#include <asm/uaccess.h>
Expand All @@ -23,6 +24,7 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
struct ext2_inode_info *ei = EXT2_I(inode);
unsigned int flags;
unsigned short rsv_window_size;
int ret;

ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);

Expand All @@ -34,14 +36,19 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
case EXT2_IOC_SETFLAGS: {
unsigned int oldflags;

if (IS_RDONLY(inode))
return -EROFS;
ret = mnt_want_write(filp->f_path.mnt);
if (ret)
return ret;

if (!is_owner_or_cap(inode))
return -EACCES;
if (!is_owner_or_cap(inode)) {
ret = -EACCES;
goto setflags_out;
}

if (get_user(flags, (int __user *) arg))
return -EFAULT;
if (get_user(flags, (int __user *) arg)) {
ret = -EFAULT;
goto setflags_out;
}

if (!S_ISDIR(inode->i_mode))
flags &= ~EXT2_DIRSYNC_FL;
Expand All @@ -50,7 +57,8 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
/* Is it quota file? Do not allow user to mess with it */
if (IS_NOQUOTA(inode)) {
mutex_unlock(&inode->i_mutex);
return -EPERM;
ret = -EPERM;
goto setflags_out;
}
oldflags = ei->i_flags;

Expand All @@ -63,7 +71,8 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
mutex_unlock(&inode->i_mutex);
return -EPERM;
ret = -EPERM;
goto setflags_out;
}
}

Expand All @@ -75,20 +84,26 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
ext2_set_inode_flags(inode);
inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
return 0;
setflags_out:
mnt_drop_write(filp->f_path.mnt);
return ret;
}
case EXT2_IOC_GETVERSION:
return put_user(inode->i_generation, (int __user *) arg);
case EXT2_IOC_SETVERSION:
if (!is_owner_or_cap(inode))
return -EPERM;
if (IS_RDONLY(inode))
return -EROFS;
if (get_user(inode->i_generation, (int __user *) arg))
return -EFAULT;
inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
return 0;
ret = mnt_want_write(filp->f_path.mnt);
if (ret)
return ret;
if (get_user(inode->i_generation, (int __user *) arg)) {
ret = -EFAULT;
} else {
inode->i_ctime = CURRENT_TIME_SEC;
mark_inode_dirty(inode);
}
mnt_drop_write(filp->f_path.mnt);
return ret;
case EXT2_IOC_GETRSVSZ:
if (test_opt(inode->i_sb, RESERVATION)
&& S_ISREG(inode->i_mode)
Expand All @@ -102,15 +117,16 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
return -ENOTTY;

if (IS_RDONLY(inode))
return -EROFS;

if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
if (!is_owner_or_cap(inode))
return -EACCES;

if (get_user(rsv_window_size, (int __user *)arg))
return -EFAULT;

ret = mnt_want_write(filp->f_path.mnt);
if (ret)
return ret;

if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;

Expand All @@ -131,6 +147,7 @@ long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
rsv->rsv_goal_size = rsv_window_size;
}
mutex_unlock(&ei->truncate_mutex);
mnt_drop_write(filp->f_path.mnt);
return 0;
}
default:
Expand Down
103 changes: 68 additions & 35 deletions fs/ext3/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <linux/capability.h>
#include <linux/ext3_fs.h>
#include <linux/ext3_jbd.h>
#include <linux/mount.h>
#include <linux/time.h>
#include <linux/compat.h>
#include <linux/smp_lock.h>
Expand All @@ -38,14 +39,19 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
unsigned int oldflags;
unsigned int jflag;

if (IS_RDONLY(inode))
return -EROFS;
err = mnt_want_write(filp->f_path.mnt);
if (err)
return err;

if (!is_owner_or_cap(inode))
return -EACCES;
if (!is_owner_or_cap(inode)) {
err = -EACCES;
goto flags_out;
}

if (get_user(flags, (int __user *) arg))
return -EFAULT;
if (get_user(flags, (int __user *) arg)) {
err = -EFAULT;
goto flags_out;
}

if (!S_ISDIR(inode->i_mode))
flags &= ~EXT3_DIRSYNC_FL;
Expand All @@ -54,7 +60,8 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
/* Is it quota file? Do not allow user to mess with it */
if (IS_NOQUOTA(inode)) {
mutex_unlock(&inode->i_mutex);
return -EPERM;
err = -EPERM;
goto flags_out;
}
oldflags = ei->i_flags;

Expand All @@ -70,7 +77,8 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
if (!capable(CAP_LINUX_IMMUTABLE)) {
mutex_unlock(&inode->i_mutex);
return -EPERM;
err = -EPERM;
goto flags_out;
}
}

Expand All @@ -81,15 +89,17 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
if (!capable(CAP_SYS_RESOURCE)) {
mutex_unlock(&inode->i_mutex);
return -EPERM;
err = -EPERM;
goto flags_out;
}
}


handle = ext3_journal_start(inode, 1);
if (IS_ERR(handle)) {
mutex_unlock(&inode->i_mutex);
return PTR_ERR(handle);
err = PTR_ERR(handle);
goto flags_out;
}
if (IS_SYNC(inode))
handle->h_sync = 1;
Expand All @@ -115,6 +125,8 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
err = ext3_change_inode_journal_flag(inode, jflag);
mutex_unlock(&inode->i_mutex);
flags_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}
case EXT3_IOC_GETVERSION:
Expand All @@ -129,21 +141,27 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,

if (!is_owner_or_cap(inode))
return -EPERM;
if (IS_RDONLY(inode))
return -EROFS;
if (get_user(generation, (int __user *) arg))
return -EFAULT;

err = mnt_want_write(filp->f_path.mnt);
if (err)
return err;
if (get_user(generation, (int __user *) arg)) {
err = -EFAULT;
goto setversion_out;
}
handle = ext3_journal_start(inode, 1);
if (IS_ERR(handle))
return PTR_ERR(handle);
if (IS_ERR(handle)) {
err = PTR_ERR(handle);
goto setversion_out;
}
err = ext3_reserve_inode_write(handle, inode, &iloc);
if (err == 0) {
inode->i_ctime = CURRENT_TIME_SEC;
inode->i_generation = generation;
err = ext3_mark_iloc_dirty(handle, inode, &iloc);
}
ext3_journal_stop(handle);
setversion_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}
#ifdef CONFIG_JBD_DEBUG
Expand Down Expand Up @@ -179,18 +197,24 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
}
return -ENOTTY;
case EXT3_IOC_SETRSVSZ: {
int err;

if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
return -ENOTTY;

if (IS_RDONLY(inode))
return -EROFS;
err = mnt_want_write(filp->f_path.mnt);
if (err)
return err;

if (!is_owner_or_cap(inode))
return -EACCES;
if (!is_owner_or_cap(inode)) {
err = -EACCES;
goto setrsvsz_out;
}

if (get_user(rsv_window_size, (int __user *)arg))
return -EFAULT;
if (get_user(rsv_window_size, (int __user *)arg)) {
err = -EFAULT;
goto setrsvsz_out;
}

if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
Expand All @@ -208,7 +232,9 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
rsv->rsv_goal_size = rsv_window_size;
}
mutex_unlock(&ei->truncate_mutex);
return 0;
setrsvsz_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}
case EXT3_IOC_GROUP_EXTEND: {
ext3_fsblk_t n_blocks_count;
Expand All @@ -218,17 +244,20 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

if (IS_RDONLY(inode))
return -EROFS;

if (get_user(n_blocks_count, (__u32 __user *)arg))
return -EFAULT;
err = mnt_want_write(filp->f_path.mnt);
if (err)
return err;

if (get_user(n_blocks_count, (__u32 __user *)arg)) {
err = -EFAULT;
goto group_extend_out;
}
err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
journal_lock_updates(EXT3_SB(sb)->s_journal);
journal_flush(EXT3_SB(sb)->s_journal);
journal_unlock_updates(EXT3_SB(sb)->s_journal);

group_extend_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}
case EXT3_IOC_GROUP_ADD: {
Expand All @@ -239,18 +268,22 @@ int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

if (IS_RDONLY(inode))
return -EROFS;
err = mnt_want_write(filp->f_path.mnt);
if (err)
return err;

if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
sizeof(input)))
return -EFAULT;
sizeof(input))) {
err = -EFAULT;
goto group_add_out;
}

err = ext3_group_add(sb, &input);
journal_lock_updates(EXT3_SB(sb)->s_journal);
journal_flush(EXT3_SB(sb)->s_journal);
journal_unlock_updates(EXT3_SB(sb)->s_journal);

group_add_out:
mnt_drop_write(filp->f_path.mnt);
return err;
}

Expand Down
Loading

0 comments on commit 42a74f2

Please sign in to comment.