Lines Matching full:fd
51 static ssize_t fd2name(int fd, char *buf, size_t bufsize) in fd2name() argument
57 size = snprintf(buf1, PATH_MAX, "/proc/self/fd/%d", fd); in fd2name()
59 printf("snprintf(%d) failed on %m\n", fd); in fd2name()
77 int r, fd; in mfd_assert_new() local
79 fd = sys_memfd_create(name, flags); in mfd_assert_new()
80 if (fd < 0) { in mfd_assert_new()
86 r = ftruncate(fd, sz); in mfd_assert_new()
92 return fd; in mfd_assert_new()
97 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_assert_write() local
99 if (fd < 0) { in sysctl_assert_write()
104 if (write(fd, val, strlen(val)) < 0) { in sysctl_assert_write()
112 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_fail_write() local
114 if (fd < 0) { in sysctl_fail_write()
119 if (write(fd, val, strlen(val)) >= 0) { in sysctl_fail_write()
129 int fd = open("/proc/sys/vm/memfd_noexec", O_RDONLY | O_CLOEXEC); in sysctl_assert_equal() local
131 if (fd < 0) { in sysctl_assert_equal()
136 if (read(fd, buf, sizeof(buf)) < 0) { in sysctl_assert_equal()
155 int fd; in mfd_assert_reopen_fd() local
158 sprintf(path, "/proc/self/fd/%d", fd_in); in mfd_assert_reopen_fd()
160 fd = open(path, O_RDWR); in mfd_assert_reopen_fd()
161 if (fd < 0) { in mfd_assert_reopen_fd()
162 printf("re-open of existing fd %d failed\n", fd_in); in mfd_assert_reopen_fd()
166 return fd; in mfd_assert_reopen_fd()
182 static unsigned int mfd_assert_get_seals(int fd) in mfd_assert_get_seals() argument
186 r = fcntl(fd, F_GET_SEALS); in mfd_assert_get_seals()
188 printf("GET_SEALS(%d) failed: %m\n", fd); in mfd_assert_get_seals()
195 static void mfd_assert_has_seals(int fd, unsigned int seals) in mfd_assert_has_seals() argument
200 fd2name(fd, buf, PATH_MAX); in mfd_assert_has_seals()
202 s = mfd_assert_get_seals(fd); in mfd_assert_has_seals()
209 static void mfd_assert_add_seals(int fd, unsigned int seals) in mfd_assert_add_seals() argument
214 s = mfd_assert_get_seals(fd); in mfd_assert_add_seals()
215 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_assert_add_seals()
217 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals); in mfd_assert_add_seals()
222 static void mfd_fail_add_seals(int fd, unsigned int seals) in mfd_fail_add_seals() argument
227 r = fcntl(fd, F_GET_SEALS); in mfd_fail_add_seals()
233 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_fail_add_seals()
236 fd, s, seals); in mfd_fail_add_seals()
241 static void mfd_assert_size(int fd, size_t size) in mfd_assert_size() argument
246 r = fstat(fd, &st); in mfd_assert_size()
248 printf("fstat(%d) failed: %m\n", fd); in mfd_assert_size()
257 static int mfd_assert_dup(int fd) in mfd_assert_dup() argument
261 r = dup(fd); in mfd_assert_dup()
263 printf("dup(%d) failed: %m\n", fd); in mfd_assert_dup()
270 static void *mfd_assert_mmap_shared(int fd) in mfd_assert_mmap_shared() argument
278 fd, in mfd_assert_mmap_shared()
288 static void *mfd_assert_mmap_private(int fd) in mfd_assert_mmap_private() argument
296 fd, in mfd_assert_mmap_private()
306 static int mfd_assert_open(int fd, int flags, mode_t mode) in mfd_assert_open() argument
311 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_assert_open()
321 static void mfd_fail_open(int fd, int flags, mode_t mode) in mfd_fail_open() argument
326 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_fail_open()
334 static void mfd_assert_read(int fd) in mfd_assert_read() argument
340 l = read(fd, buf, sizeof(buf)); in mfd_assert_read()
351 fd, in mfd_assert_read()
364 fd, in mfd_assert_read()
374 static void mfd_assert_read_shared(int fd) in mfd_assert_read_shared() argument
383 fd, in mfd_assert_read_shared()
392 static void mfd_assert_fork_private_write(int fd) in mfd_assert_fork_private_write() argument
401 fd, in mfd_assert_fork_private_write()
426 static void mfd_assert_write(int fd) in mfd_assert_write() argument
438 l = write(fd, "\0\0\0\0", 4); in mfd_assert_write()
450 fd, in mfd_assert_write()
464 fd, in mfd_assert_write()
479 fd, in mfd_assert_write()
496 r = fallocate(fd, in mfd_assert_write()
506 static void mfd_fail_write(int fd) in mfd_fail_write() argument
513 l = write(fd, "data", 4); in mfd_fail_write()
524 fd, in mfd_fail_write()
536 fd, in mfd_fail_write()
549 fd, in mfd_fail_write()
561 r = fallocate(fd, in mfd_fail_write()
571 static void mfd_assert_shrink(int fd) in mfd_assert_shrink() argument
575 r = ftruncate(fd, mfd_def_size / 2); in mfd_assert_shrink()
581 mfd_assert_size(fd, mfd_def_size / 2); in mfd_assert_shrink()
583 fd2 = mfd_assert_open(fd, in mfd_assert_shrink()
588 mfd_assert_size(fd, 0); in mfd_assert_shrink()
591 static void mfd_fail_shrink(int fd) in mfd_fail_shrink() argument
595 r = ftruncate(fd, mfd_def_size / 2); in mfd_fail_shrink()
601 mfd_fail_open(fd, in mfd_fail_shrink()
606 static void mfd_assert_grow(int fd) in mfd_assert_grow() argument
610 r = ftruncate(fd, mfd_def_size * 2); in mfd_assert_grow()
616 mfd_assert_size(fd, mfd_def_size * 2); in mfd_assert_grow()
618 r = fallocate(fd, in mfd_assert_grow()
627 mfd_assert_size(fd, mfd_def_size * 4); in mfd_assert_grow()
630 static void mfd_fail_grow(int fd) in mfd_fail_grow() argument
634 r = ftruncate(fd, mfd_def_size * 2); in mfd_fail_grow()
640 r = fallocate(fd, in mfd_fail_grow()
650 static void mfd_assert_grow_write(int fd) in mfd_assert_grow_write() argument
665 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_assert_grow_write()
671 mfd_assert_size(fd, mfd_def_size * 8); in mfd_assert_grow_write()
674 static void mfd_fail_grow_write(int fd) in mfd_fail_grow_write() argument
689 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_fail_grow_write()
696 static void mfd_assert_mode(int fd, int mode) in mfd_assert_mode() argument
702 fd2name(fd, buf, PATH_MAX); in mfd_assert_mode()
704 if (fstat(fd, &st) < 0) { in mfd_assert_mode()
716 static void mfd_assert_chmod(int fd, int mode) in mfd_assert_chmod() argument
721 fd2name(fd, buf, PATH_MAX); in mfd_assert_chmod()
723 if (fchmod(fd, mode) < 0) { in mfd_assert_chmod()
728 mfd_assert_mode(fd, mode); in mfd_assert_chmod()
731 static void mfd_fail_chmod(int fd, int mode) in mfd_fail_chmod() argument
737 fd2name(fd, buf, PATH_MAX); in mfd_fail_chmod()
739 if (fstat(fd, &st) < 0) { in mfd_fail_chmod()
744 if (fchmod(fd, mode) == 0) { in mfd_fail_chmod()
751 mfd_assert_mode(fd, st.st_mode & 07777); in mfd_fail_chmod()
828 int fd; in test_create() local
845 fd = mfd_assert_new("", 0, 0); in test_create()
846 close(fd); in test_create()
859 fd = mfd_assert_new("", 0, MFD_CLOEXEC); in test_create()
860 close(fd); in test_create()
863 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING); in test_create()
864 close(fd); in test_create()
867 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC); in test_create()
868 close(fd); in test_create()
877 int fd; in test_basic() local
881 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
886 mfd_assert_has_seals(fd, 0); in test_basic()
887 mfd_assert_add_seals(fd, F_SEAL_SHRINK | in test_basic()
889 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
893 mfd_assert_add_seals(fd, F_SEAL_SHRINK | in test_basic()
895 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
899 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL); in test_basic()
900 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
906 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_basic()
907 mfd_fail_add_seals(fd, 0); in test_basic()
909 close(fd); in test_basic()
912 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
915 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
916 mfd_fail_add_seals(fd, F_SEAL_SHRINK | in test_basic()
919 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
920 close(fd); in test_basic()
929 int fd; in test_seal_write() local
933 fd = mfd_assert_new("kern_memfd_seal_write", in test_seal_write()
936 mfd_assert_has_seals(fd, 0); in test_seal_write()
937 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_seal_write()
938 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_seal_write()
940 mfd_assert_read(fd); in test_seal_write()
941 mfd_fail_write(fd); in test_seal_write()
942 mfd_assert_shrink(fd); in test_seal_write()
943 mfd_assert_grow(fd); in test_seal_write()
944 mfd_fail_grow_write(fd); in test_seal_write()
946 close(fd); in test_seal_write()
955 int fd, fd2; in test_seal_future_write() local
960 fd = mfd_assert_new("kern_memfd_seal_future_write", in test_seal_future_write()
964 p = mfd_assert_mmap_shared(fd); in test_seal_future_write()
966 mfd_assert_has_seals(fd, 0); in test_seal_future_write()
968 mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
969 mfd_assert_has_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
972 mfd_assert_read(fd); in test_seal_future_write()
973 mfd_assert_read_shared(fd); in test_seal_future_write()
974 mfd_fail_write(fd); in test_seal_future_write()
976 fd2 = mfd_assert_reopen_fd(fd); in test_seal_future_write()
982 mfd_assert_fork_private_write(fd); in test_seal_future_write()
986 close(fd); in test_seal_future_write()
995 int fd; in test_seal_shrink() local
999 fd = mfd_assert_new("kern_memfd_seal_shrink", in test_seal_shrink()
1002 mfd_assert_has_seals(fd, 0); in test_seal_shrink()
1003 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
1004 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
1006 mfd_assert_read(fd); in test_seal_shrink()
1007 mfd_assert_write(fd); in test_seal_shrink()
1008 mfd_fail_shrink(fd); in test_seal_shrink()
1009 mfd_assert_grow(fd); in test_seal_shrink()
1010 mfd_assert_grow_write(fd); in test_seal_shrink()
1012 close(fd); in test_seal_shrink()
1021 int fd; in test_seal_grow() local
1025 fd = mfd_assert_new("kern_memfd_seal_grow", in test_seal_grow()
1028 mfd_assert_has_seals(fd, 0); in test_seal_grow()
1029 mfd_assert_add_seals(fd, F_SEAL_GROW); in test_seal_grow()
1030 mfd_assert_has_seals(fd, F_SEAL_GROW); in test_seal_grow()
1032 mfd_assert_read(fd); in test_seal_grow()
1033 mfd_assert_write(fd); in test_seal_grow()
1034 mfd_assert_shrink(fd); in test_seal_grow()
1035 mfd_fail_grow(fd); in test_seal_grow()
1036 mfd_fail_grow_write(fd); in test_seal_grow()
1038 close(fd); in test_seal_grow()
1047 int fd; in test_seal_resize() local
1051 fd = mfd_assert_new("kern_memfd_seal_resize", in test_seal_resize()
1054 mfd_assert_has_seals(fd, 0); in test_seal_resize()
1055 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1056 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1058 mfd_assert_read(fd); in test_seal_resize()
1059 mfd_assert_write(fd); in test_seal_resize()
1060 mfd_fail_shrink(fd); in test_seal_resize()
1061 mfd_fail_grow(fd); in test_seal_resize()
1062 mfd_fail_grow_write(fd); in test_seal_resize()
1064 close(fd); in test_seal_resize()
1069 * Test fd is created with exec and allow sealing.
1074 int fd; in test_exec_seal() local
1079 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1083 mfd_assert_mode(fd, 0777); in test_exec_seal()
1084 mfd_assert_chmod(fd, 0644); in test_exec_seal()
1086 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1087 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1088 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1090 mfd_assert_chmod(fd, 0600); in test_exec_seal()
1091 mfd_fail_chmod(fd, 0777); in test_exec_seal()
1092 mfd_fail_chmod(fd, 0670); in test_exec_seal()
1093 mfd_fail_chmod(fd, 0605); in test_exec_seal()
1094 mfd_fail_chmod(fd, 0700); in test_exec_seal()
1095 mfd_fail_chmod(fd, 0100); in test_exec_seal()
1096 mfd_assert_chmod(fd, 0666); in test_exec_seal()
1097 mfd_assert_write(fd); in test_exec_seal()
1098 close(fd); in test_exec_seal()
1101 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1105 mfd_assert_mode(fd, 0777); in test_exec_seal()
1106 mfd_assert_chmod(fd, 0700); in test_exec_seal()
1108 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1109 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1110 mfd_assert_has_seals(fd, F_WX_SEALS); in test_exec_seal()
1112 mfd_fail_chmod(fd, 0711); in test_exec_seal()
1113 mfd_fail_chmod(fd, 0600); in test_exec_seal()
1114 mfd_fail_write(fd); in test_exec_seal()
1115 close(fd); in test_exec_seal()
1120 * Test fd is created with exec and not allow sealing.
1124 int fd; in test_exec_no_seal() local
1129 fd = mfd_assert_new("kern_memfd_exec_no_sealing", in test_exec_no_seal()
1132 mfd_assert_mode(fd, 0777); in test_exec_no_seal()
1133 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_exec_no_seal()
1134 mfd_assert_chmod(fd, 0666); in test_exec_no_seal()
1135 close(fd); in test_exec_no_seal()
1143 int fd; in test_noexec_seal() local
1148 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1151 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1152 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1153 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1154 close(fd); in test_noexec_seal()
1157 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1160 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1161 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1162 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1163 close(fd); in test_noexec_seal()
1168 int fd; in test_sysctl_sysctl0() local
1172 fd = mfd_assert_new("kern_memfd_sysctl_0_dfl", in test_sysctl_sysctl0()
1175 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl0()
1176 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl0()
1177 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl0()
1178 close(fd); in test_sysctl_sysctl0()
1189 int fd; in test_sysctl_sysctl1() local
1193 fd = mfd_assert_new("kern_memfd_sysctl_1_dfl", in test_sysctl_sysctl1()
1196 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1197 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1198 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1199 close(fd); in test_sysctl_sysctl1()
1201 fd = mfd_assert_new("kern_memfd_sysctl_1_exec", in test_sysctl_sysctl1()
1204 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl1()
1205 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl1()
1206 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl1()
1207 close(fd); in test_sysctl_sysctl1()
1209 fd = mfd_assert_new("kern_memfd_sysctl_1_noexec", in test_sysctl_sysctl1()
1212 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1213 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1214 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1215 close(fd); in test_sysctl_sysctl1()
1226 int fd; in test_sysctl_sysctl2() local
1230 fd = mfd_assert_new("kern_memfd_sysctl_2_dfl", in test_sysctl_sysctl2()
1233 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1234 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1235 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1236 close(fd); in test_sysctl_sysctl2()
1241 fd = mfd_assert_new("kern_memfd_sysctl_2_noexec", in test_sysctl_sysctl2()
1244 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1245 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1246 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1247 close(fd); in test_sysctl_sysctl2()
1258 int fd; in sysctl_simple_child() local
1325 int fd; in sysctl_nested_child() local
1428 int fd, fd2; in test_share_dup() local
1432 fd = mfd_assert_new("kern_memfd_share_dup", in test_share_dup()
1435 mfd_assert_has_seals(fd, 0); in test_share_dup()
1437 fd2 = mfd_assert_dup(fd); in test_share_dup()
1440 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_dup()
1441 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_dup()
1445 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_dup()
1448 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1449 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_dup()
1452 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1454 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1459 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1460 close(fd); in test_share_dup()
1469 int fd; in test_share_mmap() local
1474 fd = mfd_assert_new("kern_memfd_share_mmap", in test_share_mmap()
1477 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1480 p = mfd_assert_mmap_shared(fd); in test_share_mmap()
1481 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1482 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1483 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1484 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1488 p = mfd_assert_mmap_private(fd); in test_share_mmap()
1489 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1490 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_mmap()
1493 close(fd); in test_share_mmap()
1497 * Test sealing with open(/proc/self/fd/%d)
1504 int fd, fd2; in test_share_open() local
1508 fd = mfd_assert_new("kern_memfd_share_open", in test_share_open()
1511 mfd_assert_has_seals(fd, 0); in test_share_open()
1513 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1514 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_open()
1515 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_open()
1519 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1522 close(fd); in test_share_open()
1523 fd = mfd_assert_open(fd2, O_RDONLY, 0); in test_share_open()
1525 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_open()
1526 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1530 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1533 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_open()
1537 close(fd); in test_share_open()
1546 int fd; in test_share_fork() local
1551 fd = mfd_assert_new("kern_memfd_share_fork", in test_share_fork()
1554 mfd_assert_has_seals(fd, 0); in test_share_fork()
1557 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_fork()
1558 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1560 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1561 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1565 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1566 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1568 close(fd); in test_share_fork()