Skip to content

Commit 25966fc

Browse files
mehulraoaxboe
authored andcommitted
ublk: fix NULL pointer dereference in ublk_ctrl_set_size()
ublk_ctrl_set_size() unconditionally dereferences ub->ub_disk via set_capacity_and_notify() without checking if it is NULL. ub->ub_disk is NULL before UBLK_CMD_START_DEV completes (it is only assigned in ublk_ctrl_start_dev()) and after UBLK_CMD_STOP_DEV runs (ublk_detach_disk() sets it to NULL). Since the UBLK_CMD_UPDATE_SIZE handler performs no state validation, a user can trigger a NULL pointer dereference by sending UPDATE_SIZE to a device that has been added but not yet started, or one that has been stopped. Fix this by checking ub->ub_disk under ub->mutex before dereferencing it, and returning -ENODEV if the disk is not available. Fixes: 98b9956 ("ublk: Add UBLK_U_CMD_UPDATE_SIZE") Cc: [email protected] Signed-off-by: Mehul Rao <[email protected]> Reviewed-by: Ming Lei <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent ce8ee85 commit 25966fc

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,15 +5003,22 @@ static int ublk_ctrl_get_features(const struct ublksrv_ctrl_cmd *header)
50035003
return 0;
50045004
}
50055005

5006-
static void ublk_ctrl_set_size(struct ublk_device *ub, const struct ublksrv_ctrl_cmd *header)
5006+
static int ublk_ctrl_set_size(struct ublk_device *ub, const struct ublksrv_ctrl_cmd *header)
50075007
{
50085008
struct ublk_param_basic *p = &ub->params.basic;
50095009
u64 new_size = header->data[0];
5010+
int ret = 0;
50105011

50115012
mutex_lock(&ub->mutex);
5013+
if (!ub->ub_disk) {
5014+
ret = -ENODEV;
5015+
goto out;
5016+
}
50125017
p->dev_sectors = new_size;
50135018
set_capacity_and_notify(ub->ub_disk, p->dev_sectors);
5019+
out:
50145020
mutex_unlock(&ub->mutex);
5021+
return ret;
50155022
}
50165023

50175024
struct count_busy {
@@ -5331,8 +5338,7 @@ static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
53315338
ret = ublk_ctrl_end_recovery(ub, &header);
53325339
break;
53335340
case UBLK_CMD_UPDATE_SIZE:
5334-
ublk_ctrl_set_size(ub, &header);
5335-
ret = 0;
5341+
ret = ublk_ctrl_set_size(ub, &header);
53365342
break;
53375343
case UBLK_CMD_QUIESCE_DEV:
53385344
ret = ublk_ctrl_quiesce_dev(ub, &header);

0 commit comments

Comments
 (0)