Linux 9p file system development
 help / color / mirror / Atom feed
* [PATCH v2 0/5] 9p: bound zero-copy error replies
@ 2026-11-23 20:42 Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 1/5] 9p/virtio: clamp the reply size of a zero-copy request Lianjie Wang
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa

With the error reply to a zero-copy request, a virtio-9p device can make
the client read past the posted buffers (patch 1), also with an RLERROR
under 9P2000 and 9P2000.u (patch 2) and with a reply after a fatal
signal (patch 3). Patch 4 fixes a double put of a zero-copy request
interrupted by a fatal signal while waiting for the ring. Patch 5 fixes
an uninitialized value printed in a debug message.

The patches were tested with QEMU modified to send the malformed or slow
replies.

Patches 3 to 5 of v1 are left out in v2, since patch 3 of v1 fixes the
use-after-free on a request returned after the RFLUSH, but puts a freed
request returned after an umount [1], because an umount frees the client
and the requests while the device still holds them. Fixing this entirely
appears to require deferring the destruction of the client to a
workqueue until the device has returned every request, which is costly
and beyond the current scope. Patches 4 and 5 of v1 depend on patch 3,
so these patches may be sent again in a later series.

v2:
- Rebase onto v7.4-rc4.
- Add patch 2 for the RLERROR case raised in the review [2].
- In patch 3, accept a late reply within the part of rc.sdata posted for
  the header, as raised in the review [3].
- Add patch 4 for the double put that patch 3 of v1 also fixed.
- Add patch 5 for the uninitialized ecode raised in the review [4].
- Drop patches 3 to 5 of v1.

[1] https://lore.kernel.org/all/179446999129.2199510.15094266003962078465@kernel.org/
[2] https://lore.kernel.org/all/179446998758.2199510.14994914047354508278@kernel.org/
[3] https://lore.kernel.org/all/179446998948.2199510.3182298225630042557@kernel.org/
[4] https://lore.kernel.org/all/sashiko-outbox-206901@kernel.org/
v1: https://lore.kernel.org/all/20261109105251.2545764-1-lian@is.s.u-tokyo.ac.jp/

Lianjie Wang (5):
  9p/virtio: clamp the reply size of a zero-copy request
  9p/virtio: move the error string of an RLERROR into rc.sdata
  9p: bound the zero-copy reply after a fatal signal
  9p/virtio: set REQ_STATUS_SENT after adding a zero-copy request
  9p: initialize ecode in p9_check_errors()

 net/9p/client.c       | 16 ++++++++++++----
 net/9p/trans_virtio.c | 15 ++++++++++-----
 2 files changed, 22 insertions(+), 9 deletions(-)


base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.56.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/5] 9p/virtio: clamp the reply size of a zero-copy request
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
@ 2026-11-23 20:42 ` Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 2/5] 9p/virtio: move the error string of an RLERROR into rc.sdata Lianjie Wang
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa, Yizhou Zhao, stable

req_done() takes the used length from the device as the reply size
without checking it against the posted buffers, so handle_rerror() can
read past the in_pages array when copying an oversized error reply to a
zero-copy request:

  BUG: KASAN: slab-out-of-bounds in handle_rerror+0x144/0x230
  Read of size 8 at addr ffff888002ee02c8 by task zc9p/155
  Call Trace:
   handle_rerror+0x144/0x230
   p9_virtio_zc_request+0xb8e/0xd70
   p9_client_zc_rpc+0x21c/0x970
   p9_client_read_once+0x237/0x6c0
  The buggy address is located 0 bytes to the right of
   allocated 8-byte region [ffff888002ee02c0, ffff888002ee02c8)

Also, handle_rerror() returns before truncating the reply size of a
zero-copy TWRITE, which posts no reply pages, and p9_check_errors()
skips the size check for zero-copy requests, so it can parse the error
string of an oversized error reply past the end of rc.sdata.

Fix this by clamping the reply size to the posted buffers upon getting
the reply.

This patch is assisted by Claude Opus 5.5.

Reported-by: Yizhou Zhao <zhaoyz24@mails.tsinghua.edu.cn>
Closes: https://lore.kernel.org/all/20260607140603.24342-1-zhaoyz24@mails.tsinghua.edu.cn/
Suggested-by: Dominique Martinet <asmadeus@codewreck.org>
Fixes: f615625a44c4 ("9p: handling Rerror without copy_from_iter_full()")
Cc: stable@vger.kernel.org
Signed-off-by: Lianjie Wang <lian@is.s.u-tokyo.ac.jp>
---
v2:
- No code changes. Reword the description of the KASAN report.
v1: https://lore.kernel.org/all/20261109105251.2545764-2-lian@is.s.u-tokyo.ac.jp/

 net/9p/trans_virtio.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 9fa63e3ebf673..53046668c16b1 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -537,10 +537,14 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 	 * threads - echoes to wmb() in the callback
 	 */
 	smp_rmb();
-	// RERROR needs reply (== error string) in static data
-	if (READ_ONCE(req->status) == REQ_STATUS_RCVD &&
-	    unlikely(req->rc.sdata[4] == P9_RERROR))
-		handle_rerror(req, in_hdr_len, offs, in_pages);
+	if (READ_ONCE(req->status) == REQ_STATUS_RCVD) {
+		// The device may report more than the buffers we posted
+		req->rc.size = umin(req->rc.size, in_hdr_len + inlen);
+
+		// RERROR needs reply (== error string) in static data
+		if (unlikely(req->rc.sdata[4] == P9_RERROR))
+			handle_rerror(req, in_hdr_len, offs, in_pages);
+	}
 
 	/*
 	 * Non kernel buffers are pinned, unpin them
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/5] 9p/virtio: move the error string of an RLERROR into rc.sdata
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 1/5] 9p/virtio: clamp the reply size of a zero-copy request Lianjie Wang
@ 2026-11-23 20:42 ` Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 3/5] 9p: bound the zero-copy reply after a fatal signal Lianjie Wang
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa, Sashiko, stable

