Skip to contentSkip to conversation

Fix unwanted memory overwrites

PATCH Superseded Chuck Lever 3 participants 7 messages Oct 6, 2025 – Oct 7, 2025

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

cover Fix unwanted memory overwrites

From: Chuck Lever <[email protected]>

<[email protected]> reported some memory overwrites that can be triggered by NFS client input. I was able to observe overwrites by enabling KASAN and running his reproducer [1].

NFSD caches COMPOUNDs containing only a single SEQUENCE operation whether the client requests it to or not, in order to work around a deficiency in the NFSv4.1 protocol. However, the predicate that identifies solo SEQUENCE operations was incorrect.

(Based on my reading of RFC 8881, I'm not sure NFSD should cache solo SEQUENCE operations that fail, but that is perhaps for a different day).

Chuck Lever (2):
NFSD: Do not cache failed SEQUENCE operations
NFSD: Skip close replay processing if XDR encoding fails

 fs/nfsd/nfs4xdr.c | 3 +--
 fs/nfsd/xdr4.h    | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 3 lines
2.51.0

[1] http://www.rtmrtm.org/rtm/nfsd185f.c
author maintainer
#1 lore raw

PATCH 1/2 NFSD: Do not cache failed SEQUENCE operations

From: Chuck Lever <[email protected]>

RFC 8881 Section 2.10.6.4 says:

Quoted 3 lines

If sa_cachethis or csa_cachethis is TRUE, then the replier MUST cache a reply except if an error is returned by the SEQUENCE or CB_SEQUENCE operation (see Section 2.10.6.1.2).

This text also appears in RFC 5661. Further, Section 2.10.6.1.2 says:

Quoted 4 lines

Any time SEQUENCE or CB_SEQUENCE returns an error, the sequence ID of the slot MUST NOT change. The replier MUST NOT modify the reply cache entry for the slot whenever an error is returned from SEQUENCE or CB_SEQUENCE.

NFSD caches the result of solo SEQUENCE operations, but rtm's reproducer sends two operations in the failing COMPOUND. NFSD should not attempt to cache the reply.

The logic in nfsd4_is_solo_sequence() was incorrect: it checked the current operation index, not the total count of operations in the COMPOUND. If the SEQUENCE operation, which is always operation 1, fails in a multi-operation compound, resp->opcnt is always 1. Thus when a SEQUENCE operation fails, nfsd4_is_solo_sequence() always returns true.

Fixes 468de9e54a90 ("nfsd41: expand solo sequence check")
---
 fs/nfsd/xdr4.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/nfsd/xdr4.h +2 -1
index ee0570cbdd9e..d1837a10b0c2 100644
@@ -926,7 +926,8 @@ struct nfsd4_compoundres {
 static inline bool nfsd4_is_solo_sequence(struct nfsd4_compoundres *resp)
 {
 	struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
-	return resp->opcnt == 1 && args->ops[0].opnum == OP_SEQUENCE;
+
+	return args->opcnt == 1 && args->ops[0].opnum == OP_SEQUENCE;
 }
 
 /*
Signature · 1 line
2.51.0
author maintainer
#2 lore raw

PATCH 2/2 NFSD: Skip close replay processing if XDR encoding fails

In reply to Chuck Lever

From: Chuck Lever <[email protected]>

The close replay logic added by commit 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's") cannot be done if encoding failed due to a short send buffer; there's no guarantee that the operation encoder has actually encoded the data that is being copied to the replay cache.

I think there are deeper problems here. Is stateid sequencing screwed up if XDR encoding fails? Does NFSv4.1+ even need to care about this?

Fixes 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's")
---
 fs/nfsd/nfs4xdr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/nfsd/nfs4xdr.c +1 -2
index 230bf53e39f7..85b773a65670 100644
@@ -5937,8 +5937,7 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
 		 */
 		warn_on_nonidempotent_op(op);
 		xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
-	}
-	if (so) {
+	} else if (so) {
 		int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
 
 		so->so_replay.rp_status = op->status;
Signature · 1 line
2.51.0
reviewer
#5 lore raw
In reply to Chuck Lever
Quoted 10 lines · Chuck Lever

From: Chuck Lever <[email protected]>

<[email protected]> reported some memory overwrites that can be triggered by NFS client input. I was able to observe overwrites by enabling KASAN and running his reproducer [1].

NFSD caches COMPOUNDs containing only a single SEQUENCE operation whether the client requests it to or not, in order to work around a deficiency in the NFSv4.1 protocol. However, the predicate that identifies solo SEQUENCE operations was incorrect.

I'm not sure why a SEQUENCE should be ever be cached, apart from recognizing it as one of the operations in a prior request. The idea from a protocol perspective is that the sequence is just a ind of clock that ticks once per time it's executed, and it only ticks when the sender sends something new.

IOW from a responder (server) perspective, caching it seems wrong. Can you elaborate on your interpretation of RFC8881?

Tom.

Quoted 12 lines

(Based on my reading of RFC 8881, I'm not sure NFSD should cache solo SEQUENCE operations that fail, but that is perhaps for a different day).

Chuck Lever (2):
NFSD: Do not cache failed SEQUENCE operations
NFSD: Skip close replay processing if XDR encoding fails

  fs/nfsd/nfs4xdr.c | 3 +--
  fs/nfsd/xdr4.h    | 3 ++-
  2 files changed, 3 insertions(+), 3 deletions(-)
author maintainer
#6 lore raw
Quoted 12 lines · Tom Talpey
Quoted 10 lines · Chuck Lever

From: Chuck Lever <[email protected]>

<[email protected]> reported some memory overwrites that can be triggered by NFS client input. I was able to observe overwrites by enabling KASAN and running his reproducer [1].

NFSD caches COMPOUNDs containing only a single SEQUENCE operation whether the client requests it to or not, in order to work around a deficiency in the NFSv4.1 protocol. However, the predicate that identifies solo SEQUENCE operations was incorrect.

Quoted 8 lines

I'm not sure why a SEQUENCE should be ever be cached, apart from recognizing it as one of the operations in a prior request. The idea from a protocol perspective is that the sequence is just a ind of clock that ticks once per time it's executed, and it only ticks when the sender sends something new.

IOW from a responder (server) perspective, caching it seems wrong. Can you elaborate on your interpretation of RFC8881?

I'm basing my remarks about the protocol on this old comment that is near the code repaired by this series.

/*
* The session reply cache only needs to cache replies that the client
* actually asked us to. But it's almost free for us to cache compounds
* consisting of only a SEQUENCE op, so we may as well cache those too.
* Also, the protocol doesn't give us a convenient response in the case
* of a replay of a solo SEQUENCE op that wasn't cached
* (RETRY_UNCACHED_REP can only be returned in the second op of a
* compound).
*/

Quoted 3 lines
Quoted 3 lines

(Based on my reading of RFC 8881, I'm not sure NFSD should cache solo SEQUENCE operations that fail, but that is perhaps for a different day).

The 2009 commit that broke this removed the logic that prevented caching failed solo SEQUENCE operations. It might be more sensible to simply revert that commit entirely.

Or, further, if it seems utterly broken to cache even successful solo SEQUENCE operations, then I would consider removing the solo SEQUENCE caching logic.

RFC 8881 Section 2.10.6.1.3 opens with:

Quoted 4 lines

On a per-request basis, the requester can choose to direct the replier to cache the reply to all operations after the first operation (SEQUENCE or CB_SEQUENCE) via the sa_cachethis or csa_cachethis fields of the arguments to SEQUENCE or CB_SEQUENCE.

That suggests the spec authors expected servers not to cache a COMPOUND's SEQUENCE result at all.

Signature · 1 line
Chuck Lever
maintainer Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH v1 2/2] NFSD: Skip close replay processing if XDR encoding fails

In reply to Chuck Lever
Quoted 11 lines · Chuck Lever

From: Chuck Lever <[email protected]>

The close replay logic added by commit 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's") cannot be done if encoding failed due to a short send buffer; there's no guarantee that the operation encoder has actually encoded the data that is being copied to the replay cache.

I think there are deeper problems here. Is stateid sequencing screwed up if XDR encoding fails?

I would imagine so? We will have certainly morphed the stateids in the server, but aren't sending the updated info in the reply, right? That seems like we'll end up in state recovery.

Quoted 3 lines

Does NFSv4.1+ even need to care
about this?

The stateid confusion? Is there anything we can do about that for either protocol?

Quoted 13 lines
Fixes 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's")
---
 fs/nfsd/nfs4xdr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/nfsd/nfs4xdr.c +0 -0
index 230bf53e39f7..85b773a65670 100644

fs/nfsd/nfs4xdr.c nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op) · line 5943

Quoted 9 lines
fs/nfsd/nfs4xdr.c +1 -2
@@ -5937,8 +5937,7 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
 		 */
 		warn_on_nonidempotent_op(op);
 		xdr_truncate_encode(xdr, op_status_offset + XDR_UNIT);
-	}
-	if (so) {
+	} else if (so) {
 		int len = xdr->buf->len - (op_status_offset + XDR_UNIT);
 
 		so->so_replay.rp_status = op->status;
author maintainer
#4 lore raw

Re: [PATCH v1 2/2] NFSD: Skip close replay processing if XDR encoding fails

Quoted 9 lines · Jeff Layton
Quoted 8 lines · Chuck Lever

From: Chuck Lever <[email protected]>

The close replay logic added by commit 9411b1d4c7df ("nfsd4: cleanup handling of nfsv4.0 closed stateid's") cannot be done if encoding failed due to a short send buffer; there's no guarantee that the operation encoder has actually encoded the data that is being copied to the replay cache.

Quoted 7 lines
Quoted 3 lines

I think there are deeper problems here. Is stateid sequencing screwed up if XDR encoding fails?

I would imagine so? We will have certainly morphed the stateids in the server, but aren't sending the updated info in the reply, right? That seems like we'll end up in state recovery.

Fair enough. But it feels to me like a layering violation to manage stateid accounting in the server's XDR layer.

Quoted 6 lines
Quoted 3 lines

Does NFSv4.1+ even need to care
about this?

The stateid confusion? Is there anything we can do about that for either protocol?

I'm wondering if the stateid accounting is also needed by NFSv4.1. I saw that OPEN_CONFIRM was included in the set of operations that participate in this bit of magic, and wondered if this is something that only NFSv4.0 needs to do. But the magic is in the generic NFSv4 processing path, so I guess I should assume that yes, NFSv4.1 needs this as well.

Signature · 1 line
Chuck Lever