public inbox for virtualization@lists.linux-foundation.org
 help / color / mirror / Atom feed
From: Li Wang <liwang@kylinos.cn>
To: Stefan Hajnoczi <stefanha@redhat.com>,
	German Maglione <gmaglione@redhat.com>,
	Vivek Goyal <vgoyal@redhat.com>,
	Miklos Szeredi <miklos@szeredi.hu>
Cc: "Eugenio Pérez" <eperezma@redhat.com>,
	virtualization@lists.linux.dev, linux-fsdevel@vger.kernel.org,
	"Li Wang" <liwang@kylinos.cn>
Subject: [PATCH] [PATCH v2] virtiofs: Strengthen FUSE protocol validation for server responses
Date: Mon, 23 Mar 2026 16:14:54 +0800	[thread overview]
Message-ID: <20260323081455.38007-1-liwang@kylinos.cn> (raw)

1. We should not trust the length value provided by the server;
2. Only replies have an out header/argbuf data to parse;
3. Some minor refinements for performance and log throttling.

Signed-off-by: Li Wang <liwang@kylinos.cn>
---
 fs/fuse/virtio_fs.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 2f7485ffac52..c3f7f472f517 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -759,23 +759,31 @@ static void copy_args_from_argbuf(struct fuse_args *args, struct fuse_req *req)
 }
 
 /* Verify that the server properly follows the FUSE protocol */
-static bool virtio_fs_verify_response(struct fuse_req *req, unsigned int len)
+static bool virtio_fs_verify_response(struct fuse_req *req)
 {
+	unsigned int num_out, cap;
+	struct fuse_args *args;
 	struct fuse_out_header *oh = &req->out.h;
 
-	if (len < sizeof(*oh)) {
-		pr_warn("virtio-fs: response too short (%u)\n", len);
+	if (!test_bit(FR_ISREPLY, &req->flags))
 		return false;
-	}
-	if (oh->len != len) {
-		pr_warn("virtio-fs: oh.len mismatch (%u != %u)\n", oh->len, len);
+	if (unlikely(oh->unique != req->in.h.unique)) {
+		pr_warn_ratelimited("virtio-fs: bad reply unique %llu (expected %llu)\n",
+					oh->unique, req->in.h.unique);
 		return false;
 	}
-	if (oh->unique != req->in.h.unique) {
-		pr_warn("virtio-fs: oh.unique mismatch (%llu != %llu)\n",
-			oh->unique, req->in.h.unique);
+	args = req->args;
+	num_out = args->out_numargs - args->out_pages;
+	cap = sizeof(req->out.h);
+	cap += fuse_len_args(num_out, args->out_args);
+	if (args->out_pages)
+		cap += args->out_args[args->out_numargs - 1].size;
+	if (unlikely(oh->len < sizeof(*oh) || oh->len > cap)) {
+		pr_warn_ratelimited("virtio-fs: bad reply len %u (cap %u)\n",
+					oh->len, cap);
 		return false;
 	}
+
 	return true;
 }
 
@@ -841,7 +849,7 @@ static void virtio_fs_requests_done_work(struct work_struct *work)
 		virtqueue_disable_cb(vq);
 
 		while ((req = virtqueue_get_buf(vq, &len)) != NULL) {
-			if (!virtio_fs_verify_response(req, len)) {
+			if (test_bit(FR_ISREPLY, &req->flags) && !virtio_fs_verify_response(req)) {
 				req->out.h.error = -EIO;
 				req->out.h.len = sizeof(struct fuse_out_header);
 			}
-- 
2.34.1


             reply	other threads:[~2026-03-23  8:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-23  8:14 Li Wang [this message]
2026-03-26 18:47 ` [PATCH] [PATCH v2] virtiofs: Strengthen FUSE protocol validation for server responses Stefan Hajnoczi

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=20260323081455.38007-1-liwang@kylinos.cn \
    --to=liwang@kylinos.cn \
    --cc=eperezma@redhat.com \
    --cc=gmaglione@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=stefanha@redhat.com \
    --cc=vgoyal@redhat.com \
    --cc=virtualization@lists.linux.dev \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox