From: David Woodhouse cpu_ioreq_pio() and cpu_ioreq_move() iterated req->count times with no upper bound, so a count of UINT32_MAX kept the vCPU handler thread spinning through four billion iterations. Xen's instruction emulator clips a rep batch to at most 4096 repetitions (hvmemul_virtual_to_linear() and hvmemul_linear_to_phys() both apply that bound before an ioreq is sent), so reject larger counts as malformed, and reject a zero size, as the other framing checks here already do. Fixes: 9ce94e7c8a99 ("xen: Initialize event channels and io rings") Reported-by: Feifan Qian Closes: https://gitlab.com/qemu-project/qemu/-/work_items/3756 Signed-off-by: David Woodhouse --- hw/xen/xen-hvm-common.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/hw/xen/xen-hvm-common.c b/hw/xen/xen-hvm-common.c index 62d88804c43..48ce655448b 100644 --- a/hw/xen/xen-hvm-common.c +++ b/hw/xen/xen-hvm-common.c @@ -321,10 +321,20 @@ void cpu_ioreq_pio(ioreq_t *req) trace_cpu_ioreq_pio(req, req->dir, req->df, req->data_is_ptr, req->addr, req->data, req->count, req->size); - if (req->size > sizeof(uint32_t)) { + if (req->size == 0 || req->size > sizeof(uint32_t)) { hw_error("PIO: bad size (%u)", req->size); } + /* + * Xen's instruction emulator clips a rep batch to at most 4096 + * repetitions (hvmemul_virtual_to_linear(), hvmemul_linear_to_phys()), + * so the count of a well-formed ioreq is bounded. Don't spin for + * 2^32 iterations on a corrupted one. + */ + if (req->data_is_ptr && req->count > 4096) { + hw_error("PIO: bad count (%u)", req->count); + } + if (req->dir == IOREQ_READ) { if (!req->data_is_ptr) { req->data = do_inp(req->addr, req->size); @@ -361,10 +371,15 @@ static void cpu_ioreq_move(ioreq_t *req) trace_cpu_ioreq_move(req, req->dir, req->df, req->data_is_ptr, req->addr, req->data, req->count, req->size); - if (req->size > sizeof(req->data)) { + if (req->size == 0 || req->size > sizeof(req->data)) { hw_error("MMIO: bad size (%u)", req->size); } + /* As for PIO: Xen clips a rep batch to at most 4096 repetitions. */ + if (req->count > 4096) { + hw_error("MMIO: bad count (%u)", req->count); + } + if (!req->data_is_ptr) { if (req->dir == IOREQ_READ) { for (i = 0; i < req->count; i++) { -- 2.43.0