Skip to contentSkip to conversation

fuse: virtio_fs: add checks for FUSE protocol compliance

PATCH In review Brajesh Patil 2 participants 5 messages Oct 28, 2025 – Oct 31, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse: virtio_fs: add checks for FUSE protocol compliance

Add validation in virtio-fs to ensure the server follows the FUSE protocol for response headers, addressing the existing TODO for verifying protocol compliance.

Add checks for fuse_out_header to verify:
- oh->unique matches req->in.h.unique
- FUSE_INT_REQ_BIT is not set
- error codes are valid
- oh->len does not exceed the expected size

---
 fs/fuse/virtio_fs.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
fs/fuse/virtio_fs.c +25 -5
index 6bc7c97b017d..52e8338bf436 100644
@@ -764,14 +764,34 @@ static void virtio_fs_request_complete(struct fuse_req *req,
 {
 	struct fuse_args *args;
 	struct fuse_args_pages *ap;
-	unsigned int len, i, thislen;
+	struct fuse_out_header *oh;
+	unsigned int len, i, thislen, expected_len = 0;
 	struct folio *folio;
 
-	/*
-	 * TODO verify that server properly follows FUSE protocol
-	 * (oh.uniq, oh.len)
-	 */
+	oh = &req->out.h;
+
+	if (oh->unique == 0)
+		pr_warn_once("notify through fuse-virtio-fs not supported");
+
+	if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique)
+		pr_warn_ratelimited("virtio-fs: unique mismatch, expected: %llu got %llu\n",
+				    req->in.h.unique, oh->unique & ~FUSE_INT_REQ_BIT);
+
+	WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
+
+	if (oh->error <= -ERESTARTSYS || oh->error > 0)
+		pr_warn_ratelimited("virtio-fs: invalid error code from server: %d\n",
+				    oh->error);
+
 	args = req->args;
+
+	for (i = 0; i < args->out_numargs; i++)
+		expected_len += args->out_args[i].size;
+
+	if (oh->len > sizeof(*oh) + expected_len)
+		pr_warn("FUSE reply too long! got=%u expected<=%u\n",
+			oh->len, (unsigned int)(sizeof(*oh) + expected_len));
+
 	copy_args_from_argbuf(args, req);
 
 	if (args->out_pages && args->page_zeroing) {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 30 lines · Brajesh Patil

Add validation in virtio-fs to ensure the server follows the FUSE protocol for response headers, addressing the existing TODO for verifying protocol compliance.

Add checks for fuse_out_header to verify:
- oh->unique matches req->in.h.unique
- FUSE_INT_REQ_BIT is not set
- error codes are valid
- oh->len does not exceed the expected size

---
 fs/fuse/virtio_fs.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
fs/fuse/virtio_fs.c +2 -3
index 6bc7c97b017d..52e8338bf436 100644
@@ -764,14 +764,34 @@ static void virtio_fs_request_complete(struct fuse_req *req,
 {
 	struct fuse_args *args;
 	struct fuse_args_pages *ap;
-	unsigned int len, i, thislen;
+	struct fuse_out_header *oh;
+	unsigned int len, i, thislen, expected_len = 0;
 	struct folio *folio;
 
-	/*
-	 * TODO verify that server properly follows FUSE protocol

fs/fuse/virtio_fs.c static void virtio_fs_request_complete(struct fuse_req *req, · line 778

Quoted 10 lines
fs/fuse/virtio_fs.c +8 -2
-	 * (oh.uniq, oh.len)
-	 */
+	oh = &req->out.h;
+
+	if (oh->unique == 0)
+		pr_warn_once("notify through fuse-virtio-fs not supported");
+
+	if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique)
+		pr_warn_ratelimited("virtio-fs: unique mismatch, expected: %llu got %llu\n",
+				    req->in.h.unique, oh->unique & ~FUSE_INT_REQ_BIT);

Er... shouldn't these be rejecting the response somehow? Instead of warning that something's amiss but continuing with known bad data?

--D

Quoted 23 lines
fs/fuse/virtio_fs.c +15 -0
+
+	WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
+
+	if (oh->error <= -ERESTARTSYS || oh->error > 0)
+		pr_warn_ratelimited("virtio-fs: invalid error code from server: %d\n",
+				    oh->error);
+
 	args = req->args;
+
+	for (i = 0; i < args->out_numargs; i++)
+		expected_len += args->out_args[i].size;
+
+	if (oh->len > sizeof(*oh) + expected_len)
+		pr_warn("FUSE reply too long! got=%u expected<=%u\n",
+			oh->len, (unsigned int)(sizeof(*oh) + expected_len));
+
 	copy_args_from_argbuf(args, req);
 
 	if (args->out_pages && args->page_zeroing) {
Signature · 1 line
2.43.0
#2 lore raw
Quoted 45 lines · Darrick J. Wong
Quoted 30 lines · Brajesh Patil

Add validation in virtio-fs to ensure the server follows the FUSE protocol for response headers, addressing the existing TODO for verifying protocol compliance.

Add checks for fuse_out_header to verify:
- oh->unique matches req->in.h.unique
- FUSE_INT_REQ_BIT is not set
- error codes are valid
- oh->len does not exceed the expected size

---
 fs/fuse/virtio_fs.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
fs/fuse/virtio_fs.c +2 -3
index 6bc7c97b017d..52e8338bf436 100644
@@ -764,14 +764,34 @@ static void virtio_fs_request_complete(struct fuse_req *req,
 {
 	struct fuse_args *args;
 	struct fuse_args_pages *ap;
-	unsigned int len, i, thislen;
+	struct fuse_out_header *oh;
+	unsigned int len, i, thislen, expected_len = 0;
 	struct folio *folio;
 
-	/*
-	 * TODO verify that server properly follows FUSE protocol

fs/fuse/virtio_fs.c static void virtio_fs_request_complete(struct fuse_req *req, · line 778

Quoted 10 lines
fs/fuse/virtio_fs.c +8 -2
-	 * (oh.uniq, oh.len)
-	 */
+	oh = &req->out.h;
+
+	if (oh->unique == 0)
+		pr_warn_once("notify through fuse-virtio-fs not supported");
+
+	if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique)
+		pr_warn_ratelimited("virtio-fs: unique mismatch, expected: %llu got %llu\n",
+				    req->in.h.unique, oh->unique & ~FUSE_INT_REQ_BIT);

Er... shouldn't these be rejecting the response somehow? Instead of warning that something's amiss but continuing with known bad data?

Quoted 2 lines

--D

Right, continuing here is unsafe.

I plan to update the code so that in case of any header validation failure (e.g. unique mismatch, invalid error, length mismatch), it should skip copying data and jump directly to the section that marks request as complete

Does this seem like a feasible approach?

Quoted 23 lines
Quoted 23 lines
fs/fuse/virtio_fs.c +15 -0
+
+	WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
+
+	if (oh->error <= -ERESTARTSYS || oh->error > 0)
+		pr_warn_ratelimited("virtio-fs: invalid error code from server: %d\n",
+				    oh->error);
+
 	args = req->args;
+
+	for (i = 0; i < args->out_numargs; i++)
+		expected_len += args->out_args[i].size;
+
+	if (oh->len > sizeof(*oh) + expected_len)
+		pr_warn("FUSE reply too long! got=%u expected<=%u\n",
+			oh->len, (unsigned int)(sizeof(*oh) + expected_len));
+
 	copy_args_from_argbuf(args, req);
 
 	if (args->out_pages && args->page_zeroing) {
Signature · 1 line
2.43.0
#3 lore raw
Quoted 49 lines · Brajesh Patil
Quoted 45 lines · Darrick J. Wong
Quoted 30 lines · Brajesh Patil

Add validation in virtio-fs to ensure the server follows the FUSE protocol for response headers, addressing the existing TODO for verifying protocol compliance.

Add checks for fuse_out_header to verify:
- oh->unique matches req->in.h.unique
- FUSE_INT_REQ_BIT is not set
- error codes are valid
- oh->len does not exceed the expected size

---
 fs/fuse/virtio_fs.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
fs/fuse/virtio_fs.c +2 -3
index 6bc7c97b017d..52e8338bf436 100644
@@ -764,14 +764,34 @@ static void virtio_fs_request_complete(struct fuse_req *req,
 {
 	struct fuse_args *args;
 	struct fuse_args_pages *ap;
-	unsigned int len, i, thislen;
+	struct fuse_out_header *oh;
+	unsigned int len, i, thislen, expected_len = 0;
 	struct folio *folio;
 
-	/*
-	 * TODO verify that server properly follows FUSE protocol

fs/fuse/virtio_fs.c static void virtio_fs_request_complete(struct fuse_req *req, · line 778

Quoted 10 lines
fs/fuse/virtio_fs.c +8 -2
-	 * (oh.uniq, oh.len)
-	 */
+	oh = &req->out.h;
+
+	if (oh->unique == 0)
+		pr_warn_once("notify through fuse-virtio-fs not supported");
+
+	if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique)
+		pr_warn_ratelimited("virtio-fs: unique mismatch, expected: %llu got %llu\n",
+				    req->in.h.unique, oh->unique & ~FUSE_INT_REQ_BIT);

Er... shouldn't these be rejecting the response somehow? Instead of warning that something's amiss but continuing with known bad data?

Quoted 2 lines

--D

Quoted 8 lines

Right, continuing here is unsafe.

I plan to update the code so that in case of any header validation failure (e.g. unique mismatch, invalid error, length mismatch), it should skip copying data and jump directly to the section that marks request as complete

Does this seem like a feasible approach?

Yeah, I think you can just set req->out.h.error to some errno (EIO?) and jump to fuse_request_end, sort of like what fuse_dev_do_write sort of does. I think that sends the errno back to whatever code initiated the request. I don't know if virtiofs should be throwing an error back to the server?

--D

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
fs/fuse/virtio_fs.c +15 -0
+
+	WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
+
+	if (oh->error <= -ERESTARTSYS || oh->error > 0)
+		pr_warn_ratelimited("virtio-fs: invalid error code from server: %d\n",
+				    oh->error);
+
 	args = req->args;
+
+	for (i = 0; i < args->out_numargs; i++)
+		expected_len += args->out_args[i].size;
+
+	if (oh->len > sizeof(*oh) + expected_len)
+		pr_warn("FUSE reply too long! got=%u expected<=%u\n",
+			oh->len, (unsigned int)(sizeof(*oh) + expected_len));
+
 	copy_args_from_argbuf(args, req);
 
 	if (args->out_pages && args->page_zeroing) {
Signature · 1 line
2.43.0
#4 lore raw
Quoted 59 lines · Darrick J. Wong
Quoted 49 lines · Brajesh Patil
Quoted 45 lines · Darrick J. Wong
Quoted 30 lines · Brajesh Patil

Add validation in virtio-fs to ensure the server follows the FUSE protocol for response headers, addressing the existing TODO for verifying protocol compliance.

Add checks for fuse_out_header to verify:
- oh->unique matches req->in.h.unique
- FUSE_INT_REQ_BIT is not set
- error codes are valid
- oh->len does not exceed the expected size

---
 fs/fuse/virtio_fs.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)
fs/fuse/virtio_fs.c +2 -3
index 6bc7c97b017d..52e8338bf436 100644
@@ -764,14 +764,34 @@ static void virtio_fs_request_complete(struct fuse_req *req,
 {
 	struct fuse_args *args;
 	struct fuse_args_pages *ap;
-	unsigned int len, i, thislen;
+	struct fuse_out_header *oh;
+	unsigned int len, i, thislen, expected_len = 0;
 	struct folio *folio;
 
-	/*
-	 * TODO verify that server properly follows FUSE protocol

fs/fuse/virtio_fs.c static void virtio_fs_request_complete(struct fuse_req *req, · line 778

Quoted 10 lines
fs/fuse/virtio_fs.c +8 -2
-	 * (oh.uniq, oh.len)
-	 */
+	oh = &req->out.h;
+
+	if (oh->unique == 0)
+		pr_warn_once("notify through fuse-virtio-fs not supported");
+
+	if ((oh->unique & ~FUSE_INT_REQ_BIT) != req->in.h.unique)
+		pr_warn_ratelimited("virtio-fs: unique mismatch, expected: %llu got %llu\n",
+				    req->in.h.unique, oh->unique & ~FUSE_INT_REQ_BIT);

Er... shouldn't these be rejecting the response somehow? Instead of warning that something's amiss but continuing with known bad data?

Quoted 2 lines

--D

Quoted 8 lines

Right, continuing here is unsafe.

I plan to update the code so that in case of any header validation failure (e.g. unique mismatch, invalid error, length mismatch), it should skip copying data and jump directly to the section that marks request as complete

Does this seem like a feasible approach?

Quoted 8 lines

Yeah, I think you can just set req->out.h.error to some errno (EIO?) and jump to fuse_request_end, sort of like what fuse_dev_do_write sort of does. I think that sends the errno back to whatever code initiated the request. I don't know if virtiofs should be throwing an error back to the server?

--D

I think it is okay to set oh.error in fuse_dev_do_write as it is a server side reply. But as virtio_fs is on the client side and oh.error has been set by virtiofsd, I think so we should not overwrite oh.error. Instead, if we encounter an error in any of the if conditions, skip copying arguments and jump to the line clear_bit(FR_SENT, &req->flags).

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
fs/fuse/virtio_fs.c +15 -0
+
+	WARN_ON_ONCE(oh->unique & FUSE_INT_REQ_BIT);
+
+	if (oh->error <= -ERESTARTSYS || oh->error > 0)
+		pr_warn_ratelimited("virtio-fs: invalid error code from server: %d\n",
+				    oh->error);
+
 	args = req->args;
+
+	for (i = 0; i < args->out_numargs; i++)
+		expected_len += args->out_args[i].size;
+
+	if (oh->len > sizeof(*oh) + expected_len)
+		pr_warn("FUSE reply too long! got=%u expected<=%u\n",
+			oh->len, (unsigned int)(sizeof(*oh) + expected_len));
+
 	copy_args_from_argbuf(args, req);
 
 	if (args->out_pages && args->page_zeroing) {
Signature · 1 line
2.43.0