fs: export may_linkat() for nfsd
Subsystems (3)
PATCH 1/2 fs: export may_linkat() for nfsd
vfs_link() expects its callers to enforce protected hardlink policy via may_linkat() prior to creating a hardlink. Move the declaration of may_linkat() from fs/internal.h to include/linux/fs.h and export it with EXPORT_SYMBOL_GPL() so modular in-kernel file servers such as NFSD can invoke it before calling vfs_link().
This prepares for fixing missing hardlink restrictions in nfsd_link(), which was discovered by an AI-assisted code review tool (ARTIST), validated in QEMU without kernel modifications or error injection, and verified with kernel builds and checkpatch.pl.
--- fs/internal.h | 1 - fs/namei.c | 1 + include/linux/fs.h | 1 + 3 files changed, 2 insertions(+), 1 deletion(-)
fs/internal.h +0 -1
@@ -54,7 +54,6 @@ extern int filename_lookup(int dfd, struct filename *name, unsigned flags, struct path *path, const struct path *root);int filename_rmdir (int dfd,struct filename *name);int filename_unlinkat (int dfd,struct filename *name); -int may_linkat (const struct mnt_idmap *idmap,const struct path *link);int filename_renameat2 (int olddfd,struct filename *oldname,int newdfd,struct filename *newname,unsigned int flags);int filename_mkdirat (int dfd,struct filename *name,umode_t mode);
fs/namei.c +1 -0
@@ -1385,6 +1385,7 @@ int may_linkat(const struct mnt_idmap *idmap, const struct path *link)audit_log_path_denied (AUDIT_ANOM_LINK ,"linkat" );return -EPERM ; } +EXPORT_SYMBOL_GPL (may_linkat);/** * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
include/linux/fs.h +1 -0
@@ -2740,6 +2740,7 @@ int may_delete_dentry(const struct mnt_idmap *idmap, struct inode *dir, struct dentry *victim, bool isdir);int may_create_dentry (const struct mnt_idmap *idmap,struct inode *dir,struct dentry *child); +int may_linkat (const struct mnt_idmap *idmap,const struct path *link);static inline bool execute_ok (struct inode *inode) {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 2/2 nfsd: enforce cross-export and may_linkat() checks in nfsd_link()
nfsd_link() verifies NFSD_MAY_CREATE on the destination directory filehandle (ffhp) and NFSD_MAY_NOP on the source filehandle (tfhp) before calling vfs_link() directly, omitting two checks enforced elsewhere:
1. Unlike nfsd_rename(), nfsd_link() does not verify that the source and destination filehandles belong to the same export path. In addition, because tfhp is verified second with NFSD_MAY_NOP, if two distinct svc_export entries share ex_path with different read-only or UID squashing options, fh_verify(tfhp, NFSD_MAY_NOP) skips exp_rdonly() and leaves current_cred() set to tfhp->fh_export's credentials.
2. Unlike filename_linkat(), nfsd_link() does not call may_linkat() before vfs_link(), allowing remote NFSv3 and NFSv4 clients to bypass fs.protected_hardlinks restrictions and hardlink non-owned restricted files.
Require ffhp->fh_export->ex_path and tfhp->fh_export->ex_path to match (returning nfserr_xdev on mismatch, matching nfsd_rename()). When ffhp->fh_export and tfhp->fh_export differ on the same ex_path, reject read-only source exports via exp_rdonly() with nfserr_rofs and restore ffhp->fh_export credentials via nfsd_setuser(). Finally, check may_linkat() before proceeding with the link, mapping -EOVERFLOW on unmapped inode IDs to nfserr_perm to match vfs_link().
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/vfs.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)
fs/nfsd/vfs.c +29 -5
@@ -45,6 +45,7 @@#include "netns.h" #include "stats.h" #include "vfs.h" +#include "auth.h" #include "filecache.h" #include "trace.h"
@@ -1993,6 +1994,11 @@ nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,return err; } +static int exp_rdonly (struct svc_cred *cred,struct svc_export *exp) +{ +return nfsexp_flags (cred, exp) &NFSEXP_READONLY ; +} +/** * nfsd_link - create a link * @rqstp: RPC transaction context
@@ -2011,6 +2017,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, {struct dentry *ddir, *dnew, *dold;struct inode *dirp; +struct path old_path;int type;__be32 err;int host_err;
@@ -2032,6 +2039,28 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, err = nfserr_exist;if (name_is_dot_dotdot (name, len)) goto out; + err = nfserr_xdev; +if (!path_equal (&ffhp->fh_export->ex_path, &tfhp->fh_export->ex_path)) + goto out; +if (ffhp->fh_export != tfhp->fh_export) { +if (exp_rdonly (&rqstp->rq_cred, tfhp->fh_export)) { + err = nfserr_rofs; + goto out; + } + host_err =nfsd_setuser (&rqstp->rq_cred, ffhp->fh_export); +if (host_err) { + err =nfserrno (host_err); + goto out; + } + } + + old_path.mnt = tfhp->fh_export->ex_path.mnt; + old_path.dentry = tfhp->fh_dentry; + host_err =may_linkat (&nop_mnt_idmap, &old_path); +if (host_err) { + err = host_err == -EOVERFLOW ? nfserr_perm :nfserrno (host_err); + goto out; + } err = nfs_ok; type =d_inode (tfhp->fh_dentry)->i_mode &S_IFMT ;
@@ -2592,11 +2621,6 @@ nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, inreturn err; } -static int exp_rdonly (struct svc_cred *cred,struct svc_export *exp) -{ -return nfsexp_flags (cred, exp) &NFSEXP_READONLY ; -} -#ifdef CONFIG_NFSD_V4 /* * Helper function to translate error numbers. In the case of xattr operations,
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Jeff Layton
maintainerRe: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link()
Quoted 100 lines · Dominik Maier
nfsd_link()verifiesNFSD_MAY_CREATEon the destination directory filehandle (ffhp) andNFSD_MAY_NOPon the source filehandle (tfhp) before callingvfs_link()directly, omitting two checks enforced elsewhere:1. Unlike
nfsd_rename(),nfsd_link()does not verify that the source and destination filehandles belong to the same export path. In addition, because tfhp is verified second withNFSD_MAY_NOP, if two distinctsvc_exportentries shareex_pathwith different read-only or UID squashing options,fh_verify(tfhp, NFSD_MAY_NOP)skipsexp_rdonly()and leavescurrent_cred()set to tfhp->fh_export's credentials.
2. Unlikefilename_linkat(),nfsd_link()does not callmay_linkat()beforevfs_link(), allowing remote NFSv3 and NFSv4 clients to bypass fs.protected_hardlinks restrictions and hardlink non-owned restricted files.Require
ffhp->fh_export->ex_pathandtfhp->fh_export->ex_pathto match (returningnfserr_xdevon mismatch, matchingnfsd_rename()). Whenffhp->fh_exportandtfhp->fh_exportdiffer on the sameex_path, reject read-only source exports viaexp_rdonly()withnfserr_rofsand restoreffhp->fh_exportcredentials vianfsd_setuser(). Finally, checkmay_linkat()before proceeding with the link, mapping-EOVERFLOWon unmapped inode IDs tonfserr_permto matchvfs_link().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 800179c9b8a1 ("fs: add link restrictions")Signed-off-by Dominik Maier <[email protected]>--- fs/nfsd/vfs.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-)fs/nfsd/vfs.c +29
-0@@ -45,6 +45,7 @@#include "netns.h" #include "stats.h" #include "vfs.h" +#include "auth.h" #include "filecache.h" #include "trace.h" @@ -1993,6 +1994,11 @@ nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,return err; } +static int exp_rdonly (struct svc_cred *cred,struct svc_export *exp) +{ +return nfsexp_flags (cred, exp) &NFSEXP_READONLY ; +} +/** * nfsd_link - create a link * @rqstp: RPC transaction context @@ -2011,6 +2017,7 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, {struct dentry *ddir, *dnew, *dold;struct inode *dirp; +struct path old_path;int type;__be32 err;int host_err;@@ -2032,6 +2039,28 @@ nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, err = nfserr_exist;if (name_is_dot_dotdot (name, len)) goto out; + err = nfserr_xdev; +if (!path_equal (&ffhp->fh_export->ex_path, &tfhp->fh_export->ex_path)) + goto out; +if (ffhp->fh_export != tfhp->fh_export) { +if (exp_rdonly (&rqstp->rq_cred, tfhp->fh_export)) { + err = nfserr_rofs; + goto out; + } + host_err =nfsd_setuser (&rqstp->rq_cred, ffhp->fh_export); +if (host_err) { + err =nfserrno (host_err); + goto out; + } + } + + old_path.mnt = tfhp->fh_export->ex_path.mnt; + old_path.dentry = tfhp->fh_dentry; + host_err =may_linkat (&nop_mnt_idmap, &old_path); +if (host_err) { + err = host_err == -EOVERFLOW ? nfserr_perm :nfserrno (host_err); + goto out; + } err = nfs_ok; type =d_inode (tfhp->fh_dentry)->i_mode &S_IFMT ;@@ -2592,11 +2621,6 @@ nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, inreturn err;
fs/nfsd/vfs.c nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, in · line 2626
Quoted 10 lines
fs/nfsd/vfs.c +0
-5} -static int exp_rdonly (struct svc_cred *cred,struct svc_export *exp) -{ -return nfsexp_flags (cred, exp) &NFSEXP_READONLY ; -} -#ifdef CONFIG_NFSD_V4 /* * Helper function to translate error numbers. In the case of xattr operations,
Looks sane, but sashiko has a comment on this about the credentials being used:
https://sashiko.dev/#/patchset/20261119192311.2684978-1-dmnk%40google.com
Sounds like this may require 2 may_linkat() checks?
Signature · 1 line
Jeff Layton <[email protected]>
Chuck Lever
maintainerRe: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link()
In reply to Dominik MaierHi Dominik-
The may_linkat() gap is confirmed, and NFSD should enforce fs.protected_hardlinks the same way linkat(2) and rename does.
Unfortunately there are some conflicts. There is already a patch in nfsd-testing that addresses one of the gaps your patch 2 closes. So it does not apply to my nfsd-testing branch, and if Christian takes these two, it will conflict with what I'm planning to submit for v7.5.
I'd rather not drop Daejun's fix. I think the only thing that could be done is for you to wait for v7.5-rc and rebase your two patches for Christian to take cleanly via the VFS tree.
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Quoted 6 lines · Dominik Maier
vfs_link()expects its callers to enforce protected hardlink policy viamay_linkat()prior to creating a hardlink. Move the declaration ofmay_linkat()fromfs/internal.htoinclude/linux/fs.hand export it withEXPORT_SYMBOL_GPL()so modular in-kernel file servers such as NFSD can invoke it before callingvfs_link().
Quoted 8 lines
This prepares for fixing missing hardlink restrictions in
nfsd_link(), which was discovered by an AI-assisted code review tool (ARTIST), validated in QEMU without kernel modifications or error injection, and verified with kernel builds and checkpatch.pl.Fixes 800179c9b8a1 ("fs: add link restrictions")Signed-off-by Dominik Maier <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 45 lines
--- fs/internal.h | 1 - fs/namei.c | 1 + include/linux/fs.h | 1 + 3 files changed, 2 insertions(+), 1 deletion(-)fs/internal.h +0
-1@@ -54,7 +54,6 @@ extern int filename_lookup(int dfd, struct filename *name, unsigned flags, struct path *path, const struct path *root);int filename_rmdir (int dfd,struct filename *name);int filename_unlinkat (int dfd,struct filename *name); -int may_linkat (const struct mnt_idmap *idmap,const struct path *link);int filename_renameat2 (int olddfd,struct filename *oldname,int newdfd,struct filename *newname,unsigned int flags);int filename_mkdirat (int dfd,struct filename *name,umode_t mode);fs/namei.c +1
-0@@ -1385,6 +1385,7 @@ int may_linkat(const struct mnt_idmap *idmap, const struct path *link)audit_log_path_denied (AUDIT_ANOM_LINK ,"linkat" );return -EPERM ; } +EXPORT_SYMBOL_GPL (may_linkat);/** * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory include/linux/fs.h +1
-0@@ -2740,6 +2740,7 @@ int may_delete_dentry(const struct mnt_idmap *idmap, struct inode *dir, struct dentry *victim, bool isdir);int may_create_dentry (const struct mnt_idmap *idmap,struct inode *dir,struct dentry *child); +int may_linkat (const struct mnt_idmap *idmap,const struct path *link);static inline bool execute_ok (struct inode *inode) {Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR