__ublk_complete_rq() passes io->res from the server straight to blk_update_request(). If it isn't a multiple of the logical block size, the data moves by io->res bytes but the start sector by io->res >> 9, so the requeued rest is read or written at the wrong place. Fail a READ or WRITE with -EIO if io->res isn't a multiple of the logical block size or is larger than the request, as is already done for a READ that returns 0. Fail a WRITE that returns 0 too, so that it can't be requeued forever once a short WRITE is completed by io->res again. The byte count of a READ's data that is copied only in part from the server's buffer has the same problem. Fail the READ if that count isn't a multiple of the logical block size. Fixes: 71f28f3136af ("ublk_drv: add io_uring based userspace block driver") Cc: stable@vger.kernel.org Signed-off-by: Ming Lei Reviewed-by: Caleb Sander Mateos --- drivers/block/ublk_drv.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 0699f22714fd..10dd20e7a3de 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -1488,6 +1488,23 @@ static inline bool ublk_need_unmap_req(const struct request *req) (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_DRV_IN); } +static inline bool ublk_req_is_rw(const struct request *req) +{ + return req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE; +} + +/* + * A short READ or WRITE is completed by @bytes and its rest is requeued. + * @bytes has to be whole logical blocks, so that the rest starts at the + * right sector, and not zero, or the request is requeued forever. + */ +static inline bool ublk_short_rw_valid(const struct request *req, + unsigned int bytes) +{ + return bytes && bytes < blk_rq_bytes(req) && + IS_ALIGNED(bytes, queue_logical_block_size(req->q)); +} + static unsigned int ublk_map_io(const struct request *req, const struct ublk_io *io) { @@ -1577,15 +1594,21 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, blk_status_t res = BLK_STS_OK; bool requeue; - /* failed read IO if nothing is read */ - if (!io->res && req_op(req) == REQ_OP_READ) - io->res = -EIO; - if (io->res < 0) { res = errno_to_blk_status(io->res); goto exit; } + /* + * The result of READ and WRITE is the number of bytes done. Fail an + * invalid one: more than the request, not aligned, or nothing done. + */ + if (unlikely(io->res != blk_rq_bytes(req)) && ublk_req_is_rw(req) && + !ublk_short_rw_valid(req, io->res)) { + res = BLK_STS_IOERR; + goto exit; + } + /* shmem zero copy: no data to unmap, pages already shared */ if (!need_map || !ublk_need_unmap_req(req) || ublk_iod_is_shmem_zc(req->mq_hctx->driver_data, req->tag)) @@ -1600,7 +1623,7 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io, * Re-read simply for this unlikely case. */ if (unlikely(unmapped_bytes < io->res)) { - if (unlikely(!unmapped_bytes)) { + if (!ublk_short_rw_valid(req, unmapped_bytes)) { res = BLK_STS_IOERR; goto exit; } -- 2.56.1