p9_check_errors() parses an error string from both RERROR and RLERROR
when the protocol is not 9P2000.L, but p9_virtio_zc_request() calls
handle_rerror() only for RERROR. The error string of an RLERROR reply to
a zero-copy request is therefore left in the reply pages while the reply
size still includes it, so p9_check_errors() can parse the string past
the end of rc.sdata:

  BUG: KASAN: slab-out-of-bounds in p9pdu_readf+0x151e/0x1c50
  Read of size 8179 at addr ffff888004b4c009 by task ls/155
  Call Trace:
   p9pdu_readf+0x151e/0x1c50
   p9_check_errors+0x23d/0x510
   p9_client_zc_rpc+0x4f0/0x970
   p9_client_read_once+0x237/0x6c0
   p9_client_read+0x9d/0x140
   v9fs_dir_readdir+0x2ea/0x730

Fix this by calling handle_rerror() for both error replies.

This patch is assisted by Claude Opus 5.5.

Reported-by: Sashiko <netdev-bot+sashiko@kernel.org>
Closes: https://lore.kernel.org/all/179446998758.2199510.14994914047354508278@kernel.org/
Fixes: f615625a44c4 ("9p: handling Rerror without copy_from_iter_full()")
Cc: stable@vger.kernel.org
Signed-off-by: Lianjie Wang <lian@is.s.u-tokyo.ac.jp>
---
v2:
- New patch.

 net/9p/trans_virtio.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 53046668c16b1..8d9555e64490e 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -541,8 +541,9 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 		// The device may report more than the buffers we posted
 		req->rc.size = umin(req->rc.size, in_hdr_len + inlen);
 
