Skip to content

Commit f01f527

Browse files
Yousef13710axboe
authored andcommitted
ublk: snapshot batch commands before preparing I/O
The batch prepare path rereads its userspace element array when rolling back a partially prepared batch. Userspace can change an already processed tag before the second read, causing rollback to reject the replacement tag and leave earlier I/O slots prepared. The WARN_ON_ONCE() in the rollback path then fires. Copy the bounded batch into kernel memory before changing any I/O state and use the same snapshot for preparation and rollback. Commit and fetch batches retain the existing chunked userspace walk. Fixes: b256795 ("ublk: handle UBLK_U_IO_PREP_IO_CMDS") Reported-by: syzbot+1a67ee1aa79484801ec6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=1a67ee1aa79484801ec6 Signed-off-by: Yousef Alhouseen <alhouseenyousef@gmail.com> Reviewed-by: Ming Lei <tom.leiming@gmail.com> Link: https://patch.msgid.link/20260630211827.50475-1-alhouseenyousef@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 1e56f30 commit f01f527

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

drivers/block/ublk_drv.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3584,6 +3584,7 @@ ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
35843584
#define UBLK_CMD_BATCH_TMP_BUF_SZ (48 * 10)
35853585
struct ublk_batch_io_iter {
35863586
void __user *uaddr;
3587+
const u8 *kaddr;
35873588
unsigned done, total;
35883589
unsigned char elem_bytes;
35893590
/* copy to this buffer from user space */
@@ -3632,7 +3633,10 @@ static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter,
36323633
while (iter->done < iter->total) {
36333634
unsigned int len = min(sizeof(iter->buf), iter->total - iter->done);
36343635

3635-
if (copy_from_user(iter->buf, iter->uaddr + iter->done, len)) {
3636+
if (iter->kaddr) {
3637+
memcpy(iter->buf, iter->kaddr + iter->done, len);
3638+
} else if (copy_from_user(iter->buf, iter->uaddr + iter->done,
3639+
len)) {
36363640
pr_warn("ublk%d: read batch cmd buffer failed\n",
36373641
data->ub->dev_info.dev_id);
36383642
return -EFAULT;
@@ -3723,14 +3727,21 @@ static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)
37233727
.total = uc->nr_elem * uc->elem_bytes,
37243728
.elem_bytes = uc->elem_bytes,
37253729
};
3730+
void *cmd_buf;
37263731
int ret;
37273732

3733+
cmd_buf = vmemdup_user(iter.uaddr, iter.total);
3734+
if (IS_ERR(cmd_buf))
3735+
return PTR_ERR(cmd_buf);
3736+
iter.kaddr = cmd_buf;
3737+
37283738
mutex_lock(&data->ub->mutex);
37293739
ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io);
37303740

37313741
if (ret && iter.done)
37323742
ublk_batch_revert_prep_cmd(&iter, data);
37333743
mutex_unlock(&data->ub->mutex);
3744+
kvfree(cmd_buf);
37343745
return ret;
37353746
}
37363747

0 commit comments

Comments
 (0)