Skip to contentSkip to conversation

nfsd: fix use-after-free of DRC slot in nfsd4_sequence()

PATCH Superseded Tien Phan 4 participants 8 messages Aug 19 – Aug 23 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

Tien Phan

author
#0 lore raw

PATCH 1/1 nfsd: fix use-after-free of DRC slot in nfsd4_sequence()

nfsd4_sequence() loads the slot addressed by the client-supplied seq->slotid, bounded only against se_fchannel.maxreqs:

	if (seq->slotid >= session->se_fchannel.maxreqs)
		goto out_put_session;
	slot = xa_load(&session->se_slots, seq->slotid);

When the client acknowledges a slot-count reduction, the handler frees every slot in [se_target_maxslots, maxreqs):

	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
	    slot->sl_generation == session->se_slot_gen &&
	    seq->maxslots <= session->se_target_maxslots)
		free_session_slots(session, session->se_target_maxslots);

but never checks that the slot it just loaded lies outside that range. A malicious NFSv4.1 client can pick seq->slotid >= se_target_maxslots (still < maxreqs, so the bound above passes) and, once the DRC slot shrinker has lowered se_target_maxslots below maxreqs, issue two SEQUENCE calls on that high slot: the first aligns slot->sl_generation with se_slot_gen, the second satisfies the reduce-ack condition and calls free_session_slots(), which kfree()s the very slot still referenced by the handler. The slot is then written (slot->sl_seqid = seq->seqid, slot->sl_flags, cstate->slot = slot) and used for DRC caching -- a use-after-free write.

Reject a slotid that falls within the range about to be freed before freeing it, so the handler never operates on a freed slot.

Found with an in-tree KASAN harness driving the real free_session_slots() on a live slot xarray (KASAN: slab-use-after-free, write of size 4).

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")
---
 fs/nfsd/nfs4state.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +11 -1
index a42f34842d77..f737b841b5a2 100644
@@ -4575,9 +4575,19 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
 	    slot->sl_generation == session->se_slot_gen &&
-	    seq->maxslots <= session->se_target_maxslots)
+	    seq->maxslots <= session->se_target_maxslots) {
 		/* Client acknowledged our reduce maxreqs */
+		if (seq->slotid >= session->se_target_maxslots) {
+			/*
+			 * ... but is using a slot from the range we are
+			 * about to free.  Proceeding would leave the code
+			 * below dereferencing a freed slot (use-after-free).
+			 */
+			status = nfserr_badslot;
+			goto out_put_session;
+		}
 		free_session_slots(session, session->se_target_maxslots);
+	}
 
 	buflen = (seq->cachethis) ?
 			session->se_fchannel.maxresp_cached :
Signature · 1 line
2.34.1
#1 lore raw
Quoted 29 lines · Tien Phan

nfsd4_sequence() loads the slot addressed by the client-supplied seq->slotid, bounded only against se_fchannel.maxreqs:

	if (seq->slotid >= session->se_fchannel.maxreqs)
		goto out_put_session;
	slot = xa_load(&session->se_slots, seq->slotid);

When the client acknowledges a slot-count reduction, the handler frees every slot in [se_target_maxslots, maxreqs):

	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
	    slot->sl_generation == session->se_slot_gen &&
	    seq->maxslots <= session->se_target_maxslots)
		free_session_slots(session, session->se_target_maxslots);

but never checks that the slot it just loaded lies outside that range. A malicious NFSv4.1 client can pick seq->slotid >= se_target_maxslots (still < maxreqs, so the bound above passes) and, once the DRC slot shrinker has lowered se_target_maxslots below maxreqs, issue two SEQUENCE calls on that high slot: the first aligns slot->sl_generation with se_slot_gen, the second satisfies the reduce-ack condition and calls free_session_slots(), which kfree()s the very slot still referenced by the handler. The slot is then written (slot->sl_seqid = seq->seqid, slot->sl_flags, cstate->slot = slot) and used for DRC caching -- a use-after-free write.

Reject a slotid that falls within the range about to be freed before freeing it, so the handler never operates on a freed slot.

Quoted 6 lines

