Commit b3f3107f authored by Tetsuo Handa's avatar Tetsuo Handa Committed by Greg Kroah-Hartman

block/loop: Use global lock for ioctl() operation.

commit 310ca162 upstream.

syzbot is reporting NULL pointer dereference [1] which is caused by
race condition between ioctl(loop_fd, LOOP_CLR_FD, 0) versus
ioctl(other_loop_fd, LOOP_SET_FD, loop_fd) due to traversing other
loop devices at loop_validate_file() without holding corresponding
lo->lo_ctl_mutex locks.

Since ioctl() request on loop devices is not frequent operation, we don't
need fine grained locking. Let's use global lock in order to allow safe
traversal at loop_validate_file().

Note that syzbot is also reporting circular locking dependency between
bdev->bd_mutex and lo->lo_ctl_mutex [2] which is caused by calling
blkdev_reread_part() with lock held. This patch does not address it.

[1] https://syzkaller.appspot.com/bug?id=f3cfe26e785d85f9ee259f385515291d21bd80a3
[2] https://syzkaller.appspot.com/bug?id=bf154052f0eea4bc7712499e4569505907d15889Signed-off-by: default avatarTetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reported-by: default avatarsyzbot <syzbot+bf89c128e05dd6c62523@syzkaller.appspotmail.com>
Reviewed-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 02035bea
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
static DEFINE_IDR(loop_index_idr); static DEFINE_IDR(loop_index_idr);
static DEFINE_MUTEX(loop_index_mutex); static DEFINE_MUTEX(loop_index_mutex);
static DEFINE_MUTEX(loop_ctl_mutex);
static int max_part; static int max_part;
static int part_shift; static int part_shift;
...@@ -1044,7 +1045,7 @@ static int loop_clr_fd(struct loop_device *lo) ...@@ -1044,7 +1045,7 @@ static int loop_clr_fd(struct loop_device *lo)
*/ */
if (atomic_read(&lo->lo_refcnt) > 1) { if (atomic_read(&lo->lo_refcnt) > 1) {
lo->lo_flags |= LO_FLAGS_AUTOCLEAR; lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
return 0; return 0;
} }
...@@ -1093,12 +1094,12 @@ static int loop_clr_fd(struct loop_device *lo) ...@@ -1093,12 +1094,12 @@ static int loop_clr_fd(struct loop_device *lo)
if (!part_shift) if (!part_shift)
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN; lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
loop_unprepare_queue(lo); loop_unprepare_queue(lo);
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
/* /*
* Need not hold lo_ctl_mutex to fput backing file. * Need not hold loop_ctl_mutex to fput backing file.
* Calling fput holding lo_ctl_mutex triggers a circular * Calling fput holding loop_ctl_mutex triggers a circular
* lock dependency possibility warning as fput can take * lock dependency possibility warning as fput can take
* bd_mutex which is usually taken before lo_ctl_mutex. * bd_mutex which is usually taken before loop_ctl_mutex.
*/ */
fput(filp); fput(filp);
return 0; return 0;
...@@ -1361,7 +1362,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1361,7 +1362,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
struct loop_device *lo = bdev->bd_disk->private_data; struct loop_device *lo = bdev->bd_disk->private_data;
int err; int err;
mutex_lock_nested(&lo->lo_ctl_mutex, 1); mutex_lock_nested(&loop_ctl_mutex, 1);
switch (cmd) { switch (cmd) {
case LOOP_SET_FD: case LOOP_SET_FD:
err = loop_set_fd(lo, mode, bdev, arg); err = loop_set_fd(lo, mode, bdev, arg);
...@@ -1370,7 +1371,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1370,7 +1371,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
err = loop_change_fd(lo, bdev, arg); err = loop_change_fd(lo, bdev, arg);
break; break;
case LOOP_CLR_FD: case LOOP_CLR_FD:
/* loop_clr_fd would have unlocked lo_ctl_mutex on success */ /* loop_clr_fd would have unlocked loop_ctl_mutex on success */
err = loop_clr_fd(lo); err = loop_clr_fd(lo);
if (!err) if (!err)
goto out_unlocked; goto out_unlocked;
...@@ -1406,7 +1407,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1406,7 +1407,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
default: default:
err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL; err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
} }
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
out_unlocked: out_unlocked:
return err; return err;
...@@ -1539,16 +1540,16 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode, ...@@ -1539,16 +1540,16 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
switch(cmd) { switch(cmd) {
case LOOP_SET_STATUS: case LOOP_SET_STATUS:
mutex_lock(&lo->lo_ctl_mutex); mutex_lock(&loop_ctl_mutex);
err = loop_set_status_compat( err = loop_set_status_compat(
lo, (const struct compat_loop_info __user *) arg); lo, (const struct compat_loop_info __user *) arg);
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
break; break;
case LOOP_GET_STATUS: case LOOP_GET_STATUS:
mutex_lock(&lo->lo_ctl_mutex); mutex_lock(&loop_ctl_mutex);
err = loop_get_status_compat( err = loop_get_status_compat(
lo, (struct compat_loop_info __user *) arg); lo, (struct compat_loop_info __user *) arg);
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
break; break;
case LOOP_SET_CAPACITY: case LOOP_SET_CAPACITY:
case LOOP_CLR_FD: case LOOP_CLR_FD:
...@@ -1592,7 +1593,7 @@ static void __lo_release(struct loop_device *lo) ...@@ -1592,7 +1593,7 @@ static void __lo_release(struct loop_device *lo)
if (atomic_dec_return(&lo->lo_refcnt)) if (atomic_dec_return(&lo->lo_refcnt))
return; return;
mutex_lock(&lo->lo_ctl_mutex); mutex_lock(&loop_ctl_mutex);
if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) { if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
/* /*
* In autoclear mode, stop the loop thread * In autoclear mode, stop the loop thread
...@@ -1609,7 +1610,7 @@ static void __lo_release(struct loop_device *lo) ...@@ -1609,7 +1610,7 @@ static void __lo_release(struct loop_device *lo)
loop_flush(lo); loop_flush(lo);
} }
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
} }
static void lo_release(struct gendisk *disk, fmode_t mode) static void lo_release(struct gendisk *disk, fmode_t mode)
...@@ -1655,10 +1656,10 @@ static int unregister_transfer_cb(int id, void *ptr, void *data) ...@@ -1655,10 +1656,10 @@ static int unregister_transfer_cb(int id, void *ptr, void *data)
struct loop_device *lo = ptr; struct loop_device *lo = ptr;
struct loop_func_table *xfer = data; struct loop_func_table *xfer = data;
mutex_lock(&lo->lo_ctl_mutex); mutex_lock(&loop_ctl_mutex);
if (lo->lo_encryption == xfer) if (lo->lo_encryption == xfer)
loop_release_xfer(lo); loop_release_xfer(lo);
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
return 0; return 0;
} }
...@@ -1820,7 +1821,6 @@ static int loop_add(struct loop_device **l, int i) ...@@ -1820,7 +1821,6 @@ static int loop_add(struct loop_device **l, int i)
if (!part_shift) if (!part_shift)
disk->flags |= GENHD_FL_NO_PART_SCAN; disk->flags |= GENHD_FL_NO_PART_SCAN;
disk->flags |= GENHD_FL_EXT_DEVT; disk->flags |= GENHD_FL_EXT_DEVT;
mutex_init(&lo->lo_ctl_mutex);
atomic_set(&lo->lo_refcnt, 0); atomic_set(&lo->lo_refcnt, 0);
lo->lo_number = i; lo->lo_number = i;
spin_lock_init(&lo->lo_lock); spin_lock_init(&lo->lo_lock);
...@@ -1933,19 +1933,19 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd, ...@@ -1933,19 +1933,19 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
ret = loop_lookup(&lo, parm); ret = loop_lookup(&lo, parm);
if (ret < 0) if (ret < 0)
break; break;
mutex_lock(&lo->lo_ctl_mutex); mutex_lock(&loop_ctl_mutex);
if (lo->lo_state != Lo_unbound) { if (lo->lo_state != Lo_unbound) {
ret = -EBUSY; ret = -EBUSY;
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
break; break;
} }
if (atomic_read(&lo->lo_refcnt) > 0) { if (atomic_read(&lo->lo_refcnt) > 0) {
ret = -EBUSY; ret = -EBUSY;
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
break; break;
} }
lo->lo_disk->private_data = NULL; lo->lo_disk->private_data = NULL;
mutex_unlock(&lo->lo_ctl_mutex); mutex_unlock(&loop_ctl_mutex);
idr_remove(&loop_index_idr, lo->lo_number); idr_remove(&loop_index_idr, lo->lo_number);
loop_remove(lo); loop_remove(lo);
break; break;
......
...@@ -55,7 +55,6 @@ struct loop_device { ...@@ -55,7 +55,6 @@ struct loop_device {
spinlock_t lo_lock; spinlock_t lo_lock;
int lo_state; int lo_state;
struct mutex lo_ctl_mutex;
struct kthread_worker worker; struct kthread_worker worker;
struct task_struct *worker_task; struct task_struct *worker_task;
bool use_dio; bool use_dio;
......
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