Skip to content

Commit 08e136e

Browse files
rpthibeaultaxboe
authored andcommitted
loop: don't change loop device under exclusive opener in loop_set_status
loop_set_status() is allowed to change the loop device while there are other openers of the device, even exclusive ones. In this case, it causes a KASAN: slab-out-of-bounds Read in ext4_search_dir(), since when looking for an entry in an inlined directory, e_value_offs is changed underneath the filesystem by loop_set_status(). Fix the problem by forbidding loop_set_status() from modifying the loop device while there are exclusive openers of the device. This is similar to the fix in loop_configure() by commit 33ec3e5 ("loop: Don't change loop device under exclusive opener") alongside commit ecbe6bc ("block: use bd_prepare_to_claim directly in the loop driver"). Reported-by: syzbot+3ee481e21fd75e14c397@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3ee481e21fd75e14c397 Tested-by: syzbot+3ee481e21fd75e14c397@syzkaller.appspotmail.com Tested-by: Yongpeng Yang <yangyongpeng@xiaomi.com> Signed-off-by: Raphael Pinsonneault-Thibeault <rpthibeault@gmail.com> Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 69153e8 commit 08e136e

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

drivers/block/loop.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,13 +1225,24 @@ static int loop_clr_fd(struct loop_device *lo)
12251225
}
12261226

12271227
static int
1228-
loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
1228+
loop_set_status(struct loop_device *lo, blk_mode_t mode,
1229+
struct block_device *bdev, const struct loop_info64 *info)
12291230
{
12301231
int err;
12311232
bool partscan = false;
12321233
bool size_changed = false;
12331234
unsigned int memflags;
12341235

1236+
/*
1237+
* If we don't hold exclusive handle for the device, upgrade to it
1238+
* here to avoid changing device under exclusive owner.
1239+
*/
1240+
if (!(mode & BLK_OPEN_EXCL)) {
1241+
err = bd_prepare_to_claim(bdev, loop_set_status, NULL);
1242+
if (err)
1243+
goto out_reread_partitions;
1244+
}
1245+
12351246
err = mutex_lock_killable(&lo->lo_mutex);
12361247
if (err)
12371248
return err;
@@ -1273,6 +1284,9 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
12731284
}
12741285
out_unlock:
12751286
mutex_unlock(&lo->lo_mutex);
1287+
if (!(mode & BLK_OPEN_EXCL))
1288+
bd_abort_claiming(bdev, loop_set_status);
1289+
out_reread_partitions:
12761290
if (partscan)
12771291
loop_reread_partitions(lo);
12781292

@@ -1352,25 +1366,29 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
13521366
}
13531367

13541368
static int
1355-
loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1369+
loop_set_status_old(struct loop_device *lo, blk_mode_t mode,
1370+
struct block_device *bdev,
1371+
const struct loop_info __user *arg)
13561372
{
13571373
struct loop_info info;
13581374
struct loop_info64 info64;
13591375

13601376
if (copy_from_user(&info, arg, sizeof (struct loop_info)))
13611377
return -EFAULT;
13621378
loop_info64_from_old(&info, &info64);
1363-
return loop_set_status(lo, &info64);
1379+
return loop_set_status(lo, mode, bdev, &info64);
13641380
}
13651381

13661382
static int
1367-
loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1383+
loop_set_status64(struct loop_device *lo, blk_mode_t mode,
1384+
struct block_device *bdev,
1385+
const struct loop_info64 __user *arg)
13681386
{
13691387
struct loop_info64 info64;
13701388

13711389
if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
13721390
return -EFAULT;
1373-
return loop_set_status(lo, &info64);
1391+
return loop_set_status(lo, mode, bdev, &info64);
13741392
}
13751393

13761394
static int
@@ -1549,14 +1567,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
15491567
case LOOP_SET_STATUS:
15501568
err = -EPERM;
15511569
if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1552-
err = loop_set_status_old(lo, argp);
1570+
err = loop_set_status_old(lo, mode, bdev, argp);
15531571
break;
15541572
case LOOP_GET_STATUS:
15551573
return loop_get_status_old(lo, argp);
15561574
case LOOP_SET_STATUS64:
15571575
err = -EPERM;
15581576
if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1559-
err = loop_set_status64(lo, argp);
1577+
err = loop_set_status64(lo, mode, bdev, argp);
15601578
break;
15611579
case LOOP_GET_STATUS64:
15621580
return loop_get_status64(lo, argp);
@@ -1650,16 +1668,17 @@ loop_info64_to_compat(const struct loop_info64 *info64,
16501668
}
16511669

16521670
static int
1653-
loop_set_status_compat(struct loop_device *lo,
1654-
const struct compat_loop_info __user *arg)
1671+
loop_set_status_compat(struct loop_device *lo, blk_mode_t mode,
1672+
struct block_device *bdev,
1673+
const struct compat_loop_info __user *arg)
16551674
{
16561675
struct loop_info64 info64;
16571676
int ret;
16581677

16591678
ret = loop_info64_from_compat(arg, &info64);
16601679
if (ret < 0)
16611680
return ret;
1662-
return loop_set_status(lo, &info64);
1681+
return loop_set_status(lo, mode, bdev, &info64);
16631682
}
16641683

16651684
static int
@@ -1685,7 +1704,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
16851704

16861705
switch(cmd) {
16871706
case LOOP_SET_STATUS:
1688-
err = loop_set_status_compat(lo,
1707+
err = loop_set_status_compat(lo, mode, bdev,
16891708
(const struct compat_loop_info __user *)arg);
16901709
break;
16911710
case LOOP_GET_STATUS:

0 commit comments

Comments
 (0)