From nobody Tue Sep 29 16:00:07 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hvNDR61chz6tlR5 for ; Tue, 29 Sep 2026 16:00:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hvNDR3stCz4gmq for ; Tue, 29 Sep 2026 16:00:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1790697607; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zrK+/HugFl8qtJcvBCO43ZOs0oTk91cXRp7BZBXaoNo=; b=mnAeyFAHyOUtOvqAp9G5PPVZ4JgM8IRSMWhatgCRQ4OQ5LWrWf51pd1VOICDcl3/2wBi7s XIFyLYV+gX6V/mktj3IXM7rMZtkh7IboaHpFgcs8RmgSq23fJT6TTUrI+hyYaSmjZ755e6 qJ7oM2jTWdat2iQeADR732jd5V5eF8f/X0hUyPbT5Q+ECE0qWx06dBUtFf1Q/vdYIkotQC qutP/Lzz6diVLD5Bus+MZBrXRl8FoxqOBcD0giS5/p/o9j56DotHDa8AyPHut76VcjRNAO GusS+2srsA8gRwOZ4y4SE13th7nJTtGMCzvuC5+/xapQouPOXBfh7k83MWJspg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1790697607; b=cTMuopVvBbOW0NWu9KL6wP4x6gJ9iLx9sSJ4mR7Fs156+2jYzEdECdLtCEAMIWs+R19oie acJLzdidd4/fqIvFYxwp6/SvVRvkVzG9Y8Dth+BV9mJmVEMaMC/B1KdgHQQ26I8K8xmsQ7 cvT20UlW63m+intvGURUtONj3W4g0y+hghkgH1oz++z3G1mc+aJYGZoohCtWC+PGsRTi4D Gxj57pYs63ZJ5FQzjSBsL9fFYaTN9hSCBG/nGsq2HEFq69G4hgPmhnwAARYUfj3bd1iPFU jFu+QaP9/Jv7/ImVvyEFHOtrqlgqK7Pi1UpA3uR4lCxdtkCeB2fzywBM+37mMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1790697607; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zrK+/HugFl8qtJcvBCO43ZOs0oTk91cXRp7BZBXaoNo=; b=ZKeMHighKJqH/9CSFCzo57mO7Tp4roXGnN+7IACxRZuIeFxVsxJ9Si6KPXDipb6W/UYCQ5 8zfWrPlR7BJBMNrjLUjm0JL1JZBqrmi/TQ/KyDNXUvVxo/QhdOoRI7bstVJxy259anxjv5 oqnpZxaK4KoMaNjDYsz1UGoI7uAmYd75shc8A9CExgbfFu4OfdkZpaLngnflIChIeXzNe3 Zj+ifZLY9fAgdSzlmuwdtXrfi+RyddOp8z53fPHxdUI5ad6gs49g0tAmTpQondBiIFUnqH VlHopsTsD0H753nYJLrLJ21ioPHc4W0SkpK8Ski0bLrfFtbFw0qgB8IOmT2pLQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hvNDR2c68z1KVb for ; Tue, 29 Sep 2026 16:00:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 27356 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 29 Sep 2026 16:00:07 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 4a0d28e5ed8d - releng/15.1 - fdescfs: Pass up additional metadata during lookups List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/15.1 X-Git-Reftype: branch X-Git-Commit: 4a0d28e5ed8d02f98e4ce36fd19935150a5d95e9 Auto-Submitted: auto-generated Date: Tue, 29 Sep 2026 16:00:07 +0000 Message-Id: <6abbe087.27356.7c1a06e7@gitrepo.freebsd.org> The branch releng/15.1 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=4a0d28e5ed8d02f98e4ce36fd19935150a5d95e9 commit 4a0d28e5ed8d02f98e4ce36fd19935150a5d95e9 Author: Mark Johnston AuthorDate: 2026-09-28 14:44:04 +0000 Commit: Mark Johnston CommitDate: 2026-09-29 01:01:48 +0000 fdescfs: Pass up additional metadata during lookups When an fdescfs mount has the nodup option set, fdesc_lookup(/dev/fd/n) returns the vnode referenced by file descriptor n, rather than returning an fdescfs vnode. This meant that fd metadata attached to fd n was not preserved when reopening the file, which is contrary to the expected semantics for capsicum rights and the UF_RESOLVE_BENEATH fd flag. For regular fdescfs mounts, this metadata is copied via dupfdopen(). Fix the problem by passing up this metadata through the nameidata structure. Thus, if one opens /dev/fd/n, the returned fd will inherit UF_RESOLVE_BENEATH and the capability rights of fd n. Add some regression tests as well. Approved by: so Security: FreeBSD-SA-26:66.jail Security: CVE-2026-101304 Reported by: Jan Bramkamp Reviewed by: kib Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D59886 --- sys/fs/fdescfs/fdesc_vnops.c | 37 +++++- tests/sys/fs/Makefile | 1 + tests/sys/fs/fdescfs/Makefile | 9 ++ tests/sys/fs/fdescfs/fdescfs_test.c | 236 ++++++++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+), 2 deletions(-) diff --git a/sys/fs/fdescfs/fdesc_vnops.c b/sys/fs/fdescfs/fdesc_vnops.c index 05630ef5e48c..0ff11591cabf 100644 --- a/sys/fs/fdescfs/fdesc_vnops.c +++ b/sys/fs/fdescfs/fdesc_vnops.c @@ -287,11 +287,13 @@ fdesc_lookup(struct vop_lookup_args *ap) char *pname = cnp->cn_nameptr; struct thread *td = curthread; struct file *fp; + struct filecaps fcaps; struct fdesc_get_ino_args arg; + struct vnode *fvp; int nlen = cnp->cn_namelen; u_int fd, fd1; int error; - struct vnode *fvp; + uint8_t fflags; if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { @@ -332,7 +334,8 @@ fdesc_lookup(struct vop_lookup_args *ap) /* * No rights to check since 'fp' isn't actually used. */ - if ((error = fget(td, fd, &cap_no_rights, &fp)) != 0) + if ((error = fget_cap(td, fd, &cap_no_rights, &fflags, &fp, + &fcaps)) != 0) goto bad; /* @@ -367,6 +370,36 @@ fdesc_lookup(struct vop_lookup_args *ap) error = ENOENT; } + /* + * Make sure that a nodup mount can't be used to launder away monotonic + * file descriptor metadata, namely UF_RESOLVE_BENEATH and capability + * rights. + */ + if (error == 0 && fvp->v_mount != dvp->v_mount && + ((fflags & UF_RESOLVE_BENEATH) != 0 || !filecaps_full(&fcaps))) { + struct nameidata *ndp; + + ndp = vfs_lookup_nameidata(cnp); + if (ndp == NULL) { + vput(fvp); + error = ENOTCAPABLE; + } else { + if ((fflags & UF_RESOLVE_BENEATH) != 0) + ndp->ni_resflags |= NIRES_BENEATH; + if (!filecaps_full(&fcaps)) { + if (cap_rights_is_valid( + &ndp->ni_filecaps.fc_rights)) + filecaps_intersect(&fcaps, + &ndp->ni_filecaps); + else + filecaps_move(&fcaps, + &ndp->ni_filecaps); + ndp->ni_resflags |= NIRES_STRICTREL; + } + } + } + filecaps_free(&fcaps); + if (error) goto bad; *vpp = fvp; diff --git a/tests/sys/fs/Makefile b/tests/sys/fs/Makefile index 254394f43714..a4b0b056e9a6 100644 --- a/tests/sys/fs/Makefile +++ b/tests/sys/fs/Makefile @@ -9,6 +9,7 @@ TESTSRC= ${SRCTOP}/contrib/netbsd-tests/fs #TESTS_SUBDIRS+= nullfs # XXX: needs rump # fusefs tests cannot be compiled/used without the googletest infrastructure. +TESTS_SUBDIRS+= fdescfs .if ${COMPILER_FEATURES:Mc++14} && ${MK_GOOGLETEST} != "no" TESTS_SUBDIRS+= fusefs .endif diff --git a/tests/sys/fs/fdescfs/Makefile b/tests/sys/fs/fdescfs/Makefile new file mode 100644 index 000000000000..78945f4e376d --- /dev/null +++ b/tests/sys/fs/fdescfs/Makefile @@ -0,0 +1,9 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/sys/fs/fdescfs + +ATF_TESTS_C+= fdescfs_test + +LIBADD+= util + +.include diff --git a/tests/sys/fs/fdescfs/fdescfs_test.c b/tests/sys/fs/fdescfs/fdescfs_test.c new file mode 100644 index 000000000000..f80d335108e6 --- /dev/null +++ b/tests/sys/fs/fdescfs/fdescfs_test.c @@ -0,0 +1,236 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *const linrdlnk[] = { "linrdlnk", NULL }; +static const char *const nodup[] = { "nodup", NULL }; +static const char *const nodup_linrdlnk[] = { "nodup", "linrdlnk", NULL }; + +static void +mount_fdescfs(const char *const *opts) +{ + struct iovec *iov; + char errmsg[256]; + int error, iovlen; + + ATF_REQUIRE_EQ(0, mkdir("mnt", 0755)); + iov = NULL; + iovlen = 0; + build_iovec(&iov, &iovlen, __DECONST(char *, "fstype"), + __DECONST(char *, "fdescfs"), (size_t)-1); + build_iovec(&iov, &iovlen, __DECONST(char *, "fspath"), + __DECONST(char *, "mnt"), (size_t)-1); + for (; opts != NULL && *opts != NULL; opts++) + build_iovec(&iov, &iovlen, __DECONST(char *, *opts), NULL, + (size_t)-1); + build_iovec(&iov, &iovlen, __DECONST(char *, "errmsg"), errmsg, + sizeof(errmsg)); + errmsg[0] = '\0'; + error = nmount(iov, iovlen, 0); + if (error != 0 && errno == ENODEV) + atf_tc_skip("fdescfs is not available"); + ATF_REQUIRE_MSG(error == 0, "nmount: %s", + errmsg[0] != '\0' ? errmsg : strerror(errno)); + free_iovec(&iov, &iovlen); +} + +static void +fdpath(char *path, size_t size, int fd, const char *suffix) +{ + int len; + + len = snprintf(path, size, "mnt/%d%s", fd, suffix); + ATF_REQUIRE(len > 0 && (size_t)len < size); +} + +static void +check_cap_rights(const char *const *opts) +{ + cap_rights_t expected, actual; + char path[64]; + int copy, fd; + + mount_fdescfs(opts); + fd = open("file", O_RDONLY | O_CREAT, 0644); + ATF_REQUIRE(fd >= 0); + cap_rights_init(&expected, CAP_READ, CAP_FSTAT); + ATF_REQUIRE_EQ(0, cap_rights_limit(fd, &expected)); + fdpath(path, sizeof(path), fd, ""); + copy = open(path, O_RDONLY); + ATF_REQUIRE(copy >= 0); + ATF_REQUIRE_EQ(0, cap_rights_get(copy, &actual)); + ATF_CHECK(cap_rights_contains(&actual, &expected)); + ATF_CHECK(cap_rights_contains(&expected, &actual)); + ATF_REQUIRE_EQ(0, close(copy)); + ATF_REQUIRE_EQ(0, close(fd)); + ATF_REQUIRE_EQ(0, unmount("mnt", 0)); +} + +static void +check_resolve_beneath(const char *const *opts, int oflags) +{ + char path[64]; + int copy, dirfd, fd; + + mount_fdescfs(opts); + ATF_REQUIRE_EQ(0, mkdir("dir", 0755)); + dirfd = open("dir", O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(dirfd >= 0); + ATF_REQUIRE_EQ(0, fcntl(dirfd, F_SETFD, FD_RESOLVE_BENEATH)); + fdpath(path, sizeof(path), dirfd, ""); + copy = open(path, oflags); + ATF_REQUIRE(copy >= 0); + fd = openat(copy, ".", O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE_EQ(0, close(fd)); + ATF_CHECK_ERRNO(ENOTCAPABLE, + openat(copy, "..", O_RDONLY | O_DIRECTORY) == -1); + ATF_REQUIRE_EQ(0, close(copy)); + ATF_REQUIRE_EQ(0, close(dirfd)); + ATF_REQUIRE_EQ(0, unmount("mnt", 0)); +} + +static void +check_ioctl_caps(const char *const *opts) +{ + cap_ioctl_t cmds[] = { FIOCLEX }; + char path[64]; + int copy, fd; + + mount_fdescfs(opts); + fd = open("file", O_RDONLY | O_CREAT, 0644); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE_EQ(0, cap_ioctls_limit(fd, cmds, nitems(cmds))); + fdpath(path, sizeof(path), fd, ""); + copy = open(path, O_RDONLY); + ATF_REQUIRE(copy >= 0); + ATF_REQUIRE_EQ(0, ioctl(copy, FIOCLEX, 0)); + ATF_CHECK_ERRNO(ENOTCAPABLE, ioctl(copy, FIONCLEX, 0) == -1); + ATF_REQUIRE_EQ(0, close(copy)); + ATF_REQUIRE_EQ(0, close(fd)); + ATF_REQUIRE_EQ(0, unmount("mnt", 0)); +} + +#define FDESCFS_TC(name, description) \ + ATF_TC_WITH_CLEANUP(name); \ + ATF_TC_HEAD(name, tc) \ + { \ + atf_tc_set_md_var(tc, "descr", description); \ + atf_tc_set_md_var(tc, "require.user", "root"); \ + atf_tc_set_md_var(tc, "timeout", "30"); \ + } \ + ATF_TC_CLEANUP(name, tc) \ + { \ + (void)unmount("mnt", 0); \ + } + +FDESCFS_TC(cap_rights, "fdescfs preserves capability rights"); +ATF_TC_BODY(cap_rights, tc) +{ + check_cap_rights(NULL); +} + +FDESCFS_TC(nodup_cap_rights, "nodup mounts preserve capability rights"); +ATF_TC_BODY(nodup_cap_rights, tc) +{ + check_cap_rights(nodup); +} + +FDESCFS_TC(resolve_beneath, "fdescfs preserves the FD_RESOLVE_BENEATH flag"); +ATF_TC_BODY(resolve_beneath, tc) +{ + check_resolve_beneath(NULL, O_RDONLY); +} + +FDESCFS_TC(nodup_resolve_beneath, + "nodup mounts preserve the FD_RESOLVE_BENEATH flag"); +ATF_TC_BODY(nodup_resolve_beneath, tc) +{ + check_resolve_beneath(nodup, O_RDONLY | O_DIRECTORY); +} + +FDESCFS_TC(ioctl_caps, "fdescfs preserves ioctl capability restrictions"); +ATF_TC_BODY(ioctl_caps, tc) +{ + check_ioctl_caps(NULL); +} + +FDESCFS_TC(nodup_ioctl_caps, + "nodup mounts preserve ioctl capability restrictions"); +ATF_TC_BODY(nodup_ioctl_caps, tc) +{ + check_ioctl_caps(nodup); +} + +FDESCFS_TC(nodup_ioctl_cap_intersection, + "nodup mounts intersect ioctl caps from source fd and dirfd"); +ATF_TC_BODY(nodup_ioctl_cap_intersection, tc) +{ + cap_ioctl_t fd_cmds[] = { FIOCLEX }; + cap_ioctl_t both_cmds[] = { FIOCLEX, FIONCLEX }; + cap_ioctl_t fionclex_cmds[] = { FIONCLEX }; + char fd_path[16]; + int copy, fd, len, mntfd; + + mount_fdescfs(nodup); + fd = open("file", O_RDONLY | O_CREAT, 0644); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE_EQ(0, cap_ioctls_limit(fd, fd_cmds, nitems(fd_cmds))); + len = snprintf(fd_path, sizeof(fd_path), "%d", fd); + ATF_REQUIRE(len > 0 && (size_t)len < sizeof(fd_path)); + + /* + * Non-empty intersection: loop removes FIONCLEX, leaving {FIOCLEX}. + */ + mntfd = open("mnt", O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(mntfd >= 0); + ATF_REQUIRE_EQ(0, cap_ioctls_limit(mntfd, both_cmds, nitems(both_cmds))); + copy = openat(mntfd, fd_path, O_RDONLY); + ATF_REQUIRE(copy >= 0); + ATF_REQUIRE_EQ(0, ioctl(copy, FIOCLEX, 0)); + ATF_CHECK_ERRNO(ENOTCAPABLE, ioctl(copy, FIONCLEX, 0) == -1); + ATF_REQUIRE_EQ(0, close(copy)); + ATF_REQUIRE_EQ(0, close(mntfd)); + + /* + * Empty intersection: loop removes FIONCLEX from {FIONCLEX} since fd + * only allows {FIOCLEX}, leaving an empty list. + */ + mntfd = open("mnt", O_RDONLY | O_DIRECTORY); + ATF_REQUIRE(mntfd >= 0); + ATF_REQUIRE_EQ(0, + cap_ioctls_limit(mntfd, fionclex_cmds, nitems(fionclex_cmds))); + copy = openat(mntfd, fd_path, O_RDONLY); + ATF_REQUIRE(copy >= 0); + ATF_CHECK_ERRNO(ENOTCAPABLE, ioctl(copy, FIOCLEX, 0) == -1); + ATF_CHECK_ERRNO(ENOTCAPABLE, ioctl(copy, FIONCLEX, 0) == -1); + ATF_REQUIRE_EQ(0, close(copy)); + ATF_REQUIRE_EQ(0, close(mntfd)); + + ATF_REQUIRE_EQ(0, close(fd)); + ATF_REQUIRE_EQ(0, unmount("mnt", 0)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, cap_rights); + ATF_TP_ADD_TC(tp, nodup_cap_rights); + ATF_TP_ADD_TC(tp, resolve_beneath); + ATF_TP_ADD_TC(tp, nodup_resolve_beneath); + ATF_TP_ADD_TC(tp, ioctl_caps); + ATF_TP_ADD_TC(tp, nodup_ioctl_caps); + ATF_TP_ADD_TC(tp, nodup_ioctl_cap_intersection); + return (atf_no_error()); +}