public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: passt-dev@passt.top
Cc: Laurent Vivier <lvivier@redhat.com>
Subject: [PATCH] virtio: check if avail ring is configured
Date: Wed, 27 Nov 2024 11:25:21 +0100	[thread overview]
Message-ID: <20241127102521.3511320-1-lvivier@redhat.com> (raw)

If the connection to the vhost-user front end is closed during transfers
virtio rings are deconfigured and not available anymore, but we can
try to access them to process queued data. This can trigger a SIGSEG as
we try to access unavailable memory.
To fix that check vq->vring.avail is sane before accessing the vring

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 virtio.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/virtio.c b/virtio.c
index 6a97435e2965..0598ff479858 100644
--- a/virtio.c
+++ b/virtio.c
@@ -284,6 +284,9 @@ static int virtqueue_read_next_desc(const struct vring_desc *desc,
  */
 bool vu_queue_empty(struct vu_virtq *vq)
 {
+	if (!vq->vring.avail)
+		return true;
+
 	if (vq->shadow_avail_idx != vq->last_avail_idx)
 		return false;
 
@@ -327,6 +330,9 @@ static bool vring_can_notify(const struct vu_dev *dev, struct vu_virtq *vq)
  */
 void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq)
 {
+	if (!vq->vring.avail)
+		return;
+
 	if (!vring_can_notify(dev, vq)) {
 		debug("vhost-user: virtqueue can skip notify...");
 		return;
@@ -502,6 +508,9 @@ int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq, struct vu_virtq_elemen
 	unsigned int head;
 	int ret;
 
+	if (!vq->vring.avail)
+		return -1;
+
 	if (vu_queue_empty(vq))
 		return -1;
 
@@ -591,6 +600,9 @@ void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
 {
 	struct vring_used_elem uelem;
 
+	if (!vq->vring.avail)
+		return;
+
 	idx = (idx + vq->used_idx) % vq->vring.num;
 
 	uelem.id = htole32(index);
@@ -633,6 +645,9 @@ void vu_queue_flush(struct vu_virtq *vq, unsigned int count)
 {
 	uint16_t old, new;
 
+	if (!vq->vring.avail)
+		return;
+
 	/* Make sure buffer is written before we update index. */
 	smp_wmb();
 
-- 
@@ -284,6 +284,9 @@ static int virtqueue_read_next_desc(const struct vring_desc *desc,
  */
 bool vu_queue_empty(struct vu_virtq *vq)
 {
+	if (!vq->vring.avail)
+		return true;
+
 	if (vq->shadow_avail_idx != vq->last_avail_idx)
 		return false;
 
@@ -327,6 +330,9 @@ static bool vring_can_notify(const struct vu_dev *dev, struct vu_virtq *vq)
  */
 void vu_queue_notify(const struct vu_dev *dev, struct vu_virtq *vq)
 {
+	if (!vq->vring.avail)
+		return;
+
 	if (!vring_can_notify(dev, vq)) {
 		debug("vhost-user: virtqueue can skip notify...");
 		return;
@@ -502,6 +508,9 @@ int vu_queue_pop(struct vu_dev *dev, struct vu_virtq *vq, struct vu_virtq_elemen
 	unsigned int head;
 	int ret;
 
+	if (!vq->vring.avail)
+		return -1;
+
 	if (vu_queue_empty(vq))
 		return -1;
 
@@ -591,6 +600,9 @@ void vu_queue_fill_by_index(struct vu_virtq *vq, unsigned int index,
 {
 	struct vring_used_elem uelem;
 
+	if (!vq->vring.avail)
+		return;
+
 	idx = (idx + vq->used_idx) % vq->vring.num;
 
 	uelem.id = htole32(index);
@@ -633,6 +645,9 @@ void vu_queue_flush(struct vu_virtq *vq, unsigned int count)
 {
 	uint16_t old, new;
 
+	if (!vq->vring.avail)
+		return;
+
 	/* Make sure buffer is written before we update index. */
 	smp_wmb();
 
-- 
2.47.0


                 reply	other threads:[~2024-11-27 10:25 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=20241127102521.3511320-1-lvivier@redhat.com \
    --to=lvivier@redhat.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).