Skip to contentSkip to conversation

NFSD: Apply export security when a compound crosses exports

PATCH New Chuck Lever 1 participant 8 messages Nov 12

Subsystems (2)
lore
Share & tools
lore

Chuck Lever

author maintainer
#0 lore raw

v3 cover NFSD: Apply export security when a compound crosses exports

VeroFess reported that a compound can move the current filehandle into another export without NFSD applying all of that export's policy:

https://patch.msgid.link/[email protected]

fh_verify() is where NFSD checks an export's secure-port setting and installs the credentials its squash options map a request to. A LOOKUP that crosses a mountpoint, RESTOREFH, and the saved filehandle that COPY and CLONE consume do not go through fh_verify(). An operation that then uses an OPEN stateid, such as READ, CLONE, or COPY, resolves the stateid without calling fh_verify() either. The request runs under the previous export's port policy and credential mapping. The requester must already hold an OPEN stateid that NFSD granted after a full check, so the exposure is a client that keeps a stateid across a change of port or of export options.

Rather than repeat fh_verify() at every crossing, this series makes the crossing path itself complete. check_nfsd_access() gains the port check, so a check added there covers LOOKUP, LOOKUPP, and the COPY source alongside PUTFH. nfsd_setuser() runs after a crossing and after RESTOREFH, and once for each side of a cross-export COPY or CLONE.

---
Changes in v3:
- Mark the transport valid when a deferred check passes (sashiko)
- Verify the LINK target directory last in nfsd_link() (sashiko)
- Drop the nfsd_setuser() call that v2 added to nfsd4_link()
- Clear OP_HANDLES_WRONGSEC on OPEN, which nothing reads any longer
- Explain why a sys-only secure export still gets NFS4ERR_WRONGSEC
- Describe READDIR's result for a port denial at a mount crossing
- State that an NFSv4.0 PUTFH before SECINFO still reports the denial
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Install credentials after PUTROOTFH and PUTPUBFH (sashiko)
- Name COPY in the comment that PUTROOTFH carries
- Apply the current export's credentials after LINK (new patch, sashiko)
- Skip svc_xprt_set_valid() on a deferred check (new patch, sashiko)
- Name NFSD_MAY_BYPASS_PORT in the __nfsd_lookup() kernel-doc (sashiko)
- Correct the failed-decode comment in open_handles_wrongsec() (sashiko)
- Link to v1: https://patch.msgid.link/[email protected]

---
Chuck Lever (7):
NFSD: Check the secure-port policy when entering an export
NFSD: Do not validate the transport before a deferred flavor check
NFSD: Defer the PUTFH secure-port check
NFSD: Report WRONGSEC from PUTFH when OPEN uses the current filehandle
NFSD: Apply the entered export's credentials after LOOKUP and RESTOREFH
NFSD: Use each export's credentials for the COPY and CLONE stateids
NFSD: Verify the target directory last in nfsd_link()

 fs/nfsd/export.c   | 24 +++++++++++-----
 fs/nfsd/export.h   |  3 +-
 fs/nfsd/nfs4proc.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++--------
 fs/nfsd/nfsfh.c    | 49 +++++++++++++++++++++++++-------
 fs/nfsd/nfsfh.h    |  2 ++
 fs/nfsd/trace.h    |  3 +-
 fs/nfsd/vfs.c      | 28 +++++++++++++------
 fs/nfsd/vfs.h      |  1 +
 8 files changed, 153 insertions(+), 39 deletions(-)

---
base-commit: 1ba84639d05b57a0cdd182966e64b4942fbe82ac
change-id: 20261103-verofess-2-3101d53cfc18

Best regards,

Signature · 1 line
Chuck Lever <[email protected]>
author maintainer
#1 lore raw

PATCH v3 1/7 NFSD: Check the secure-port policy when entering an export

When LOOKUP or LOOKUPP crosses into another export, check_nfsd_access() checks the xprtsec policy and security flavor of the entered export but skips the secure-port setting. An AUTH_SYS request from an insecure port can therefore enter a secure export by LOOKUP, though PUTFH of the same filehandle returns NFS4ERR_PERM. A client that obtained an OPEN stateid for a file in that export from a privileged port can then READ or CLONE the file from an insecure port. Commit 6fa02839bf94 ("nfsd4: recheck for secure ports in fh_verify") rechecks the port in fh_verify(), but READ and CLONE with an OPEN stateid go through nfs4_preprocess_stateid_op() and never call fh_verify().

Check the secure-port setting in check_nfsd_access() with nfsd_check_port(), factored out of fh_verify(). The SECINFO lookups must still skip the flavor check, so pass the NFSD_MAY_* access flags to check_nfsd_access() rather than have the mount-crossing paths assemble their own subset of its checks. After the change, a check added to check_nfsd_access() also covers the mount-crossing paths.

Report a port denial to an NFSv4 client as NFS4ERR_WRONGSEC. RFC 8881 does not permit NFS4ERR_PERM, the current result, from LOOKUP, LOOKUPP, or the put-filehandle operations. NFS4ERR_WRONGSEC is permitted there and is accurate, since a GSS flavor is exempt from the port check. READDIR, COPY, CLONE, SECINFO, and the NFSv2 and NFSv3 status mapping already convert NFS4ERR_WRONGSEC to an access error. After the change, READDIR treats a port denial at a mount crossing like a flavor denial. The entry carries only rdattr_error, and READDIR fails with NFS4ERR_ACCESS when the client did not request rdattr_error.

