Linux NFS development
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Chuck Lever <cel@kernel.org>, Jeff Layton <jlayton@kernel.org>,
	 Christian Brauner <brauner@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Jan Kara <jack@suse.cz>, NeilBrown <neil@brown.name>,
	 Olga Kornievskaia <okorniev@redhat.com>,
	Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
	 linux-nfs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	 dmnk+artist@google.com, Dominik Maier <dmnk@google.com>
Subject: [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link()
Date: Thu, 19 Nov 2026 20:23:11 +0100	[thread overview]
Message-ID: <20261119192311.2684978-2-dmnk@google.com> (raw)
In-Reply-To: <20261119192311.2684978-1-dmnk@google.com>

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

  reply	other threads:[~2026-11-19 19:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 19:23 [PATCH 1/2] fs: export may_linkat() for nfsd Dominik Maier
2026-11-19 19:23 ` Dominik Maier [this message]
2026-11-19 21:33   ` [PATCH 2/2] nfsd: enforce cross-export and may_linkat() checks in nfsd_link() 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119192311.2684978-2-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=Dai.Ngo@oracle.com \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=dmnk+artist@google.com \
    --cc=jack@suse.cz \
    --cc=jlayton@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=neil@brown.name \
    --cc=okorniev@redhat.com \
    --cc=tom@talpey.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox