Skip to contentSkip to conversation

nfsd: enforce search permission on directory delegations without breaking stat()

PATCH Superseded Dominik Maier 3 participants 5 messages Nov 24

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover nfsd: enforce search permission on directory delegations without breaking stat()

Enforce NFSD_MAY_EXEC (and limit NFSD_MAY_OWNER_OVERRIDE to S_IFREG) when acquiring directory delegations, preceded by returning GDD4_UNAVAIL on nfsd_file_acquire_dir() failure so Linux NFS clients issuing PUTFH + GET_DIR_DELEGATION + GETATTR do not fail stat() with EACCES on directories lacking read or search permission.

Changes in v2:
- Add patch 1/2 returning NFS4_OK with GDD4_UNAVAIL when nfsd_file_acquire_dir() fails (Chuck Lever).
- Add Jeff Layton's Reviewed-by on patch 2/2, keep the single root

Fixes 544a0ee152f0 tag, and drop the commit-body tool boilerplate.

- v1: https://lore.kernel.org/linux-nfs/[email protected]/

Patch 2/2 was found with AI-assisted code review and reproduced against an unmodified kernel in QEMU; the v2 series is based on nfsd-testing.

Dominik Maier (2):
nfsd: return GDD4_UNAVAIL when nfsd_file_acquire_dir() fails
nfsd: require NFSD_MAY_EXEC when acquiring directory delegations

 fs/nfsd/filecache.c | 10 ++++++----
 fs/nfsd/nfs4proc.c  |  6 ++++--
 2 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.56.1.831.g720f3f5937-goog
#1 lore raw

PATCH v2 1/2 nfsd: return GDD4_UNAVAIL when nfsd_file_acquire_dir() fails

The Linux NFS client requests a directory delegation by inserting GET_DIR_DELEGATION between PUTFH and GETATTR in a single COMPOUND, and _nfs4_proc_getattr() does not tolerate -EACCES. When nfsd_file_acquire_dir() fails (for example for a non-owner doing stat() on a mode 0711 directory without read permission), returning the error aborts the COMPOUND before GETATTR and surfaces EACCES to stat().

Reply NFS4_OK with GDD4_UNAVAIL when nfsd_file_acquire_dir() fails, matching the non-fatal nfsd_get_dir_deleg() path below so the delegation is withheld while the trailing GETATTR still completes.

Fixes 8b99f6a8c116 ("nfsd: wire up GET_DIR_DELEGATION handling")

---
Note: matching your snippet, this folds any nfsd_file_acquire_dir() error into GDD4_UNAVAIL; let me know if you prefer narrowing it to status == nfserr_acces so errors like NFS4ERR_NOTDIR or NFS4ERR_WRONGSEC still surface directly.

 fs/nfsd/nfs4proc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4proc.c +4 -2
index b76320c265e1..dd17a6b47222 100644
@@ -2873,8 +2873,10 @@ nfsd4_get_dir_delegation(struct svc_rqst *rqstp,
 	__be32 status;
 
 	status = nfsd_file_acquire_dir(rqstp, &cstate->current_fh, &nf);
-	if (status != nfs_ok)
-		return status;
+	if (status != nfs_ok) {
+		gdd->gddrnf_status = GDD4_UNAVAIL;
+		return nfs_ok;
+	}
 
 	/*
 	 * Offer no notifications to an order-aware client. RFC8881bis section
Signature · 1 line
2.56.1.831.g720f3f5937-goog
#4 lore raw

PATCH v2 2/2 nfsd: require NFSD_MAY_EXEC when acquiring directory delegations

In reply to Dominik Maier

nfsd_file_acquire_dir() opens a directory for nfsd4_get_dir_delegation() with NFSD_MAY_READ | NFSD_MAY_64BIT_COOKIE, omitting NFSD_MAY_EXEC, and nfsd_file_do_acquire() unconditionally sets NFSD_MAY_OWNER_OVERRIDE even for S_IFDIR (unlike nfsd_open(), which limits it to S_IFREG).

On a directory with read permission but no search permission (such as mode 0744 for a non-owner, or mode 0400 / ACL-denied search for the owner), an NFSv4.1 client can still acquire a directory delegation with NOTIFY4_ADD_ENTRY and FATTR4_WORD0_FILEHANDLE and receive valid child filehandles in CB_NOTIFY callbacks when entries are created, despite LOOKUP returning NFS4ERR_ACCESS.

Require NFSD_MAY_EXEC in nfsd_file_acquire_dir() and restrict NFSD_MAY_OWNER_OVERRIDE in nfsd_file_do_acquire() to S_IFREG, matching nfsd_open().

Fixes 544a0ee152f0 ("nfsd: allow filecache to hold S_IFDIR files")
---
 fs/nfsd/filecache.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/nfsd/filecache.c +6 -4
index 162d07eb5be4..a703d376a52f 100644
@@ -1197,13 +1197,14 @@ nfsd_file_do_acquire(struct svc_rqst *rqstp, struct net *net,
 	__be32 status;
 	int ret;
 
+	if (type == S_IFREG)
+		may_flags |= NFSD_MAY_OWNER_OVERRIDE;
 retry:
 	if (rqstp)
-		status = fh_verify(rqstp, fhp, type,
-				   may_flags|NFSD_MAY_OWNER_OVERRIDE);
+		status = fh_verify(rqstp, fhp, type, may_flags);
 	else
 		status = fh_verify_local(net, cred, client, fhp, type,
-					 may_flags|NFSD_MAY_OWNER_OVERRIDE);
+					 may_flags);
 
 	if (status != nfs_ok)
 		return status;
@@ -1475,7 +1476,8 @@ nfsd_file_acquire_dir(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		      struct nfsd_file **pnf)
 {
 	return nfsd_file_do_acquire(rqstp, SVC_NET(rqstp), NULL, NULL, fhp,
-				    NFSD_MAY_READ|NFSD_MAY_64BIT_COOKIE,
+				    NFSD_MAY_READ | NFSD_MAY_EXEC |
+				    NFSD_MAY_64BIT_COOKIE,
 				    NULL, S_IFDIR, false, pnf);
 }
 
Signature · 1 line
2.56.1.831.g720f3f5937-goog
maintainer
#2 lore raw

Re: [PATCH v2 1/2] nfsd: return GDD4_UNAVAIL when nfsd_file_acquire_dir() fails

In reply to Dominik Maier
Quoted 4 lines

Note: matching your snippet, this folds any nfsd_file_acquire_dir() error into GDD4_UNAVAIL; let me know if you prefer narrowing it to status == nfserr_acces so errors like NFS4ERR_NOTDIR or NFS4ERR_WRONGSEC still surface directly.

Yes, please narrow it. My snippet was too broad.

Quoted 5 lines
fs/nfsd/nfs4proc.c +0 -0
index b76320c265e1..dd17a6b47222 100644

fs/nfsd/nfs4proc.c nfsd4_get_dir_delegation(struct svc_rqst *rqstp, · line 2879

Quoted 9 lines
fs/nfsd/nfs4proc.c +4 -2
@@ -2873,8 +2873,10 @@ nfsd4_get_dir_delegation(struct svc_rqst *rqstp,
 	__be32 status;
 
 	status = nfsd_file_acquire_dir(rqstp, &cstate->current_fh, &nf);
-	if (status != nfs_ok)
-		return status;
+	if (status != nfs_ok) {
+		gdd->gddrnf_status = GDD4_UNAVAIL;
+		return nfs_ok;
+	}

nfsd_file_acquire_dir() verifies the filehandle with S_IFDIR, so a GET_DIR_DELEGATION on a regular file fails here with nfserr_notdir. RFC 8881 lists NFS4ERR_NOTDIR for this operation. With this change, NFSD replies NFS4_OK to a client that asks for a delegation on a regular file.

The empty-filehandle case is less obvious. Both retry paths in nfsd_file_do_acquire() call fh_put() on cstate->current_fh before they call fh_verify() again. If that second fh_verify() fails (say, nfserr_stale after a racing unlink), current_fh.fh_dentry is NULL when this function returns nfs_ok. Doesn't the trailing GETATTR then fail with NFS4ERR_NOFILEHANDLE, rather than GET_DIR_DELEGATION reporting the stale handle?

Testing for nfserr_acces covers both cases. __fh_verify() calls nfsd_set_fh_dentry() before nfsd_permission(), so an nfserr_acces always comes back with fh_dentry set:

	if (status == nfserr_acces) {
		gdd->gddrnf_status = GDD4_UNAVAIL;
		return nfs_ok;
	}
	if (status != nfs_ok)
		return status;
Signature · 1 line
Chuck Lever
maintainer Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] nfsd: return GDD4_UNAVAIL when nfsd_file_acquire_dir() fails

In reply to Dominik Maier
Quoted 30 lines · Dominik Maier

The Linux NFS client requests a directory delegation by inserting GET_DIR_DELEGATION between PUTFH and GETATTR in a single COMPOUND, and _nfs4_proc_getattr() does not tolerate -EACCES. When nfsd_file_acquire_dir() fails (for example for a non-owner doing stat() on a mode 0711 directory without read permission), returning the error aborts the COMPOUND before GETATTR and surfaces EACCES to stat().

Reply NFS4_OK with GDD4_UNAVAIL when nfsd_file_acquire_dir() fails, matching the non-fatal nfsd_get_dir_deleg() path below so the delegation is withheld while the trailing GETATTR still completes.

Fixes 8b99f6a8c116 ("nfsd: wire up GET_DIR_DELEGATION handling")

---
Note: matching your snippet, this folds any nfsd_file_acquire_dir() error into GDD4_UNAVAIL; let me know if you prefer narrowing it to status == nfserr_acces so errors like NFS4ERR_NOTDIR or NFS4ERR_WRONGSEC still surface directly.

 fs/nfsd/nfs4proc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/nfsd/nfs4proc.c +0 -0
index b76320c265e1..dd17a6b47222 100644
@@ -2873,8 +2873,10 @@ nfsd4_get_dir_delegation(struct svc_rqst *rqstp,
 	__be32 status;
 

fs/nfsd/nfs4proc.c nfsd4_get_dir_delegation(struct svc_rqst *rqstp, · line 2882

Quoted 10 lines
fs/nfsd/nfs4proc.c +4 -2
 	status = nfsd_file_acquire_dir(rqstp, &cstate->current_fh, &nf);
-	if (status != nfs_ok)
-		return status;
+	if (status != nfs_ok) {
+		gdd->gddrnf_status = GDD4_UNAVAIL;
+		return nfs_ok;
+	}
 
 	/*
 	 * Offer no notifications to an order-aware client. RFC8881bis section

Nice catch!