When the export accepts only flavors that are subject to the port check, SECINFO lists the flavor that was just denied, and a retry with that flavor fails again. NFS4ERR_WRONGSEC is still reported in that case. RFC 8881 does not permit a put-filehandle operation to return NFS4ERR_ACCESS or NFS4ERR_PERM. The Linux NFS client retries a LOOKUP once after SECINFO and then returns EPERM.

---
 fs/nfsd/export.c   | 24 +++++++++++++++++-------
 fs/nfsd/export.h   |  3 ++-
 fs/nfsd/nfs4proc.c |  5 +++--
 fs/nfsd/nfsfh.c    | 34 +++++++++++++++++++++++++++++-----
 fs/nfsd/nfsfh.h    |  2 ++
 fs/nfsd/vfs.c      |  8 ++------
 6 files changed, 55 insertions(+), 21 deletions(-)
fs/nfsd/export.c +17 -7
index a058897297bf..99a7d92dc43b 100644
@@ -1976,10 +1976,8 @@ static struct svc_export *exp_find(struct cache_detail *cd,
  *
  * Helper function for check_nfsd_access().  Note that callers should be
  * using check_nfsd_access() instead of calling this function directly.  The
- * exceptions are __fh_verify(), which has logic that may result in one
- * or both of the helpers being skipped, and the mount-crossing paths in
- * nfsd_cross_mnt() and nfsd_lookup_parent(), which enforce only the
- * transport policy when NFSD_MAY_BYPASS_FLAVOR is set.
+ * one exception is __fh_verify() since it has logic that may result in one
+ * or both of the helpers being skipped.
  *
  * Return values:
  *   %nfs_ok if access is granted, or
@@ -2075,18 +2073,30 @@ __be32 check_security_flavor(struct svc_export *exp, struct svc_rqst *rqstp,
  * check_nfsd_access - check if access to export is allowed.
  * @exp: svc_export that is being accessed.
  * @rqstp: svc_rqst attempting to access @exp.
+ * @access: NFSD_MAY_* flags; %NFSD_MAY_BYPASS_FLAVOR skips the security
+ *	flavor check
+ *
+ * Context: Process context.
  *
  * Return values:
- *   %nfs_ok if access is granted, or
- *   %nfserr_wrongsec if access is denied
+ *   %nfs_ok if access is granted,
+ *   %nfserr_wrongsec if access is denied, or
+ *   %nfserr_perm if the secure-port check denies an NFSv2 or NFSv3
+ *   request
  */
-__be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp)
+__be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
+			 int access)
 {
 	__be32 status;
 
+	status = nfsd_check_port(rqstp, &rqstp->rq_cred, exp);
+	if (status != nfs_ok)
+		return status;
 	status = check_xprtsec_policy(exp, rqstp);
 	if (status != nfs_ok)
 		return status;
+	if (access & NFSD_MAY_BYPASS_FLAVOR)
+		return nfs_ok;
 	return check_security_flavor(exp, rqstp, false);
 }
 
fs/nfsd/export.h +2 -1
index dcff16618edf..640109da3dda 100644
@@ -123,7 +123,8 @@ int nfsexp_flags(struct svc_cred *cred, struct svc_export *exp);
 __be32 check_xprtsec_policy(struct svc_export *exp, struct svc_rqst *rqstp);
 __be32 check_security_flavor(struct svc_export *exp, struct svc_rqst *rqstp,
 			     bool may_bypass_gss);
-__be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp);
+__be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
+			 int access);
 
 /*
  * Function declarations
fs/nfsd/nfs4proc.c +3 -2
index dc87142a227c..db4a07b2c1a8 100644
@@ -1526,7 +1526,7 @@ nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		return nfserr_nofilehandle;
 
 	/* A cached open stateid does not authorize the saved source export. */
-	status = check_nfsd_access(cstate->save_fh.fh_export, rqstp);
+	status = check_nfsd_access(cstate->save_fh.fh_export, rqstp, 0);
 	if (status)
 		return status == nfserr_wrongsec ? nfserr_acces : status;
 
@@ -3502,7 +3502,8 @@ nfsd4_proc_compound(struct svc_rqst *rqstp)
 
 			if (current_fh->fh_export &&
 					need_wrongsec_check(rqstp))
