Skip to content

Commit 3b2dd8c

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 139a000d20f2f38ce34296feddd641d730fe1c08)
1 parent eab8e24 commit 3b2dd8c

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
@@ -1404,16 +1404,33 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
14041404
return error;
14051405
}
14061406

1407-
static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
1407+
static int loop_set_block_size(struct loop_device *lo, blk_mode_t mode,
1408+
struct block_device *bdev, unsigned long arg)
14081409
{
14091410
struct queue_limits lim;
14101411
int err = 0;
14111412

1412-
if (lo->lo_state != Lo_bound)
1413-
return -ENXIO;
1413+
/*
1414+
* If we don't hold exclusive handle for the device, upgrade to it
1415+
* here to avoid changing device under exclusive owner.
1416+
*/
1417+
if (!(mode & BLK_OPEN_EXCL)) {
1418+
err = bd_prepare_to_claim(bdev, loop_set_block_size, NULL);
1419+
if (err)
1420+
return err;
1421+
}
1422+
1423+
err = mutex_lock_killable(&lo->lo_mutex);
1424+
if (err)
1425+
goto abort_claim;
1426+
1427+
if (lo->lo_state != Lo_bound) {
1428+
err = -ENXIO;
1429+
goto unlock;
1430+
}
14141431

14151432
if (lo->lo_queue->limits.logical_block_size == arg)
1416-
return 0;
1433+
goto unlock;
14171434

14181435
sync_blockdev(lo->lo_device);
14191436
invalidate_bdev(lo->lo_device);
@@ -1425,6 +1442,11 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
14251442
loop_update_dio(lo);
14261443
blk_mq_unfreeze_queue(lo->lo_queue);
14271444

1445+
unlock:
1446+
mutex_unlock(&lo->lo_mutex);
1447+
abort_claim:
1448+
if (!(mode & BLK_OPEN_EXCL))
1449+
bd_abort_claiming(bdev, loop_set_block_size);
14281450
return err;
14291451
}
14301452

@@ -1443,9 +1465,6 @@ static int lo_simple_ioctl(struct loop_device *lo, unsigned int cmd,
14431465
case LOOP_SET_DIRECT_IO:
14441466
err = loop_set_dio(lo, arg);
14451467
break;
1446-
case LOOP_SET_BLOCK_SIZE:
1447-
err = loop_set_block_size(lo, arg);
1448-
break;
14491468
default:
14501469
err = -EINVAL;
14511470
}
@@ -1500,9 +1519,12 @@ static int lo_ioctl(struct block_device *bdev, blk_mode_t mode,
15001519
break;
15011520
case LOOP_GET_STATUS64:
15021521
return loop_get_status64(lo, argp);
1522+
case LOOP_SET_BLOCK_SIZE:
1523+
if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
1524+
return -EPERM;
1525+
return loop_set_block_size(lo, mode, bdev, arg);
15031526
case LOOP_SET_CAPACITY:
15041527
case LOOP_SET_DIRECT_IO:
1505-
case LOOP_SET_BLOCK_SIZE:
15061528
if (!(mode & BLK_OPEN_WRITE) && !capable(CAP_SYS_ADMIN))
15071529
return -EPERM;
15081530
fallthrough;

0 commit comments

Comments
 (0)