Skip to contentSkip to conversation

nfsd: restrict nfsd4_spo_must_allow() to bounded cleanup compounds

PATCH In review Dominik Maier 2 participants 2 messages Nov 19 – Nov 20

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

PATCH 1/1 nfsd: restrict nfsd4_spo_must_allow() to bounded cleanup compounds

nfsd4_spo_must_allow() scans all remaining operations in an NFSv4.1+ compound and latches cstate->spo_must_allowed = true for the entire compound whenever any later operation appears in clp->cl_spo_must_allow.

Because check_security_flavor() returns nfs_ok whenever nfsd4_spo_must_allow() returns true, an authenticated SP4_MACH_CRED client can place operations that are not in cl_spo_must_allow (such as OPEN, READ, or WRITE against a sec=krb5p export while using krb5i machine credentials) in the same compound as a permitted spo_must_allow operation such as CLOSE or TEST_STATEID and bypass NFS4ERR_WRONGSEC.

Remove the compound-wide cstate->spo_must_allowed flag and validate that the entire compound matches a bounded SP4_MACH_CRED stateid management or state cleanup compound (2 to 6 operations, bounds-checked against LAST_NFS4_OP and free of decode errors):

- 2-operation compounds ending in a cl_spo_must_allow operation (such as SEQUENCE + TEST_STATEID or FREE_STATEID); or
- compounds starting with SEQUENCE + PUTFH, ending in a filehandle-using cl_spo_must_allow operation (CLOSE, OPEN_DOWNGRADE, LOCKU, or DELEGRETURN, excluding ALLOWED_WITHOUT_FH operations so no trailing I/O operations can follow), and containing only legitimate cleanup companion operations in between (LAYOUTRETURN, delegated-timestamp-only SETATTR, and non-ACL/non-security-label GETATTR), preserving Linux NFSv4.1/4.2 CLOSE and DELEGRETURN compounds.

This issue was discovered by an AI-assisted code review tool (ARTIST) and validated in QEMU against an unmodified v7.4-rc3 kernel without kernel modifications or error injection. A standalone reproducer is available on request. The fix was build-tested with KCFLAGS=-Werror and checked with checkpatch.pl --strict.

Fixes ed94164398c9 ("nfsd: implement machine credential support for some operations")

---
Note: Based on nfsd-next commit 18b212a69f92 ("nfsd: bounds-check opnum before testing spo_must_allow").

 fs/nfsd/nfs4proc.c | 83 ++++++++++++++++++++++++++++++++++++++----------------
 fs/nfsd/xdr4.h     |  1 -
 2 files changed, 59 insertions(+), 25 deletions(-)
fs/nfsd/nfs4proc.c +59 -24
index ccef2c6..199a931 100644
@@ -4282,45 +4282,80 @@ static const struct nfsd4_operation nfsd4_ops[] = {
 	},
 };
 
+static bool nfsd4_spo_cleanup_companion_ok(const struct nfsd4_op *op)
+{
+	switch (op->opnum) {
+	case OP_LAYOUTRETURN:
+		return true;
+	case OP_SETATTR:
+		return !op->u.setattr.sa_bmval[0] &&
+		       !op->u.setattr.sa_bmval[1] &&
+		       op->u.setattr.sa_bmval[2] &&
+		       !(op->u.setattr.sa_bmval[2] &
+			 ~(FATTR4_WORD2_TIME_DELEG_ACCESS |
+			   FATTR4_WORD2_TIME_DELEG_MODIFY));
+	case OP_GETATTR:
+		return !(op->u.getattr.ga_bmval[0] & FATTR4_WORD0_ACL) &&
+		       !(op->u.getattr.ga_bmval[2] &
+			 (FATTR4_WORD2_SECURITY_LABEL |
+			  FATTR4_WORD2_POSIX_DEFAULT_ACL |
+			  FATTR4_WORD2_POSIX_ACCESS_ACL));
+	default:
+		return false;
+	}
+}
+
 /**
- * nfsd4_spo_must_allow - Determine if the compound op contains an
- * operation that is allowed to be sent with machine credentials
+ * nfsd4_spo_must_allow - Check if compound is allowed with machine creds
+ * @rqstp: RPC transaction context
  *
- * @rqstp: a pointer to the struct svc_rqst
+ * Validates that the NFSv4.1+ compound is a bounded SP4_MACH_CRED state
+ * cleanup or stateid management compound and was sent with the client's
+ * negotiated machine credentials.
  *
- * Checks to see if the compound contains a spo_must_allow op
- * and confirms that it was sent with the proper machine creds.
+ * Return: true if the compound may bypass export security flavor checks.
  */
