Skip to contentSkip to conversation

nfsd: fix READ payload landing one page ahead of the XDR accounting

PATCH Superseded Dairui Zhang 2 participants 7 messages Sep 27 – Oct 5

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

PATCH 1/1 nfsd: fix READ payload landing one page ahead of the XDR accounting

nfsd4_encode_readv() passes the in-page offset
base = page_len & ~PAGE_MASK to nfsd_iter_read(), which writes the file data at that offset into *rq_next_page. After each operation rq_next_page is set to page_ptr + 1, so when the reply stream is mid-page the data lands one page ahead of where the payload accounting continues (which stays in the current page at base). The wire then carries the stale tail of the current page (previous RPC payloads, since reply pages are reallocated nonzeroed) instead of the file data: a remote kernel info leak to an authenticated client, plus silently corrupted READ results.

Trigger: any compound where a READ follows an op that left the stream mid-page, e.g. [PUTFH, READ(100), READ(N)] or READDIR+READ.

When base is nonzero, place the data in the page the accounting is filling (page_ptr) instead of one page ahead.

Fixes ed4a567a179e ("NFSD: Update rq_next_page between COMPOUND operations")
Assisted-by LLM

---

From code reading only - no runtime test yet; the trigger compound is in the commit message. First patch to this list, so if I've misread the flow somewhere, please tell me.

 fs/nfsd/nfs4xdr.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/nfsd/nfs4xdr.c +9 -0
index 6a0a97d..17aff48 100644
@@ -4933,6 +4933,15 @@ static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
 	__be32 zero = xdr_zero;
 	__be32 nfserr;
 
+	/*
+	 * rq_next_page only tracks full pages. When the reply stream is
+	 * mid-page, the payload accounting continues in the current page
+	 * at base, so the data must be placed there instead of one page
+	 * ahead.
+	 */
+	if (base)
+		resp->rqstp->rq_next_page = xdr->page_ptr;
+
 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, read->rd_nf,
 				read->rd_offset, &maxcount, base,
 				&read->rd_eof);
Signature · 1 line
2.53.0
maintainer
#1 lore raw

fs/nfsd/nfs4xdr.c static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp, · line 4943

Quoted 8 lines · Dairui Zhang
fs/nfsd/nfs4xdr.c +8 -0
+	/*
+	 * rq_next_page only tracks full pages. When the reply stream is
+	 * mid-page, the payload accounting continues in the current page
+	 * at base, so the data must be placed there instead of one page
+	 * ahead.
+	 */
+	if (base)
+		resp->rqstp->rq_next_page = xdr->page_ptr;

xdr_truncate_encode() sets xdr->page_ptr from page_base + page_len. When a preceding op truncates to an exact page boundary, page_ptr names the empty page after the last full one, with xdr->p at its start. The next nfsd4_encode_operation() then sets

rq_next_page = page_ptr + 1

which is two pages past the last full page, and base is zero here.

How about this instead:

	resp->rqstp->rq_next_page = xdr->buf->pages +
				    (xdr->buf->page_len >> PAGE_SHIFT);

That is the same accounting xdr_reserve_space_vec() extends after the read, so page and base can never disagree.

Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
#2 lore raw
In reply to Dairui Zhang
Quoted 5 lines · Chuck Lever

When a preceding op truncates to an exact page boundary, page_ptr names the empty page after the last full one, with xdr->p at its start. The next nfsd4_encode_operation() then sets rq_next_page = page_ptr + 1, which is two pages past the last full page, and base is zero here.

You're right - the if (base) guard only covers the mid-page case. At an exact page boundary base is zero, so it does nothing and rq_next_page stays at page_ptr + 1, two pages past the last full page. Your form computes the accounting page directly and covers both. v2 uses it, thanks for the careful read.

Thanks,
Dairui Zhang

#3 lore raw

PATCH v2 1/1 nfsd: fix READ payload landing one page ahead of the XDR accounting

In reply to Dairui Zhang

nfsd4_encode_readv() computes the in-page offset
base = page_len & ~PAGE_MASK and passes it to nfsd_iter_read(), which writes the file data at that offset into *rq_next_page. After each operation rq_next_page is set to page_ptr + 1, so when the reply stream is mid-page the data lands one page ahead of where the payload accounting continues (in the current page at base). The client is then sent the stale tail of the current page - previous RPC payloads, since reply pages are reused without zeroing - instead of the file data: an info leak and corrupted READ results.

