nfsd: require NFSD_MAY_EXEC when acquiring directory delegations
Subsystems (2)
PATCH 1/1 nfsd: require NFSD_MAY_EXEC when acquiring directory delegations
nfsd_file_acquire_dir() opens a directory on behalf of nfsd4_get_dir_delegation() with NFSD_MAY_READ | NFSD_MAY_64BIT_COOKIE, omitting NFSD_MAY_EXEC, while nfsd_file_do_acquire() unconditionally ORs NFSD_MAY_OWNER_OVERRIDE into may_flags even for S_IFDIR (unlike nfsd_open(), which restricts NFSD_MAY_OWNER_OVERRIDE to S_IFREG).
On a directory that grants read permission without execute (search) permission (for example mode 0744, where LOOKUP is denied with NFS4ERR_ACCESS), an unprivileged NFSv4.1 client can still acquire a directory delegation requesting NOTIFY4_ADD_ENTRY and FATTR4_WORD0_FILEHANDLE. In addition, NFSD_MAY_OWNER_OVERRIDE causes nfsd_permission() to return early before inode_permission() for the directory owner, bypassing missing execute bits (such as mode 0400 on a read-only or immutable directory) as well as POSIX ACL or LSM search denials. When entries are subsequently created in that directory, setup_notify_fhandle() constructs and signs valid child filehandles in CB_NOTIFY callbacks, allowing the client to open child files without search permission on the parent directory.
Add NFSD_MAY_EXEC to the access mask in nfsd_file_acquire_dir() and restrict NFSD_MAY_OWNER_OVERRIDE in nfsd_file_do_acquire() to type == S_IFREG, matching nfsd_open().
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.
--- fs/nfsd/filecache.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/nfsd/filecache.c +6 -4
@@ -1183,13 +1183,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;
@@ -1457,7 +1458,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.750.gf70dc682cf-goog
Quoted 60 lines · Dominik Maier
nfsd_file_acquire_dir()opens a directory on behalf ofnfsd4_get_dir_delegation()withNFSD_MAY_READ|NFSD_MAY_64BIT_COOKIE, omittingNFSD_MAY_EXEC, whilenfsd_file_do_acquire()unconditionally ORsNFSD_MAY_OWNER_OVERRIDEintomay_flagseven forS_IFDIR(unlikenfsd_open(), which restrictsNFSD_MAY_OWNER_OVERRIDEtoS_IFREG).On a directory that grants read permission without execute (search) permission (for example mode 0744, where LOOKUP is denied with
NFS4ERR_ACCESS), an unprivileged NFSv4.1 client can still acquire a directory delegation requestingNOTIFY4_ADD_ENTRYandFATTR4_WORD0_FILEHANDLE. In addition,NFSD_MAY_OWNER_OVERRIDEcausesnfsd_permission()to return early beforeinode_permission()for the directory owner, bypassing missing execute bits (such as mode 0400 on a read-only or immutable directory) as well as POSIX ACL or LSM search denials. When entries are subsequently created in that directory,setup_notify_fhandle()constructs and signs valid child filehandles inCB_NOTIFYcallbacks, allowing the client to open child files without search permission on the parent directory.Add
NFSD_MAY_EXECto the access mask innfsd_file_acquire_dir()and restrictNFSD_MAY_OWNER_OVERRIDEinnfsd_file_do_acquire()to type ==S_IFREG, matchingnfsd_open().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 544a0ee152f0 ("nfsd: allow filecache to hold S_IFDIR files")Fixes c10ce74117ad ("nfsd: add the filehandle to returned attributes in CB_NOTIFY")Signed-off-by Dominik Maier <[email protected]>--- fs/nfsd/filecache.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)fs/nfsd/filecache.c +4
-3@@ -1183,13 +1183,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;
fs/nfsd/filecache.c nfsd_file_acquire_dir(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1465
Quoted 9 lines
fs/nfsd/filecache.c +2
-1@@ -1457,7 +1458,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); }
Chuck Lever
maintainerfs/nfsd/filecache.c nfsd_file_acquire_dir(struct svc_rqst *rqstp, struct svc_fh *fhp, · line 1482
Quoted 9 lines · Dominik Maier
fs/nfsd/filecache.c +2
-1@@ -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); }
Hi Dominik,
Closing the search permission gap is right, but the stricter check might change what a plain stat() sees on a Linux NFS client.
The Linux NFS client requests a directory delegation by placing GET_DIR_DELEGATION between PUTFH and GETATTR in a single COMPOUND. _nfs4_proc_getattr() does not handle -EACCES. The error reaches stat(), even though stat() needs no permission on the directory itself.
Before this patch, looks like that failure already hit a non-owner looking at a directory without read permission, such as a 0711 home directory. With NFSD_MAY_EXEC added and NFSD_MAY_OWNER_OVERRIDE limited to S_IFREG, the failure now also hits a non-owner on a 0744 directory and an owner on a 0600 or 0400 directory. After an access() call on such a directory, "ls -ld" fails with EACCES.
So... RFC 8881 permits NFS4ERR_ACCESS from GET_DIR_DELEGATION, so the Linux NFS client is arguably wrong not to tolerate it. But deployed clients behave this way today, and NFSD already has a non-fatal path:
nfsd4_get_dir_delegation() replies NFS4_OK with GDD4_UNAVAIL when nfsd_get_dir_deleg() fails, as the comment above that call explains. Taking the same path when nfsd_file_acquire_dir() fails still withholds the delegation from a client that lacks search permission, and the GETATTR that follows completes:
status = nfsd_file_acquire_dir(rqstp, &cstate->current_fh, &nf);
if (status != nfs_ok) {
gdd->gddrnf_status = GDD4_UNAVAIL;
return nfs_ok;
}The 0711 case predates your patch, so I'd prefer the change above as a separate patch ahead of this one, with its own Fixes: tag, re-rolled as a v2 series.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)