git: ea44ef6f5f33 - stable/15 - nfsserver: Do not increment seqid on BADSEQID

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Sun, 15 Nov 2026 17:28:33 UTC
The branch stable/15 has been updated by des:

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

commit ea44ef6f5f33c4ca0e399c2f5b741232e7832702
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-05 21:11:30 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-14 15:49:21 +0000

    nfsserver: Do not increment seqid on BADSEQID
    
    When returning NFSERR_BADSEQID, the server should not have incremented
    the sequence ID, yet the call to nfsrv_checkseqid() a few lines above
    does precisely that.  Instead, check for a legitimate retransmission
    of the original successful request, and reject otherwise, before the
    ticket is used and sequence ID incremented.
    
    MFC after:      1 week
    Fixes:          ff45b9fc1a8c ("Bjorn reported a problem...")
    Sponsored by:   Klara, Inc.
    Reviewed by:    rmacklem
    Differential Revision:  https://reviews.freebsd.org/D61156
    
    (cherry picked from commit 878084c774c6d68fcdb65e00ba8f74726dda0999)
---
 sys/fs/nfsserver/nfs_nfsdstate.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/sys/fs/nfsserver/nfs_nfsdstate.c b/sys/fs/nfsserver/nfs_nfsdstate.c
index 9e64f3492939..320e690724fa 100644
--- a/sys/fs/nfsserver/nfs_nfsdstate.c
+++ b/sys/fs/nfsserver/nfs_nfsdstate.c
@@ -2006,19 +2006,31 @@ tryagain:
 
 	      if (error == 0) {
 		    if (new_stp->ls_flags & NFSLCK_OPENTOLOCK) {
+			if (stp->ls_flags & NFSLCK_OPEN)
+			    nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
 			/*
 			 * If haslock set, we've already checked the seqid.
 			 */
 			if (!haslock) {
-			    if (stp->ls_flags & NFSLCK_OPEN)
-				error = nfsrv_checkseqid(nd, new_stp->ls_seq,
-				    stp->ls_openowner, new_stp->ls_op);
-			    else
+			    if (stp->ls_flags & NFSLCK_OPEN) {
+				/*
+				 * A new NFSv4.0 request cannot reuse a lock
+				 * owner.  Reject it before checkseqid consumes
+				 * the open seqid and replaces the cached reply.
+				 * The current seqid may be a retransmission of
+				 * the original LOCK; let checkseqid validate it.
+				 */
+				if (lckstp != NULL &&
+				    (nd->nd_flag & ND_NFSV41) == 0 &&
+				    new_stp->ls_seq != stp->ls_openowner->ls_seq)
+				    error = NFSERR_BADSEQID;
+				else
+				    error = nfsrv_checkseqid(nd, new_stp->ls_seq,
+					stp->ls_openowner, new_stp->ls_op);
+			    } else
 				error = NFSERR_BADSTATEID;
 			}
-			if (!error)
-			    nfsrv_getowner(&stp->ls_open, new_stp, &lckstp);
-			if (lckstp) {
+			if (!error && lckstp) {
 			    /*
 			     * For NFSv4.1 and NFSv4.2 allow an
 			     * open_to_lock_owner when the lock_owner already
@@ -2031,7 +2043,7 @@ tryagain:
 				new_stp->ls_flags &= ~NFSLCK_OPENTOLOCK;
 			    else
 				error = NFSERR_BADSEQID;
-			} else
+			} else if (lckstp == NULL)
 			    lckstp = new_stp;
 		    } else if (new_stp->ls_flags&(NFSLCK_LOCK|NFSLCK_UNLOCK)) {
 			/*