Trigger: any compound where a READ follows an op that left the stream mid-page, e.g. [PUTFH, READ(100), READ(N)] or READDIR+READ.

Place the data in the page the accounting is filling, buf->pages[page_len >> PAGE_SHIFT], instead of one page ahead. At an exact page boundary page_ptr is the empty page after the last full one and page_ptr + 1 is one page too far, so this covers that case too.

Fixes ed4a567a179e ("NFSD: Update rq_next_page between COMPOUND operations")
Assisted-by LLM

---
v1 -> v2:
- Use buf->pages[page_len >> PAGE_SHIFT] for rq_next_page instead of page_ptr with an if (base) guard, per Chuck Lever's review: the
guard only covered the mid-page case, while at an exact page
boundary page_ptr is the empty page after the last full one and page_ptr + 1 is already two pages past it. The new form matches
the accounting xdr_reserve_space_vec() extends.
- v1: https://lore.kernel.org/linux-nfs/[email protected]/

From code reading only - I haven't runtime-tested it, and this is my first patch to this list. If I've misread the flow somewhere, please tell me.

---
 fs/nfsd/nfs4xdr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/nfsd/nfs4xdr.c +8 -0
index 6a0a97d..e4b8285 100644
@@ -4933,6 +4933,14 @@ static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
 	__be32 zero = xdr_zero;
 	__be32 nfserr;
 
+	/*
+	 * The payload accounting continues at
+	 * pages[page_len >> PAGE_SHIFT]; write the data there, not one
+	 * page ahead.
+	 */
+	resp->rqstp->rq_next_page = xdr->buf->pages +
+				    (xdr->buf->page_len >> PAGE_SHIFT);
+
 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, read->rd_nf,
 				read->rd_offset, &maxcount, base,
 				&read->rd_eof);
Signature · 1 line
2.53.0
maintainer
#4 lore raw
Quoted 38 lines · Dairui Zhang

nfsd4_encode_readv() computes the in-page offset
base = page_len & ~PAGE_MASK and passes it to nfsd_iter_read(), which writes the file data at that offset into *rq_next_page. After each operation rq_next_page is set to page_ptr + 1, so when the reply stream is mid-page the data lands one page ahead of where the payload accounting continues (in the current page at base). The client is then sent the stale tail of the current page - previous RPC payloads, since reply pages are reused without zeroing - instead of the file data: an info leak and corrupted READ results.

Trigger: any compound where a READ follows an op that left the stream mid-page, e.g. [PUTFH, READ(100), READ(N)] or READDIR+READ.

Place the data in the page the accounting is filling, buf->pages[page_len >> PAGE_SHIFT], instead of one page ahead. At an exact page boundary page_ptr is the empty page after the last full one and page_ptr + 1 is one page too far, so this covers that case too.

