git: 4efbfe6ab09b - stable/15 - fdescfs: descend through /dev/fd/N when mounted linrdlnk

From: Nick Price <nprice_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 20:33:28 UTC
The branch stable/15 has been updated by nprice:

URL: https://cgit.FreeBSD.org/src/commit/?id=4efbfe6ab09b42d04fe3c1c6e5c45a85f08d8ced

commit 4efbfe6ab09b42d04fe3c1c6e5c45a85f08d8ced
Author:     Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-09-02 17:43:13 +0000
Commit:     Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-26 19:58:55 +0000

    fdescfs: descend through /dev/fd/N when mounted linrdlnk
    
    Linux resolves a path below /proc/self/fd/N in the directory the
    descriptor names, and linprocfs makes /proc/<pid>/fd a symlink to
    /dev/fd.  Under linrdlnk the fdescfs node carries only VV_READLINK,
    which namei will not walk through, so such a path fails with ENOTDIR.
    
    Return the underlying vnode from fdesc_lookup for a non-final component,
    or a trailing slash, reusing the machinery the nodup option already
    uses.  The last component is untouched, so open("/dev/fd/N") keeps its
    dup(2) semantic; a descriptor with no vnode behind it, such as a pipe,
    yields ENOTDIR.
    
    Add ATF coverage for traversal, descriptor reuse, and preservation of
    last-component and mount-option semantics.
    
    Approved by:    adrian (mentor)
    Reviewed by:    kib, adrian
    Differential Revision:  https://reviews.freebsd.org/D59393
    
    (cherry picked from commit b3f7a438bb233c85290dcdc5241d86c49100b6b7)
---
 etc/mtree/BSD.tests.dist            |   2 +
 share/man/man4/fdescfs.4            |   7 +-
 sys/fs/fdescfs/fdesc_vnops.c        |  17 +-
 tests/sys/fs/fdescfs/fdescfs_test.c | 302 ++++++++++++++++++++++++++++++++++++
 4 files changed, 326 insertions(+), 2 deletions(-)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 5fd3efd0f258..cb53ec9bd54e 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -828,6 +828,8 @@
         fs
             cd9660
             ..
+            fdescfs
+            ..
             fusefs
             ..
             pjdfstest
diff --git a/share/man/man4/fdescfs.4 b/share/man/man4/fdescfs.4
index f8bf413fc274..9bdd6efd7012 100644
--- a/share/man/man4/fdescfs.4
+++ b/share/man/man4/fdescfs.4
@@ -38,7 +38,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd Jul 11, 2023
+.Dd September 6, 2026
 .Dt FDESCFS 4
 .Os
 .Sh NAME
@@ -85,6 +85,11 @@ instead of
 .Fx
 traditional
 .Dv VCHR .
+Path components following a directory descriptor entry are resolved relative
+to the referenced directory.
+A trailing slash also resolves to the referenced directory.
+Opening an entry as the last path component, without a trailing slash,
+retains the behavior described below.
 For
 .Xr linux 4
 ABI compatibility mount
diff --git a/sys/fs/fdescfs/fdesc_vnops.c b/sys/fs/fdescfs/fdesc_vnops.c
index e312e09cf4a6..2f34f810db3e 100644
--- a/sys/fs/fdescfs/fdesc_vnops.c
+++ b/sys/fs/fdescfs/fdesc_vnops.c
@@ -249,6 +249,7 @@ struct fdesc_get_ino_args {
 	struct file *fp;
 	struct thread *td;
 	bool fdropped;
+	bool traverse;
 };
 
 static int
@@ -262,7 +263,8 @@ fdesc_get_ino_alloc(struct mount *mp, void *arg, int lkflags,
 
 	a = arg;
 	fdm = VFSTOFDESC(mp);
-	if ((fdm->flags & FMNT_NODUP) != 0 && a->fp->f_type == DTYPE_VNODE) {
+	if ((a->traverse || (fdm->flags & FMNT_NODUP) != 0) &&
+	    a->fp->f_type == DTYPE_VNODE) {
 		vp = a->fp->f_vnode;
 		vget(vp, lkflags | LK_RETRY);
 		*rvp = vp;
@@ -295,6 +297,7 @@ fdesc_lookup(struct vop_lookup_args *ap)
 	u_int fd, fd1;
 	int error;
 	uint8_t fflags;
+	bool traverse;
 
 	if ((cnp->cn_flags & ISLASTCN) &&
 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
@@ -339,6 +342,17 @@ fdesc_lookup(struct vop_lookup_args *ap)
 	    &fcaps)) != 0)
 		goto bad;
 
+	/* A component below /dev/fd/N resolves in the directory N names. */
+	traverse = ((cnp->cn_flags & ISLASTCN) == 0 ||
+	    (cnp->cn_flags & TRAILINGSLASH) != 0) &&
+	    (VFSTOFDESC(dvp->v_mount)->flags & FMNT_LINRDLNKF) != 0;
+	if (traverse && fp->f_type != DTYPE_VNODE) {
+		fdrop(fp, td);
+		filecaps_free(&fcaps);
+		error = ENOTDIR;
+		goto bad;
+	}
+
 	/*
 	 * Make sure we do not deadlock looking up the dvp itself.
 	 *
@@ -354,6 +368,7 @@ fdesc_lookup(struct vop_lookup_args *ap)
 	arg.fp = fp;
 	arg.td = td;
 	arg.fdropped = false;
+	arg.traverse = traverse;
 	error = vn_vget_ino_gen(dvp, fdesc_get_ino_alloc, &arg,
 	    LK_EXCLUSIVE, &fvp);
 
diff --git a/tests/sys/fs/fdescfs/fdescfs_test.c b/tests/sys/fs/fdescfs/fdescfs_test.c
index 4f1151f7d221..b629c11c38d1 100644
--- a/tests/sys/fs/fdescfs/fdescfs_test.c
+++ b/tests/sys/fs/fdescfs/fdescfs_test.c
@@ -55,6 +55,148 @@ fdpath(char *path, size_t size, int fd, const char *suffix)
 	ATF_REQUIRE(len > 0 && (size_t)len < size);
 }
 
+static void
+create_directory(const char *path, char marker)
+{
+	int dirfd, fd;
+
+	ATF_REQUIRE_EQ(0, mkdir(path, 0755));
+	ATF_REQUIRE((dirfd = open(path, O_RDONLY | O_DIRECTORY)) >= 0);
+	ATF_REQUIRE(
+	    (fd = openat(dirfd, "marker", O_WRONLY | O_CREAT, 0644)) >= 0);
+	ATF_REQUIRE_EQ(1, write(fd, &marker, 1));
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, close(dirfd));
+}
+
+static void
+check_identity(const struct stat *expected, const struct stat *actual)
+{
+	ATF_CHECK_EQ(expected->st_dev, actual->st_dev);
+	ATF_CHECK_EQ(expected->st_ino, actual->st_ino);
+	ATF_CHECK_EQ(expected->st_mode & S_IFMT, actual->st_mode & S_IFMT);
+}
+
+static void
+check_directory(int dirfd, const char *suffix)
+{
+	struct stat expected, actual;
+	char path[64];
+	int fd;
+
+	fdpath(path, sizeof(path), dirfd, suffix);
+	ATF_REQUIRE_EQ(0, fstat(dirfd, &expected));
+	ATF_REQUIRE_MSG(stat(path, &actual) == 0, "stat(%s): %s", path,
+	    strerror(errno));
+	check_identity(&expected, &actual);
+	ATF_REQUIRE_MSG((fd = open(path, O_RDONLY | O_DIRECTORY)) >= 0,
+	    "open(%s): %s", path, strerror(errno));
+	ATF_REQUIRE_EQ(0, fstat(fd, &actual));
+	check_identity(&expected, &actual);
+	ATF_REQUIRE_EQ(0, close(fd));
+}
+
+static void
+check_marker(int dirfd, char expected)
+{
+	char path[64], actual;
+	int fd;
+
+	fdpath(path, sizeof(path), dirfd, "/marker");
+	ATF_REQUIRE_MSG((fd = open(path, O_RDONLY)) >= 0, "open(%s): %s", path,
+	    strerror(errno));
+	ATF_REQUIRE_EQ(1, read(fd, &actual, 1));
+	ATF_CHECK_EQ(expected, actual);
+	ATF_REQUIRE_EQ(0, close(fd));
+}
+
+static void
+check_bare(int fd, const char *target)
+{
+	struct stat sb;
+	char path[64], expected[PATH_MAX], actual[PATH_MAX];
+	ssize_t len;
+	int copy, flags, actual_flags;
+
+	fdpath(path, sizeof(path), fd, "");
+	ATF_REQUIRE_EQ(0, lstat(path, &sb));
+	ATF_CHECK(S_ISLNK(sb.st_mode));
+	ATF_REQUIRE(realpath(target, expected) != NULL);
+	ATF_REQUIRE((len = readlink(path, actual, sizeof(actual))) >= 0);
+	ATF_REQUIRE_EQ(strlen(expected), (size_t)len);
+	ATF_CHECK_EQ(0, memcmp(expected, actual, len));
+	ATF_REQUIRE((copy = open(path, O_RDONLY)) >= 0);
+	ATF_REQUIRE((flags = fcntl(fd, F_GETFL)) >= 0);
+	ATF_REQUIRE_EQ(0, fcntl(copy, F_SETFL, flags ^ O_NONBLOCK));
+	ATF_REQUIRE((actual_flags = fcntl(fd, F_GETFL)) >= 0);
+	ATF_CHECK_EQ((flags ^ O_NONBLOCK) & O_NONBLOCK,
+	    actual_flags & O_NONBLOCK);
+	ATF_REQUIRE_EQ(0, fcntl(fd, F_SETFL, flags));
+	ATF_REQUIRE_EQ(0, close(copy));
+}
+
+static void
+check_open_offset(bool duplicate)
+{
+	char path[64], buf[2], byte;
+	int fd, copy;
+
+	ATF_REQUIRE((fd = open("file", O_RDWR | O_CREAT, 0644)) >= 0);
+	ATF_REQUIRE_EQ(6, write(fd, "abcdef", 6));
+	ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
+	ATF_REQUIRE_EQ(2, read(fd, buf, sizeof(buf)));
+	ATF_REQUIRE_EQ(0, memcmp(buf, "ab", sizeof(buf)));
+	fdpath(path, sizeof(path), fd, "");
+	ATF_REQUIRE((copy = open(path, O_RDONLY)) >= 0);
+	ATF_CHECK_EQ(duplicate ? 2 : 0, lseek(copy, 0, SEEK_CUR));
+	ATF_REQUIRE_EQ(1, read(copy, &byte, 1));
+	ATF_CHECK_EQ(duplicate ? 'c' : 'a', byte);
+	ATF_CHECK_EQ(duplicate ? 3 : 2, lseek(fd, 0, SEEK_CUR));
+	ATF_REQUIRE_EQ(0, close(copy));
+	ATF_REQUIRE_EQ(0, close(fd));
+}
+
+static void
+check_notdir(int fd)
+{
+	const char *suffixes[] = { "/child", "/" };
+	char path[64];
+	size_t i;
+
+	for (i = 0; i < nitems(suffixes); i++) {
+		fdpath(path, sizeof(path), fd, suffixes[i]);
+		ATF_REQUIRE_ERRNO(ENOTDIR, open(path, O_RDONLY) == -1);
+	}
+}
+
+static void
+check_traverse(int flags)
+{
+	struct stat expected, actual;
+	char path[64];
+	int fd;
+
+	mount_fdescfs(linrdlnk);
+	ATF_REQUIRE_EQ(0, mkdir("parent", 0755));
+	create_directory("parent/target", 'a');
+	ATF_REQUIRE((fd = open("parent/target", flags | O_DIRECTORY)) >= 0);
+	fdpath(path, sizeof(path), fd, "/..");
+	ATF_REQUIRE_EQ(0, stat("parent", &expected));
+	ATF_REQUIRE_MSG(stat(path, &actual) == 0, "stat(%s): %s", path,
+	    strerror(errno));
+	check_identity(&expected, &actual);
+	check_marker(fd, 'a');
+	fdpath(path, sizeof(path), fd, "/child");
+	ATF_REQUIRE_MSG(mkdir(path, 0755) == 0, "mkdir(%s): %s", path,
+	    strerror(errno));
+	ATF_REQUIRE_EQ(0, stat("parent/target/child", &expected));
+	ATF_REQUIRE_EQ(0, stat(path, &actual));
+	ATF_CHECK(S_ISDIR(actual.st_mode));
+	check_identity(&expected, &actual);
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
 static void
 check_cap_rights(const char *const *opts)
 {
@@ -136,6 +278,156 @@ check_ioctl_caps(const char *const *opts)
 		(void)unmount("mnt", 0);                       \
 	}
 
+FDESCFS_TC(traverse_dir, "Traverse a directory descriptor under linrdlnk");
+ATF_TC_BODY(traverse_dir, tc)
+{
+	check_traverse(O_RDONLY);
+}
+
+FDESCFS_TC(traverse_path, "Traverse an O_PATH descriptor under linrdlnk");
+ATF_TC_BODY(traverse_path, tc)
+{
+	check_traverse(O_PATH);
+}
+
+FDESCFS_TC(trailing_slash,
+    "Trailing slashes resolve to the referenced directory");
+ATF_TC_BODY(trailing_slash, tc)
+{
+	const int flags[] = { O_RDONLY, O_PATH };
+	size_t i;
+	int fd;
+
+	mount_fdescfs(linrdlnk);
+	create_directory("target", 'a');
+	for (i = 0; i < nitems(flags); i++) {
+		ATF_REQUIRE((fd = open("target", flags[i] | O_DIRECTORY)) >= 0);
+		check_directory(fd, "/");
+		ATF_REQUIRE_EQ(0, close(fd));
+	}
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(last_component_dups,
+    "Bare linrdlnk opens share the original offset");
+ATF_TC_BODY(last_component_dups, tc)
+{
+	mount_fdescfs(linrdlnk);
+	check_open_offset(true);
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(lookup_order,
+    "Bare and traversing lookups do not affect each other");
+ATF_TC_BODY(lookup_order, tc)
+{
+	int first, second;
+
+	mount_fdescfs(linrdlnk);
+	create_directory("target", 'a');
+	ATF_REQUIRE((first = open("target", O_RDONLY | O_DIRECTORY)) >= 0);
+	ATF_REQUIRE((second = open("target", O_RDONLY | O_DIRECTORY)) >= 0);
+	check_bare(first, "target");
+	check_marker(first, 'a');
+	check_directory(first, "/");
+	check_bare(first, "target");
+	check_marker(second, 'a');
+	check_directory(second, "/");
+	check_bare(second, "target");
+	check_marker(second, 'a');
+	ATF_REQUIRE_EQ(0, close(second));
+	ATF_REQUIRE_EQ(0, close(first));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(descriptor_reuse, "Reusing a descriptor selects its current target");
+ATF_TC_BODY(descriptor_reuse, tc)
+{
+	int fd, replacement;
+
+	mount_fdescfs(linrdlnk);
+	create_directory("first", 'a');
+	create_directory("second", 'b');
+	ATF_REQUIRE((fd = open("first", O_RDONLY | O_DIRECTORY)) >= 0);
+	check_bare(fd, "first");
+	check_marker(fd, 'a');
+	ATF_REQUIRE(
+	    (replacement = open("second", O_RDONLY | O_DIRECTORY)) >= 0);
+	ATF_REQUIRE_EQ(fd, dup2(replacement, fd));
+	ATF_REQUIRE_EQ(0, close(replacement));
+	check_marker(fd, 'b');
+	check_directory(fd, "/");
+	check_bare(fd, "second");
+	ATF_REQUIRE((replacement = open("second/marker", O_RDONLY)) >= 0);
+	ATF_REQUIRE_EQ(fd, dup2(replacement, fd));
+	ATF_REQUIRE_EQ(0, close(replacement));
+	check_notdir(fd);
+	check_bare(fd, "second/marker");
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(traverse_nondir, "Files and pipes reject traversal");
+ATF_TC_BODY(traverse_nondir, tc)
+{
+	int fd, pipes[2];
+
+	mount_fdescfs(linrdlnk);
+	ATF_REQUIRE((fd = open("file", O_RDONLY | O_CREAT, 0644)) >= 0);
+	check_notdir(fd);
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, pipe(pipes));
+	check_notdir(pipes[0]);
+	ATF_REQUIRE_EQ(0, close(pipes[0]));
+	ATF_REQUIRE_EQ(0, close(pipes[1]));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(plain_mount, "Plain mounts reject traversal");
+ATF_TC_BODY(plain_mount, tc)
+{
+	int fd;
+
+	mount_fdescfs(NULL);
+	create_directory("target", 'a');
+	ATF_REQUIRE((fd = open("target", O_RDONLY | O_DIRECTORY)) >= 0);
+	check_notdir(fd);
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(nodup_linrdlnk_mount, "Linrdlnk preserves nodup behavior");
+ATF_TC_BODY(nodup_linrdlnk_mount, tc)
+{
+	int fd;
+
+	mount_fdescfs(nodup_linrdlnk);
+	create_directory("target", 'a');
+	ATF_REQUIRE((fd = open("target", O_RDONLY | O_DIRECTORY)) >= 0);
+	check_marker(fd, 'a');
+	check_directory(fd, "/");
+	ATF_REQUIRE_EQ(0, close(fd));
+	check_open_offset(false);
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
+FDESCFS_TC(root_reference,
+    "Traverse a descriptor referencing the fdescfs root");
+ATF_TC_BODY(root_reference, tc)
+{
+	char suffix[32];
+	int fd, len;
+
+	mount_fdescfs(linrdlnk);
+	ATF_REQUIRE((fd = open("mnt", O_RDONLY | O_DIRECTORY)) >= 0);
+	check_directory(fd, "/");
+	len = snprintf(suffix, sizeof(suffix), "/%d/", fd);
+	ATF_REQUIRE(len > 0 && (size_t)len < sizeof(suffix));
+	check_directory(fd, suffix);
+	ATF_REQUIRE_EQ(0, close(fd));
+	ATF_REQUIRE_EQ(0, unmount("mnt", 0));
+}
+
 FDESCFS_TC(cap_rights, "fdescfs preserves capability rights");
 ATF_TC_BODY(cap_rights, tc)
 {
@@ -296,6 +588,16 @@ ATF_TC_BODY(nodup_truncate_ro, tc)
 
 ATF_TP_ADD_TCS(tp)
 {
+	ATF_TP_ADD_TC(tp, traverse_dir);
+	ATF_TP_ADD_TC(tp, traverse_path);
+	ATF_TP_ADD_TC(tp, trailing_slash);
+	ATF_TP_ADD_TC(tp, last_component_dups);
+	ATF_TP_ADD_TC(tp, lookup_order);
+	ATF_TP_ADD_TC(tp, descriptor_reuse);
+	ATF_TP_ADD_TC(tp, traverse_nondir);
+	ATF_TP_ADD_TC(tp, plain_mount);
+	ATF_TP_ADD_TC(tp, nodup_linrdlnk_mount);
+	ATF_TP_ADD_TC(tp, root_reference);
 	ATF_TP_ADD_TC(tp, cap_rights);
 	ATF_TP_ADD_TC(tp, nodup_cap_rights);
 	ATF_TP_ADD_TC(tp, resolve_beneath);