Skip to content

Commit 310ca16

Browse files
Tetsuo Handaaxboe
authored andcommitted
block/loop: Use global lock for ioctl() operation.
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=bf154052f0eea4bc7712499e4569505907d15889 Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Reported-by: syzbot <syzbot+bf89c128e05dd6c62523@syzkaller.appspotmail.com> Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b1ab5fa commit 310ca16

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

‎drivers/block/loop.c‎

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
static DEFINE_IDR(loop_index_idr);
8686
static DEFINE_MUTEX(loop_index_mutex);
87+
static DEFINE_MUTEX(loop_ctl_mutex);
8788

8889
static int max_part;
8990
static int part_shift;
@@ -1046,7 +1047,7 @@ static int loop_clr_fd(struct loop_device *lo)
10461047
*/
10471048
if (atomic_read(&lo->lo_refcnt) > 1) {
10481049
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
1049-
mutex_unlock(&lo->lo_ctl_mutex);
1050+
mutex_unlock(&loop_ctl_mutex);
10501051
return 0;
10511052
}
10521053

@@ -1099,12 +1100,12 @@ static int loop_clr_fd(struct loop_device *lo)
10991100
if (!part_shift)
11001101
lo->lo_disk->flags |= GENHD_FL_NO_PART_SCAN;
11011102
loop_unprepare_queue(lo);
1102-
mutex_unlock(&lo->lo_ctl_mutex);
1103+
mutex_unlock(&loop_ctl_mutex);
11031104
/*
1104-
* Need not hold lo_ctl_mutex to fput backing file.
1105-
* Calling fput holding lo_ctl_mutex triggers a circular
1105+
* Need not hold loop_ctl_mutex to fput backing file.
1106+
* Calling fput holding loop_ctl_mutex triggers a circular
11061107
* lock dependency possibility warning as fput can take
1107-
* bd_mutex which is usually taken before lo_ctl_mutex.
1108+
* bd_mutex which is usually taken before loop_ctl_mutex.
11081109
*/
11091110
fput(filp);
11101111
return 0;
@@ -1209,7 +1210,7 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info)
12091210
int ret;
12101211

12111212
if (lo->lo_state != Lo_bound) {
1212-
mutex_unlock(&lo->lo_ctl_mutex);
1213+
mutex_unlock(&loop_ctl_mutex);
12131214
return -ENXIO;
12141215
}
12151216

@@ -1228,10 +1229,10 @@ loop_get_status(struct loop_device *lo, struct loop_info64 *info)
12281229
lo->lo_encrypt_key_size);
12291230
}
12301231