Found with an in-tree KASAN harness driving the real free_session_slots() on a live slot xarray (KASAN: slab-use-after-free, write of size 4).

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")

Did you forget an Assisted-by: tag here?

thanks,

greg k-h

A message that is not in the archive (sent off-list or not yet received): CALjHmwND53Gdy0xMaPNMtvBP5=9KQ7M9-E2S+W1wweQw30tEmw@mail.gmail.com

#3 lore raw
Quoted 1 line · Tien Phan

Thanks Greg,

Please do not top-post :(

Quoted 7 lines

This was AI-assisted (the vuln was found by our automated analysis agent "Sentinel", and the reproduction harness + fix were drafted with AI help; I reviewed and verified it and take responsibility).

Sending v2 now with Reported-by:/Assisted-by: trailers.

Happy to adjust the wording/format if you'd prefer a specific form.

LLMs love to be chatty and say things that are not needed and add unneeded comments. Just like this changelog and patch contains.

Please reword them to be sane.

thanks,

greg k-h

maintainer
#4 lore raw
Quoted 9 lines · Greg KH
Quoted 1 line · Tien Phan

Thanks Greg,

Please do not top-post :(

Quoted 4 lines

This was AI-assisted (the vuln was found by our automated analysis agent "Sentinel", and the reproduction harness + fix were drafted with AI help; I reviewed and verified it and take responsibility).

Quoted 8 lines
Quoted 3 lines

Sending v2 now with Reported-by:/Assisted-by: trailers.

Happy to adjust the wording/format if you'd prefer a specific form.

LLMs love to be chatty and say things that are not needed and add unneeded comments. Just like this changelog and patch contains.

...and it's totally just coincidence that these LLM providers charge by the token!

Signature · 1 line
Jeff Layton <[email protected]>
author
#5 lore raw

PATCH v2 1/1 nfsd: fix use-after-free of DRC slot in nfsd4_sequence()

In reply to Tien Phan

nfsd4_sequence() loads the slot addressed by the client-supplied seq->slotid, bounded only against se_fchannel.maxreqs:

	if (seq->slotid >= session->se_fchannel.maxreqs)
		goto out_put_session;
	slot = xa_load(&session->se_slots, seq->slotid);

When the client acknowledges a slot-count reduction, the handler frees every slot in [se_target_maxslots, maxreqs):

	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
	    slot->sl_generation == session->se_slot_gen &&
	    seq->maxslots <= session->se_target_maxslots)
		free_session_slots(session, session->se_target_maxslots);

but never checks that the slot it just loaded lies outside that range. A malicious NFSv4.1 client can pick seq->slotid >= se_target_maxslots (still < maxreqs, so the bound above passes) and, once the DRC slot shrinker has lowered se_target_maxslots below maxreqs, issue two SEQUENCE calls on that high slot: the first aligns slot->sl_generation with se_slot_gen, the second satisfies the reduce-ack condition and calls free_session_slots(), which kfree()s the very slot still referenced by the handler. The slot is then written (slot->sl_seqid = seq->seqid, slot->sl_flags, cstate->slot = slot) and used for DRC caching -- a use-after-free write.

Reject a slotid that falls within the range about to be freed before freeing it, so the handler never operates on a freed slot.

This bug was found by Sentinel, an automated vulnerability-analysis agent, and reproduced with an in-tree KASAN harness driving the real free_session_slots() on a live slot xarray (KASAN: slab-use-after-free, write of size 4). The analysis and fix were prepared with AI assistance.

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")
Reported-by Sentinel (automated vulnerability-analysis agent)
Assisted-by Claude (Anthropic)

---
v2:
- Add Reported-by: (Sentinel, the automated analysis agent that found this) and Assisted-by: (AI assistance) trailers, per Greg KH's request.
- Correct NeilBrown's address to [email protected] (v1 bounced at suse.de).
- No code changes since v1.
v1: https://lore.kernel.org/all/[email protected]/

 fs/nfsd/nfs4state.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +11 -1
index a42f34842d77..f737b841b5a2 100644
@@ -4575,9 +4575,19 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
 	    slot->sl_generation == session->se_slot_gen &&
-	    seq->maxslots <= session->se_target_maxslots)
+	    seq->maxslots <= session->se_target_maxslots) {
 		/* Client acknowledged our reduce maxreqs */
+		if (seq->slotid >= session->se_target_maxslots) {
+			/*
+			 * ... but is using a slot from the range we are
+			 * about to free.  Proceeding would leave the code
+			 * below dereferencing a freed slot (use-after-free).
+			 */
+			status = nfserr_badslot;
+			goto out_put_session;
+		}
 		free_session_slots(session, session->se_target_maxslots);
+	}
 
 	buflen = (seq->cachethis) ?
 			session->se_fchannel.maxresp_cached :
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v3 1/1 nfsd: fix use-after-free of DRC slot in nfsd4_sequence()

