git: 954f20253644 - releng/15.2 - nfsd: Make nfsrv_getstate() validate the stateid type

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:09:34 UTC
The branch releng/15.2 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=954f20253644a748b3a7895821ff57fd4bf18ec4

commit 954f20253644a748b3a7895821ff57fd4bf18ec4
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2026-11-12 17:19:03 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-17 13:34:22 +0000

    nfsd: Make nfsrv_getstate() validate the stateid type
    
    Otherwise nfsrv_delegupdate() can be tricked by a client into freeing a
    state when it shouldn't, resulting in a use-after-free.
    
    Approved by:    re (cperciva)
    Approved by:    so
    Security:       FreeBSD-SA-26:79.nfs
    Security:       CVE-2026-101317
    Reported by:    H. P. Cook <barry@unseen.sh>
---
 sys/fs/nfsserver/nfs_nfsdstate.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/sys/fs/nfsserver/nfs_nfsdstate.c b/sys/fs/nfsserver/nfs_nfsdstate.c
index 9e64f3492939..14642d14447a 100644
--- a/sys/fs/nfsserver/nfs_nfsdstate.c
+++ b/sys/fs/nfsserver/nfs_nfsdstate.c
@@ -1743,7 +1743,7 @@ nfsrv_freenfslockfile(struct nfslockfile *lfp)
  * This function looks up an nfsstate structure via stateid.
  */
 static int
-nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32_t flags,
+nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, u_int32_t flags,
     struct nfsstate **stpp)
 {
 	struct nfsstate *stp;
@@ -1761,7 +1761,8 @@ nfsrv_getstate(struct nfsclient *clp, nfsv4stateid_t *stateidp, __unused u_int32
 	/*
 	 * If no state id in list, return NFSERR_BADSTATEID.
 	 */
-	if (stp == LIST_END(hp)) {
+	if (stp == LIST_END(hp) || (flags == NFSLCK_DELEGRETURN &&
+	    (stp->ls_flags & (NFSLCK_DELEGREAD | NFSLCK_DELEGWRITE)) == 0)) {
 		error = NFSERR_BADSTATEID;
 		goto out;
 	}
@@ -1948,8 +1949,7 @@ tryagain:
 		/*
 		 * Look up the stateid
 		 */
-		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
-		  new_stp->ls_flags, &stp);
+		error = nfsrv_getstate(clp, &new_stp->ls_stateid, 0, &stp);
 	      /*
 	       * do some sanity checks for an unconfirmed open or a
 	       * stateid that refers to the wrong file, for an open stateid
@@ -3470,8 +3470,7 @@ nfsrv_openupdate(vnode_t vp, struct nfsstate *new_stp, nfsquad_t clientid,
 	error = nfsrv_getclient(clientid, CLOPS_RENEW, &clp, NULL,
 	    (nfsquad_t)((u_quad_t)0), 0, nd, p);
 	if (!error)
-		error = nfsrv_getstate(clp, &new_stp->ls_stateid,
-		    new_stp->ls_flags, &stp);
+		error = nfsrv_getstate(clp, &new_stp->ls_stateid, 0, &stp);
 
 	/*
 	 * Sanity check the open.