NFSD: Enforce export security after a deferred PUTFH check
Subsystems (2)
Chuck Lever
maintainerv6 cover NFSD: Enforce export security after a deferred PUTFH check
VeroFess reported that an AUTH_SYS NFSv4.0 client can read or write a file on a sec=krb5p export with another client's OPEN stateid by placing SAVEFH between PUTFH and the I/O operation. The v4 posting carried that fix together with changes to COPY, LOOKUP, READDIR, and SECINFO in one patch, which would not apply to the stable kernels that need the fix. This series splits that patch. The need_wrongsec_check() change stands alone (patch 1) and applies to every LTS kernel, and the remaining patches carry only the Fixes:
tags that match their code. VeroFess developed the original fix with the assistance of an LLM, and an LLM assisted with the split and the commit messages in this series.
VeroFess confirmed on a kernel with only the need_wrongsec_check() change that PUTFH; SAVEFH; READ and PUTFH; SAVEFH; WRITE return NFS4ERR_WRONGSEC, and on a kernel with that change plus the COPY source check that PUTFH; SAVEFH; PUTFH; COPY returns NFS4ERR_ACCESS. The series as posted has not been run against the reproducer yet. VeroFess has offered to do that.
After this series, NFSD still relies on per-operation check_nfsd_access() calls to enforce a PUTFH check that was deferred. A filehandle could instead carry a check-pending state that the COMPOUND loop enforces before any operation consumes the filehandle. Carrying such a state is later work.
Based on nfsd-testing.
---
Changes in v6:
- Verify the current filehandle in SECINFO_NO_NAME(CURRENT_FH) (sashiko).
- Note that PUTFH still returns WRONGSEC for a GSS or xprtsec= mismatch.
- Link to v5: https://patch.msgid.link/[email protected]
Changes in v5:
- Split the single patch into three, each with its own Fixes: tags.
- Drop the nfs4_check_file() and COPY_NOTIFY export checks.
- Check the entered export in nfsd_cross_mnt() and nfsd_lookup_parent().
- Replace the for_secinfo flag with NFSD_MAY_SECINFO.
- Report a SECINFO transport policy mismatch as NFS4ERR_ACCESS.
- Keep the PUTFH export check for NFSv4.0 PUTFH; SECINFO.
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Skip only the flavor check for SECINFO; xprtsec= stays enforced.
- Re-check xprtsec= when a SECINFO lookup selects another export.
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Use nfsd_lookup_dentry() for SECINFO_NO_NAME(PARENT).
- Let SECINFO and SECINFO_NO_NAME(PARENT) query a strict starting export.
- Skip the source export's policy check on a SECINFO mount crossing.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Check cached stateid I/O and COPY_NOTIFY against the current export.
- Check the selected export after LOOKUP and READDIR mount crossing.
- Map READDIR's NFS4ERR_WRONGSEC to NFS4ERR_ACCESS without rdattr_error.
- Add the missing Fixes: tags and regenerate the whitespace-damaged diff.
- v1 was sent off-list.
---
VeroFess (3):
NFSD: Fix PUTFH wrongsec check when SAVEFH follows
NFSD: Check export security on the COPY and CLONE source
NFSD: Enforce export security on LOOKUP, READDIR, and SECINFO
fs/nfsd/export.c | 6 ++-- fs/nfsd/nfs3proc.c | 2 +- fs/nfsd/nfs4proc.c | 84 ++++++++++++++++++++++++++++++++++++++--------------- fs/nfsd/nfs4state.c | 2 +- fs/nfsd/nfs4xdr.c | 8 +++-- fs/nfsd/nfsfh.c | 12 ++++++-- fs/nfsd/nfsproc.c | 2 +- fs/nfsd/trace.h | 3 +- fs/nfsd/vfs.c | 72 +++++++++++++++++++++++++-------------------- fs/nfsd/vfs.h | 43 +++++++++++++++++++++++---- 10 files changed, 162 insertions(+), 72 deletions(-)
---
base-commit: d9130c9f6535392d423351ffb5521e7689cae6b6
change-id: 20261030-verofess-f0281be339bc
Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
Chuck Lever
maintainerPATCH v6 1/3 NFSD: Fix PUTFH wrongsec check when SAVEFH follows
From: VeroFess <[email protected]>
An AUTH_SYS NFSv4.0 client can read or write a file on a sec=krb5p export using another client's valid OPEN stateid by inserting SAVEFH between PUTFH and READ or WRITE.
PUTFH resolves the handle with NFSD_MAY_BYPASS_GSS and defers the security flavor check to need_wrongsec_check(), which looks only at the next operation. SAVEFH carries OP_HANDLES_WRONGSEC even though it only saves the handle, so PUTFH skips NFS4ERR_WRONGSEC. The READ or WRITE that follows then reuses a cached nfsd_file.
With these export options:
rw,sync,no_subtree_check,fsid=0,sec=krb5p,secure,root_squash
a Kerberos privacy NFSv4.0 client held an OPEN stateid. With the same stateid, PUTFH; READ over AUTH_SYS was rejected, but PUTFH; SAVEFH; READ and PUTFH; SAVEFH; WRITE succeeded, and the Kerberos client subsequently read the changed contents.
SAVEFH leaves CURRENT_FH in place. When deciding whether PUTFH must report NFS4ERR_WRONGSEC, look past SAVEFH to the first operation that uses or replaces CURRENT_FH. If another PUTFH replaces it, leave the earlier PUTFH unchecked, as RFC 8881 Section 2.6.3.1.1 requires. Drop OP_HANDLES_WRONGSEC from SAVEFH, which performs no check of its own and is never consulted once it is skipped.
--- fs/nfsd/nfs4proc.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-)
fs/nfsd/nfs4proc.c +21 -14
@@ -3237,32 +3237,39 @@ static bool need_wrongsec_check(struct svc_rqst *rqstp)struct nfsd4_compoundres *resp = rqstp->rq_resp;struct nfsd4_compoundargs *argp = rqstp->rq_argp;struct nfsd4_op *this = &argp->ops[resp->opcnt -1 ]; -struct nfsd4_op *next = &argp->ops[resp->opcnt]; +struct nfsd4_op *next =NULL ;const struct nfsd4_operation *thisd =OPDESC (this);const struct nfsd4_operation *nextd; +unsigned int i; -/* -* Most ops check wronsec on our own; only the putfh-like ops -* have special rules. -*/ +/* Only put-filehandle operations need a deferred WRONGSEC check. */ if (!(thisd->op_flags &OP_IS_PUTFH_LIKE ))return false; +/* -* rfc 5661 2.6.3.1.1.6: don't bother erroring out a -* put-filehandle operation if we're not going to use the -* result: +* RFC 8881 Section 2.6.3.1.1.1: SAVEFH does not count as the +* next operation. */ -if (argp->opcnt == resp->opcnt) +for (i = resp->opcnt; i < argp->opcnt; i++) { + next = &argp->ops[i]; +if (next->opnum !=OP_SAVEFH ) +break ; + } + +/* RFC 8881 Section 2.6.3.1.1.6: nothing uses the filehandle. */ +if (i == argp->opcnt)return false; +if (next->opnum ==OP_ILLEGAL )return false; nextd =OPDESC (next);/* -* Rest of 2.6.3.1.1: certain operations will return WRONGSEC -* errors themselves as necessary; others should check for them -* now: +* RFC 8881 Section 2.6.3.1.1.2: a later put-filehandle operation +* replaces this one. Other marked operations check WRONGSEC +* themselves. */ -return !(nextd->op_flags &OP_HANDLES_WRONGSEC ); +return !(nextd->op_flags & + (OP_HANDLES_WRONGSEC |OP_IS_PUTFH_LIKE )); }#ifdef CONFIG_NFSD_V4_2_INTER_SSC
@@ -4047,7 +4054,7 @@ static const struct nfsd4_operation nfsd4_ops[] = { }, [OP_SAVEFH] = { .op_func = nfsd4_savefh, - .op_flags = OP_HANDLES_WRONGSEC | OP_MODIFIES_SOMETHING, + .op_flags = OP_MODIFIES_SOMETHING, .op_name = "OP_SAVEFH", .op_rsize_bop = nfsd4_only_status_rsize, },
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH v6 2/3 NFSD: Check export security on the COPY and CLONE source
In reply to Chuck LeverFrom: VeroFess <[email protected]>
RFC 8881 Section 2.6.3.1.1 forbids PUTFH from returning NFS4ERR_WRONGSEC when another PUTFH follows, so in PUTFH; SAVEFH; PUTFH; COPY the source handle reaches nfsd4_verify_copy() with its export's security flavor unchecked. The source stateid then finds a cached nfsd_file and the copy proceeds.
On a kernel with only the need_wrongsec_check() fix, an AUTH_SYS READ of a sec=krb5p source returned NFS4ERR_WRONGSEC, but PUTFH; SAVEFH; PUTFH; COPY of the same file completed and wrote the source bytes to the destination.
Check the saved export with check_nfsd_access() before using the source stateid. Map NFS4ERR_WRONGSEC to NFS4ERR_ACCESS, since neither COPY nor CLONE permits NFS4ERR_WRONGSEC.
--- fs/nfsd/nfs4proc.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/nfsd/nfs4proc.c +5 -0
@@ -1491,6 +1491,11 @@ nfsd4_verify_copy(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,if (!cstate->save_fh.fh_dentry)return nfserr_nofilehandle; +/* A cached open stateid does not authorize the saved source export. */ + status =check_nfsd_access (cstate->save_fh.fh_export, rqstp); +if (status) +return status == nfserr_wrongsec ? nfserr_acces : status; + status =nfs4_preprocess_stateid_op (rqstp, cstate, &cstate->save_fh, src_stateid,RD_STATE , src,NULL );if (status)
Signature · 1 line
2.56.0
Chuck Lever
maintainerPATCH v6 3/3 NFSD: Enforce export security on LOOKUP, READDIR, and SECINFO
In reply to Chuck LeverFrom: VeroFess <[email protected]>
A bind-mounted regular file can have a stricter export than its parent. nfsd_cross_mnt() checks the request's flavor against the export being left, not the one being entered, so LOOKUP, READDIR, and the NFSv2 and NFSv3 CREATE paths all hand out filehandles on the child export without consulting its sec= list. Stepping up with ".." has the same gap: nfsd_lookup_parent() switches to the parent export without any check, so LOOKUPP from the root of a sec=sys export returns a filehandle on a sec=krb5p parent. Every caller has already passed fh_verify() on the export being left, so check only the export being entered, in nfsd_cross_mnt() and in nfsd_lookup_parent().
READDIR crosses into child exports to fetch entry attributes, so the new check also fires there. When the client requests rdattr_error, report NFS4ERR_WRONGSEC in that attribute, as RFC 8881 Section 2.6.3.1 directs. Otherwise the whole READDIR fails, as Section 18.23.3 requires; NFS4ERR_WRONGSEC is not a READDIR status, so report NFS4ERR_ACCESS. The Linux, FreeBSD, illumos, and Windows clients all request rdattr_error on every READDIR.
SECINFO and SECINFO_NO_NAME report export security policies. RFC 8881 Section 2.6.3.1.1.5 forbids NFS4ERR_WRONGSEC from either operation, which allows a client using any flavor to learn whether a name exists in a directory on a sec=krb5p export. The starting directory's fh_verify(..., NFSD_MAY_EXEC) call rejects SECINFO and SECINFO_NO_NAME(PARENT) when that export does not allow the request's flavor, and a mount crossing rejects it again on the crossed-into export. SECINFO_NO_NAME(CURRENT_FH) never calls fh_verify() at all, so after PUTROOTFH it reports the pseudo-root's flavors without checking that export's transport policy or secure port requirement.
Add NFSD_MAY_SECINFO, which fh_verify() and the lookup path both
read: it skips the starting export's security flavor check and the flavor check on a mount crossing or parent step-up. Verify the current filehandle with it in SECINFO_NO_NAME(CURRENT_FH) as well. NFSv4.0 is different: RFC 7530 permits NFS4ERR_WRONGSEC from PUTFH and from neither SECINFO nor the operation after it, and a v4.0 SECINFO leaves the current filehandle in place. A v4.0 put-filehandle operation followed by SECINFO therefore keeps its own export check, so the error is reported where RFC 7530 allows it. The Linux and illumos clients send SECINFO only with a parent directory they already hold under the same flavor, and FreeBSD does not send SECINFO at all, so the check does not fire for any of them. The export's transport policy (xprtsec=) is a transport-layer requirement, not part of the flavor negotiation SECINFO performs, so SECINFO still enforces it on the starting export and on any export a crossing enters. When SECINFO or SECINFO_NO_NAME itself detects the transport policy mismatch, report NFS4ERR_ACCESS, the error RFC 8881 Section 18.29.3 specifies for a SECINFO access failure. The directory type, secure-port, and execute-permission checks still apply.
The put-filehandle operation that precedes SECINFO is unchanged. Its fh_verify(..., NFSD_MAY_BYPASS_GSS) call waives the flavor check only for AUTH_NULL and AUTH_SYS, so a krb5 or krb5i request against a sec=krb5p export still gets NFS4ERR_WRONGSEC from PUTFH, and so does a plaintext request against an xprtsec=tls export. Section 2.6.3.1.1.5 forbids both for NFSv4.1 and later.
--- fs/nfsd/export.c | 6 +++-- fs/nfsd/nfs3proc.c | 2 +- fs/nfsd/nfs4proc.c | 44 ++++++++++++++++++++++++-------- fs/nfsd/nfs4state.c | 2 +- fs/nfsd/nfs4xdr.c | 8 ++++-- fs/nfsd/nfsfh.c | 12 ++++++--- fs/nfsd/nfsproc.c | 2 +- fs/nfsd/trace.h | 3 ++- fs/nfsd/vfs.c | 72 ++++++++++++++++++++++++++++++----------------------- fs/nfsd/vfs.h | 43 +++++++++++++++++++++++++++----- 10 files changed, 136 insertions(+), 58 deletions(-)
fs/nfsd/export.c +4 -2
@@ -1976,8 +1976,10 @@ 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 -* one exception is __fh_verify() since it has logic that may result in one -* or both of the helpers being skipped. +* 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 for SECINFO. * * Return values: * %nfs_ok if access is granted, or
fs/nfsd/nfs3proc.c +1 -1
@@ -961,7 +961,7 @@ nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *dirfhp,*/ if (d_is_reg (child) &&unlikely (nfsd_mountpoint (child, exp))) { - status =nfsd_cross_mnt (rqstp, &child, &exp); + status =nfsd_cross_mnt (rqstp, &child, &exp,0 );if (status != nfs_ok) {exp_put (exp); goto out;
fs/nfsd/nfs4proc.c +34 -10
93 lines not on this page · raw message
fs/nfsd/nfs4state.c +1 -1
@@ -7126,7 +7126,7 @@ nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp, err =nfsd_lookup_dentry (open->op_rqstp, parent, open->op_fname, open->op_fnamelen, - &exp, &child); + &exp, &child,0 );if (err)return -ESTALE ;
fs/nfsd/nfs4xdr.c +6 -2
@@ -4676,7 +4676,7 @@ nfsd4_encode_entry4_fattr(struct nfsd4_readdir *cd, const char *name,* Different "."/".." handling? Something else? * At least, add a comment here to explain.... */ - nfserr =nfsd_cross_mnt (cd->rd_rqstp, &dentry, &exp); + nfserr =nfsd_cross_mnt (cd->rd_rqstp, &dentry, &exp,0 );if (nfserr) goto out_put; crossed = true;
@@ -4775,8 +4775,12 @@ nfsd4_encode_entry4(void *ccdv, const char *name, int namlen,* then in accordance with the spec, we fail the * entire READDIR operation(!) */ -if (!(cd->rd_bmval[0 ] &FATTR4_WORD0_RDATTR_ERROR )) +if (!(cd->rd_bmval[0 ] &FATTR4_WORD0_RDATTR_ERROR )) { +/* READDIR cannot return NFS4ERR_WRONGSEC itself. */ +if (nfserr == nfserr_wrongsec) + nfserr = nfserr_acces; goto fail; + }if (nfsd4_encode_entry4_rdattr_error (xdr, nfserr)) { nfserr = nfserr_toosmall; goto fail;
fs/nfsd/nfsfh.c +9 -3
@@ -471,9 +471,15 @@ __fh_verify(struct svc_rqst *rqstp, && exp->ex_path.dentry == dentry) may_bypass_gss = true; - error =check_security_flavor (exp, rqstp, may_bypass_gss); -if (error) - goto out; +/* +* SECINFO reports the export's security flavors instead of +* failing on a flavor mismatch. The xprtsec= policy still applies. +*/ +if (!(access &NFSD_MAY_SECINFO )) { + error =check_security_flavor (exp, rqstp, may_bypass_gss); +if (error) + goto out; + }svc_xprt_set_valid (rqstp->rq_xprt);
fs/nfsd/nfsproc.c +1 -1
@@ -712,7 +712,7 @@ nfsd_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, exp =exp_get (fhp->fh_export);if (d_is_reg (dchild) &&unlikely (nfsd_mountpoint (dchild, exp))) { - status =nfsd_cross_mnt (rqstp, &dchild, &exp); + status =nfsd_cross_mnt (rqstp, &dchild, &exp,0 );if (status != nfs_ok) {exp_put (exp); goto out;
fs/nfsd/trace.h +2 -1
@@ -142,7 +142,8 @@ DEFINE_NFSD_DYNTHREAD_EVENT(trylock_fail); { NFSD_MAY_BYPASS_GSS, "BYPASS_GSS" }, \ { NFSD_MAY_READ_IF_EXEC, "READ_IF_EXEC" }, \ { NFSD_MAY_64BIT_COOKIE, "64BIT_COOKIE" }, \ - { NFSD_MAY_LOCALIO, "LOCALIO" }) + { NFSD_MAY_LOCALIO, "LOCALIO" }, \ + { NFSD_MAY_SECINFO, "SECINFO" }) TRACE_EVENT(nfsd_compound, TP_PROTO(
fs/nfsd/vfs.c +41 -31
148 lines not on this page · raw message
fs/nfsd/vfs.h +37 -6
55 lines not on this page · raw message
Signature · 1 line
2.56.0
Quoted 2 lines · Chuck Lever
Changes in v6:
- Verify the current filehandle inSECINFO_NO_NAME(CURRENT_FH)(sashiko).
Hi Chuck,
I tested all three v6 patches on the stated nfsd-testing base, d9130c9f6535392d423351ffb5521e7689cae6b6. I built and booted patch 1 alone, patches 1 and 2 together, and the full series in an x86_64 KASAN guest. The full-series tests also used a second guest for inter-server COPY and RPC-over-TLS.
I checked the v6 change directly with AUTH_SYS PUTROOTFH; SECINFO_NO_NAME(CURRENT_FH) on NFSv4.1 and v4.2. With an xprtsec=tls export over plain TCP, v5 returned OK and v6 returned NFS4ERR_ACCESS at SECINFO_NO_NAME. With a secure export and a high source port, v5 returned OK and v6 returned NFS4ERR_PERM at that operation. PUTROOTFH succeeded in both v6 cases. A mismatched sec= flavor still returned OK and listed krb5p, as SECINFO_NO_NAME needs to do. The operation succeeded from a reserved port and over real TLS 1.3 or mTLS when the export allowed the transport; TLS without a client certificate was denied by an xprtsec=mtls export.
The original NFSv4.0 Kerberos OPEN stateid reproducer returned NFS4ERR_WRONGSEC at PUTFH for AUTH_SYS direct and SAVEFH READ/WRITE. The file was unchanged, and the Kerberos client could still read it. A second guest tried the same 108 stateid candidates as in the earlier test and obtained no data. With only patch 1, a protected saved-source COPY succeeded and changed the destination; CLONE also returned OK. With patch 2 added, both returned NFS4ERR_ACCESS at their own operations and left the destination unchanged; authorized COPY and CLONE passed.
With patches 1 and 2, LOOKUPP still crossed from a sec=sys child to a sec=krb5p parent. The full series returned NFS4ERR_WRONGSEC there on v4.0, v4.1, and v4.2. SECINFO_NO_NAME(PARENT) remained usable on v4.1 and v4.2. I also reran the SECINFO name and permission cases, READDIR with and without rdattr_error or FILEHANDLE, the NFSv2/v3 CREATE cases, and NFSv4 OPEN. Their status and error placement matched the v5 run.
The other regression tests passed: permitted Btrfs COPY and reflink CLONE returned matching destination bytes; asynchronous inter-server COPY completed 4096 bytes between the two guests; protected local COPY, LINK, and RENAME were rejected; and retained stateids could not be used after tightening the export's sec= policy. Plaintext, TLS, and mTLS mount crossings behaved as before on v4.0, v4.1, and v4.2. A raw v4.0 SECINFO request also succeeded over both TLS and mTLS.
The 259-case source-derived decision test passed under ASan/UBSan. W=1 fs/nfsd/nfs4proc.o built cleanly with inter-server COPY enabled and disabled. I saw no KASAN report or kernel Oops. I found no v6 regression in these tests.
Thanks,
VeroFess