In reply to Tien Phan

nfsd4_sequence() loads the slot for the client-supplied seq->slotid (bounded only by se_fchannel.maxreqs), then on the reduce-ack path frees every slot in [se_target_maxslots, maxreqs) with free_session_slots() without checking that the loaded slot is outside that range.

Once the DRC shrinker has lowered se_target_maxslots below maxreqs, a client using seq->slotid >= se_target_maxslots has its own slot freed and then dereferenced and written (slot->sl_seqid, cstate->slot): a use-after-free.

Reject such a slotid before freeing the slots.

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")
Reported-by Sentinel (automated analysis agent)
Assisted-by Claude (Anthropic)

---
v3: trim commit message; drop the added in-code comment (Greg KH).
v2: add Reported-by:/Assisted-by: trailers (Greg KH); fix NeilBrown address.
v1: https://lore.kernel.org/all/[email protected]/

 fs/nfsd/nfs4state.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +6 -1
index a42f34842d77..def1ea534e18 100644
@@ -4575,9 +4575,14 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
 	    slot->sl_generation == session->se_slot_gen &&
-	    seq->maxslots <= session->se_target_maxslots)
+	    seq->maxslots <= session->se_target_maxslots) {
 		/* Client acknowledged our reduce maxreqs */
+		if (seq->slotid >= session->se_target_maxslots) {
+			status = nfserr_badslot;
+			goto out_put_session;
+		}
 		free_session_slots(session, session->se_target_maxslots);
+	}
 
 	buflen = (seq->cachethis) ?
 			session->se_fchannel.maxresp_cached :
Signature · 1 line
2.34.1
maintainer
#7 lore raw
Quoted 34 lines · Tien Phan

nfsd4_sequence() loads the slot for the client-supplied seq->slotid (bounded only by se_fchannel.maxreqs), then on the reduce-ack path frees every slot in [se_target_maxslots, maxreqs) with free_session_slots() without checking that the loaded slot is outside that range.

Once the DRC shrinker has lowered se_target_maxslots below maxreqs, a client using seq->slotid >= se_target_maxslots has its own slot freed and then dereferenced and written (slot->sl_seqid, cstate->slot): a use-after-free.

Reject such a slotid before freeing the slots.

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")
Reported-by Sentinel (automated analysis agent)
Assisted-by Claude (Anthropic)

---
v3: trim commit message; drop the added in-code comment (Greg KH).
v2: add Reported-by:/Assisted-by: trailers (Greg KH); fix NeilBrown address.
v1: https://lore.kernel.org/all/[email protected]/

 fs/nfsd/nfs4state.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +1 -1
index a42f34842d77..def1ea534e18 100644
@@ -4575,9 +4575,14 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
 	    slot->sl_generation == session->se_slot_gen &&
-	    seq->maxslots <= session->se_target_maxslots)
+	    seq->maxslots <= session->se_target_maxslots) {

fs/nfsd/nfs4state.c nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, · line 4588

Quoted 10 lines
fs/nfsd/nfs4state.c +5 -0
 		/* Client acknowledged our reduce maxreqs */
+		if (seq->slotid >= session->se_target_maxslots) {
+			status = nfserr_badslot;
+			goto out_put_session;
+		}
 		free_session_slots(session, session->se_target_maxslots);
+	}
 
 	buflen = (seq->cachethis) ?
 			session->se_fchannel.maxresp_cached :