-		// RERROR needs reply (== error string) in static data
-		if (unlikely(req->rc.sdata[4] == P9_RERROR))
+		// RERROR/RLERROR needs reply (== error string) in static data
+		if (unlikely(req->rc.sdata[4] == P9_RERROR ||
+			     req->rc.sdata[4] == P9_RLERROR))
 			handle_rerror(req, in_hdr_len, offs, in_pages);
 	}
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/5] 9p: bound the zero-copy reply after a fatal signal
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 1/5] 9p/virtio: clamp the reply size of a zero-copy request Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 2/5] 9p/virtio: move the error string of an RLERROR into rc.sdata Lianjie Wang
@ 2026-11-23 20:42 ` Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 4/5] 9p/virtio: set REQ_STATUS_SENT after adding a zero-copy request Lianjie Wang
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa, stable

When a fatal signal interrupts p9_virtio_zc_request() while it waits
for the reply to a zero-copy TREAD or TREADDIR, the device can still
answer the request before the RFLUSH, and p9_client_zc_rpc() parses that
reply. p9_virtio_zc_request() has returned by then, so the reply size is
not clamped and handle_rerror() is not called. p9_check_errors() skips
the size check for zero-copy requests, so it parses the error string of
an oversized reply past the end of rc.sdata:

  BUG: KASAN: slab-out-of-bounds in p9pdu_readf+0x151e/0x1c50
  Read of size 8179 at addr ffff8880043f8009 by task zc9p/155
  Call Trace:
   p9pdu_readf+0x151e/0x1c50
   p9_check_errors+0x23d/0x510
   p9_client_zc_rpc+0x4f0/0x970
   p9_client_read_once+0x237/0x6c0

Also, p9_check_errors() parses the error string from rc.sdata, where the
device wrote only the header, so p9_errstr2errno() can print
uninitialized memory.

Fix this by accepting such a reply only when it fits within the part of
rc.sdata posted for the header, and by returning -ERESTARTSYS otherwise,
as p9_client_zc_rpc() already does when the device cancels the request.
A zero-copy TWRITE posts all of rc.sdata for the reply, so a late RWRITE
is still accepted.

This patch is assisted by Claude Opus 5.5.

Fixes: f615625a44c4 ("9p: handling Rerror without copy_from_iter_full()")
Cc: stable@vger.kernel.org
Signed-off-by: Lianjie Wang <lian@is.s.u-tokyo.ac.jp>
---
v2:
- Accept a late reply within the part of rc.sdata posted for the header,
  so the RWRITE of a completed write is not dropped.
v1: https://lore.kernel.org/all/20261109105251.2545764-3-lian@is.s.u-tokyo.ac.jp/

 net/9p/client.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/net/9p/client.c b/net/9p/client.c
index e63e8c2a853df..e21d13e274691 100644
--- a/net/9p/client.c
+++ b/net/9p/client.c
@@ -707,9 +707,17 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
 		if (c->trans_mod->cancel(c, req))
 			p9_client_flush(c, req);
 
-		/* if we received the response anyway, don't signal error */
-		if (READ_ONCE(req->status) == REQ_STATUS_RCVD)
-			err = 0;
+		/* if we received the response anyway, don't signal error.
+		 * zc_request() may have returned without clamping the size
+		 * or handling RERROR/RLERROR, so only accept a response
+		 * within in_hdrlen.
+		 */
+		if (READ_ONCE(req->status) == REQ_STATUS_RCVD) {
+			/* echoes to wmb() in the callback */
+			smp_rmb();
+			if (req->rc.size <= in_hdrlen)
+				err = 0;
+		}
 	}
 recalc_sigpending:
 	if (sigpending) {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 4/5] 9p/virtio: set REQ_STATUS_SENT after adding a zero-copy request
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
                   ` (2 preceding siblings ...)
  2026-11-23 20:42 ` [PATCH v2 3/5] 9p: bound the zero-copy reply after a fatal signal Lianjie Wang
@ 2026-11-23 20:42 ` Lianjie Wang
  2026-11-23 20:42 ` [PATCH v2 5/5] 9p: initialize ecode in p9_check_errors() Lianjie Wang
  2026-11-23 20:52 ` [PATCH v2 0/5] 9p: bound zero-copy error replies sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa, stable

p9_virtio_zc_request() sets REQ_STATUS_SENT before adding the request to
the virtqueue. If the wait for the ring is interrupted by a fatal
signal, it drops the virtio transport's reference to the request, since
the reply won't come, and returns -ERESTARTSYS. p9_client_zc_rpc()
flushes the request on -ERESTARTSYS, and p9_client_flush() calls
p9_virtio_cancelled() for a request in REQ_STATUS_SENT, which can drop
the reference again:

  refcount_t: underflow; use-after-free.
  WARNING: lib/refcount.c:37 at refcount_warn_saturate+0x84/0xd0, CPU#3: dd/314
  Call Trace:
   p9_req_put+0xe5/0x190
   p9_client_zc_rpc+0x537/0x980
   p9_client_read_once+0x237/0x6c0
   p9_client_read+0x9d/0x140

Fix this by setting REQ_STATUS_SENT after virtqueue_add_sgs() succeeds,
so p9_virtio_cancelled() is not called for a request outside the
virtqueue. The status is set under chan->lock, before req_done() can
set REQ_STATUS_RCVD.

This patch is assisted by Claude Fable 5.2.

Fixes: 728356dedeff ("9p: Add refcount to p9_req_t")
Cc: stable@vger.kernel.org
Signed-off-by: Lianjie Wang <lian@is.s.u-tokyo.ac.jp>
---
v2:
- New patch.

 net/9p/trans_virtio.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 8d9555e64490e..6d1e6093db095 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -467,7 +467,6 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 			inlen = n;
 		}
 	}
-	WRITE_ONCE(req->status, REQ_STATUS_SENT);
 req_retry_pinned:
 	spin_lock_irqsave(&chan->lock, flags);
 
@@ -526,6 +525,7 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
 			goto err_out;
 		}
 	}
+	WRITE_ONCE(req->status, REQ_STATUS_SENT);
 	virtqueue_kick(chan->vq);
 	spin_unlock_irqrestore(&chan->lock, flags);
 	kicked = 1;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 5/5] 9p: initialize ecode in p9_check_errors()
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
                   ` (3 preceding siblings ...)
  2026-11-23 20:42 ` [PATCH v2 4/5] 9p/virtio: set REQ_STATUS_SENT after adding a zero-copy request Lianjie Wang
@ 2026-11-23 20:42 ` Lianjie Wang
  2026-11-23 20:52 ` [PATCH v2 0/5] 9p: bound zero-copy error replies sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: Lianjie Wang @ 2026-11-23 20:42 UTC (permalink / raw)
  To: Eric Van Hensbergen, Latchesar Ionkov, Dominique Martinet
  Cc: Lianjie Wang, Christian Schoenebeck, Al Viro, Tomas Bortoli, v9fs,
	linux-kernel, Takahiro Shinagawa, Sashiko

An RERROR of 9P2000 has no error code, so p9pdu_readf() returns at the
"?" of the format "s?d" without writing ecode, and p9_check_errors()
prints the uninitialized value with p9_debug() when CONFIG_NET_9P_DEBUG
and P9_DEBUG_9P are set.

Fix this by initializing ecode to zero.

This patch is assisted by Claude Fable 5.2.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-206901@kernel.org/
Fixes: 51a87c552dfd ("9p: rework client code to use new protocol support functions")
Signed-off-by: Lianjie Wang <lian@is.s.u-tokyo.ac.jp>
---
v2:
- New patch.

 net/9p/client.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/net/9p/client.c b/net/9p/client.c
index e21d13e274691..482be6f375234 100644
--- a/net/9p/client.c
+++ b/net/9p/client.c
@@ -400,7 +400,7 @@ static int p9_check_errors(struct p9_client *c, struct p9_req_t *req)
 {
 	s8 type;
 	int err;
-	int ecode;
+	int ecode = 0;
 
 	err = p9_parse_header(&req->rc, NULL, &type, NULL, 0);
 	if (req->rc.size > req->rc.capacity && !req->rc.zc) {
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/5] 9p: bound zero-copy error replies
  2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
                   ` (4 preceding siblings ...)
  2026-11-23 20:42 ` [PATCH v2 5/5] 9p: initialize ecode in p9_check_errors() Lianjie Wang
@ 2026-11-23 20:52 ` sashiko-bot
  5 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-23 20:52 UTC (permalink / raw)
  To: Lianjie Wang; +Cc: v9fs

Sashiko has reviewed this 5-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123204257.3055515-1-lian@is.s.u-tokyo.ac.jp

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-23 20:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 20:42 [PATCH v2 0/5] 9p: bound zero-copy error replies Lianjie Wang
2026-11-23 20:42 ` [PATCH v2 1/5] 9p/virtio: clamp the reply size of a zero-copy request Lianjie Wang
2026-11-23 20:42 ` [PATCH v2 2/5] 9p/virtio: move the error string of an RLERROR into rc.sdata Lianjie Wang
2026-11-23 20:42 ` [PATCH v2 3/5] 9p: bound the zero-copy reply after a fatal signal Lianjie Wang
2026-11-23 20:42 ` [PATCH v2 4/5] 9p/virtio: set REQ_STATUS_SENT after adding a zero-copy request Lianjie Wang
2026-11-23 20:42 ` [PATCH v2 5/5] 9p: initialize ecode in p9_check_errors() Lianjie Wang
2026-11-23 20:52 ` [PATCH v2 0/5] 9p: bound zero-copy error replies sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox