* [PATCH 1/2] fs: export may_linkat() for nfsd
@ 2026-11-19 19:23 Dominik Maier
2026-11-19 19:23 ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() Dominik Maier
2026-11-20 10:18 ` [PATCH 1/2] fs: export may_linkat() for nfsd Jan Kara
0 siblings, 2 replies; 6+ messages in thread
From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw)
To: Chuck Lever, Jeff Layton, Christian Brauner, Alexander Viro
Cc: Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey,
linux-nfs, linux-fsdevel, dmnk+artist, Dominik Maier
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.
Fixes: 800179c9b8a1 ("fs: add link restrictions")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
fs/internal.h | 1 -
fs/namei.c | 1 +
include/linux/fs.h | 1 +
3 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/internal.h b/fs/internal.h
index 3f2fbac..7c30669 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -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);
diff --git a/fs/namei.c b/fs/namei.c
index 925aeb6..8f7c2dc 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -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
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5b3a100..c165f27 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -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)
{
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() 2026-11-19 19:23 [PATCH 1/2] fs: export may_linkat() for nfsd Dominik Maier @ 2026-11-19 19:23 ` Dominik Maier 2026-11-19 21:33 ` Jeff Layton 2026-11-20 0:08 ` Chuck Lever 2026-11-20 10:18 ` [PATCH 1/2] fs: export may_linkat() for nfsd Jan Kara 1 sibling, 2 replies; 6+ messages in thread From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw) To: Chuck Lever, Jeff Layton, Christian Brauner, Alexander Viro Cc: Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, linux-nfs, linux-fsdevel, dmnk+artist, Dominik Maier 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. Fixes: 800179c9b8a1 ("fs: add link restrictions") Cc: stable@vger.kernel.org Signed-off-by: Dominik Maier <dmnk@google.com> --- fs/nfsd/vfs.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c index 56c49b5..0c4e052 100644 --- a/fs/nfsd/vfs.c +++ b/fs/nfsd/vfs.c @@ -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, in return 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, -- 2.56.1.750.gf70dc682cf-goog ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() 2026-11-19 19:23 ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() Dominik Maier @ 2026-11-19 21:33 ` Jeff Layton 2026-11-20 0:08 ` Chuck Lever 1 sibling, 0 replies; 6+ messages in thread From: Jeff Layton @ 2026-11-19 21:33 UTC (permalink / raw) To: Dominik Maier, Chuck Lever, Christian Brauner, Alexander Viro Cc: Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, linux-nfs, linux-fsdevel, dmnk+artist On Thu, 2026-11-19 at 20:23 +0100, Dominik Maier wrote: > 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. > > Fixes: 800179c9b8a1 ("fs: add link restrictions") > Cc: stable@vger.kernel.org > Signed-off-by: Dominik Maier <dmnk@google.com> > --- > fs/nfsd/vfs.c | 34 +++++++++++++++++++++++++++++----- > 1 file changed, 29 insertions(+), 5 deletions(-) > > diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c > index 56c49b5..0c4e052 100644 > --- a/fs/nfsd/vfs.c > +++ b/fs/nfsd/vfs.c > @@ -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, in > return 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, 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? -- Jeff Layton <jlayton@kernel.org> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() 2026-11-19 19:23 ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() Dominik Maier 2026-11-19 21:33 ` Jeff Layton @ 2026-11-20 0:08 ` Chuck Lever 2026-11-24 18:11 ` Dominik Maier 1 sibling, 1 reply; 6+ messages in thread From: Chuck Lever @ 2026-11-20 0:08 UTC (permalink / raw) To: Dominik Maier Cc: Jeff Layton, Christian Brauner, Alexander Viro, Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, linux-nfs, linux-fsdevel Hi 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. -- Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org) ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() 2026-11-20 0:08 ` Chuck Lever @ 2026-11-24 18:11 ` Dominik Maier 0 siblings, 0 replies; 6+ messages in thread From: Dominik Maier @ 2026-11-24 18:11 UTC (permalink / raw) To: Chuck Lever Cc: Jeff Layton, Christian Brauner, Alexander Viro, Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, linux-nfs, linux-fsdevel, dmnk+artist Hi Chuck, Sounds good, I'll wait for v7.5-rc so Daejun's fix lands first and resend rebased for Christian's tree then. Thanks for checking (and thanks Jan for reviewing patch 1/2)! Best Dominik ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] fs: export may_linkat() for nfsd 2026-11-19 19:23 [PATCH 1/2] fs: export may_linkat() for nfsd Dominik Maier 2026-11-19 19:23 ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() Dominik Maier @ 2026-11-20 10:18 ` Jan Kara 1 sibling, 0 replies; 6+ messages in thread From: Jan Kara @ 2026-11-20 10:18 UTC (permalink / raw) To: Dominik Maier Cc: Chuck Lever, Jeff Layton, Christian Brauner, Alexander Viro, Jan Kara, NeilBrown, Olga Kornievskaia, Dai Ngo, Tom Talpey, linux-nfs, linux-fsdevel, dmnk+artist On Thu 19-11-26 20:23:10, Dominik Maier wrote: > 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. > > Fixes: 800179c9b8a1 ("fs: add link restrictions") > Cc: stable@vger.kernel.org > Signed-off-by: Dominik Maier <dmnk@google.com> Looks good. Feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> Honza > --- > fs/internal.h | 1 - > fs/namei.c | 1 + > include/linux/fs.h | 1 + > 3 files changed, 2 insertions(+), 1 deletion(-) > > diff --git a/fs/internal.h b/fs/internal.h > index 3f2fbac..7c30669 100644 > --- a/fs/internal.h > +++ b/fs/internal.h > @@ -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); > diff --git a/fs/namei.c b/fs/namei.c > index 925aeb6..8f7c2dc 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -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 > diff --git a/include/linux/fs.h b/include/linux/fs.h > index 5b3a100..c165f27 100644 > --- a/include/linux/fs.h > +++ b/include/linux/fs.h > @@ -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) > { > -- > 2.56.1.750.gf70dc682cf-goog > -- Jan Kara <jack@suse.com> SUSE Labs, CR ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-24 18:11 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-19 19:23 [PATCH 1/2] fs: export may_linkat() for nfsd Dominik Maier 2026-11-19 19:23 ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() Dominik Maier 2026-11-19 21:33 ` Jeff Layton 2026-11-20 0:08 ` Chuck Lever 2026-11-24 18:11 ` Dominik Maier 2026-11-20 10:18 ` [PATCH 1/2] fs: export may_linkat() for nfsd Jan Kara
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox