Skip to content

Commit ee40552

Browse files
raphaelpthibeaultkawasaki
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: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=3ee481e21fd75e14c397 Tested-by: [email protected] Signed-off-by: Raphael Pinsonneault-Thibeault <[email protected]>
1 parent 4458758 commit ee40552

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

drivers/block/loop.c

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

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

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

@@ -1353,25 +1367,29 @@ loop_info64_to_old(const struct loop_info64 *info64, struct loop_info *info)
13531367
}
13541368

13551369
static int
1356-
loop_set_status_old(struct loop_device *lo, const struct loop_info __user *arg)
1370+
loop_set_status_old(struct loop_device *lo, blk_mode_t mode,
1371+
struct block_device *bdev,
1372+
const struct loop_info __user *arg)
13571373
{
13581374
struct loop_info info;
13591375
struct loop_info64 info64;
13601376

13611377
if (copy_from_user(&info, arg, sizeof (struct loop_info)))
13621378
return -EFAULT;
13631379
loop_info64_from_old(&info, &info64);
1364-
return loop_set_status(lo, &info64);
1380+
return loop_set_status(lo, mode, bdev, &info64);
13651381
}
13661382

13671383
static int
1368-
loop_set_status64(struct loop_device *lo, const struct loop_info64 __user *arg)
1384+
loop_set_status64(struct loop_device *lo, blk_mode_t mode,
1385+
struct block_device *bdev,
1386+
const struct loop_info64 __user *arg)
13691387
{
13701388
struct loop_info64 info64;
13711389

13721390
if (copy_from_user(&info64, arg, sizeof (struct loop_info64)))
13731391
return -EFAULT;
1374-
return loop_set_status(lo, &info64);
1392+
return loop_set_status(lo, mode, bdev, &info64);
13751393
}
13761394

13771395
static int
@@ -1550,14 +1568,14 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
15501568
case LOOP_SET_STATUS:
15511569
err = -EPERM;
15521570
if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1553-
err = loop_set_status_old(lo, argp);
1571+
err = loop_set_status_old(lo, mode, bdev, argp);
15541572
break;
15551573
case LOOP_GET_STATUS:
15561574
return loop_get_status_old(lo, argp);
15571575
case LOOP_SET_STATUS64:
15581576
err = -EPERM;
15591577
if ((mode & BLK_OPEN_WRITE) || capable(CAP_SYS_ADMIN))
1560-
err = loop_set_status64(lo, argp);
1578+
err = loop_set_status64(lo, mode, bdev, argp);
15611579
break;
15621580
case LOOP_GET_STATUS64:
15631581
return loop_get_status64(lo, argp);
@@ -1651,16 +1669,17 @@ loop_info64_to_compat(const struct loop_info64 *info64,
16511669
}
16521670

16531671
static int
1654-
loop_set_status_compat(struct loop_device *lo,
1655-
const struct compat_loop_info __user *arg)
1672+
loop_set_status_compat(struct loop_device *lo, blk_mode_t mode,
1673+
struct block_device *bdev,
1674+
const struct compat_loop_info __user *arg)
16561675
{
16571676
struct loop_info64 info64;
16581677
int ret;
16591678

16601679
ret = loop_info64_from_compat(arg, &info64);
16611680
if (ret < 0)
16621681
return ret;
1663-
return loop_set_status(lo, &info64);
1682+
return loop_set_status(lo, mode, bdev, &info64);
16641683
}
16651684

16661685
static int
@@ -1686,7 +1705,7 @@ static int lo_compat_ioctl(struct block_device *bdev, blk_mode_t mode,
16861705

16871706
switch(cmd) {
16881707
case LOOP_SET_STATUS:
1689-
err = loop_set_status_compat(lo,
1708+
err = loop_set_status_compat(lo, mode, bdev,
16901709
(const struct compat_loop_info __user *)arg);
16911710
break;
16921711
case LOOP_GET_STATUS:

0 commit comments

Comments
 (0)