From: Thanos Makatos <[email protected]> If the vfio-user server responds with a value larger than max_xfer_size vfio_device_get_region_info() blindly uses it in the next loop in g_realloc. An value larger than max_xfer_size is anyway rejected by the check at the beginning of vfio_user_get_region_info(), however that only happens _after_ the g_realloc, and if that value is excessively large it can cause g_realloc to fail, so check it here.
Signed-off-by: Thanos Makatos <[email protected]> Fixes: 667866d66620 ("vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO") Reviewed-by: Cédric Le Goater <[email protected]> Link: https://lore.kernel.org/qemu-devel/[email protected] Signed-off-by: Cédric Le Goater <[email protected]> (cherry picked from commit 6a51aab908e0f043387b91ad6a198b0eae5f2b58) Signed-off-by: Michael Tokarev <[email protected]> diff --git a/hw/vfio-user/device.c b/hw/vfio-user/device.c index df86dabe1e6..580559d9b95 100644 --- a/hw/vfio-user/device.c +++ b/hw/vfio-user/device.c @@ -129,6 +129,16 @@ static int vfio_user_get_region_info(VFIOUserProxy *proxy, return -EINVAL; } + /* + * The server can respond with a larger argsz in the reply to request a + * larger buffer on the next iteration via vfio_device_get_region_info(). + * Reject values that would trigger an oversized realloc. + */ + if (msgp->argsz > proxy->max_xfer_size) { + error_printf("vfio_user_get_region_info reply argsz too large\n"); + return -E2BIG; + } + memcpy(info, &msgp->argsz, info->argsz); /* -- 2.47.3
