2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021 The FreeBSD Foundation
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Basic regression tests for handling of O_PATH descriptors.
35 #include <sys/param.h>
36 #include <sys/capsicum.h>
37 #include <sys/event.h>
38 #include <sys/ioctl.h>
39 #include <sys/memrange.h>
41 #include <sys/ptrace.h>
42 #include <sys/socket.h>
61 #define FMT_ERR(s) s ": %s", strerror(errno)
63 #define CHECKED_CLOSE(fd) \
64 ATF_REQUIRE_MSG(close(fd) == 0, FMT_ERR("close"))
66 /* Create a temporary regular file containing some data. */
68 mktfile(char path[PATH_MAX], const char *template)
73 snprintf(path, PATH_MAX, "%s", template);
75 ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("mkstemp"));
76 memset(buf, 0, sizeof(buf));
77 ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) == sizeof(buf),
82 /* Make a temporary directory. */
84 mktdir(char path[PATH_MAX], const char *template)
86 snprintf(path, PATH_MAX, "%s", template);
87 ATF_REQUIRE_MSG(mkdtemp(path) == path, FMT_ERR("mkdtemp"));
90 /* Wait for a child process to exit with status 0. */
92 waitchild(pid_t child, int exstatus)
96 error = waitpid(child, &status, 0);
97 ATF_REQUIRE_MSG(error != -1, FMT_ERR("waitpid"));
98 ATF_REQUIRE_MSG(WIFEXITED(status), "child exited abnormally, status %d",
100 ATF_REQUIRE_MSG(WEXITSTATUS(status) == exstatus,
101 "child exit status is %d, expected %d",
102 WEXITSTATUS(status), exstatus);
105 ATF_TC_WITHOUT_HEAD(path_access);
106 ATF_TC_BODY(path_access, tc)
110 struct timespec ts[2];
111 struct timeval tv[2];
114 mktfile(path, "path_access.XXXXXX");
116 pathfd = open(path, O_PATH);
117 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
119 ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0666) == -1);
120 ATF_REQUIRE_ERRNO(EBADF, fchown(pathfd, getuid(), getgid()) == -1);
121 ATF_REQUIRE_ERRNO(EBADF, fchflags(pathfd, UF_NODUMP) == -1);
122 memset(tv, 0, sizeof(tv));
123 ATF_REQUIRE_ERRNO(EBADF, futimes(pathfd, tv) == -1);
124 memset(ts, 0, sizeof(ts));
125 ATF_REQUIRE_ERRNO(EBADF, futimens(pathfd, ts) == -1);
127 /* fpathconf(2) and fstat(2) are permitted. */
128 ATF_REQUIRE_MSG(fstat(pathfd, &sb) == 0, FMT_ERR("fstat"));
129 ATF_REQUIRE_MSG(fpathconf(pathfd, _PC_LINK_MAX) != -1,
130 FMT_ERR("fpathconf"));
132 CHECKED_CLOSE(pathfd);
135 /* Basic tests to verify that AIO operations fail. */
136 ATF_TC_WITHOUT_HEAD(path_aio);
137 ATF_TC_BODY(path_aio, tc)
140 char buf[BUFSIZ], path[PATH_MAX];
143 mktfile(path, "path_aio.XXXXXX");
145 pathfd = open(path, O_PATH);
146 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
148 memset(&aio, 0, sizeof(aio));
150 aio.aio_nbytes = sizeof(buf);
151 aio.aio_fildes = pathfd;
154 ATF_REQUIRE_ERRNO(EBADF, aio_read(&aio) == -1);
155 ATF_REQUIRE_ERRNO(EBADF, aio_write(&aio) == -1);
156 ATF_REQUIRE_ERRNO(EBADF, aio_fsync(O_SYNC, &aio) == -1);
157 ATF_REQUIRE_ERRNO(EBADF, aio_fsync(O_DSYNC, &aio) == -1);
159 CHECKED_CLOSE(pathfd);
162 /* Basic tests to verify that Capsicum restrictions apply to path fds. */
163 ATF_TC_WITHOUT_HEAD(path_capsicum);
164 ATF_TC_BODY(path_capsicum, tc)
171 mktfile(path, "path_capsicum.XXXXXX");
173 /* Make sure that filesystem namespace restrictions apply to O_PATH. */
175 ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
177 if (cap_enter() != 0)
179 if (open(path, O_PATH) >= 0)
181 if (errno != ECAPMODE)
183 if (open("/usr/bin/true", O_PATH | O_EXEC) >= 0)
185 if (errno != ECAPMODE)
191 /* Make sure that CAP_FEXECVE is required. */
193 ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
195 truefd = open("/usr/bin/true", O_PATH | O_EXEC);
198 cap_rights_init(&rights);
199 if (cap_rights_limit(truefd, &rights) != 0)
201 (void)fexecve(truefd,
202 (char * const[]){__DECONST(char *, "/usr/bin/true"), NULL},
204 if (errno != ENOTCAPABLE)
211 /* Make sure that ptrace(PT_COREDUMP) cannot be used to write to a path fd. */
212 ATF_TC_WITHOUT_HEAD(path_coredump);
213 ATF_TC_BODY(path_coredump, tc)
216 struct ptrace_coredump pc;
217 int error, pathfd, status;
220 mktdir(path, "path_coredump.XXXXXX");
223 ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
229 pathfd = open(path, O_PATH);
230 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
232 error = ptrace(PT_ATTACH, child, 0, 0);
233 ATF_REQUIRE_MSG(error == 0, FMT_ERR("ptrace"));
234 error = waitpid(child, &status, 0);
235 ATF_REQUIRE_MSG(error != -1, FMT_ERR("waitpid"));
236 ATF_REQUIRE_MSG(WIFSTOPPED(status), "unexpected status %d", status);
241 error = ptrace(PT_COREDUMP, child, (void *)&pc, sizeof(pc));
242 ATF_REQUIRE_ERRNO(EBADF, error == -1);
244 error = ptrace(PT_DETACH, child, 0, 0);
245 ATF_REQUIRE_MSG(error == 0, FMT_ERR("ptrace"));
247 ATF_REQUIRE_MSG(kill(child, SIGKILL) == 0, FMT_ERR("kill"));
249 CHECKED_CLOSE(pathfd);
252 /* Verify operations on directory path descriptors. */
253 ATF_TC_WITHOUT_HEAD(path_directory);
254 ATF_TC_BODY(path_directory, tc)
261 mktdir(path, "path_directory.XXXXXX");
263 pathfd = open(path, O_PATH | O_DIRECTORY);
264 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
266 /* Should not be possible to list directory entries. */
267 ATF_REQUIRE_ERRNO(EBADF,
268 getdirentries(pathfd, (char *)&de, sizeof(de), NULL) == -1);
270 /* It should be possible to create files under pathfd. */
271 fd = openat(pathfd, "test", O_RDWR | O_CREAT, 0600);
272 ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));
273 ATF_REQUIRE_MSG(fstatat(pathfd, "test", &sb, 0) == 0,
277 /* ... but doing so requires write access. */
278 if (geteuid() != 0) {
279 ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0500) == -1);
280 ATF_REQUIRE_MSG(chmod(path, 0500) == 0, FMT_ERR("chmod"));
281 ATF_REQUIRE_ERRNO(EACCES,
282 openat(pathfd, "test2", O_RDWR | O_CREAT, 0600) < 0);
285 /* fchdir(2) is permitted. */
286 ATF_REQUIRE_MSG(fchdir(pathfd) == 0, FMT_ERR("fchdir"));
288 CHECKED_CLOSE(pathfd);
291 /* Verify access permission checking for a directory path fd. */
292 ATF_TC_WITH_CLEANUP(path_directory_not_root);
293 ATF_TC_HEAD(path_directory_not_root, tc)
295 atf_tc_set_md_var(tc, "require.user", "unprivileged");
297 ATF_TC_BODY(path_directory_not_root, tc)
302 mktdir(path, "path_directory.XXXXXX");
304 pathfd = open(path, O_PATH | O_DIRECTORY);
305 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
307 ATF_REQUIRE_ERRNO(EBADF, fchmod(pathfd, 0500) == -1);
308 ATF_REQUIRE_MSG(chmod(path, 0500) == 0, FMT_ERR("chmod"));
309 ATF_REQUIRE_ERRNO(EACCES,
310 openat(pathfd, "test2", O_RDWR | O_CREAT, 0600) < 0);
312 CHECKED_CLOSE(pathfd);
314 ATF_TC_CLEANUP(path_directory_not_root, tc)
318 /* Validate system calls that handle AT_EMPTY_PATH. */
319 ATF_TC_WITHOUT_HEAD(path_empty);
320 ATF_TC_BODY(path_empty, tc)
323 struct timespec ts[2];
327 mktfile(path, "path_empty.XXXXXX");
329 pathfd = open(path, O_PATH);
330 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
332 /* Various *at operations should work on path fds. */
333 ATF_REQUIRE_MSG(faccessat(pathfd, "", F_OK, AT_EMPTY_PATH) == 0,
334 FMT_ERR("faccessat"));
335 ATF_REQUIRE_MSG(chflagsat(pathfd, "", UF_NODUMP, AT_EMPTY_PATH) == 0,
336 FMT_ERR("chflagsat"));
337 ATF_REQUIRE_MSG(fchmodat(pathfd, "", 0600, AT_EMPTY_PATH) == 0,
338 FMT_ERR("fchmodat"));
339 ATF_REQUIRE_MSG(fchownat(pathfd, "", getuid(), getgid(),
340 AT_EMPTY_PATH) == 0, FMT_ERR("fchownat"));
341 ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
343 ATF_REQUIRE_MSG(sb.st_size == BUFSIZ,
344 "unexpected size %ju", (uintmax_t)sb.st_size);
345 memset(ts, 0, sizeof(ts));
346 ATF_REQUIRE_MSG(utimensat(pathfd, "", ts, AT_EMPTY_PATH) == 0,
347 FMT_ERR("utimensat"));
349 CHECKED_CLOSE(pathfd);
352 /* Verify that various operations on a path fd have access checks. */
353 ATF_TC_WITH_CLEANUP(path_empty_not_root);
354 ATF_TC_HEAD(path_empty_not_root, tc)
356 atf_tc_set_md_var(tc, "require.user", "unprivileged");
358 ATF_TC_BODY(path_empty_not_root, tc)
362 pathfd = open("/dev/null", O_PATH);
363 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
365 ATF_REQUIRE_ERRNO(EPERM,
366 chflagsat(pathfd, "", UF_NODUMP, AT_EMPTY_PATH) == -1);
367 ATF_REQUIRE_ERRNO(EPERM,
368 fchownat(pathfd, "", getuid(), getgid(), AT_EMPTY_PATH) == -1);
369 ATF_REQUIRE_ERRNO(EPERM,
370 fchmodat(pathfd, "", 0600, AT_EMPTY_PATH) == -1);
371 ATF_REQUIRE_ERRNO(EPERM,
372 linkat(pathfd, "", AT_FDCWD, "test", AT_EMPTY_PATH) == -1);
374 CHECKED_CLOSE(pathfd);
376 ATF_TC_CLEANUP(path_empty_not_root, tc)
380 /* Test linkat(2) with AT_EMPTY_PATH, which requires privileges. */
381 ATF_TC_WITH_CLEANUP(path_empty_root);
382 ATF_TC_HEAD(path_empty_root, tc)
384 atf_tc_set_md_var(tc, "require.user", "root");
386 ATF_TC_BODY(path_empty_root, tc)
392 mktfile(path, "path_empty_root.XXXXXX");
394 pathfd = open(path, O_PATH);
395 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
396 ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
399 ATF_REQUIRE_MSG(linkat(pathfd, "", AT_FDCWD, "test", AT_EMPTY_PATH) ==
400 0, FMT_ERR("linkat"));
401 ATF_REQUIRE_MSG(fstatat(AT_FDCWD, "test", &sb2, 0) == 0,
403 ATF_REQUIRE_MSG(sb.st_dev == sb2.st_dev, "st_dev mismatch");
404 ATF_REQUIRE_MSG(sb.st_ino == sb2.st_ino, "st_ino mismatch");
406 CHECKED_CLOSE(pathfd);
409 ATF_TC_CLEANUP(path_empty_root, tc)
413 /* poll(2) never returns an event for path fds, but kevent(2) does. */
414 ATF_TC_WITHOUT_HEAD(path_event);
415 ATF_TC_BODY(path_event, tc)
417 char buf[BUFSIZ], path[PATH_MAX];
419 struct pollfd pollfd;
422 mktfile(path, "path_event.XXXXXX");
424 pathfd = open(path, O_PATH);
425 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
427 /* poll(2) should return POLLNVAL. */
429 pollfd.events = POLLIN;
431 ATF_REQUIRE_MSG(poll(&pollfd, 1, 0) == 1, FMT_ERR("poll"));
432 ATF_REQUIRE_MSG(pollfd.revents == POLLNVAL, "unexpected revents %x",
434 pollfd.events = POLLOUT;
436 ATF_REQUIRE_MSG(poll(&pollfd, 1, 0) == 1, FMT_ERR("poll"));
437 ATF_REQUIRE_MSG(pollfd.revents == POLLNVAL, "unexpected revents %x",
440 /* Try to get a EVFILT_READ event through a path fd. */
442 ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
443 EV_SET(&ev, pathfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
444 ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
446 ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
448 ATF_REQUIRE_MSG((ev.flags & EV_ERROR) == 0, "EV_ERROR is set");
449 ATF_REQUIRE_MSG(ev.data == sizeof(buf),
450 "data is %jd", (intmax_t)ev.data);
451 EV_SET(&ev, pathfd, EVFILT_READ, EV_DELETE, 0, 0, 0);
452 ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
455 /* Try to get a EVFILT_VNODE/NOTE_LINK event through a path fd. */
456 EV_SET(&ev, pathfd, EVFILT_VNODE, EV_ADD | EV_ENABLE, NOTE_LINK, 0, 0);
457 ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
459 ATF_REQUIRE_MSG(funlinkat(AT_FDCWD, path, pathfd, 0) == 0,
460 FMT_ERR("funlinkat"));
461 ATF_REQUIRE_MSG(kevent(kq, NULL, 0, &ev, 1, NULL) == 1,
463 EV_SET(&ev, pathfd, EVFILT_VNODE, EV_DELETE, 0, 0, 0);
464 ATF_REQUIRE_MSG(kevent(kq, &ev, 1, NULL, 0, NULL) == 0,
468 CHECKED_CLOSE(pathfd);
471 /* Check various fcntl(2) operations on a path desriptor. */
472 ATF_TC_WITHOUT_HEAD(path_fcntl);
473 ATF_TC_BODY(path_fcntl, tc)
476 int flags, pathfd, pathfd2;
478 mktfile(path, "path_fcntl.XXXXXX");
480 pathfd = open(path, O_PATH);
481 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
483 /* O_PATH should appear in the fd flags. */
484 flags = fcntl(pathfd, F_GETFL);
485 ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
486 ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
488 ATF_REQUIRE_ERRNO(EBADF,
489 fcntl(pathfd, F_SETFL, flags & ~O_PATH));
490 ATF_REQUIRE_ERRNO(EBADF,
491 fcntl(pathfd, F_SETFL, flags | O_APPEND));
493 /* A dup'ed O_PATH fd had better have O_PATH set too. */
494 pathfd2 = fcntl(pathfd, F_DUPFD, 0);
495 ATF_REQUIRE_MSG(pathfd2 >= 0, FMT_ERR("fcntl"));
496 flags = fcntl(pathfd2, F_GETFL);
497 ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
498 ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
499 CHECKED_CLOSE(pathfd2);
501 /* Double check with dup(2). */
502 pathfd2 = dup(pathfd);
503 ATF_REQUIRE_MSG(pathfd2 >= 0, FMT_ERR("dup"));
504 flags = fcntl(pathfd2, F_GETFL);
505 ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
506 ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH not set");
507 CHECKED_CLOSE(pathfd2);
509 /* It should be possible to set O_CLOEXEC. */
510 ATF_REQUIRE_MSG(fcntl(pathfd, F_SETFD, FD_CLOEXEC) == 0,
512 ATF_REQUIRE_MSG(fcntl(pathfd, F_GETFD) == FD_CLOEXEC,
515 CHECKED_CLOSE(pathfd);
518 /* Verify that we can execute a file opened with O_PATH. */
519 ATF_TC_WITHOUT_HEAD(path_fexecve);
520 ATF_TC_BODY(path_fexecve, tc)
527 ATF_REQUIRE_MSG(child != -1, FMT_ERR("fork"));
529 pathfd = open("/usr/bin/true", O_PATH | O_EXEC);
533 (char * const[]){__DECONST(char *, "/usr/bin/true"), NULL},
540 * Also verify that access permissions are checked when opening with
543 snprintf(path, sizeof(path), "path_fexecve.XXXXXX");
544 ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
546 fd = open(path, O_CREAT | O_RDONLY, 0600);
547 ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open"));
549 pathfd = open(path, O_PATH | O_EXEC);
550 ATF_REQUIRE_ERRNO(EACCES, pathfd < 0);
553 /* Make sure that O_PATH restrictions apply to named pipes as well. */
554 ATF_TC_WITHOUT_HEAD(path_fifo);
555 ATF_TC_BODY(path_fifo, tc)
557 char path[PATH_MAX], buf[BUFSIZ];
561 snprintf(path, sizeof(path), "path_fifo.XXXXXX");
562 ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
564 ATF_REQUIRE_MSG(mkfifo(path, 0666) == 0, FMT_ERR("mkfifo"));
566 pathfd = open(path, O_PATH);
567 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
568 memset(buf, 0, sizeof(buf));
569 ATF_REQUIRE_ERRNO(EBADF, write(pathfd, buf, sizeof(buf)));
570 ATF_REQUIRE_ERRNO(EBADF, read(pathfd, buf, sizeof(buf)));
573 ATF_REQUIRE_MSG(kq >= 0, FMT_ERR("kqueue"));
574 EV_SET(&ev, pathfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
575 ATF_REQUIRE_ERRNO(EBADF, kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
577 CHECKED_CLOSE(pathfd);
580 /* Files may be unlinked using a path fd. */
581 ATF_TC_WITHOUT_HEAD(path_funlinkat);
582 ATF_TC_BODY(path_funlinkat, tc)
588 mktfile(path, "path_rights.XXXXXX");
590 pathfd = open(path, O_PATH);
591 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
593 ATF_REQUIRE_MSG(funlinkat(AT_FDCWD, path, pathfd, 0) == 0,
594 FMT_ERR("funlinkat"));
595 ATF_REQUIRE_ERRNO(ENOENT, stat(path, &sb) == -1);
597 CHECKED_CLOSE(pathfd);
600 /* Verify that various I/O operations fail on an O_PATH descriptor. */
601 ATF_TC_WITHOUT_HEAD(path_io);
602 ATF_TC_BODY(path_io, tc)
604 char path[PATH_MAX], path2[PATH_MAX];
607 int error, fd, pathfd, sd[2];
609 /* It shouldn't be possible to create new files with O_PATH. */
610 snprintf(path, sizeof(path), "path_io.XXXXXX");
611 ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
612 ATF_REQUIRE_ERRNO(ENOENT, open(path, O_PATH | O_CREAT, 0600) < 0);
614 /* Create a non-empty file for use in the rest of the tests. */
615 mktfile(path, "path_io.XXXXXX");
617 pathfd = open(path, O_PATH);
618 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
620 /* Make sure that basic I/O operations aren't possible. */
622 iov.iov_len = strlen(path);
623 ATF_REQUIRE_ERRNO(EBADF,
624 write(pathfd, iov.iov_base, iov.iov_len) == -1);
625 ATF_REQUIRE_ERRNO(EBADF,
626 pwrite(pathfd, iov.iov_base, iov.iov_len, 0) == -1);
627 ATF_REQUIRE_ERRNO(EBADF,
628 writev(pathfd, &iov, 1) == -1);
629 ATF_REQUIRE_ERRNO(EBADF,
630 pwritev(pathfd, &iov, 1, 0) == -1);
631 ATF_REQUIRE_ERRNO(EBADF,
632 read(pathfd, path, 1) == -1);
633 ATF_REQUIRE_ERRNO(EBADF,
634 pread(pathfd, path, 1, 0) == -1);
635 ATF_REQUIRE_ERRNO(EBADF,
636 readv(pathfd, &iov, 1) == -1);
637 ATF_REQUIRE_ERRNO(EBADF,
638 preadv(pathfd, &iov, 1, 0) == -1);
640 /* copy_file_range() should not be permitted. */
641 mktfile(path2, "path_io.XXXXXX");
642 fd = open(path2, O_RDWR);
643 ATF_REQUIRE_ERRNO(EBADF,
644 copy_file_range(fd, NULL, pathfd, NULL, sizeof(buf), 0) == -1);
645 ATF_REQUIRE_ERRNO(EBADF,
646 copy_file_range(pathfd, NULL, fd, NULL, sizeof(buf), 0) == -1);
649 /* sendfile() should not be permitted. */
650 ATF_REQUIRE_MSG(socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) == 0,
651 FMT_ERR("socketpair"));
652 ATF_REQUIRE_ERRNO(EBADF,
653 sendfile(pathfd, sd[0], 0, 0, NULL, NULL, 0));
654 CHECKED_CLOSE(sd[0]);
655 CHECKED_CLOSE(sd[1]);
658 ATF_REQUIRE_ERRNO(ESPIPE,
659 lseek(pathfd, 0, SEEK_SET) == -1);
661 /* No operations on the file extent. */
662 ATF_REQUIRE_ERRNO(EINVAL,
663 ftruncate(pathfd, 0) == -1);
664 error = posix_fallocate(pathfd, 0, sizeof(buf) * 2);
665 ATF_REQUIRE_MSG(error == ESPIPE, "posix_fallocate() returned %d", error);
666 error = posix_fadvise(pathfd, 0, sizeof(buf), POSIX_FADV_NORMAL);
667 ATF_REQUIRE_MSG(error == ESPIPE, "posix_fadvise() returned %d", error);
669 /* mmap() is not allowed. */
670 ATF_REQUIRE_ERRNO(ENODEV,
671 mmap(NULL, PAGE_SIZE, PROT_READ, MAP_SHARED, pathfd, 0) ==
673 ATF_REQUIRE_ERRNO(ENODEV,
674 mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_SHARED, pathfd, 0) ==
676 ATF_REQUIRE_ERRNO(ENODEV,
677 mmap(NULL, PAGE_SIZE, PROT_READ, MAP_PRIVATE, pathfd, 0) ==
680 /* No fsync() or fdatasync(). */
681 ATF_REQUIRE_ERRNO(EBADF, fsync(pathfd) == -1);
682 ATF_REQUIRE_ERRNO(EBADF, fdatasync(pathfd) == -1);
684 CHECKED_CLOSE(pathfd);
687 /* ioctl(2) is not permitted on path fds. */
688 ATF_TC_WITHOUT_HEAD(path_ioctl);
689 ATF_TC_BODY(path_ioctl, tc)
692 struct mem_extract me;
695 mktfile(path, "path_ioctl.XXXXXX");
697 /* Standard file descriptor ioctls should fail. */
698 pathfd = open(path, O_PATH);
699 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
702 ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONBIO, &val) == -1);
703 ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONREAD, &val) == -1);
704 ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONWRITE, &val) == -1);
705 ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, FIONSPACE, &val) == -1);
707 CHECKED_CLOSE(pathfd);
709 /* Device ioctls should fail. */
710 pathfd = open("/dev/mem", O_PATH);
711 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
713 me.me_vaddr = (uintptr_t)&me;
714 ATF_REQUIRE_ERRNO(EBADF, ioctl(pathfd, MEM_EXTRACT_PADDR, &me) == -1);
716 CHECKED_CLOSE(pathfd);
719 ATF_TC_WITHOUT_HEAD(path_lock);
720 ATF_TC_BODY(path_lock, tc)
722 char buf[BUFSIZ], path[PATH_MAX];
726 snprintf(path, sizeof(path), "path_rights.XXXXXX");
727 fd = mkostemp(path, O_SHLOCK);
728 ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("mkostemp"));
729 memset(buf, 0, sizeof(buf));
730 ATF_REQUIRE_MSG(write(fd, buf, sizeof(buf)) == sizeof(buf),
733 /* Verify that O_EXLOCK is ignored when combined with O_PATH. */
734 pathfd = open(path, O_PATH | O_EXLOCK);
735 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
739 /* flock(2) is prohibited. */
740 ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_SH) == -1);
741 ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_EX) == -1);
742 ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_SH | LOCK_NB) == -1);
743 ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_EX | LOCK_NB) == -1);
744 ATF_REQUIRE_ERRNO(EOPNOTSUPP, flock(pathfd, LOCK_UN) == -1);
746 /* fcntl(2) file locks are prohibited. */
747 memset(&flk, 0, sizeof(flk));
748 flk.l_whence = SEEK_CUR;
749 ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_GETLK, &flk) == -1);
750 flk.l_type = F_RDLCK;
751 ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLK, &flk) == -1);
752 ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLKW, &flk) == -1);
753 flk.l_type = F_WRLCK;
754 ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLK, &flk) == -1);
755 ATF_REQUIRE_ERRNO(EBADF, fcntl(pathfd, F_SETLKW, &flk) == -1);
757 CHECKED_CLOSE(pathfd);
760 /* Verify that we can send an O_PATH descriptor over a unix socket. */
761 ATF_TC_WITHOUT_HEAD(path_rights);
762 ATF_TC_BODY(path_rights, tc)
765 struct cmsghdr *cmsg;
768 int flags, pathfd, pathfd_copy, sd[2];
771 ATF_REQUIRE_MSG(socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) == 0,
772 FMT_ERR("socketpair"));
774 mktfile(path, "path_rights.XXXXXX");
776 pathfd = open(path, O_PATH);
777 ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
779 /* Package up the O_PATH and send it over the socket pair. */
780 cmsg = malloc(CMSG_SPACE(sizeof(pathfd)));
781 ATF_REQUIRE_MSG(cmsg != NULL, FMT_ERR("malloc"));
783 cmsg->cmsg_len = CMSG_LEN(sizeof(pathfd));
784 cmsg->cmsg_level = SOL_SOCKET;
785 cmsg->cmsg_type = SCM_RIGHTS;
786 *(int *)(void *)CMSG_DATA(cmsg) = pathfd;
792 memset(&msg, 0, sizeof(msg));
795 msg.msg_control = cmsg;
796 msg.msg_controllen = CMSG_SPACE(sizeof(pathfd));
798 ATF_REQUIRE_MSG(sendmsg(sd[0], &msg, 0) == sizeof(c),
801 /* Grab the pathfd copy from the other end of the pair. */
802 memset(&msg, 0, sizeof(msg));
805 msg.msg_control = cmsg;
806 msg.msg_controllen = CMSG_SPACE(sizeof(pathfd));
808 ATF_REQUIRE_MSG(recvmsg(sd[1], &msg, 0) == 1,
810 pathfd_copy = *(int *)(void *)CMSG_DATA(cmsg);
811 ATF_REQUIRE_MSG(pathfd_copy != pathfd,
812 "pathfd and pathfd_copy are equal");
814 /* Verify that the copy has O_PATH properties. */
815 flags = fcntl(pathfd_copy, F_GETFL);
816 ATF_REQUIRE_MSG(flags != -1, FMT_ERR("fcntl"));
817 ATF_REQUIRE_MSG((flags & O_PATH) != 0, "O_PATH is not set");
818 ATF_REQUIRE_ERRNO(EBADF,
819 read(pathfd_copy, &c, 1) == -1);
820 ATF_REQUIRE_ERRNO(EBADF,
821 write(pathfd_copy, &c, 1) == -1);
823 CHECKED_CLOSE(pathfd);
824 CHECKED_CLOSE(pathfd_copy);
825 CHECKED_CLOSE(sd[0]);
826 CHECKED_CLOSE(sd[1]);
829 /* Verify that a local socket can't be opened with O_PATH. */
830 ATF_TC_WITHOUT_HEAD(path_unix);
831 ATF_TC_BODY(path_unix, tc)
834 struct sockaddr_un sun;
837 snprintf(path, sizeof(path), "path_unix.XXXXXX");
838 ATF_REQUIRE_MSG(mktemp(path) == path, FMT_ERR("mktemp"));
840 sd = socket(PF_LOCAL, SOCK_STREAM, 0);
841 ATF_REQUIRE_MSG(sd >= 0, FMT_ERR("socket"));
843 memset(&sun, 0, sizeof(sun));
844 sun.sun_family = PF_LOCAL;
845 (void)strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
846 ATF_REQUIRE_MSG(bind(sd, (struct sockaddr *)&sun, SUN_LEN(&sun)) == 0,
849 pathfd = open(path, O_RDONLY);
850 ATF_REQUIRE_ERRNO(EOPNOTSUPP, pathfd < 0);
857 ATF_TP_ADD_TC(tp, path_access);
858 ATF_TP_ADD_TC(tp, path_aio);
859 ATF_TP_ADD_TC(tp, path_capsicum);
860 ATF_TP_ADD_TC(tp, path_coredump);
861 ATF_TP_ADD_TC(tp, path_directory);
862 ATF_TP_ADD_TC(tp, path_directory_not_root);
863 ATF_TP_ADD_TC(tp, path_empty);
864 ATF_TP_ADD_TC(tp, path_empty_not_root);
865 ATF_TP_ADD_TC(tp, path_empty_root);
866 ATF_TP_ADD_TC(tp, path_event);
867 ATF_TP_ADD_TC(tp, path_fcntl);
868 ATF_TP_ADD_TC(tp, path_fexecve);
869 ATF_TP_ADD_TC(tp, path_fifo);
870 ATF_TP_ADD_TC(tp, path_funlinkat);
871 ATF_TP_ADD_TC(tp, path_io);
872 ATF_TP_ADD_TC(tp, path_ioctl);
873 ATF_TP_ADD_TC(tp, path_lock);
874 ATF_TP_ADD_TC(tp, path_rights);
875 ATF_TP_ADD_TC(tp, path_unix);
877 return (atf_no_error());