-				op->status = check_nfsd_access(current_fh->fh_export, rqstp);
+				op->status = check_nfsd_access(current_fh->fh_export,
+							       rqstp, 0);
 		}
 encode_op:
 		if (op->status == nfserr_replay_me) {
fs/nfsd/nfsfh.c +29 -5
index 09391464803f..f76ce9cc405c 100644
@@ -102,11 +102,23 @@ static bool nfsd_originating_port_ok(struct svc_rqst *rqstp,
 	return test_bit(RQ_SECURE, &rqstp->rq_flags);
 }
 
-static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
-					  struct svc_cred *cred,
-					  struct svc_export *exp)
+/**
+ * nfsd_check_port - check an export's secure-port requirement
+ * @rqstp: request being checked, or %NULL for a local caller
+ * @cred: credential whose flavor selects the export options to check
+ * @exp: export being checked
+ *
+ * Context: Process context.
+ *
+ * A GSS flavor is exempt from the port check, so a denial reports
+ * NFS4ERR_WRONGSEC to an NFSv4 client.
+ *
+ * Return: %nfs_ok if allowed, %nfserr_wrongsec for an NFSv4 client,
+ * %nfserr_perm otherwise.
+ */
+__be32 nfsd_check_port(struct svc_rqst *rqstp,
+		       struct svc_cred *cred, struct svc_export *exp)
 {
-	/* Check if the request originated from a secure port. */
 	if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) {
 		if (IS_ENABLED(CONFIG_SUNRPC_DEBUG)) {
 			char buf[RPC_MAX_ADDRBUFLEN];
@@ -114,8 +126,20 @@ static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
 			dprintk("nfsd: request from insecure port %s!\n",
 			        svc_print_addr(rqstp, buf, sizeof(buf)));
 		}
-		return nfserr_perm;
+		return nfsd_v4client(rqstp) ? nfserr_wrongsec : nfserr_perm;
 	}
