diff options
| author | Konstantin Belousov <kib@FreeBSD.org> | 2026-11-12 20:00:18 +0000 |
|---|---|---|
| committer | Mark Johnston <markj@FreeBSD.org> | 2026-11-16 21:21:09 +0000 |
| commit | c0de320f18f00ac628a2832607fe46e2baa8f17c (patch) | |
| tree | a42ee9f53319326c1fae178d76cc32cdf608874f | |
| parent | 5260699f92e7c0ad0314c2a06a4ce4f804f10934 (diff) | |
lookup_cap_dotdot test: check symlink escape below a union mount root
Let mount_tmpfs() add the union option, and leave creating the mount
point to the callers, so that the covered directory can be populated
before the mount.
Approved by: so
Security: FreeBSD-SA-26:81.vfs
Sponsored by: The FreeBSD Foundation
| -rw-r--r-- | tests/sys/vfs/lookup_cap_dotdot.c | 84 |
1 files changed, 74 insertions, 10 deletions
diff --git a/tests/sys/vfs/lookup_cap_dotdot.c b/tests/sys/vfs/lookup_cap_dotdot.c index 08c3f81b0d57..1fde0f13834c 100644 --- a/tests/sys/vfs/lookup_cap_dotdot.c +++ b/tests/sys/vfs/lookup_cap_dotdot.c @@ -263,15 +263,16 @@ ATF_TC_BODY(lookup_cap_dotdot__root, tc) */ static char mntpath[MAXPATHLEN]; +/* Mount tmpfs over the existing directory "mnt". */ static void -mount_tmpfs(void) +mount_tmpfs(bool unionmnt) { - struct iovec iov[4]; + struct iovec iov[6]; char cwd[MAXPATHLEN]; + u_int iovlen; ATF_REQUIRE(getcwd(cwd, sizeof(cwd)) != NULL); snprintf(mntpath, sizeof(mntpath), "%s/mnt", cwd); - ATF_REQUIRE(mkdir(mntpath, 0777) == 0); iov[0].iov_base = __DECONST(char *, "fstype"); iov[0].iov_len = sizeof("fstype"); iov[1].iov_base = __DECONST(char *, "tmpfs"); @@ -280,10 +281,29 @@ mount_tmpfs(void) iov[2].iov_len = sizeof("fspath"); iov[3].iov_base = mntpath; iov[3].iov_len = strlen(mntpath) + 1; - ATF_REQUIRE_MSG(nmount(iov, nitems(iov), 0) == 0, "nmount: %s", + iovlen = 4; + if (unionmnt) { + iov[4].iov_base = __DECONST(char *, "union"); + iov[4].iov_len = sizeof("union"); + iov[5].iov_base = NULL; + iov[5].iov_len = 0; + iovlen = 6; + } + ATF_REQUIRE_MSG(nmount(iov, iovlen, 0) == 0, "nmount: %s", strerror(errno)); } +static void +unmount_tmpfs(void) +{ + char cwd[MAXPATHLEN]; + + if (getcwd(cwd, sizeof(cwd)) == NULL) + return; + snprintf(mntpath, sizeof(mntpath), "%s/mnt", cwd); + (void)unmount(mntpath, MNT_FORCE); +} + ATF_TC_WITH_CLEANUP(lookup_cap_dotdot__mountroot); ATF_TC_HEAD(lookup_cap_dotdot__mountroot, tc) { @@ -297,7 +317,8 @@ ATF_TC_BODY(lookup_cap_dotdot__mountroot, tc) int dfd; check_capsicum(); - mount_tmpfs(); + ATF_REQUIRE(mkdir("mnt", 0777) == 0); + mount_tmpfs(false); ATF_REQUIRE((dfd = open(mntpath, O_RDONLY | O_DIRECTORY)) >= 0); ATF_REQUIRE(mkdirat(dfd, "sub", 0777) == 0); @@ -317,12 +338,54 @@ ATF_TC_BODY(lookup_cap_dotdot__mountroot, tc) ATF_TC_CLEANUP(lookup_cap_dotdot__mountroot, tc) { - char cwd[MAXPATHLEN]; + unmount_tmpfs(); +} - if (getcwd(cwd, sizeof(cwd)) == NULL) - return; - snprintf(mntpath, sizeof(mntpath), "%s/mnt", cwd); - (void)unmount(mntpath, MNT_FORCE); +/* + * A name missing from the root of a union mount is looked up in the + * covered directory. A relative symlink found there is resolved from + * the covered directory, and its ".." must not leave the starting + * directory either. + */ +ATF_TC_WITH_CLEANUP(lookup_cap_dotdot__union); +ATF_TC_HEAD(lookup_cap_dotdot__union, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Validate that symlinks below a union mount root stay beneath it"); + atf_tc_set_md_var(tc, "require.user", "root"); +} + +ATF_TC_BODY(lookup_cap_dotdot__union, tc) +{ + int dfd; + + check_capsicum(); + touchat(AT_FDCWD, "outside"); + ATF_REQUIRE(mkdir("mnt", 0777) == 0); + touchat(AT_FDCWD, "mnt/lower"); + ATF_REQUIRE(symlink("lower", "mnt/lnklower") == 0); + ATF_REQUIRE(symlink("../outside", "mnt/lnkout") == 0); + mount_tmpfs(true); + + ATF_REQUIRE((dfd = open(mntpath, O_RDONLY | O_DIRECTORY)) >= 0); + + /* Entries of the covered directory are still found. */ + ATF_REQUIRE_MSG(openat(dfd, "lower", O_RDONLY | + O_RESOLVE_BENEATH) >= 0, "%s", strerror(errno)); + ATF_REQUIRE_MSG(openat(dfd, "lnklower", O_RDONLY | + O_RESOLVE_BENEATH) >= 0, "%s", strerror(errno)); + + ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dfd, "lnkout", O_RDONLY | + O_RESOLVE_BENEATH) < 0); + + ATF_REQUIRE(cap_enter() >= 0); + + ATF_REQUIRE_ERRNO(ENOTCAPABLE, openat(dfd, "lnkout", O_RDONLY) < 0); +} + +ATF_TC_CLEANUP(lookup_cap_dotdot__union, tc) +{ + unmount_tmpfs(); } ATF_TP_ADD_TCS(tp) @@ -338,6 +401,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, lookup_cap_dotdot__negative); ATF_TP_ADD_TC(tp, lookup_cap_dotdot__root); ATF_TP_ADD_TC(tp, lookup_cap_dotdot__mountroot); + ATF_TP_ADD_TC(tp, lookup_cap_dotdot__union); return (atf_no_error()); } |
