xref: /freebsd/tests/sys/file/path_test.c (revision b59851e99c20f3a72c34bdf9919e3bf49b894e4e)
13a248c84SMark Johnston /*-
23a248c84SMark Johnston  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
33a248c84SMark Johnston  *
43a248c84SMark Johnston  * Copyright (c) 2021 The FreeBSD Foundation
53a248c84SMark Johnston  *
63a248c84SMark Johnston  * This software was developed by Mark Johnston under sponsorship from
73a248c84SMark Johnston  * the FreeBSD Foundation.
83a248c84SMark Johnston  *
93a248c84SMark Johnston  * Redistribution and use in source and binary forms, with or without
103a248c84SMark Johnston  * modification, are permitted provided that the following conditions are
113a248c84SMark Johnston  * met:
123a248c84SMark Johnston  * 1. Redistributions of source code must retain the above copyright
133a248c84SMark Johnston  *    notice, this list of conditions and the following disclaimer.
143a248c84SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
153a248c84SMark Johnston  *    notice, this list of conditions and the following disclaimer in
163a248c84SMark Johnston  *    the documentation and/or other materials provided with the distribution.
173a248c84SMark Johnston  *
183a248c84SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
193a248c84SMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203a248c84SMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
213a248c84SMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
223a248c84SMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
233a248c84SMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
243a248c84SMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
253a248c84SMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263a248c84SMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
273a248c84SMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
283a248c84SMark Johnston  * SUCH DAMAGE.
293a248c84SMark Johnston  */
303a248c84SMark Johnston 
313a248c84SMark Johnston /*
323a248c84SMark Johnston  * Basic regression tests for handling of O_PATH descriptors.
333a248c84SMark Johnston  */
343a248c84SMark Johnston 
353a248c84SMark Johnston #include <sys/param.h>
363a248c84SMark Johnston #include <sys/capsicum.h>
373a248c84SMark Johnston #include <sys/event.h>
383a248c84SMark Johnston #include <sys/ioctl.h>
393a248c84SMark Johnston #include <sys/memrange.h>
403a248c84SMark Johnston #include <sys/mman.h>
41*b59851e9SMark Johnston #include <sys/ptrace.h>
423a248c84SMark Johnston #include <sys/socket.h>
433a248c84SMark Johnston #include <sys/stat.h>
443a248c84SMark Johnston #include <sys/time.h>
453a248c84SMark Johnston #include <sys/uio.h>
46*b59851e9SMark Johnston #include <sys/un.h>
473a248c84SMark Johnston #include <sys/wait.h>
483a248c84SMark Johnston 
493a248c84SMark Johnston #include <aio.h>
503a248c84SMark Johnston #include <dirent.h>
513a248c84SMark Johnston #include <errno.h>
523a248c84SMark Johnston #include <fcntl.h>
533a248c84SMark Johnston #include <poll.h>
54*b59851e9SMark Johnston #include <signal.h>
553a248c84SMark Johnston #include <stdio.h>
563a248c84SMark Johnston #include <stdlib.h>
573a248c84SMark Johnston #include <unistd.h>
583a248c84SMark Johnston 
593a248c84SMark Johnston #include <atf-c.h>
603a248c84SMark Johnston 
613a248c84SMark Johnston #define	FMT_ERR(s)		s ": %s", strerror(errno)
623a248c84SMark Johnston 
633a248c84SMark Johnston #define	CHECKED_CLOSE(fd)	\
643a248c84SMark Johnston 	ATF_REQUIRE_MSG(close(fd) == 0, FMT_ERR("close"))
653a248c84SMark Johnston 
663a248c84SMark Johnston /* Create a temporary regular file containing some data. */
673a248c84SMark Johnston static void
683a248c84SMark Johnston mktfile(char path[PATH_MAX], const char *template)
693a248c84SMark Johnston {
703a248c84SMark Johnston 	char buf[BUFSIZ];
713a248c84SMark Johnston 	int fd;
723a248c84SMark Johnston 
733a248c84SMark Johnston 	snprintf(path, PATH_MAX, "%s", template);
743a248c84SMark Johnston 	fd = mkstemp(path);
753a248c84SMark Johnston 	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("mkstemp"));
763a248c84SMark Johnston 	memset(buf, 0, sizeof(buf));
773a248c84SMark Johnston 	ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) == sizeof(buf),
783a248c84SMark Johnston 	    FMT_ERR("write"));
793a248c84SMark Johnston 	CHECKED_CLOSE(fd);
803a248c84SMark Johnston }
813a248c84SMark Johnston 
823a248c84SMark Johnston /* Make a temporary directory. */
833a248c84SMark Johnston static void
843a248c84SMark Johnston mktdir(char path[PATH_MAX], const char *template)
853a248c84SMark Johnston {
863a248c84SMark Johnston 	snprintf(path, PATH_MAX, "%s", template);
873a248c84SMark Johnston 	ATF_REQUIRE_MSG(mkdtemp(path) == path, FMT_ERR("mkdtemp"));
883a248c84SMark Johnston }
893a248c84SMark Johnston 
903a248c84SMark Johnston /* Wait for a child process to exit with status 0. */
913a248c84SMark Johnston static void
923a248c84SMark Johnston waitchild(pid_t child, int exstatus)
933a248c84SMark Johnston {
943a248c84SMark Johnston 	int error, status;
953a248c84SMark Johnston 
963a248c84SMark Johnston 	error = waitpid(child, &status, 0);
973a248c84SMark Johnston 	ATF_REQUIRE_MSG(error != -1, FMT_ERR("waitpid"));
983a248c84SMark Johnston 	ATF_REQUIRE_MSG(WIFEXITED(status), "child exited abnormally, status %d",
993a248c84SMark Johnston 	    status);
1003a248c84SMark Johnston 	ATF_REQUIRE_MSG(WEXITSTATUS(status) == exstatus,
1013a248c84SMark Johnston 	    "child exit status is %d, expected %d",
1023a248c84SMark Johnston 	    WEXITSTATUS(status), exstatus);
1033a248c84SMark Johnston }
1043a248c84SMark Johnston 
1053a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_access);
1063a248c84SMark Johnston ATF_TC_BODY(path_access, tc)
1073a248c84SMark Johnston {
1083a248c84SMark Johnston 	char path[PATH_MAX];
1093a248c84SMark Johnston 	struct stat sb;
1103a248c84SMark Johnston 	struct timespec ts[2];
1113a248c84SMark Johnston 	struct timeval tv[2];
1123a248c84SMark Johnston 	int pathfd;
1133a248c84SMark Johnston 
1143a248c84SMark Johnston 	mktfile(path, "path_access.XXXXXX");
1153a248c84SMark Johnston 
1163a248c84SMark Johnston 	pathfd = open(path, O_PATH);
1173a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
1183a248c84SMark Johnston 
1193a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0666) == -1);
1203a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fchown(pathfd, getuid(), getgid()) == -1);
1213a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fchflags(pathfd, UF_NODUMP) == -1);
1223a248c84SMark Johnston 	memset(tv, 0, sizeof(tv));
1233a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, futimes(pathfd, tv) == -1);
1243a248c84SMark Johnston 	memset(ts, 0, sizeof(ts));
1253a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, futimens(pathfd, ts) == -1);
1263a248c84SMark Johnston 
1273a248c84SMark Johnston 	/* fpathconf(2) and fstat(2) are permitted. */
1283a248c84SMark Johnston 	ATF_REQUIRE_MSG(fstat(pathfd, &sb) == 0, FMT_ERR("fstat"));
1293a248c84SMark Johnston 	ATF_REQUIRE_MSG(fpathconf(pathfd, _PC_LINK_MAX) != -1,
1303a248c84SMark Johnston 	    FMT_ERR("fpathconf"));
1313a248c84SMark Johnston 
1323a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
1333a248c84SMark Johnston }
1343a248c84SMark Johnston 
1353a248c84SMark Johnston /* Basic tests to verify that AIO operations fail. */
1363a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_aio);
1373a248c84SMark Johnston ATF_TC_BODY(path_aio, tc)
1383a248c84SMark Johnston {
1393a248c84SMark Johnston 	struct aiocb aio;
1403a248c84SMark Johnston 	char buf[BUFSIZ], path[PATH_MAX];
1413a248c84SMark Johnston 	int pathfd;
1423a248c84SMark Johnston 
1433a248c84SMark Johnston 	mktfile(path, "path_aio.XXXXXX");
1443a248c84SMark Johnston 
1453a248c84SMark Johnston 	pathfd = open(path, O_PATH);
1463a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
1473a248c84SMark Johnston 
1483a248c84SMark Johnston 	memset(&aio, 0, sizeof(aio));
1493a248c84SMark Johnston 	aio.aio_buf = buf;
1503a248c84SMark Johnston 	aio.aio_nbytes = sizeof(buf);
1513a248c84SMark Johnston 	aio.aio_fildes = pathfd;
1523a248c84SMark Johnston 	aio.aio_offset = 0;
1533a248c84SMark Johnston 
1543a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, aio_read(&aio) == -1);
1553a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, aio_write(&aio) == -1);
1563a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, aio_fsync(O_SYNC, &aio) == -1);
1573a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, aio_fsync(O_DSYNC, &aio) == -1);
1583a248c84SMark Johnston 
1593a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
1603a248c84SMark Johnston }
1613a248c84SMark Johnston 
1623a248c84SMark Johnston /* Basic tests to verify that Capsicum restrictions apply to path fds. */
1633a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_capsicum);
1643a248c84SMark Johnston ATF_TC_BODY(path_capsicum, tc)
1653a248c84SMark Johnston {
1663a248c84SMark Johnston 	char path[PATH_MAX];
1673a248c84SMark Johnston 	cap_rights_t rights;
1683a248c84SMark Johnston 	int truefd;
1693a248c84SMark Johnston 	pid_t child;
1703a248c84SMark Johnston 
1713a248c84SMark Johnston 	mktfile(path, "path_capsicum.XXXXXX");
1723a248c84SMark Johnston 
1733a248c84SMark Johnston 	/* Make sure that filesystem namespace restrictions apply to O_PATH. */
1743a248c84SMark Johnston 	child = fork();
1753a248c84SMark Johnston 	ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
1763a248c84SMark Johnston 	if (child == 0) {
1773a248c84SMark Johnston 		if (cap_enter() != 0)
1783a248c84SMark Johnston 			_exit(1);
1793a248c84SMark Johnston 		if (open(path, O_PATH) >= 0)
1803a248c84SMark Johnston 			_exit(2);
1813a248c84SMark Johnston 		if (errno != ECAPMODE)
1823a248c84SMark Johnston 			_exit(3);
1833a248c84SMark Johnston 		if (open("/usr/bin/true", O_PATH | O_EXEC) >= 0)
1843a248c84SMark Johnston 			_exit(4);
1853a248c84SMark Johnston 		if (errno != ECAPMODE)
1863a248c84SMark Johnston 			_exit(5);
1873a248c84SMark Johnston 		_exit(0);
1883a248c84SMark Johnston 	}
1893a248c84SMark Johnston 	waitchild(child, 0);
1903a248c84SMark Johnston 
1913a248c84SMark Johnston 	/* Make sure that CAP_FEXECVE is required. */
1923a248c84SMark Johnston 	child = fork();
1933a248c84SMark Johnston 	ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
1943a248c84SMark Johnston 	if (child == 0) {
1953a248c84SMark Johnston 		truefd = open("/usr/bin/true", O_PATH | O_EXEC);
1963a248c84SMark Johnston 		if (truefd < 0)
1973a248c84SMark Johnston 			_exit(1);
1983a248c84SMark Johnston 		cap_rights_init(&rights);
1993a248c84SMark Johnston 		if (cap_rights_limit(truefd, &rights) != 0)
2003a248c84SMark Johnston 			_exit(2);
2013a248c84SMark Johnston 		(void)fexecve(truefd,
2023a248c84SMark Johnston 		    (char * const[]){__DECONST(char *, "/usr/bin/true"), NULL},
2033a248c84SMark Johnston 		    NULL);
2043a248c84SMark Johnston 		if (errno != ENOTCAPABLE)
2053a248c84SMark Johnston 			_exit(3);
2063a248c84SMark Johnston 		_exit(4);
2073a248c84SMark Johnston 	}
2083a248c84SMark Johnston 	waitchild(child, 4);
2093a248c84SMark Johnston }
2103a248c84SMark Johnston 
211*b59851e9SMark Johnston /* Make sure that ptrace(PT_COREDUMP) cannot be used to write to a path fd. */
212*b59851e9SMark Johnston ATF_TC_WITHOUT_HEAD(path_coredump);
213*b59851e9SMark Johnston ATF_TC_BODY(path_coredump, tc)
214*b59851e9SMark Johnston {
215*b59851e9SMark Johnston 	char path[PATH_MAX];
216*b59851e9SMark Johnston 	struct ptrace_coredump pc;
217*b59851e9SMark Johnston 	int error, pathfd, status;
218*b59851e9SMark Johnston 	pid_t child;
219*b59851e9SMark Johnston 
220*b59851e9SMark Johnston 	mktdir(path, "path_coredump.XXXXXX");
221*b59851e9SMark Johnston 
222*b59851e9SMark Johnston 	child = fork();
223*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
224*b59851e9SMark Johnston 	if (child == 0) {
225*b59851e9SMark Johnston 		while (true)
226*b59851e9SMark Johnston 			(void)sleep(1);
227*b59851e9SMark Johnston 	}
228*b59851e9SMark Johnston 
229*b59851e9SMark Johnston 	pathfd = open(path, O_PATH);
230*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
231*b59851e9SMark Johnston 
232*b59851e9SMark Johnston 	error = ptrace(PT_ATTACH, child, 0, 0);
233*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(error == 0, FMT_ERR("ptrace"));
234*b59851e9SMark Johnston 	error = waitpid(child, &status, 0);
235*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(error != -1, FMT_ERR("waitpid"));
236*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(WIFSTOPPED(status), "unexpected status %d", status);
237*b59851e9SMark Johnston 
238*b59851e9SMark Johnston 	pc.pc_fd = pathfd;
239*b59851e9SMark Johnston 	pc.pc_flags = 0;
240*b59851e9SMark Johnston 	pc.pc_limit = 0;
241*b59851e9SMark Johnston 	error = ptrace(PT_COREDUMP, child, (void *)&pc, sizeof(pc));
242*b59851e9SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, error == -1);
243*b59851e9SMark Johnston 
244*b59851e9SMark Johnston 	error = ptrace(PT_DETACH, child, 0, 0);
245*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(error == 0, FMT_ERR("ptrace"));
246*b59851e9SMark Johnston 
247*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(kill(child, SIGKILL) == 0, FMT_ERR("kill"));
248*b59851e9SMark Johnston 
249*b59851e9SMark Johnston 	CHECKED_CLOSE(pathfd);
250*b59851e9SMark Johnston }
251*b59851e9SMark Johnston 
2523a248c84SMark Johnston /* Verify operations on directory path descriptors. */
2533a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_directory);
2543a248c84SMark Johnston ATF_TC_BODY(path_directory, tc)
2553a248c84SMark Johnston {
2563a248c84SMark Johnston 	struct dirent de;
2573a248c84SMark Johnston 	struct stat sb;
2583a248c84SMark Johnston 	char path[PATH_MAX];
2593a248c84SMark Johnston 	int fd, pathfd;
2603a248c84SMark Johnston 
2613a248c84SMark Johnston 	mktdir(path, "path_directory.XXXXXX");
2623a248c84SMark Johnston 
2633a248c84SMark Johnston 	pathfd = open(path, O_PATH | O_DIRECTORY);
2643a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
2653a248c84SMark Johnston 
2663a248c84SMark Johnston 	/* Should not be possible to list directory entries. */
2673a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
2683a248c84SMark Johnston 	    getdirentries(pathfd, (char *)&de, sizeof(de), NULL) == -1);
2693a248c84SMark Johnston 
2703a248c84SMark Johnston 	/* It should be possible to create files under pathfd. */
2713a248c84SMark Johnston 	fd = openat(pathfd, "test", O_RDWR | O_CREAT, 0600);
2723a248c84SMark Johnston 	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));
2733a248c84SMark Johnston 	ATF_REQUIRE_MSG(fstatat(pathfd, "test", &sb, 0) == 0,
2743a248c84SMark Johnston 	    FMT_ERR("fstatat"));
2753a248c84SMark Johnston 	CHECKED_CLOSE(fd);
2763a248c84SMark Johnston 
2773a248c84SMark Johnston 	/* ... but doing so requires write access. */
2783a248c84SMark Johnston 	if (geteuid() != 0) {
2793a248c84SMark Johnston 		ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0500) == -1);
2803a248c84SMark Johnston 		ATF_REQUIRE_MSG(chmod(path, 0500) == 0, FMT_ERR("chmod"));
2813a248c84SMark Johnston 		ATF_REQUIRE_ERRNO(EACCES,
2823a248c84SMark Johnston 		    openat(pathfd, "test2", O_RDWR | O_CREAT, 0600) < 0);
2833a248c84SMark Johnston 	}
2843a248c84SMark Johnston 
2853a248c84SMark Johnston 	/* fchdir(2) is permitted. */
2863a248c84SMark Johnston 	ATF_REQUIRE_MSG(fchdir(pathfd) == 0, FMT_ERR("fchdir"));
2873a248c84SMark Johnston 
2883a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
2893a248c84SMark Johnston }
2903a248c84SMark Johnston 
2913a248c84SMark Johnston /* Verify access permission checking for a directory path fd. */
2923a248c84SMark Johnston ATF_TC_WITH_CLEANUP(path_directory_not_root);
2933a248c84SMark Johnston ATF_TC_HEAD(path_directory_not_root, tc)
2943a248c84SMark Johnston {
2953a248c84SMark Johnston 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
2963a248c84SMark Johnston }
2973a248c84SMark Johnston ATF_TC_BODY(path_directory_not_root, tc)
2983a248c84SMark Johnston {
2993a248c84SMark Johnston 	char path[PATH_MAX];
3003a248c84SMark Johnston 	int pathfd;
3013a248c84SMark Johnston 
3023a248c84SMark Johnston 	mktdir(path, "path_directory.XXXXXX");
3033a248c84SMark Johnston 
3043a248c84SMark Johnston 	pathfd = open(path, O_PATH | O_DIRECTORY);
3053a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
3063a248c84SMark Johnston 
3073a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0500) == -1);
3083a248c84SMark Johnston 	ATF_REQUIRE_MSG(chmod(path, 0500) == 0, FMT_ERR("chmod"));
3093a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EACCES,
3103a248c84SMark Johnston 	    openat(pathfd, "test2", O_RDWR | O_CREAT, 0600) < 0);
3113a248c84SMark Johnston 
3123a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
3133a248c84SMark Johnston }
3143a248c84SMark Johnston ATF_TC_CLEANUP(path_directory_not_root, tc)
3153a248c84SMark Johnston {
3163a248c84SMark Johnston }
3173a248c84SMark Johnston 
3183a248c84SMark Johnston /* Validate system calls that handle AT_EMPTY_PATH. */
3193a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_empty);
3203a248c84SMark Johnston ATF_TC_BODY(path_empty, tc)
3213a248c84SMark Johnston {
3223a248c84SMark Johnston 	char path[PATH_MAX];
3233a248c84SMark Johnston 	struct timespec ts[2];
3243a248c84SMark Johnston 	struct stat sb;
3253a248c84SMark Johnston 	int pathfd;
3263a248c84SMark Johnston 
3273a248c84SMark Johnston 	mktfile(path, "path_empty.XXXXXX");
3283a248c84SMark Johnston 
3293a248c84SMark Johnston 	pathfd = open(path, O_PATH);
3303a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
3313a248c84SMark Johnston 
3323a248c84SMark Johnston 	/* Various *at operations should work on path fds. */
3333a248c84SMark Johnston 	ATF_REQUIRE_MSG(faccessat(pathfd, "", F_OK, AT_EMPTY_PATH) == 0,
3343a248c84SMark Johnston 	    FMT_ERR("faccessat"));
3353a248c84SMark Johnston 	ATF_REQUIRE_MSG(chflagsat(pathfd, "", UF_NODUMP, AT_EMPTY_PATH) == 0,
3363a248c84SMark Johnston 	    FMT_ERR("chflagsat"));
3373a248c84SMark Johnston 	ATF_REQUIRE_MSG(fchmodat(pathfd, "", 0600, AT_EMPTY_PATH) == 0,
3383a248c84SMark Johnston 	    FMT_ERR("fchmodat"));
3393a248c84SMark Johnston 	ATF_REQUIRE_MSG(fchownat(pathfd, "", getuid(), getgid(),
3403a248c84SMark Johnston 	    AT_EMPTY_PATH) == 0, FMT_ERR("fchownat"));
3413a248c84SMark Johnston 	ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
3423a248c84SMark Johnston 	    FMT_ERR("fstatat"));
3433a248c84SMark Johnston 	ATF_REQUIRE_MSG(sb.st_size == BUFSIZ,
3443a248c84SMark Johnston 	    "unexpected size %ju", (uintmax_t)sb.st_size);
3453a248c84SMark Johnston 	memset(ts, 0, sizeof(ts));
3463a248c84SMark Johnston 	ATF_REQUIRE_MSG(utimensat(pathfd, "", ts, AT_EMPTY_PATH) == 0,
3473a248c84SMark Johnston 	    FMT_ERR("utimensat"));
3483a248c84SMark Johnston 
3493a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
3503a248c84SMark Johnston }
3513a248c84SMark Johnston 
3523a248c84SMark Johnston /* Verify that various operations on a path fd have access checks. */
3533a248c84SMark Johnston ATF_TC_WITH_CLEANUP(path_empty_not_root);
3543a248c84SMark Johnston ATF_TC_HEAD(path_empty_not_root, tc)
3553a248c84SMark Johnston {
3563a248c84SMark Johnston 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
3573a248c84SMark Johnston }
3583a248c84SMark Johnston ATF_TC_BODY(path_empty_not_root, tc)
3593a248c84SMark Johnston {
3603a248c84SMark Johnston 	int pathfd;
3613a248c84SMark Johnston 
3623a248c84SMark Johnston 	pathfd = open("/dev/null", O_PATH);
3633a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
3643a248c84SMark Johnston 
3653a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EPERM,
3663a248c84SMark Johnston 	    chflagsat(pathfd, "", UF_NODUMP, AT_EMPTY_PATH) == -1);
3673a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EPERM,
3683a248c84SMark Johnston 	    fchownat(pathfd, "", getuid(), getgid(), AT_EMPTY_PATH) == -1);
3693a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EPERM,
3703a248c84SMark Johnston 	    fchmodat(pathfd, "", 0600, AT_EMPTY_PATH) == -1);
3713a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EPERM,
3723a248c84SMark Johnston 	    linkat(pathfd, "", AT_FDCWD, "test", AT_EMPTY_PATH) == -1);
3733a248c84SMark Johnston 
3743a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
3753a248c84SMark Johnston }
3763a248c84SMark Johnston ATF_TC_CLEANUP(path_empty_not_root, tc)
3773a248c84SMark Johnston {
3783a248c84SMark Johnston }
3793a248c84SMark Johnston 
3803a248c84SMark Johnston /* Test linkat(2) with AT_EMPTY_PATH, which requires privileges. */
3813a248c84SMark Johnston ATF_TC_WITH_CLEANUP(path_empty_root);
3823a248c84SMark Johnston ATF_TC_HEAD(path_empty_root, tc)
3833a248c84SMark Johnston {
3843a248c84SMark Johnston 	atf_tc_set_md_var(tc, "require.user", "root");
3853a248c84SMark Johnston }
3863a248c84SMark Johnston ATF_TC_BODY(path_empty_root, tc)
3873a248c84SMark Johnston {
3883a248c84SMark Johnston 	char path[PATH_MAX];
3893a248c84SMark Johnston 	struct stat sb, sb2;
3903a248c84SMark Johnston 	int pathfd;
3913a248c84SMark Johnston 
3923a248c84SMark Johnston 	mktfile(path, "path_empty_root.XXXXXX");
3933a248c84SMark Johnston 
3943a248c84SMark Johnston 	pathfd = open(path, O_PATH);
3953a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
3963a248c84SMark Johnston 	ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
3973a248c84SMark Johnston 	    FMT_ERR("fstatat"));
3983a248c84SMark Johnston 
3993a248c84SMark Johnston 	ATF_REQUIRE_MSG(linkat(pathfd, "", AT_FDCWD, "test", AT_EMPTY_PATH) ==
4003a248c84SMark Johnston 	    0, FMT_ERR("linkat"));
4013a248c84SMark Johnston 	ATF_REQUIRE_MSG(fstatat(AT_FDCWD, "test", &sb2, 0) == 0,
4023a248c84SMark Johnston 	    FMT_ERR("fstatat"));
4033a248c84SMark Johnston 	ATF_REQUIRE_MSG(sb.st_dev == sb2.st_dev, "st_dev mismatch");
4043a248c84SMark Johnston 	ATF_REQUIRE_MSG(sb.st_ino == sb2.st_ino, "st_ino mismatch");
4053a248c84SMark Johnston 
4063a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
4073a248c84SMark Johnston 
4083a248c84SMark Johnston }
4093a248c84SMark Johnston ATF_TC_CLEANUP(path_empty_root, tc)
4103a248c84SMark Johnston {
4113a248c84SMark Johnston }
4123a248c84SMark Johnston 
4133a248c84SMark Johnston /* poll(2) never returns an event for path fds, but kevent(2) does. */
4143a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_event);
4153a248c84SMark Johnston ATF_TC_BODY(path_event, tc)
4163a248c84SMark Johnston {
4173a248c84SMark Johnston 	char buf[BUFSIZ], path[PATH_MAX];
4183a248c84SMark Johnston 	struct kevent ev;
4193a248c84SMark Johnston 	struct pollfd pollfd;
4203a248c84SMark Johnston 	int kq, pathfd;
4213a248c84SMark Johnston 
4223a248c84SMark Johnston 	mktfile(path, "path_event.XXXXXX");
4233a248c84SMark Johnston 
4243a248c84SMark Johnston 	pathfd = open(path, O_PATH);
4253a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
4263a248c84SMark Johnston 
4273a248c84SMark Johnston 	/* poll(2) should return POLLNVAL. */
4283a248c84SMark Johnston 	pollfd.fd = pathfd;
4293a248c84SMark Johnston 	pollfd.events = POLLIN;
4303a248c84SMark Johnston 	pollfd.revents = 0;
4313a248c84SMark Johnston 	ATF_REQUIRE_MSG(poll(&pollfd, 1, 0) == 1, FMT_ERR("poll"));
4323a248c84SMark Johnston 	ATF_REQUIRE_MSG(pollfd.revents == POLLNVAL, "unexpected revents %x",
4333a248c84SMark Johnston 	    pollfd.revents);
4343a248c84SMark Johnston 	pollfd.events = POLLOUT;
4353a248c84SMark Johnston 	pollfd.revents = 0;
4363a248c84SMark Johnston 	ATF_REQUIRE_MSG(poll(&pollfd, 1, 0) == 1, FMT_ERR("poll"));
4373a248c84SMark Johnston 	ATF_REQUIRE_MSG(pollfd.revents == POLLNVAL, "unexpected revents %x",
4383a248c84SMark Johnston 	    pollfd.revents);
4393a248c84SMark Johnston 
4403a248c84SMark Johnston 	/* Try to get a EVFILT_READ event through a path fd. */
4413a248c84SMark Johnston 	kq = kqueue();
4423a248c84SMark Johnston 	ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
4433a248c84SMark Johnston 	EV_SET(&ev, pathfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
4443a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
4453a248c84SMark Johnston 	    FMT_ERR("kevent"));
4463a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
4473a248c84SMark Johnston 	    FMT_ERR("kevent"));
4483a248c84SMark Johnston 	ATF_REQUIRE_MSG((ev.flags & EV_ERROR) == 0, "EV_ERROR is set");
4493a248c84SMark Johnston 	ATF_REQUIRE_MSG(ev.data == sizeof(buf),
4503a248c84SMark Johnston 	    "data is %jd", (intmax_t)ev.data);
4513a248c84SMark Johnston 	EV_SET(&ev, pathfd, EVFILT_READ, EV_DELETE, 0, 0, 0);
4523a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
4533a248c84SMark Johnston 	    FMT_ERR("kevent"));
4543a248c84SMark Johnston 
4553a248c84SMark Johnston 	/* Try to get a EVFILT_VNODE/NOTE_LINK event through a path fd. */
4563a248c84SMark Johnston 	EV_SET(&ev, pathfd, EVFILT_VNODE, EV_ADD | EV_ENABLE, NOTE_LINK, 0, 0);
4573a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
4583a248c84SMark Johnston 	    FMT_ERR("kevent"));
4593a248c84SMark Johnston 	ATF_REQUIRE_MSG(funlinkat(AT_FDCWD, path, pathfd, 0) == 0,
4603a248c84SMark Johnston 	    FMT_ERR("funlinkat"));
4613a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
4623a248c84SMark Johnston 	    FMT_ERR("kevent"));
4633a248c84SMark Johnston 	EV_SET(&ev, pathfd, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
4643a248c84SMark Johnston 	ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
4653a248c84SMark Johnston 	    FMT_ERR("kevent"));
4663a248c84SMark Johnston 
4673a248c84SMark Johnston 	CHECKED_CLOSE(kq);
4683a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
4693a248c84SMark Johnston }
4703a248c84SMark Johnston 
4713a248c84SMark Johnston /* Check various fcntl(2) operations on a path desriptor. */
4723a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_fcntl);
4733a248c84SMark Johnston ATF_TC_BODY(path_fcntl, tc)
4743a248c84SMark Johnston {
4753a248c84SMark Johnston 	char path[PATH_MAX];
4763a248c84SMark Johnston 	int flags, pathfd, pathfd2;
4773a248c84SMark Johnston 
4783a248c84SMark Johnston 	mktfile(path, "path_fcntl.XXXXXX");
4793a248c84SMark Johnston 
4803a248c84SMark Johnston 	pathfd = open(path, O_PATH);
4813a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
4823a248c84SMark Johnston 
4833a248c84SMark Johnston 	/* O_PATH should appear in the fd flags. */
4843a248c84SMark Johnston 	flags = fcntl(pathfd, F_GETFL);
4853a248c84SMark Johnston 	ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
4863a248c84SMark Johnston 	ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
4873a248c84SMark Johnston 
4883a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
4893a248c84SMark Johnston 	    fcntl(pathfd, F_SETFL, flags & ~O_PATH));
4903a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
4913a248c84SMark Johnston 	    fcntl(pathfd, F_SETFL, flags | O_APPEND));
4923a248c84SMark Johnston 
4933a248c84SMark Johnston 	/* A dup'ed O_PATH fd had better have O_PATH set too. */
4943a248c84SMark Johnston 	pathfd2 = fcntl(pathfd, F_DUPFD, 0);
4953a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd2 >= 0, FMT_ERR("fcntl"));
4963a248c84SMark Johnston 	flags = fcntl(pathfd2, F_GETFL);
4973a248c84SMark Johnston 	ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
4983a248c84SMark Johnston 	ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
4993a248c84SMark Johnston 	CHECKED_CLOSE(pathfd2);
5003a248c84SMark Johnston 
5013a248c84SMark Johnston 	/* Double check with dup(2). */
5023a248c84SMark Johnston 	pathfd2 = dup(pathfd);
5033a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd2 >= 0, FMT_ERR("dup"));
5043a248c84SMark Johnston 	flags = fcntl(pathfd2, F_GETFL);
5053a248c84SMark Johnston 	ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
5063a248c84SMark Johnston 	ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
5073a248c84SMark Johnston 	CHECKED_CLOSE(pathfd2);
5083a248c84SMark Johnston 
5093a248c84SMark Johnston 	/* It should be possible to set O_CLOEXEC. */
5103a248c84SMark Johnston 	ATF_REQUIRE_MSG(fcntl(pathfd, F_SETFD, FD_CLOEXEC) == 0,
5113a248c84SMark Johnston 	    FMT_ERR("fcntl"));
5123a248c84SMark Johnston 	ATF_REQUIRE_MSG(fcntl(pathfd, F_GETFD) == FD_CLOEXEC,
5133a248c84SMark Johnston 	    FMT_ERR("fcntl"));
5143a248c84SMark Johnston 
5153a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
5163a248c84SMark Johnston }
5173a248c84SMark Johnston 
5183a248c84SMark Johnston /* Verify that we can execute a file opened with O_PATH. */
5193a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_fexecve);
5203a248c84SMark Johnston ATF_TC_BODY(path_fexecve, tc)
5213a248c84SMark Johnston {
5223a248c84SMark Johnston 	char path[PATH_MAX];
5233a248c84SMark Johnston 	pid_t child;
5243a248c84SMark Johnston 	int fd, pathfd;
5253a248c84SMark Johnston 
5263a248c84SMark Johnston 	child = fork();
5273a248c84SMark Johnston 	ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
5283a248c84SMark Johnston 	if (child == 0) {
5293a248c84SMark Johnston 		pathfd = open("/usr/bin/true", O_PATH | O_EXEC);
5303a248c84SMark Johnston 		if (pathfd < 0)
5313a248c84SMark Johnston 			_exit(1);
5323a248c84SMark Johnston 		fexecve(pathfd,
5333a248c84SMark Johnston 		    (char * const[]){__DECONST(char *, "/usr/bin/true"), NULL},
5343a248c84SMark Johnston 		    NULL);
5353a248c84SMark Johnston 		_exit(2);
5363a248c84SMark Johnston 	}
5373a248c84SMark Johnston 	waitchild(child, 0);
5383a248c84SMark Johnston 
5393a248c84SMark Johnston 	/*
5403a248c84SMark Johnston 	 * Also verify that access permissions are checked when opening with
5413a248c84SMark Johnston 	 * O_PATH.
5423a248c84SMark Johnston 	 */
5433a248c84SMark Johnston 	snprintf(path, sizeof(path), "path_fexecve.XXXXXX");
5443a248c84SMark Johnston 	ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
5453a248c84SMark Johnston 
5463a248c84SMark Johnston 	fd = open(path, O_CREAT | O_RDONLY, 0600);
5473a248c84SMark Johnston 	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));
5483a248c84SMark Johnston 
5493a248c84SMark Johnston 	pathfd = open(path, O_PATH | O_EXEC);
5503a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EACCES, pathfd < 0);
5513a248c84SMark Johnston }
5523a248c84SMark Johnston 
553*b59851e9SMark Johnston /* Make sure that O_PATH restrictions apply to named pipes as well. */
554*b59851e9SMark Johnston ATF_TC_WITHOUT_HEAD(path_fifo);
555*b59851e9SMark Johnston ATF_TC_BODY(path_fifo, tc)
556*b59851e9SMark Johnston {
557*b59851e9SMark Johnston 	char path[PATH_MAX], buf[BUFSIZ];
558*b59851e9SMark Johnston 	struct kevent ev;
559*b59851e9SMark Johnston 	int kq, pathfd;
560*b59851e9SMark Johnston 
561*b59851e9SMark Johnston 	snprintf(path, sizeof(path), "path_fifo.XXXXXX");
562*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
563*b59851e9SMark Johnston 
564*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(mkfifo(path, 0666) == 0, FMT_ERR("mkfifo"));
565*b59851e9SMark Johnston 
566*b59851e9SMark Johnston 	pathfd = open(path, O_PATH);
567*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
568*b59851e9SMark Johnston 	memset(buf, 0, sizeof(buf));
569*b59851e9SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, write(pathfd, buf, sizeof(buf)));
570*b59851e9SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, read(pathfd, buf, sizeof(buf)));
571*b59851e9SMark Johnston 
572*b59851e9SMark Johnston 	kq = kqueue();
573*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
574*b59851e9SMark Johnston 	EV_SET(&ev, pathfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
575*b59851e9SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
576*b59851e9SMark Johnston 
577*b59851e9SMark Johnston 	CHECKED_CLOSE(pathfd);
578*b59851e9SMark Johnston }
579*b59851e9SMark Johnston 
5803a248c84SMark Johnston /* Files may be unlinked using a path fd. */
5813a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_funlinkat);
5823a248c84SMark Johnston ATF_TC_BODY(path_funlinkat, tc)
5833a248c84SMark Johnston {
5843a248c84SMark Johnston 	char path[PATH_MAX];
5853a248c84SMark Johnston 	struct stat sb;
5863a248c84SMark Johnston 	int pathfd;
5873a248c84SMark Johnston 
5883a248c84SMark Johnston 	mktfile(path, "path_rights.XXXXXX");
5893a248c84SMark Johnston 
5903a248c84SMark Johnston 	pathfd = open(path, O_PATH);
5913a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
5923a248c84SMark Johnston 
5933a248c84SMark Johnston 	ATF_REQUIRE_MSG(funlinkat(AT_FDCWD, path, pathfd, 0) == 0,
5943a248c84SMark Johnston 	    FMT_ERR("funlinkat"));
5953a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ENOENT, stat(path, &sb) == -1);
5963a248c84SMark Johnston 
5973a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
5983a248c84SMark Johnston }
5993a248c84SMark Johnston 
6003a248c84SMark Johnston /* Verify that various I/O operations fail on an O_PATH descriptor. */
6013a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_io);
6023a248c84SMark Johnston ATF_TC_BODY(path_io, tc)
6033a248c84SMark Johnston {
6043a248c84SMark Johnston 	char path[PATH_MAX], path2[PATH_MAX];
6053a248c84SMark Johnston 	char buf[BUFSIZ];
6063a248c84SMark Johnston 	struct iovec iov;
6073a248c84SMark Johnston 	int error, fd, pathfd, sd[2];
6083a248c84SMark Johnston 
6093a248c84SMark Johnston 	/* It shouldn't be possible to create new files with O_PATH. */
6103a248c84SMark Johnston 	snprintf(path, sizeof(path), "path_io.XXXXXX");
6113a248c84SMark Johnston 	ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
6123a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ENOENT, open(path, O_PATH | O_CREAT, 0600) < 0);
6133a248c84SMark Johnston 
6143a248c84SMark Johnston 	/* Create a non-empty file for use in the rest of the tests. */
6153a248c84SMark Johnston 	mktfile(path, "path_io.XXXXXX");
6163a248c84SMark Johnston 
6173a248c84SMark Johnston 	pathfd = open(path, O_PATH);
6183a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
6193a248c84SMark Johnston 
6203a248c84SMark Johnston 	/* Make sure that basic I/O operations aren't possible. */
6213a248c84SMark Johnston 	iov.iov_base = path;
6223a248c84SMark Johnston 	iov.iov_len = strlen(path);
6233a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6243a248c84SMark Johnston 	    write(pathfd, iov.iov_base, iov.iov_len) == -1);
6253a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6263a248c84SMark Johnston 	    pwrite(pathfd, iov.iov_base, iov.iov_len, 0) == -1);
6273a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6283a248c84SMark Johnston 	    writev(pathfd, &iov, 1) == -1);
6293a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6303a248c84SMark Johnston 	    pwritev(pathfd, &iov, 1, 0) == -1);
6313a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6323a248c84SMark Johnston 	    read(pathfd, path, 1) == -1);
6333a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6343a248c84SMark Johnston 	    pread(pathfd, path, 1, 0) == -1);
6353a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6363a248c84SMark Johnston 	    readv(pathfd, &iov, 1) == -1);
6373a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6383a248c84SMark Johnston 	    preadv(pathfd, &iov, 1, 0) == -1);
6393a248c84SMark Johnston 
6403a248c84SMark Johnston 	/* copy_file_range() should not be permitted. */
6413a248c84SMark Johnston 	mktfile(path2, "path_io.XXXXXX");
6423a248c84SMark Johnston 	fd = open(path2, O_RDWR);
6433a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6443a248c84SMark Johnston 	    copy_file_range(fd, NULL, pathfd, NULL, sizeof(buf), 0) == -1);
6453a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6463a248c84SMark Johnston 	    copy_file_range(pathfd, NULL, fd, NULL, sizeof(buf), 0) == -1);
6473a248c84SMark Johnston 	CHECKED_CLOSE(fd);
6483a248c84SMark Johnston 
6493a248c84SMark Johnston 	/* sendfile() should not be permitted. */
6503a248c84SMark Johnston 	ATF_REQUIRE_MSG(socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) == 0,
6513a248c84SMark Johnston 	    FMT_ERR("socketpair"));
6523a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
6533a248c84SMark Johnston 	    sendfile(pathfd, sd[0], 0, 0, NULL, NULL, 0));
6543a248c84SMark Johnston 	CHECKED_CLOSE(sd[0]);
6553a248c84SMark Johnston 	CHECKED_CLOSE(sd[1]);
6563a248c84SMark Johnston 
6573a248c84SMark Johnston 	/* No seeking. */
6583a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ESPIPE,
6593a248c84SMark Johnston 	    lseek(pathfd, 0, SEEK_SET) == -1);
6603a248c84SMark Johnston 
6613a248c84SMark Johnston 	/* No operations on the file extent. */
6623a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EINVAL,
6633a248c84SMark Johnston 	    ftruncate(pathfd, 0) == -1);
6643a248c84SMark Johnston 	error = posix_fallocate(pathfd, 0, sizeof(buf) * 2);
6653a248c84SMark Johnston 	ATF_REQUIRE_MSG(error == ESPIPE, "posix_fallocate() returned %d", error);
6663a248c84SMark Johnston 	error = posix_fadvise(pathfd, 0, sizeof(buf), POSIX_FADV_NORMAL);
6673a248c84SMark Johnston 	ATF_REQUIRE_MSG(error == ESPIPE, "posix_fadvise() returned %d", error);
6683a248c84SMark Johnston 
6693a248c84SMark Johnston 	/* mmap() is not allowed. */
6703a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ENODEV,
6713a248c84SMark Johnston 	    mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, pathfd, 0) ==
6723a248c84SMark Johnston 	    MAP_FAILED);
6733a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ENODEV,
6743a248c84SMark Johnston 	    mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_SHARED, pathfd, 0) ==
6753a248c84SMark Johnston 	    MAP_FAILED);
6763a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(ENODEV,
6773a248c84SMark Johnston 	    mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, pathfd, 0) ==
6783a248c84SMark Johnston 	    MAP_FAILED);
6793a248c84SMark Johnston 
6803a248c84SMark Johnston 	/* No fsync() or fdatasync(). */
6813a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fsync(pathfd) == -1);
6823a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fdatasync(pathfd) == -1);
6833a248c84SMark Johnston 
6843a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
6853a248c84SMark Johnston }
6863a248c84SMark Johnston 
6873a248c84SMark Johnston /* ioctl(2) is not permitted on path fds. */
6883a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_ioctl);
6893a248c84SMark Johnston ATF_TC_BODY(path_ioctl, tc)
6903a248c84SMark Johnston {
6913a248c84SMark Johnston 	char path[PATH_MAX];
6923a248c84SMark Johnston 	struct mem_extract me;
6933a248c84SMark Johnston 	int pathfd, val;
6943a248c84SMark Johnston 
6953a248c84SMark Johnston 	mktfile(path, "path_ioctl.XXXXXX");
6963a248c84SMark Johnston 
6973a248c84SMark Johnston 	/* Standard file descriptor ioctls should fail. */
6983a248c84SMark Johnston 	pathfd = open(path, O_PATH);
6993a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
7003a248c84SMark Johnston 
7013a248c84SMark Johnston 	val = 0;
7023a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONBIO, &val) == -1);
7033a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONREAD, &val) == -1);
7043a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONWRITE, &val) == -1);
7053a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONSPACE, &val) == -1);
7063a248c84SMark Johnston 
7073a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
7083a248c84SMark Johnston 
7093a248c84SMark Johnston 	/* Device ioctls should fail. */
7103a248c84SMark Johnston 	pathfd = open("/dev/mem", O_PATH);
7113a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
7123a248c84SMark Johnston 
7133a248c84SMark Johnston 	me.me_vaddr = (uintptr_t)&me;
7143a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, MEM_EXTRACT_PADDR, &me) == -1);
7153a248c84SMark Johnston 
7163a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
7173a248c84SMark Johnston }
7183a248c84SMark Johnston 
7193a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_lock);
7203a248c84SMark Johnston ATF_TC_BODY(path_lock, tc)
7213a248c84SMark Johnston {
7223a248c84SMark Johnston 	char buf[BUFSIZ], path[PATH_MAX];
7233a248c84SMark Johnston 	struct flock flk;
7243a248c84SMark Johnston 	int fd, pathfd;
7253a248c84SMark Johnston 
7263a248c84SMark Johnston 	snprintf(path, sizeof(path), "path_rights.XXXXXX");
7273a248c84SMark Johnston 	fd = mkostemp(path, O_SHLOCK);
7283a248c84SMark Johnston 	ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("mkostemp"));
7293a248c84SMark Johnston 	memset(buf, 0, sizeof(buf));
7303a248c84SMark Johnston 	ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) == sizeof(buf),
7313a248c84SMark Johnston 	    FMT_ERR("write()"));
7323a248c84SMark Johnston 
7333a248c84SMark Johnston 	/* Verify that O_EXLOCK is ignored when combined with O_PATH. */
7343a248c84SMark Johnston 	pathfd = open(path, O_PATH | O_EXLOCK);
7353a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
7363a248c84SMark Johnston 
7373a248c84SMark Johnston 	CHECKED_CLOSE(fd);
7383a248c84SMark Johnston 
7393a248c84SMark Johnston 	/* flock(2) is prohibited. */
7403a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_SH) == -1);
7413a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_EX) == -1);
7423a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_SH | LOCK_NB) == -1);
7433a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_EX | LOCK_NB) == -1);
7443a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_UN) == -1);
7453a248c84SMark Johnston 
7463a248c84SMark Johnston 	/* fcntl(2) file locks are prohibited. */
7473a248c84SMark Johnston 	memset(&flk, 0, sizeof(flk));
7483a248c84SMark Johnston 	flk.l_whence = SEEK_CUR;
7493a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_GETLK, &flk) == -1);
7503a248c84SMark Johnston 	flk.l_type = F_RDLCK;
7513a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLK, &flk) == -1);
7523a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLKW, &flk) == -1);
7533a248c84SMark Johnston 	flk.l_type = F_WRLCK;
7543a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLK, &flk) == -1);
7553a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLKW, &flk) == -1);
7563a248c84SMark Johnston 
7573a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
7583a248c84SMark Johnston }
7593a248c84SMark Johnston 
7603a248c84SMark Johnston /* Verify that we can send an O_PATH descriptor over a unix socket. */
7613a248c84SMark Johnston ATF_TC_WITHOUT_HEAD(path_rights);
7623a248c84SMark Johnston ATF_TC_BODY(path_rights, tc)
7633a248c84SMark Johnston {
7643a248c84SMark Johnston 	char path[PATH_MAX];
7653a248c84SMark Johnston 	struct cmsghdr *cmsg;
7663a248c84SMark Johnston 	struct msghdr msg;
7673a248c84SMark Johnston 	struct iovec iov;
7683a248c84SMark Johnston 	int flags, pathfd, pathfd_copy, sd[2];
7693a248c84SMark Johnston 	char c;
7703a248c84SMark Johnston 
7713a248c84SMark Johnston 	ATF_REQUIRE_MSG(socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) == 0,
7723a248c84SMark Johnston 	    FMT_ERR("socketpair"));
7733a248c84SMark Johnston 
7743a248c84SMark Johnston 	mktfile(path, "path_rights.XXXXXX");
7753a248c84SMark Johnston 
7763a248c84SMark Johnston 	pathfd = open(path, O_PATH);
7773a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
7783a248c84SMark Johnston 
7793a248c84SMark Johnston 	/* Package up the O_PATH and send it over the socket pair. */
7803a248c84SMark Johnston 	cmsg = malloc(CMSG_SPACE(sizeof(pathfd)));
7813a248c84SMark Johnston 	ATF_REQUIRE_MSG(cmsg != NULL, FMT_ERR("malloc"));
7823a248c84SMark Johnston 
7833a248c84SMark Johnston 	cmsg->cmsg_len = CMSG_LEN(sizeof(pathfd));
7843a248c84SMark Johnston 	cmsg->cmsg_level = SOL_SOCKET;
7853a248c84SMark Johnston 	cmsg->cmsg_type = SCM_RIGHTS;
7863a248c84SMark Johnston 	*(int *)(void *)CMSG_DATA(cmsg) = pathfd;
7873a248c84SMark Johnston 
7883a248c84SMark Johnston 	c = 0;
7893a248c84SMark Johnston 	iov.iov_base = &c;
7903a248c84SMark Johnston 	iov.iov_len = 1;
7913a248c84SMark Johnston 
7923a248c84SMark Johnston 	memset(&msg, 0, sizeof(msg));
7933a248c84SMark Johnston 	msg.msg_iov = &iov;
7943a248c84SMark Johnston 	msg.msg_iovlen = 1;
7953a248c84SMark Johnston 	msg.msg_control = cmsg;
7963a248c84SMark Johnston 	msg.msg_controllen = CMSG_SPACE(sizeof(pathfd));
7973a248c84SMark Johnston 
7983a248c84SMark Johnston 	ATF_REQUIRE_MSG(sendmsg(sd[0], &msg, 0) == sizeof(c),
7993a248c84SMark Johnston 	    FMT_ERR("sendmsg"));
8003a248c84SMark Johnston 
8013a248c84SMark Johnston 	/* Grab the pathfd copy from the other end of the pair. */
8023a248c84SMark Johnston 	memset(&msg, 0, sizeof(msg));
8033a248c84SMark Johnston 	msg.msg_iov = &iov;
8043a248c84SMark Johnston 	msg.msg_iovlen = 1;
8053a248c84SMark Johnston 	msg.msg_control = cmsg;
8063a248c84SMark Johnston 	msg.msg_controllen = CMSG_SPACE(sizeof(pathfd));
8073a248c84SMark Johnston 
8083a248c84SMark Johnston 	ATF_REQUIRE_MSG(recvmsg(sd[1], &msg, 0) == 1,
8093a248c84SMark Johnston 	    FMT_ERR("recvmsg"));
8103a248c84SMark Johnston 	pathfd_copy = *(int *)(void *)CMSG_DATA(cmsg);
8113a248c84SMark Johnston 	ATF_REQUIRE_MSG(pathfd_copy != pathfd,
8123a248c84SMark Johnston 	    "pathfd and pathfd_copy are equal");
8133a248c84SMark Johnston 
8143a248c84SMark Johnston 	/* Verify that the copy has O_PATH properties. */
8153a248c84SMark Johnston 	flags = fcntl(pathfd_copy, F_GETFL);
8163a248c84SMark Johnston 	ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
8173a248c84SMark Johnston 	ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH is not set");
8183a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
8193a248c84SMark Johnston 	    read(pathfd_copy, &c, 1) == -1);
8203a248c84SMark Johnston 	ATF_REQUIRE_ERRNO(EBADF,
8213a248c84SMark Johnston 	    write(pathfd_copy, &c, 1) == -1);
8223a248c84SMark Johnston 
8233a248c84SMark Johnston 	CHECKED_CLOSE(pathfd);
8243a248c84SMark Johnston 	CHECKED_CLOSE(pathfd_copy);
8253a248c84SMark Johnston 	CHECKED_CLOSE(sd[0]);
8263a248c84SMark Johnston 	CHECKED_CLOSE(sd[1]);
8273a248c84SMark Johnston }
8283a248c84SMark Johnston 
829*b59851e9SMark Johnston /* Verify that a local socket can't be opened with O_PATH. */
830*b59851e9SMark Johnston ATF_TC_WITHOUT_HEAD(path_unix);
831*b59851e9SMark Johnston ATF_TC_BODY(path_unix, tc)
832*b59851e9SMark Johnston {
833*b59851e9SMark Johnston 	char path[PATH_MAX];
834*b59851e9SMark Johnston 	struct sockaddr_un sun;
835*b59851e9SMark Johnston 	int pathfd, sd;
836*b59851e9SMark Johnston 
837*b59851e9SMark Johnston 	snprintf(path, sizeof(path), "path_unix.XXXXXX");
838*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
839*b59851e9SMark Johnston 
840*b59851e9SMark Johnston 	sd = socket(PF_LOCAL, SOCK_STREAM, 0);
841*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(sd >= 0, FMT_ERR("socket"));
842*b59851e9SMark Johnston 
843*b59851e9SMark Johnston 	memset(&sun, 0, sizeof(sun));
844*b59851e9SMark Johnston 	sun.sun_family = PF_LOCAL;
845*b59851e9SMark Johnston 	(void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
846*b59851e9SMark Johnston 	ATF_REQUIRE_MSG(bind(sd, (struct sockaddr *)&sun, SUN_LEN(&sun)) == 0,
847*b59851e9SMark Johnston 	    FMT_ERR("bind"));
848*b59851e9SMark Johnston 
849*b59851e9SMark Johnston 	pathfd = open(path, O_RDONLY);
850*b59851e9SMark Johnston 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, pathfd < 0);
851*b59851e9SMark Johnston 
852*b59851e9SMark Johnston 	CHECKED_CLOSE(sd);
853*b59851e9SMark Johnston }
854*b59851e9SMark Johnston 
8553a248c84SMark Johnston ATF_TP_ADD_TCS(tp)
8563a248c84SMark Johnston {
8573a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_access);
8583a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_aio);
8593a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_capsicum);
860*b59851e9SMark Johnston 	ATF_TP_ADD_TC(tp, path_coredump);
8613a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_directory);
8623a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_directory_not_root);
8633a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_empty);
8643a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_empty_not_root);
8653a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_empty_root);
8663a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_event);
8673a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_fcntl);
8683a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_fexecve);
869*b59851e9SMark Johnston 	ATF_TP_ADD_TC(tp, path_fifo);
8703a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_funlinkat);
8713a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_io);
8723a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_ioctl);
8733a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_lock);
8743a248c84SMark Johnston 	ATF_TP_ADD_TC(tp, path_rights);
875*b59851e9SMark Johnston 	ATF_TP_ADD_TC(tp, path_unix);
8763a248c84SMark Johnston 
8773a248c84SMark Johnston 	return (atf_no_error());
8783a248c84SMark Johnston }
879