Skip to content

Commit b367318

Browse files
jankaraopsiff
authored andcommitted
loop: Avoid updating block size under exclusive owner
[ Upstream commit 7e49538 ] Syzbot came up with a reproducer where a loop device block size is changed underneath a mounted filesystem. This causes a mismatch between the block device block size and the block size stored in the superblock causing confusion in various places such as fs/buffer.c. The particular issue triggered by syzbot was a warning in __getblk_slow() due to requested buffer size not matching block device block size. Fix the problem by getting exclusive hold of the loop device to change its block size. This fails if somebody (such as filesystem) has already an exclusive ownership of the block device and thus prevents modifying the loop device under some exclusive owner which doesn't expect it. Reported-by: syzbot+01ef7a8da81a975e1ccd@syzkaller.appspotmail.com Signed-off-by: Jan Kara <jack@suse.cz> Tested-by: syzbot+01ef7a8da81a975e1ccd@syzkaller.appspotmail.com Link: https://lore.kernel.org/r/20250711163202.19623-2-jack@suse.cz Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org> (cherry picked from commit ce8da5d13d8c2a7b30b2fb376a22e8eb1a70b8bb)
1 parent 118ba78 commit b367318

1 file changed

Lines changed: 30 additions & 8 deletions

File tree

drivers/block/loop.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,19 +1472,36 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
14721472
return error;
14731473
}
14741474

1475-
static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1475+
static int loop_set_block_size(struct loop_device *lo, blk_mode_t mode,
1476+
struct block_device *bdev, unsigned long arg)
14761477
{
14771478
int err = 0;
14781479

1479-
if (lo->lo_state != Lo_bound)
1480-
return -ENXIO;
1480+
/*
1481+
* If we don't hold exclusive handle for the device, upgrade to it
1482+
* here to avoid changing device under exclusive owner.
1483+
*/
1484+
if (!(mode & BLK_OPEN_EXCL)) {
1485+
err = bd_prepare_to_claim(bdev, loop_set_block_size, NULL);
1486+
if (err)
1487+
return err;
1488+
}
1489+
1490+
err = mutex_lock_killable(&lo->lo_mutex);
1491+
if (err)
1492+
goto abort_claim;
1493+
1494+
if (lo->lo_state != Lo_bound) {
1495+
err = -ENXIO;
1496+
goto unlock;
1497+
}
14811498

14821499
err = blk_validate_block_size(arg);
14831500
if (err)
14841501
return err;
14851502

14861503
if (lo->lo_queue->limits.logical_block_size == arg)
1487-
return 0;
1504+
goto unlock;
14881505

14891506
sync_blockdev(lo->lo_device);
14901507
invalidate_bdev(lo->lo_device);
@@ -1496,6 +1513,11 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
14961513
loop_update_dio(lo);
14971514
blk_mq_unfreeze_queue(lo->lo_queue);
14981515

1516+
unlock:
1517+
mutex_unlock(&lo->lo_mutex);
1518+
abort_claim:
1519+
if (!(mode & BLK_OPEN_EXCL))
1520+
bd_abort_claiming(bdev, loop_set_block_size);
14991521
return err;
15001522
}
15011523

@@ -1514,9 +1536,6 @@ static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
15141536
case LOOP_SET_DIRECT_IO:
15151537
err = loop_set_dio(lo, arg);
15161538
break;
1517-
case LOOP_SET_BLOCK_SIZE:
1518-
err = loop_set_block_size(lo, arg);
1519-
break;
15201539
default:
15211540
err = -EINVAL;
15221541
}
@@ -1571,9 +1590,12 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
15711590
break;
15721591
case LOOP_GET_STATUS64:
15731592
return loop_get_status64(lo, argp);
1593+
case LOOP_SET_BLOCK_SIZE:
1594+
if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
1595+
return -EPERM;
1596+
return loop_set_block_size(lo, mode, bdev, arg);
15741597
case LOOP_SET_CAPACITY:
15751598
case LOOP_SET_DIRECT_IO:
1576-
case LOOP_SET_BLOCK_SIZE:
15771599
if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
15781600
return -EPERM;
15791601
fallthrough;

0 commit comments

Comments
 (0)