Skip to contentSkip to conversation

nfsd: enforce export checks in nfsd4_restorefh() and nfs4_check_file()

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

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

PATCH 1/1 nfsd: enforce export checks in nfsd4_restorefh() and nfs4_check_file()

When an NFSv4 compound saves a filehandle from a UID-squashed export (all_squash or root_squash), switches current_fh to an unsquashed export via PUTFH, and then restores the saved filehandle via RESTOREFH, nfsd4_restorefh() duplicates save_fh back into current_fh without recomputing the worker thread's subjective credentials via nfsd_setuser().

In addition, when nfs4_check_file() finds a cached nfsd_file on a stateid, it calls nfsd_permission() directly instead of running fh_verify() for fhp (unlike the uncached nfsd_file_acquire() path). Because PUTFH and SAVEFH defer security-flavor checks via OP_HANDLES_WRONGSEC, both post-RESTOREFH operations and cross-export COPY/CLONE operations (via nfsd4_verify_copy()) can skip nfsd_setuser(), check_xprtsec_policy(), check_security_flavor(), and originating-port checks on the cached-file fast path. While nfsd4_restorefh() has omitted credential restoration since early NFSv4 compound support, nfs4_check_file() and commit b7342204253a ("NFSD check stateids against copy stateids") expanded cached-stateid checks that bypass fh_verify().

Call nfsd_setuser() when restoring save_fh in nfsd4_restorefh() (preserving deferred OP_IS_PUTFH_LIKE WRONGSEC handling) and call fh_verify() on the cached-file branch in nfs4_check_file() to match the uncached branch.

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 b7342204253a ("NFSD check stateids against copy stateids")
---
 fs/nfsd/nfs4proc.c  | 9 +++++++++
 fs/nfsd/nfs4state.c | 4 +---
 2 files changed, 10 insertions(+), 3 deletions(-)
fs/nfsd/nfs4proc.c +9 -0
index 3a82af3..1e7abe5 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"
@@ -847,9 +848,17 @@ static __be32
 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		union nfsd4_op_u *u)
 {
+	int err;
+
 	if (!cstate->save_fh.fh_dentry)
 		return nfserr_restorefh;
 
+	if (cstate->save_fh.fh_export) {
+		err = nfsd_setuser(&rqstp->rq_cred, cstate->save_fh.fh_export);
+		if (err)
+			return nfserrno(err);
+	}
+
 	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));
fs/nfsd/nfs4state.c +1 -3
index 567a856..8e5c3ca 100644
@@ -8434,9 +8434,7 @@ nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
 
 	nf = nfs4_find_file(s, flags);
 	if (nf) {
-		status = nfsd_permission(&rqstp->rq_cred,
-					 fhp->fh_export, fhp->fh_dentry,
-				acc | NFSD_MAY_OWNER_OVERRIDE);
+		status = fh_verify(rqstp, fhp, 0, acc | NFSD_MAY_OWNER_OVERRIDE);
 		if (status) {
 			nfsd_file_put(nf);
 			goto out;
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Dominik Maier

When an NFSv4 compound saves a filehandle from a UID-squashed export (all_squash or root_squash), switches current_fh to an unsquashed export via PUTFH, and then restores the saved filehandle via RESTOREFH, nfsd4_restorefh() duplicates save_fh back into current_fh without recomputing the worker thread's subjective credentials via nfsd_setuser().

In addition, when nfs4_check_file() finds a cached nfsd_file on a stateid, it calls nfsd_permission() directly instead of running fh_verify() for fhp (unlike the uncached nfsd_file_acquire() path). Because PUTFH and SAVEFH defer security-flavor checks via OP_HANDLES_WRONGSEC, both post-RESTOREFH operations and cross-export COPY/CLONE operations (via nfsd4_verify_copy()) can skip nfsd_setuser(), check_xprtsec_policy(), check_security_flavor(), and originating-port checks on the cached-file fast path. While nfsd4_restorefh() has omitted credential restoration since early NFSv4 compound support, nfs4_check_file() and commit b7342204253a ("NFSD check stateids against copy stateids") expanded cached-stateid checks that bypass fh_verify().

Call nfsd_setuser() when restoring save_fh in nfsd4_restorefh() (preserving deferred OP_IS_PUTFH_LIKE WRONGSEC handling) and call fh_verify() on the cached-file branch in nfs4_check_file() to match the uncached branch.

Quoted 7 lines

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 b7342204253a ("NFSD check stateids against copy stateids")

nit: Sashiko says the Fixes: tag may be wrong.

Quoted 42 lines
---
 fs/nfsd/nfs4proc.c  | 9 +++++++++
 fs/nfsd/nfs4state.c | 4 +---
 2 files changed, 10 insertions(+), 3 deletions(-)
fs/nfsd/nfs4proc.c +9 -0
index 3a82af3..1e7abe5 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"
@@ -847,9 +848,17 @@ static __be32
 nfsd4_restorefh(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
 		union nfsd4_op_u *u)
 {
+	int err;
+
 	if (!cstate->save_fh.fh_dentry)
 		return nfserr_restorefh;
 
+	if (cstate->save_fh.fh_export) {
+		err = nfsd_setuser(&rqstp->rq_cred, cstate->save_fh.fh_export);
+		if (err)
+			return nfserrno(err);
+	}
+
 	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));
fs/nfsd/nfs4state.c +0 -0
index 567a856..8e5c3ca 100644

fs/nfsd/nfs4state.c nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, · line 8440

Quoted 10 lines
fs/nfsd/nfs4state.c +1 -3
@@ -8434,9 +8434,7 @@ nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
 
 	nf = nfs4_find_file(s, flags);
 	if (nf) {
-		status = nfsd_permission(&rqstp->rq_cred,
-					 fhp->fh_export, fhp->fh_dentry,
-				acc | NFSD_MAY_OWNER_OVERRIDE);
+		status = fh_verify(rqstp, fhp, 0, acc | NFSD_MAY_OWNER_OVERRIDE);
 		if (status) {
 			nfsd_file_put(nf);
 			goto out;
maintainer
#2 lore raw
In reply to Dominik Maier
Quoted 6 lines · Dominik Maier

When an NFSv4 compound saves a filehandle from a UID-squashed export (all_squash or root_squash), switches current_fh to an unsquashed export via PUTFH, and then restores the saved filehandle via RESTOREFH, nfsd4_restorefh() duplicates save_fh back into current_fh without recomputing the worker thread's subjective credentials via nfsd_setuser().

Thanks for the report. Both problems are already addressed in nfsd-testing by fixes that went to the list earlier this month:

9bfcc9001962 ("NFSD: Apply the entered export's credentials after LOOKUP and RESTOREFH") e98bd0f51645 ("NFSD: Check export security on the COPY and CLONE source") ba7c5a136a13 ("NFSD: Use each export's credentials for the COPY and CLONE stateids") 305bf9791dd1 ("NFSD: Check the secure-port policy when entering an export")

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