+	return nfs_ok;
+}
+
+static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
+					  struct svc_cred *cred,
+					  struct svc_export *exp)
+{
+	__be32 status;
+
+	status = nfsd_check_port(rqstp, cred, exp);
+	if (status)
+		return status;
 
 	/* Set user creds for this exportpoint */
 	return nfserrno(nfsd_setuser(cred, exp));
fs/nfsd/nfsfh.h +2 -0
index aa3ff3086406..90b6429cbcef 100644
@@ -226,6 +226,8 @@ extern char * SVCFH_fmt(struct svc_fh *fhp);
 /*
  * Function prototypes
  */
+__be32	nfsd_check_port(struct svc_rqst *rqstp,
+			struct svc_cred *cred, struct svc_export *exp);
 __be32	fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
 __be32	fh_verify_local(struct net *, struct svc_cred *, struct auth_domain *,
 			struct svc_fh *, umode_t, int);
fs/nfsd/vfs.c +2 -6
index f60e7c346311..8db5fa3be86a 100644
@@ -167,9 +167,7 @@ nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
 			err = 0;
 	} else if (nfsd_v4client(rqstp) ||
 		(exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
-		nfserr = (access & NFSD_MAY_BYPASS_FLAVOR) ?
-			 check_xprtsec_policy(exp2, rqstp) :
-			 check_nfsd_access(exp2, rqstp);
+		nfserr = check_nfsd_access(exp2, rqstp, access);
 		if (nfserr == nfs_ok) {
 			/* successfully crossed mount point */
 			/*
@@ -221,9 +219,7 @@ static __be32 nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent,
 	} else if (IS_ERR(exp2)) {
 		nfserr = nfserrno(PTR_ERR(exp2));
 	} else {
-		nfserr = (access & NFSD_MAY_BYPASS_FLAVOR) ?
-			 check_xprtsec_policy(exp2, rqstp) :
-			 check_nfsd_access(exp2, rqstp);
+		nfserr = check_nfsd_access(exp2, rqstp, access);
 		if (nfserr != nfs_ok) {
 			exp_put(exp2);
 		} else {
Signature · 1 line
2.56.0
author maintainer
#2 lore raw

PATCH v3 2/7 NFSD: Do not validate the transport before a deferred flavor check

In reply to Chuck Lever

fh_verify() marks the transport valid once a filehandle passes the export's checks, and svc_check_conn_limits() closes only connections that are not marked valid. Since commit 6a7d0b390a0b ("NFSD: Defer the PUTFH export security flavor check"), fh_verify() skips the flavor check when the caller passes NFSD_MAY_BYPASS_FLAVOR but still marks the transport valid. A peer whose security flavor the export does not accept can send PUTFH on any number of connections, and svc_check_conn_limits() never closes them.

Mark the transport valid only when fh_verify() has run the flavor check. READ and WRITE with an OPEN stateid do not call fh_verify(), so also mark the transport valid when the deferred check passes in nfsd4_proc_compound(). A connection that carries only those compounds would otherwise stay subject to closure by svc_check_conn_limits().

Fixes 6a7d0b390a0b ("NFSD: Defer the PUTFH export security flavor check")
---
 fs/nfsd/nfs4proc.c | 6 +++++-
 fs/nfsd/nfsfh.c    | 3 +--
 2 files changed, 6 insertions(+), 3 deletions(-)
fs/nfsd/nfs4proc.c +5 -1
index db4a07b2c1a8..60e365678d4c 100644
@@ -3501,9 +3501,13 @@ nfsd4_proc_compound(struct svc_rqst *rqstp)
 				clear_current_stateid(cstate);
 
 			if (current_fh->fh_export &&
-					need_wrongsec_check(rqstp))
+					need_wrongsec_check(rqstp)) {
 				op->status = check_nfsd_access(current_fh->fh_export,
 							       rqstp, 0);
+				/* READ with a stateid does not call fh_verify(). */
+				if (!op->status)
+					svc_xprt_set_valid(rqstp->rq_xprt);
+			}
 		}
 encode_op:
 		if (op->status == nfserr_replay_me) {
fs/nfsd/nfsfh.c +1 -2
index f76ce9cc405c..6a18cf52ab8e 100644
@@ -500,10 +500,9 @@ __fh_verify(struct svc_rqst *rqstp,
 		error = check_security_flavor(exp, rqstp, may_bypass_gss);
 		if (error)
 			goto out;
+		svc_xprt_set_valid(rqstp->rq_xprt);
 	}
 
-	svc_xprt_set_valid(rqstp->rq_xprt);
-
 check_permissions:
 	/* Finally, check access permissions. */
 	error = nfsd_permission(cred, exp, dentry, access);
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH v3 3/7 NFSD: Defer the PUTFH secure-port check

In reply to Chuck Lever

When an AUTH_SYS request from an insecure port presents a filehandle in an export that requires a secure port, fh_verify() fails PUTFH with NFS4ERR_WRONGSEC. RFC 8881 Sections 2.6.3.1.1.2 through 2.6.3.1.1.5 forbid NFS4ERR_WRONGSEC from a put-filehandle operation that another put-filehandle operation, LOOKUP, LOOKUPP, OPEN, SECINFO, or SECINFO_NO_NAME follows. PUTFH therefore cannot report the denial before it knows which operation follows. SECINFO and SECINFO_NO_NAME also fail a port denial themselves, so a client cannot learn which flavors the export accepts.

Defer the port check in PUTFH, as "NFSD: Defer the PUTFH export security flavor check" deferred the flavor check. need_wrongsec_check() needs no change, because check_nfsd_access() already checks the port. Skip the port check in SECINFO and SECINFO_NO_NAME, as those operations skip the flavor check. Do not mark the transport valid when fh_verify() skips the port check. After the change, LOOKUP, LOOKUPP, OPEN, LINK, and RENAME return NFS4ERR_WRONGSEC for a port denial. For NFSv4.1 and later, SECINFO and SECINFO_NO_NAME report the flavors the export accepts. An NFSv4.0 PUTFH that SECINFO follows still reports the denial itself. NFSv2 and NFSv3 still return NFSERR_PERM.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/nfsd/export.c   |  4 ++--
 fs/nfsd/nfs4proc.c | 16 ++++++++++------
 fs/nfsd/nfsfh.c    | 24 +++++++++++++++---------
 fs/nfsd/nfsfh.h    |  4 ++--
 fs/nfsd/trace.h    |  3 ++-
 fs/nfsd/vfs.c      |  3 ++-
 fs/nfsd/vfs.h      |  1 +
 7 files changed, 34 insertions(+), 21 deletions(-)
fs/nfsd/export.c +2 -2
index 99a7d92dc43b..d952fdde7314 100644
@@ -2074,7 +2074,7 @@ __be32 check_security_flavor(struct svc_export *exp, struct svc_rqst *rqstp,
  * @exp: svc_export that is being accessed.
  * @rqstp: svc_rqst attempting to access @exp.
  * @access: NFSD_MAY_* flags; %NFSD_MAY_BYPASS_FLAVOR skips the security
- *	flavor check
+ *	flavor check, %NFSD_MAY_BYPASS_PORT skips the secure-port check
  *
  * Context: Process context.
  *
@@ -2089,7 +2089,7 @@ __be32 check_nfsd_access(struct svc_export *exp, struct svc_rqst *rqstp,
 {
 	__be32 status;
 
-	status = nfsd_check_port(rqstp, &rqstp->rq_cred, exp);
+	status = nfsd_check_port(rqstp, &rqstp->rq_cred, exp, access);
 	if (status != nfs_ok)
 		return status;
 	status = check_xprtsec_policy(exp, rqstp);
fs/nfsd/nfs4proc.c +10 -6
index 60e365678d4c..39d648c34ae7 100644
@@ -843,8 +843,9 @@ nfsd4_putfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	cstate->current_fh.fh_handle.fh_size = putfh->pf_fhlen;
 	memcpy(&cstate->current_fh.fh_handle.fh_raw, putfh->pf_fhval,
 	       putfh->pf_fhlen);
-	/* need_wrongsec_check() decides whether the flavor check applies. */
-	ret = fh_verify(rqstp, &cstate->current_fh, 0, NFSD_MAY_BYPASS_FLAVOR);
+	/* need_wrongsec_check() decides whether the bypassed checks apply. */
+	ret = fh_verify(rqstp, &cstate->current_fh, 0,
+			NFSD_MAY_BYPASS_FLAVOR | NFSD_MAY_BYPASS_PORT);
 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
 	if (ret == nfserr_stale && putfh->no_verify) {
 		SET_FH_FLAG(&cstate->current_fh, NFSD4_FH_FOREIGN);
@@ -1280,13 +1281,15 @@ nfsd4_secinfo(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	__be32 err;
 
 	err = fh_verify(rqstp, &cstate->current_fh, S_IFDIR,
-			NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR);
+			NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR |
+			NFSD_MAY_BYPASS_PORT);
 	if (err)
 		return nfsd4_secinfo_status(err);
 	err = nfsd_lookup_dentry(rqstp, &cstate->current_fh,
 				    secinfo->si_name, secinfo->si_namelen,
 				    &exp, &dentry,
-				    NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR);
+				    NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR |
+				    NFSD_MAY_BYPASS_PORT);
 	if (err)
 		return nfsd4_secinfo_status(err);
 	if (d_really_is_negative(dentry)) {
@@ -1310,13 +1313,14 @@ nfsd4_secinfo_no_name(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstat
 	switch (u->secinfo_no_name.sin_style) {
 	case NFS4_SECINFO_STYLE4_CURRENT_FH:
 		err = fh_verify(rqstp, &cstate->current_fh, 0,
-				NFSD_MAY_BYPASS_FLAVOR);
+				NFSD_MAY_BYPASS_FLAVOR | NFSD_MAY_BYPASS_PORT);
 		if (err)
 			return nfsd4_secinfo_status(err);
 		break;
 	case NFS4_SECINFO_STYLE4_PARENT:
 		err = nfsd4_do_lookupp(rqstp, &cstate->current_fh,
-				       NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR);
+				       NFSD_MAY_EXEC | NFSD_MAY_BYPASS_FLAVOR |
+				       NFSD_MAY_BYPASS_PORT);
 		if (err)
 			return nfsd4_secinfo_status(err);
 		break;
fs/nfsd/nfsfh.c +15 -9
index 6a18cf52ab8e..6f85701cbe8b 100644
@@ -107,6 +107,7 @@ static bool nfsd_originating_port_ok(struct svc_rqst *rqstp,
  * @rqstp: request being checked, or %NULL for a local caller
  * @cred: credential whose flavor selects the export options to check
  * @exp: export being checked
+ * @access: NFSD_MAY_* flags; %NFSD_MAY_BYPASS_PORT skips the check
  *
  * Context: Process context.
  *
@@ -116,9 +117,11 @@ static bool nfsd_originating_port_ok(struct svc_rqst *rqstp,
  * Return: %nfs_ok if allowed, %nfserr_wrongsec for an NFSv4 client,
  * %nfserr_perm otherwise.
  */
-__be32 nfsd_check_port(struct svc_rqst *rqstp,
-		       struct svc_cred *cred, struct svc_export *exp)
+__be32 nfsd_check_port(struct svc_rqst *rqstp, struct svc_cred *cred,
+		       struct svc_export *exp, int access)
 {
+	if (access & NFSD_MAY_BYPASS_PORT)
+		return nfs_ok;
 	if (rqstp && !nfsd_originating_port_ok(rqstp, cred, exp)) {
 		if (IS_ENABLED(CONFIG_SUNRPC_DEBUG)) {
 			char buf[RPC_MAX_ADDRBUFLEN];
@@ -133,11 +136,11 @@ __be32 nfsd_check_port(struct svc_rqst *rqstp,
 
 static __be32 nfsd_setuser_and_check_port(struct svc_rqst *rqstp,
 					  struct svc_cred *cred,
-					  struct svc_export *exp)
+					  struct svc_export *exp, int access)
 {
 	__be32 status;
 
-	status = nfsd_check_port(rqstp, cred, exp);
+	status = nfsd_check_port(rqstp, cred, exp, access);
 	if (status)
 		return status;
 
@@ -226,7 +229,7 @@ static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct net *net,
 				 struct svc_cred *cred,
 				 struct auth_domain *client,
 				 struct auth_domain *gssclient,
-				 struct svc_fh *fhp)
+				 struct svc_fh *fhp, int access)
 {
 	struct knfsd_fh	*fh = &fhp->fh_handle;
 	struct fid *fid = NULL;
@@ -305,7 +308,7 @@ static __be32 nfsd_set_fh_dentry(struct svc_rqst *rqstp, struct net *net,
 					   new->cap_permitted);
 		put_cred(override_creds(new));
 	} else {
-		error = nfsd_setuser_and_check_port(rqstp, cred, exp);
+		error = nfsd_setuser_and_check_port(rqstp, cred, exp, access);
 		if (error)
 			goto out;
 	}
@@ -423,7 +426,7 @@ __fh_verify(struct svc_rqst *rqstp,
 
 	if (!fhp->fh_dentry) {
 		error = nfsd_set_fh_dentry(rqstp, net, cred, client,
-					   gssclient, fhp);
+					   gssclient, fhp, access);
 		if (error)
 			goto out;
 	}
@@ -452,7 +455,7 @@ __fh_verify(struct svc_rqst *rqstp,
 	if (error)
 		goto out;
 
-	error = nfsd_setuser_and_check_port(rqstp, cred, exp);
+	error = nfsd_setuser_and_check_port(rqstp, cred, exp, access);
 	if (error)
 		goto out;
 
@@ -500,9 +503,12 @@ __fh_verify(struct svc_rqst *rqstp,
 		error = check_security_flavor(exp, rqstp, may_bypass_gss);
 		if (error)
 			goto out;
-		svc_xprt_set_valid(rqstp->rq_xprt);
 	}
 
+	/* A deferred check has not yet shown that the peer has access. */
+	if (!(access & (NFSD_MAY_BYPASS_FLAVOR | NFSD_MAY_BYPASS_PORT)))
+		svc_xprt_set_valid(rqstp->rq_xprt);
+
 check_permissions:
 	/* Finally, check access permissions. */
 	error = nfsd_permission(cred, exp, dentry, access);
fs/nfsd/nfsfh.h +2 -2
index 90b6429cbcef..51788712d630 100644
@@ -226,8 +226,8 @@ extern char * SVCFH_fmt(struct svc_fh *fhp);
 /*
  * Function prototypes
  */
-__be32	nfsd_check_port(struct svc_rqst *rqstp,
-			struct svc_cred *cred, struct svc_export *exp);
+__be32	nfsd_check_port(struct svc_rqst *rqstp, struct svc_cred *cred,
+			struct svc_export *exp, int access);
 __be32	fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
 __be32	fh_verify_local(struct net *, struct svc_cred *, struct auth_domain *,
 			struct svc_fh *, umode_t, int);
fs/nfsd/trace.h +2 -1
index 4a2d68410a7d..11a6d7d66f6f 100644
@@ -143,7 +143,8 @@ DEFINE_NFSD_DYNTHREAD_EVENT(trylock_fail);
 		{ NFSD_MAY_READ_IF_EXEC,	"READ_IF_EXEC" },	\
 		{ NFSD_MAY_64BIT_COOKIE,	"64BIT_COOKIE" },	\
 		{ NFSD_MAY_LOCALIO,		"LOCALIO" },	\
-		{ NFSD_MAY_BYPASS_FLAVOR,	"BYPASS_FLAVOR" })
+		{ NFSD_MAY_BYPASS_FLAVOR,	"BYPASS_FLAVOR" },	\
+		{ NFSD_MAY_BYPASS_PORT,		"BYPASS_PORT" })
 
 TRACE_EVENT(nfsd_compound,
 	TP_PROTO(
fs/nfsd/vfs.c +2 -1
index 8db5fa3be86a..b9d76a1a3e65 100644
@@ -326,7 +326,8 @@ nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  * @len:     length of name to examine
  * @resfh:   pointer to pre-initialised filehandle to hold result.
  * @access:  NFSD_MAY_* flags for the fh_verify() of @fhp; adding
- *	     NFSD_MAY_BYPASS_FLAVOR skips the export security flavor checks
+ *	     NFSD_MAY_BYPASS_FLAVOR skips the export security flavor checks,
+ *	     NFSD_MAY_BYPASS_PORT the export's secure-port check
  *
  * Variant of nfsd_lookup() that takes the fh_verify() access flags.
  * The filehandle and mountpoint crossing rules are documented there.
fs/nfsd/vfs.h +1 -0
index f8d90714ef60..40115d9aa245 100644
@@ -34,6 +34,7 @@
 
 #define NFSD_MAY_LOCALIO		0x2000 /* for tracing, reflects when localio used */
 #define NFSD_MAY_BYPASS_FLAVOR		0x4000 /* skip the export's sec= check */
+#define NFSD_MAY_BYPASS_PORT		0x8000 /* skip the export's secure-port check */
 
 #define NFSD_MAY_CREATE		(NFSD_MAY_EXEC|NFSD_MAY_WRITE)
 #define NFSD_MAY_REMOVE		(NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC)
Signature · 1 line
2.56.0
author maintainer
#4 lore raw

PATCH v3 4/7 NFSD: Report WRONGSEC from PUTFH when OPEN uses the current filehandle

In reply to Chuck Lever

need_wrongsec_check() leaves the deferred security check to any following OPEN, because OPEN carries OP_HANDLES_WRONGSEC. That is right for an OPEN by component name, which verifies the parent directory during its lookup. An OPEN that uses the current filehandle (CLAIM_PREVIOUS, CLAIM_FH, CLAIM_DELEG_CUR_FH, or CLAIM_DELEG_PREV_FH) instead reports the denial from its own fh_verify() call. RFC 8881 Section 2.6.3.1.1.7 requires the put-filehandle operation to return NFS4ERR_WRONGSEC in that case and forbids NFS4ERR_WRONGSEC from an OPEN by filehandle.

Treat an OPEN that uses the current filehandle like any other consumer of the filehandle, so PUTFH reports the denial. Clear OP_HANDLES_WRONGSEC on OPEN, because need_wrongsec_check() now decides by claim type and no longer reads the flag for OPEN.

Fixes 68d93184352f ("nfsd4: fix wrongsec handling for PUTFH + op cases")
---
 fs/nfsd/nfs4proc.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
fs/nfsd/nfs4proc.c +22 -1
index 39d648c34ae7..85bbb0073874 100644
@@ -3275,6 +3275,25 @@ bool nfsd4_cache_this_op(struct nfsd4_op *op)
 	return OPDESC(op)->op_flags & OP_CACHEME;
 }
 
+/*
+ * RFC 8881 Section 2.6.3.1.1.7: only an OPEN by component name may
+ * report NFS4ERR_WRONGSEC itself.
+ */
+static bool open_handles_wrongsec(const struct nfsd4_op *op)
+{
+	/* After a failed decode, op_claim_type can read as CLAIM_NULL. */
+	if (op->status != nfs_ok)
+		return false;
+	switch (op->u.open.op_claim_type) {
+	case NFS4_OPEN_CLAIM_NULL:
+	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
+	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static bool need_wrongsec_check(struct svc_rqst *rqstp)
 {
 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
@@ -3312,6 +3331,8 @@ static bool need_wrongsec_check(struct svc_rqst *rqstp)
 	 */
 	if (resp->cstate.minorversion == 0 && next->opnum == OP_SECINFO)
 		return true;
+	if (next->opnum == OP_OPEN)
+		return !open_handles_wrongsec(next);
 	nextd = OPDESC(next);
 	/*
 	 * RFC 8881 Section 2.6.3.1.1.2: a later put-filehandle operation
@@ -4023,7 +4044,7 @@ static const struct nfsd4_operation nfsd4_ops[] = {
 	},
 	[OP_OPEN] = {
 		.op_func = nfsd4_open,
-		.op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING,
+		.op_flags = OP_MODIFIES_SOMETHING,
 		.op_name = "OP_OPEN",
 		.op_rsize_bop = nfsd4_open_rsize,
 		.op_set_currentstateid = nfsd4_set_openstateid,
Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH v3 5/7 NFSD: Apply the entered export's credentials after LOOKUP and RESTOREFH

In reply to Chuck Lever

fh_verify() installs the credentials that an export's squash options map a request to. When LOOKUP crosses into another export, RESTOREFH restores a filehandle from another export, or PUTROOTFH sets the root filehandle, nothing installs the new export's credentials. Operations that use the current filehandle without calling fh_verify(), such as READ with an OPEN stateid, then run with the previous export's mapping. If the previous export does not squash root and the new export does, READ runs as root. If the new export uses all_squash, READ can fail even though the anonymous UID or GID would be granted access.

Call nfsd_setuser() for the entered export after a crossing in __nfsd_lookup(), after RESTOREFH, and after PUTROOTFH and PUTPUBFH. The crossing has already run check_nfsd_access() on that export, so a full fh_verify() would repeat those checks on every LOOKUP that crosses an export. RESTOREFH likewise calls only nfsd_setuser(), so its flavor check stays deferred. NFSv2 and NFSv3 verify the filehandle on every request, so only an NFSv4 LOOKUP installs the credentials. After this change, every NFSv4 operation that sets the current filehandle installs the credentials for its export, so RESTOREFH skips the call when the saved filehandle is in the current export.

---
 fs/nfsd/nfs4proc.c | 16 +++++++++++++++-
 fs/nfsd/vfs.c      |  9 +++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
fs/nfsd/nfs4proc.c +15 -1
index 85bbb0073874..5121e7e80db1 100644
@@ -49,6 +49,7 @@
 #include "xdr4.h"
 #include "nfs4ctl.h"
 #include "vfs.h"
+#include "auth.h"
 #include "current_stateid.h"
 #include "netns.h"
 #include "acl.h"
@@ -859,23 +860,36 @@ static __be32
 nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		union nfsd4_op_u *u)
 {
+	__be32 status;
+
 	fh_put(&cstate->current_fh);
 
-	return exp_pseudoroot(rqstp, &cstate->current_fh);
+	status = exp_pseudoroot(rqstp, &cstate->current_fh);
+	if (status)
+		return status;
+	/* RESTOREFH assumes the credentials for current_fh are installed. */
+	return nfserrno(nfsd_setuser(&rqstp->rq_cred,
+				     cstate->current_fh.fh_export));
 }
 
 static __be32
 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		union nfsd4_op_u *u)
 {
+	bool crossed;
+
 	if (!cstate->save_fh.fh_dentry)
 		return nfserr_restorefh;
 
+	crossed = cstate->current_fh.fh_export != cstate->save_fh.fh_export;
 	fh_dup2(&cstate->current_fh, &cstate->save_fh);
 	if (HAS_CSTATE_FLAG(cstate, SAVED_STATE_ID_FLAG)) {
 		memcpy(&cstate->current_stateid, &cstate->save_stateid, sizeof(stateid_t));
 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
 	}
+	/* RFC 8881 Section 18.27.3 does not permit NFS4ERR_DELAY here. */
+	if (crossed && nfsd_setuser(&rqstp->rq_cred, cstate->current_fh.fh_export))
+		return nfserr_serverfault;
 	return nfs_ok;
 }
 
fs/nfsd/vfs.c +9 -0
index b9d76a1a3e65..c227a3d3a805 100644
@@ -40,6 +40,7 @@
 #endif /* CONFIG_NFSD_V4 */
 
 #include "nfsd.h"
+#include "auth.h"
 #include "nfserr.h"
 #include "nfs4ctl.h"
 #include "netns.h"
@@ -338,6 +339,7 @@ __nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
 {
 	struct svc_export	*exp;
 	struct dentry		*dentry;
+	bool crossed;
 	__be32 err;
 
 	err = fh_verify(rqstp, fhp, S_IFDIR, access);
@@ -350,9 +352,16 @@ __nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
 	 * Note: we compose the file handle now, but as the
 	 * dentry may be negative, it may need to be updated.
 	 */
+	crossed = exp != fhp->fh_export;
 	err = fh_compose(resfh, exp, dentry, fhp);
 	if (!err && d_really_is_negative(dentry))
 		err = nfserr_noent;
+	/*
+	 * The crossing checked exp but did not install exp's creds, and
+	 * an NFSv4 stateid op uses the new filehandle without fh_verify().
+	 */
+	if (!err && crossed && nfsd_v4client(rqstp))
+		err = nfserrno(nfsd_setuser(&rqstp->rq_cred, exp));
 
 	dput(dentry);
 	exp_put(exp);
Signature · 1 line
2.56.0
author maintainer
#6 lore raw

PATCH v3 6/7 NFSD: Use each export's credentials for the COPY and CLONE stateids

In reply to Chuck Lever

nfsd4_verify_copy() checks the source export with check_nfsd_access() but resolves the source stateid under the credentials that PUTFH of the destination installed. For an OPEN stateid, nfsd_permission() then checks the source file with the destination export's squash mapping. When the source export squashes root and the destination export does not, the source file is checked as root. When the source export uses all_squash, nfsd_permission() runs as the caller instead of the anonymous UID and GID, and can reject a request the source export permits.

Install the source export's credentials before resolving the source stateid, and restore the destination export's credentials before resolving the destination stateid. Both exports have already passed their security checks, so neither filehandle needs a full fh_verify(). When the source and destination share an export, the credentials PUTFH installed already apply, so skip both calls.

Fixes ffa0160a1039 ("nfsd: implement the NFSv4.2 CLONE operation")
---
 fs/nfsd/nfs4proc.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
fs/nfsd/nfs4proc.c +17 -1
index 5121e7e80db1..702fa3e99c67 100644
@@ -867,7 +867,7 @@ nfsd4_putrootfh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	status = exp_pseudoroot(rqstp, &cstate->current_fh);
 	if (status)
 		return status;
-	/* RESTOREFH assumes the credentials for current_fh are installed. */
+	/* RESTOREFH and COPY assume current_fh's credentials are installed. */
 	return nfserrno(nfsd_setuser(&rqstp->rq_cred,
 				     cstate->current_fh.fh_export));
 }
@@ -1538,6 +1538,7 @@ nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		  stateid_t *src_stateid, struct nfsd_file **src,
 		  stateid_t *dst_stateid, struct nfsd_file **dst)
 {
+	bool crossed;
 	__be32 status;
 
 	if (!cstate->save_fh.fh_dentry)
@@ -1547,12 +1548,27 @@ nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 	status = check_nfsd_access(cstate->save_fh.fh_export, rqstp, 0);
 	if (status)
 		return status == nfserr_wrongsec ? nfserr_acces : status;
+	crossed = cstate->save_fh.fh_export != cstate->current_fh.fh_export;
+	if (crossed) {
+		status = nfserrno(nfsd_setuser(&rqstp->rq_cred,
+					       cstate->save_fh.fh_export));
+		if (status)
+			return status;
+	}
 
 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->save_fh,
 					    src_stateid, RD_STATE, src, NULL);
 	if (status)
 		goto out;
 
+	/* The destination export was checked when current_fh was set. */
+	if (crossed) {
+		status = nfserrno(nfsd_setuser(&rqstp->rq_cred,
+					       cstate->current_fh.fh_export));
+		if (status)
+			goto out_put_src;
+	}
+
 	status = nfs4_preprocess_stateid_op(rqstp, cstate, &cstate->current_fh,
 					    dst_stateid, WR_STATE, dst, NULL);
 	if (status)
Signature · 1 line
2.56.0
author maintainer
#7 lore raw

PATCH v3 7/7 NFSD: Verify the target directory last in nfsd_link()

In reply to Chuck Lever

nfsd_link() verifies the target directory first and the source file last, and fh_verify() installs the credentials of the export it verifies. When the two filehandles are in different exports of one file system, vfs_link() checks and modifies the target directory with the source export's squash mapping, and a LINK that the target export permits can fail with EACCES. After an NFSv4 LINK, the source export's credentials also stay installed while the current filehandle stays in the target export. Operations that follow in the same compound without calling fh_verify(), such as READ with an OPEN stateid, then run on the target export with the source export's squash mapping. RENAME verifies its target directory last, so it is not affected.

Verify the source file first and the target directory last. After the change, the target export's credentials are in effect for vfs_link() and for the rest of an NFSv4 compound. When neither filehandle passes fh_verify(), nfsd_link() now returns the source file's error.

---
 fs/nfsd/vfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/nfsd/vfs.c +6 -2
index c227a3d3a805..77ca88618b13 100644
@@ -2297,10 +2297,14 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
 
 	trace_nfsd_vfs_link(rqstp, ffhp, tfhp, name, len);
 
-	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
+	/*
+	 * Verify @ffhp last so that the credentials of its export are in
+	 * effect for vfs_link() and after return.
+	 */
+	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
 	if (err)
 		goto out;
-	err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
+	err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
 	if (err)
 		goto out;
 	err = nfserr_isdir;
Signature · 1 line
2.56.0