1231-
/* Drop lo_ctl_mutex while we call into the filesystem. */
1232+
/* Drop loop_ctl_mutex while we call into the filesystem. */
12321233
path = lo->lo_backing_file->f_path;
12331234
path_get(&path);
1234-
mutex_unlock(&lo->lo_ctl_mutex);
1235+
mutex_unlock(&loop_ctl_mutex);
12351236
ret = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
12361237
if (!ret) {
12371238
info->lo_device = huge_encode_dev(stat.dev);
@@ -1323,7 +1324,7 @@ loop_get_status_old(struct loop_device *lo, struct loop_info __user *arg) {
13231324
int err;
13241325

13251326
if (!arg) {
1326-
mutex_unlock(&lo->lo_ctl_mutex);
1327+
mutex_unlock(&loop_ctl_mutex);
13271328
return -EINVAL;
13281329
}
13291330
err = loop_get_status(lo, &info64);
@@ -1341,7 +1342,7 @@ loop_get_status64(struct loop_device *lo, struct loop_info64 __user *arg) {
13411342
int err;
13421343

13431344
if (!arg) {
1344-
mutex_unlock(&lo->lo_ctl_mutex);
1345+
mutex_unlock(&loop_ctl_mutex);
13451346
return -EINVAL;
13461347
}
13471348
err = loop_get_status(lo, &info64);
@@ -1399,7 +1400,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
13991400
struct loop_device *lo = bdev->bd_disk->private_data;
14001401
int err;
14011402

1402-
err = mutex_lock_killable_nested(&lo->lo_ctl_mutex, 1);
1403+
err = mutex_lock_killable_nested(&loop_ctl_mutex, 1);
14031404
if (err)
14041405
goto out_unlocked;
14051406

@@ -1411,7 +1412,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
14111412
err = loop_change_fd(lo, bdev, arg);
14121413
break;
14131414
case LOOP_CLR_FD:
1414-
/* loop_clr_fd would have unlocked lo_ctl_mutex on success */
1415+
/* loop_clr_fd would have unlocked loop_ctl_mutex on success */
14151416
err = loop_clr_fd(lo);
14161417
if (!err)
14171418
goto out_unlocked;
@@ -1424,7 +1425,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
14241425
break;
14251426
case LOOP_GET_STATUS:
14261427
err = loop_get_status_old(lo, (struct loop_info __user *) arg);
1427-
/* loop_get_status() unlocks lo_ctl_mutex */
1428+
/* loop_get_status() unlocks loop_ctl_mutex */
14281429
goto out_unlocked;
14291430
case LOOP_SET_STATUS64:
14301431
err = -EPERM;
@@ -1434,7 +1435,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
14341435
break;
14351436
case LOOP_GET_STATUS64:
14361437
err = loop_get_status64(lo, (struct loop_info64 __user *) arg);
1437-
/* loop_get_status() unlocks lo_ctl_mutex */
1438+
/* loop_get_status() unlocks loop_ctl_mutex */
14381439
goto out_unlocked;
14391440
case LOOP_SET_CAPACITY:
14401441
err = -EPERM;
@@ -1454,7 +1455,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
14541455
default:
14551456
err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
14561457
}
1457-
mutex_unlock(&lo->lo_ctl_mutex);
1458+
mutex_unlock(&loop_ctl_mutex);
14581459

14591460
out_unlocked:
14601461
return err;
@@ -1571,7 +1572,7 @@ loop_get_status_compat(struct loop_device *lo,
15711572
int err;
15721573

15731574
if (!arg) {
1574-
mutex_unlock(&lo->lo_ctl_mutex);
1575+
mutex_unlock(&loop_ctl_mutex);
15751576
return -EINVAL;
15761577
}
15771578
err = loop_get_status(lo, &info64);
@@ -1588,19 +1589,19 @@ static int lo_compat_ioctl(struct block_device *bdev, fmode_t mode,
15881589

15891590
switch(cmd) {
15901591
case LOOP_SET_STATUS:
1591-
err = mutex_lock_killable(&lo->lo_ctl_mutex);
1592+
err = mutex_lock_killable(&loop_ctl_mutex);
15921593
if (!err) {
15931594
err = loop_set_status_compat(lo,
15941595
(const struct compat_loop_info __user *)arg);
1595-
mutex_unlock(&lo->lo_ctl_mutex);
1596+
mutex_unlock(&loop_ctl_mutex);
15961597
}
15971598
break;
15981599
case LOOP_GET_STATUS:
1599-
err = mutex_lock_killable(&lo->lo_ctl_mutex);
1600+
err = mutex_lock_killable(&loop_ctl_mutex);
16001601
if (!err) {
16011602
err = loop_get_status_compat(lo,
16021603
(struct compat_loop_info __user *)arg);
1603-
/* loop_get_status() unlocks lo_ctl_mutex */
1604+
/* loop_get_status() unlocks loop_ctl_mutex */
16041605
}
16051606
break;
16061607
case LOOP_SET_CAPACITY:
@@ -1647,7 +1648,7 @@ static void __lo_release(struct loop_device *lo)
16471648
if (atomic_dec_return(&lo->lo_refcnt))
16481649
return;
16491650

1650-
mutex_lock(&lo->lo_ctl_mutex);
1651+
mutex_lock(&loop_ctl_mutex);
16511652
if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
16521653
/*
16531654
* In autoclear mode, stop the loop thread
@@ -1665,7 +1666,7 @@ static void __lo_release(struct loop_device *lo)
16651666
blk_mq_unfreeze_queue(lo->lo_queue);
16661667
}
16671668

1668-
mutex_unlock(&lo->lo_ctl_mutex);
1669+
mutex_unlock(&loop_ctl_mutex);
16691670
}
16701671

16711672
static void lo_release(struct gendisk *disk, fmode_t mode)
@@ -1711,10 +1712,10 @@ static int unregister_transfer_cb(int id, void *ptr, void *data)
17111712
struct loop_device *lo = ptr;
17121713
struct loop_func_table *xfer = data;
17131714

1714-
mutex_lock(&lo->lo_ctl_mutex);
1715+
mutex_lock(&loop_ctl_mutex);
17151716
if (lo->lo_encryption == xfer)
17161717
loop_release_xfer(lo);
1717-
mutex_unlock(&lo->lo_ctl_mutex);
1718+
mutex_unlock(&loop_ctl_mutex);
17181719
return 0;
17191720
}
17201721

@@ -1895,7 +1896,6 @@ static int loop_add(struct loop_device **l, int i)
18951896
if (!part_shift)
18961897
disk->flags |= GENHD_FL_NO_PART_SCAN;
18971898
disk->flags |= GENHD_FL_EXT_DEVT;
1898-
mutex_init(&lo->lo_ctl_mutex);
18991899
atomic_set(&lo->lo_refcnt, 0);
19001900
lo->lo_number = i;
19011901
spin_lock_init(&lo->lo_lock);
@@ -2008,21 +2008,21 @@ static long loop_control_ioctl(struct file *file, unsigned int cmd,
20082008
ret = loop_lookup(&lo, parm);
20092009
if (ret < 0)
20102010
break;
2011-
ret = mutex_lock_killable(&lo->lo_ctl_mutex);
2011+
ret = mutex_lock_killable(&loop_ctl_mutex);
20122012
if (ret)
20132013
break;
20142014
if (lo->lo_state != Lo_unbound) {
20152015
ret = -EBUSY;
2016-
mutex_unlock(&lo->lo_ctl_mutex);
2016+
mutex_unlock(&loop_ctl_mutex);
20172017
break;
20182018
}
20192019
if (atomic_read(&lo->lo_refcnt) > 0) {
20202020
ret = -EBUSY;
2021-
mutex_unlock(&lo->lo_ctl_mutex);
2021+
mutex_unlock(&loop_ctl_mutex);
20222022
break;
20232023
}
20242024
lo->lo_disk->private_data = NULL;
2025-
mutex_unlock(&lo->lo_ctl_mutex);
2025+
mutex_unlock(&loop_ctl_mutex);
20262026
idr_remove(&loop_index_idr, lo->lo_number);
20272027
loop_remove(lo);
20282028
break;

‎drivers/block/loop.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ struct loop_device {
5454

5555
spinlock_t lo_lock;
5656
int lo_state;
57-
struct mutex lo_ctl_mutex;
5857
struct kthread_worker worker;
5958
struct task_struct *worker_task;
6059
bool use_dio;

0 commit comments

Comments
 (0)