-
 bool nfsd4_spo_must_allow(struct svc_rqst *rqstp)
 {
 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
-	struct nfsd4_op *this;
 	struct nfsd4_compound_state *cstate = &resp->cstate;
-	struct nfs4_op_map *allow = &cstate->clp->cl_spo_must_allow;
-	u32 opiter;
+	struct nfs4_op_map *allow;
+	struct nfsd4_op *last;
+	u32 i;
 
 	if (rqstp->rq_procinfo != &nfsd_version4.vs_proc[NFSPROC4_COMPOUND] ||
-	    cstate->minorversion == 0)
+	    cstate->minorversion == 0 || !cstate->clp || !resp->opcnt ||
+	    argp->opcnt < 2 || argp->opcnt > 6)
 		return false;
 
-	if (cstate->spo_must_allowed)
-		return true;
+	if (!cstate->clp->cl_mach_cred ||
+	    !nfsd4_mach_creds_match(cstate->clp, rqstp))
+		return false;
 
-	opiter = resp->opcnt;
-	while (opiter < argp->opcnt) {
-		this = &argp->ops[opiter++];
-		if (this->opnum <= LAST_NFS4_OP &&
-		    test_bit(this->opnum, allow->u.longs) &&
-		    cstate->clp->cl_mach_cred &&
-		    nfsd4_mach_creds_match(cstate->clp, rqstp)) {
-			cstate->spo_must_allowed = true;
-			return true;
-		}
+	allow = &cstate->clp->cl_spo_must_allow;
+	last = &argp->ops[argp->opcnt - 1];
+	if (last->status || last->opnum > LAST_NFS4_OP ||
+	    !test_bit(last->opnum, allow->u.longs))
+		return false;
+
+	if (argp->opcnt == 2)
+		return !(OPDESC(last)->op_flags & OP_IS_PUTFH_LIKE);
+
+	if (OPDESC(last)->op_flags & ALLOWED_WITHOUT_FH)
+		return false;
+
+	if (argp->ops[1].status || argp->ops[1].opnum != OP_PUTFH)
+		return false;
+
+	for (i = 2; i < argp->opcnt - 1; i++) {
+		if (argp->ops[i].status ||
+		    argp->ops[i].opnum > LAST_NFS4_OP ||
+		    !nfsd4_spo_cleanup_companion_ok(&argp->ops[i]))
+			return false;
 	}
-	cstate->spo_must_allowed = false;
-	return false;
+
+	return true;
 }
 
 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op)
fs/nfsd/xdr4.h +0 -1
index ec19899..73aba10 100644
@@ -61,7 +61,6 @@ struct nfsd4_compound_state {
 	struct nfsd4_session	*session;
 	struct nfsd4_slot	*slot;
 	int			data_offset;
-	bool                    spo_must_allowed;
 	bool			slot_owned;
 	size_t			iovlen;
 	u32			minorversion;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer
#1 lore raw

Hi Dominik-

Thanks for the patch, but it does not apply to the nfsd-testing branch. After 18b212a69f92, two commits from the series I posted on November 17 rewrote nfsd4_spo_must_allow():

e63e70c8d0f5 ("NFSD: Check for a COMPOUND before reading nfsd4_compound_state") 8489270d1cb9 ("NFSD: Move the spo_must_allow check out of check_security_flavor()")

The spo_must_allowed latch is gone. NFSD now applies the SP4_MACH_CRED exemption in the deferred WRONGSEC check that follows PUTFH, and records the result as fh_spo_allowed on the current filehandle. The scan stops at the next PUTFH-like operation or at an operation that handles NFS4ERR_WRONGSEC itself. It skips operations flagged ALLOWED_WITHOUT_FH. A TEST_STATEID or FREE_STATEID no longer exempts any filehandle, and an OPEN by name gets its own flavor check.

Your report still holds for one case, though. In a COMPOUND of SEQUENCE, PUTFH, WRITE, CLOSE, the CLOSE causes NFSD to set fh_spo_allowed on the filehandle. __fh_verify() then skips the flavor check for the WRITE as well. The description of 8489270d1cb9 calls out this gap.

Because fh_spo_allowed is new code I'm still working on, I plan to close that gap myself in a follow-up to that series, and I'll credit you with Reported-by. Your nfsd4_spo_cleanup_companion_ok() is useful input there. It records which operations the Linux NFS client sends with the machine credential alongside CLOSE and DELEGRETURN. Any fix has to keep those COMPOUNDs working.

Could you send me the reproducer? I'd like to run it against the fix before I post it.

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