public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: Mayur Agnihotri <mayur.agnihotri0021@gmail.com>,
	Laurent Vivier <lvivier@redhat.com>
Subject: [PATCH] vhost-user: Abort right away on invalid queue indices from front-end
Date: Mon, 23 Nov 2026 01:27:28 +0100	[thread overview]
Message-ID: <20261123002728.4072795-1-sbrivio@redhat.com> (raw)

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 <mayur.agnihotri0021@gmail.com>
Suggested-by: Mayur Agnihotri <mayur.agnihotri0021@gmail.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: Laurent Vivier <lvivier@redhat.com>
---
 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


                 reply	other threads:[~2026-11-23  0:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123002728.4072795-1-sbrivio@redhat.com \
    --to=sbrivio@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=mayur.agnihotri0021@gmail.com \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).