Fixes ed4a567a179e ("NFSD: Update rq_next_page between COMPOUND

operations")

---
v1 -> v2:
- Use buf->pages[page_len >> PAGE_SHIFT] for rq_next_page instead of page_ptr with an if (base) guard, per Chuck Lever's review: the guard only covered the mid-page case, while at an exact page boundary page_ptr is the empty page after the last full one and page_ptr + 1 is already two pages past it. The new form matches the accounting xdr_reserve_space_vec() extends.
- v1:
https://lore.kernel.org/linux-nfs/[email protected]/

Quoted 3 lines

From code reading only - I haven't runtime-tested it, and this is my first patch to this list. If I've misread the flow somewhere, please tell me.

The comment is superfluous now, so it can be dropped.

Go ahead and test vigorously and let me know how it goes!

Quoted 26 lines
---
 fs/nfsd/nfs4xdr.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/nfsd/nfs4xdr.c +8 -0
index 6a0a97d..e4b8285 100644
@@ -4933,6 +4933,14 @@ static __be32 nfsd4_encode_readv(struct 
 nfsd4_compoundres *resp,
 	__be32 zero = xdr_zero;
 	__be32 nfserr;
 
+	/*
+	 * The payload accounting continues at
+	 * pages[page_len >> PAGE_SHIFT]; write the data there, not one
+	 * page ahead.
+	 */
+	resp->rqstp->rq_next_page = xdr->buf->pages +
+				    (xdr->buf->page_len >> PAGE_SHIFT);
+
 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, read->rd_nf,
 				read->rd_offset, &maxcount, base,

&read->rd_eof);

Signature · 1 line
2.53.0
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
#5 lore raw

PATCH v3 1/1 nfsd: fix READ payload landing one page ahead of the XDR accounting

In reply to Dairui Zhang

From a4eb1e6b98f6246af5f37ba5bf48b8f8a1a54224 Mon Sep 17 00:00:00 2001
From: Dairui Zhang <[email protected]>
Date: Tue, 29 Sep 2026 03:14:22 +0800
Subject: [PATCH v3] nfsd: fix READ payload landing one page ahead of the XDR
accounting

nfsd4_encode_readv() computes the in-page offset
base = page_len & ~PAGE_MASK and passes it to nfsd_iter_read(), which writes the file data at that offset into *rq_next_page. After each operation rq_next_page is set to page_ptr + 1, so when the reply stream is mid-page the data lands one page ahead of where the payload accounting continues (in the current page at base). The client is then sent the stale tail of the current page - previous RPC payloads, since reply pages are reused without zeroing - instead of the file data: an info leak and corrupted READ results.

Trigger: any compound where a READ follows an op that left the stream mid-page, e.g. [PUTFH, READ(100), READ(N)] or READDIR+READ.

Place the data in the page the accounting is filling, buf->pages[page_len >> PAGE_SHIFT], instead of one page ahead. At an exact page boundary page_ptr is the empty page after the last full one and page_ptr + 1 is one page too far, so this covers that case too.

Fixes 0d32a6bbb8e7bf ("NFSD: Fix zero NFSv4 READ results when RQ_SPLICE_OK is not set")
Assisted-by LLM

---
v1 -> v2:
- Use buf->pages[page_len >> PAGE_SHIFT] for rq_next_page instead of page_ptr + 1, per Chuck Lever's review. The if (base) guard in v1
only covered the mid-page case.
- v1: https://lore.kernel.org/linux-nfs/[email protected]/

v2 -> v3:
- Drop the comment above the new assignment, per Chuck Lever's review. No functional change from v2.
- Correct the Fixes tag: the bug in this form starts with 0d32a6bbb8e7bf (v6.6), when base was hoisted before the reserve. The earlier rq_vec form is not affected.

Tested on 7.2.6 in a QEMU guest. The test file tags every 4K page with its index. A raw COMPOUND{PUTFH, READ(0,100), READ(4096,4096)} forces the readv path (readcount=2); ftrace confirmed nfsd_iter_read for both READs.

Unpatched, the second READ returns stale page contents instead of
the file data (heap bytes in one run, zeros in another). The first
READ only looked correct because the stale page happened to hold
the same page-0 content. Patched, both READs return the expected
bytes. A localhost v4.1 mount with 128K/1M dd reads (splice path)
gives identical md5 before and after.
- v2: https://lore.kernel.org/linux-nfs/[email protected]/

---
 fs/nfsd/nfs4xdr.c | 3 +++
 1 file changed, 3 insertions(+)
fs/nfsd/nfs4xdr.c +3 -0
@@ -4933,6 +4933,9 @@ static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
 	__be32 zero = xdr_zero;
 	__be32 nfserr;
 
+	resp->rqstp->rq_next_page = xdr->buf->pages +
+				    (xdr->buf->page_len >> PAGE_SHIFT);
+
 	nfserr = nfsd_iter_read(resp->rqstp, read->rd_fhp, read->rd_nf,
 				read->rd_offset, &maxcount, base,
 				&read->rd_eof);
Signature · 1 line
2.53.0
maintainer Applied
#6 lore raw
Quoted 11 lines · Dairui Zhang

nfsd4_encode_readv() computes the in-page offset
base = page_len & ~PAGE_MASK and passes it to nfsd_iter_read(), which writes the file data at that offset into *rq_next_page. After each operation rq_next_page is set to page_ptr + 1, so when the reply stream is mid-page the data lands one page ahead of where the payload accounting continues (in the current page at base). The client is then sent the stale tail of the current page - previous RPC payloads, since reply pages are reused without zeroing - instead of the file data: an info leak and corrupted READ results.

Quoted 1 line

[...]

Applied, thanks!

[1/1] nfsd: fix READ payload landing one page ahead of the XDR accounting

      commit: a8ac5817d6808a59e15a297e186dd7fd25f27062

Best regards,

Signature · 1 line
Chuck Lever <[email protected]>