From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 775B35A0271; Mon, 23 Nov 2026 01:27:28 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH] vhost-user: Abort right away on invalid queue indices from front-end Date: Mon, 23 Nov 2026 01:27:28 +0100 Message-ID: <20261123002728.4072795-1-sbrivio@redhat.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 6NY5FHBE5WR7N23XA6EBJV5EJMJDZZC2 X-Message-ID-Hash: 6NY5FHBE5WR7N23XA6EBJV5EJMJDZZC2 X-MailFrom: sbrivio@passt.top X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Mayur Agnihotri , Laurent Vivier X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Mayur Agnihotri reports that, for vhost-user commands SET_VRING_NUM, SET_VRING_ADDR, SET_VRING_BASE, and GET_VRING_BASE, we use the queue index passed by the front-end without further validation. We have, however, only VHOST_USER_MAX_VQS (2) entries in vdev->vq[], which might lead to out-of-bound accesses. These are not security relevant, because the front-end (hypervisor) is trusted, and even the most critical robustness impact we could achieve is setting feature flags, or the equivalent outcome of other (unintended) commands that the vhost-user front-end could anyway issue. In any case, this is a set of CWE-129 weaknesses that we want to address, regardless of the impact, just like QEMU commit 758ef96a2dba ("libvhost-user: protect against OOB vring queue access") did for the equivalent implementation in QEMU: verify the indices before using them, and abort right away if they are invalid. On top of those four commands, note that the handlers for SET_VRING_KICK and SET_VRING_ERR have the same issue: fix them as well. We could simply ignore those commands, and that might appear a more graceful way of handling invalid values, but if we receive bad indices in these commands, we should assume that something went wrong with our state and, overall, we'll probably offer users a better behaviour by terminating and giving the hypervisor the chance to see that the back-end failed. At least in some cases (e.g. libvirt), by default, we would be restarted and the front-end would try to reconnect. While at it, change some indices to unsigned types, for consistency and for correctness. We would like to thank Mayur Agnihotri for the detailed report and for the insightful investigation. Reported-by: Mayur Agnihotri Suggested-by: Mayur Agnihotri Signed-off-by: Stefano Brivio Reviewed-by: Laurent Vivier --- vhost_user.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/vhost_user.c b/vhost_user.c index 272295f..627bbee 100644 --- a/vhost_user.c +++ b/vhost_user.c @@ -639,6 +639,10 @@ static bool vu_set_vring_num_exec(struct vu_dev *vdev, trace("State.index: %u", idx); trace("State.num: %u", num); + + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (SET_VRING_NUM): %u", idx); + vdev->vq[idx].vring.num = num; return false; @@ -659,7 +663,14 @@ static bool vu_set_vring_addr_exec(struct vu_dev *vdev, * can be unaligned as it is packed. */ struct vhost_vring_addr addr = vmsg->payload.addr; - struct vu_virtq *vq = &vdev->vq[addr.index]; + struct vu_virtq *vq; + + if (addr.index >= VHOST_USER_MAX_VQS) { + die("Invalid vhost-user queue index (SET_VRING_ADDR): %u", + addr.index); + } + + vq = &vdev->vq[addr.index]; debug("vhost_vring_addr:"); debug(" index: %d", addr.index); @@ -705,6 +716,10 @@ static bool vu_set_vring_base_exec(struct vu_dev *vdev, debug("State.index: %u", idx); debug("State.num: %u", num); + + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (SET_VRING_BASE): %u", idx); + vdev->vq[idx].shadow_avail_idx = vdev->vq[idx].last_avail_idx = num; return false; @@ -724,6 +739,10 @@ static bool vu_get_vring_base_exec(struct vu_dev *vdev, unsigned int idx = vmsg->payload.state.index; debug("State.index: %u", idx); + + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (GET_VRING_BASE): %u", idx); + vmsg->payload.state.num = vdev->vq[idx].last_avail_idx; vmsg->hdr.size = sizeof(vmsg->payload.state); @@ -792,11 +811,14 @@ static void vu_check_queue_msg_file(struct vhost_user_msg *vmsg) static bool vu_set_vring_kick_exec(struct vu_dev *vdev, struct vhost_user_msg *vmsg) { + unsigned idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; - int idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; debug("u64: 0x%016"PRIx64, vmsg->payload.u64); + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (SET_VRING_KICK): %u", idx); + vu_check_queue_msg_file(vmsg); if (vdev->vq[idx].kick_fd != -1) { @@ -832,11 +854,14 @@ static bool vu_set_vring_kick_exec(struct vu_dev *vdev, static bool vu_set_vring_call_exec(struct vu_dev *vdev, struct vhost_user_msg *vmsg) { + unsigned idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; - int idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; debug("u64: 0x%016"PRIx64, vmsg->payload.u64); + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (SET_VRING_ADDR): %u", idx); + vu_check_queue_msg_file(vmsg); if (vdev->vq[idx].call_fd != -1) { @@ -867,11 +892,14 @@ static bool vu_set_vring_call_exec(struct vu_dev *vdev, static bool vu_set_vring_err_exec(struct vu_dev *vdev, struct vhost_user_msg *vmsg) { + unsigned idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; bool nofd = vmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK; - int idx = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK; debug("u64: 0x%016"PRIx64, vmsg->payload.u64); + if (idx >= VHOST_USER_MAX_VQS) + die("Invalid vhost-user queue index (SET_VRING_ERR): %u", idx); + vu_check_queue_msg_file(vmsg); if (vdev->vq[idx].err_fd != -1) { -- 2.43.0