I think this problem is already fixed (and more comprehensively) by this patch in Chuck's nfsd-testing branch:

f5d22e372f4a nfsd: don't free session slots that are still in use

If you're scanning for bugs in sunrpc/nfsd/nlm, then please do check whether they've already been fixed in the development trees before you waste your time.

Signature · 1 line
Jeff Layton <[email protected]>
#8 lore raw
Quoted 4 lines · Jeff Layton

I think this problem is already fixed (and more comprehensively) by this patch in Chuck's nfsd-testing branch:

f5d22e372f4a nfsd: don't free session slots that are still in use

Commit/patch attached below for convenience - and it's now upstream as well already.

Thanks,

Ingo

=========================>
# From f5d22e372f4ac3eb287037a488c29691d52a6330 Mon Sep 17 00:00:00 2001
From: Jeff Layton <[email protected]>
Date: Tue, 26 May 2026 12:24:48 -0400
Subject: [PATCH] nfsd: don't free session slots that are still in use

nfsd4_sequence() can free the very slot it is currently processing. When the session shrinker has reduced se_target_maxslots below se_fchannel.maxreqs, the shrink path checks three conditions before calling free_session_slots():

1. se_target_maxslots < maxreqs (shrink was advertised)
2. slot->sl_generation == se_slot_gen (slot is up-to-date)
3. seq->maxslots <= se_target_maxslots (client acknowledges)

However, seq->slotid is never checked against se_target_maxslots. A client using a slot in the range [se_target_maxslots, maxreqs) can satisfy all three conditions: its slot has the current generation (set by a prior SEQUENCE), and it sends sa_highest_slotid <= se_target_maxslots to acknowledge the reduction.

free_session_slots() then kfrees every slot at index >= se_target_maxslots, including the caller's own slot. The function continues to write sl_seqid, sl_flags, sl_generation, and stores the dangling pointer in cstate->slot. Later, nfsd4_store_cache_entry() copies up to maxresp_cached bytes of the compound reply into the freed sl_data[] array, corrupting whatever slab object now occupies that address.

Additionally, a concurrent thread processing SEQUENCE on a different high-numbered slot can have its slot freed out from under it. NFSD4_SLOT_INUSE is set under nn->client_lock before the lock is released, so any concurrent thread past SEQUENCE will have its slot marked. However, free_session_slots() does not check NFSD4_SLOT_INUSE before freeing.

Fix both problems by:
1. Checking that the current request's slotid is below the shrink boundary.
2. Scanning slots in the to-be-freed range for NFSD4_SLOT_INUSE and deferring the shrink if any are active.

Fixes fc8738c68d0b ("nfsd: add support for freeing unused session-DRC slots")
Assisted-by Claude:claude-opus-4-6
---
 fs/nfsd/nfs4state.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
fs/nfsd/nfs4state.c +16 -1
index 8f90fba53357..0d6a7440a4e2 100644
@@ -4496,6 +4496,19 @@ static void nfsd4_construct_sequence_response(struct nfsd4_session *session,
 		seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED;
 }
 
+static bool nfsd4_slots_inuse(struct nfsd4_session *ses, int from)
+{
+	int i;
+
+	for (i = from; i < ses->se_fchannel.maxreqs; i++) {
+		struct nfsd4_slot *slot = xa_load(&ses->se_slots, i);
+
+		if (slot->sl_flags & NFSD4_SLOT_INUSE)
+			return true;
+	}
+	return false;
+}
+
 __be32
 nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		union nfsd4_op_u *u)
@@ -4575,7 +4588,9 @@ nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 
 	if (session->se_target_maxslots < session->se_fchannel.maxreqs &&
 	    slot->sl_generation == session->se_slot_gen &&
-	    seq->maxslots <= session->se_target_maxslots)
+	    seq->maxslots <= session->se_target_maxslots &&
+	    seq->slotid < session->se_target_maxslots &&
+	    !nfsd4_slots_inuse(session, session->se_target_maxslots))
 		/* Client acknowledged our reduce maxreqs */
 		free_session_slots(session, session->se_target_maxslots);