Lines Matching refs:fd
50 static ssize_t fd2name(int fd, char *buf, size_t bufsize) in fd2name() argument
56 size = snprintf(buf1, PATH_MAX, "/proc/self/fd/%d", fd); in fd2name()
58 printf("snprintf(%d) failed on %m\n", fd); in fd2name()
76 int r, fd; in mfd_assert_new() local
78 fd = sys_memfd_create(name, flags); in mfd_assert_new()
79 if (fd < 0) { in mfd_assert_new()
85 r = ftruncate(fd, sz); in mfd_assert_new()
91 return fd; in mfd_assert_new()
96 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_assert_write() local
98 if (fd < 0) { in sysctl_assert_write()
103 if (write(fd, val, strlen(val)) < 0) { in sysctl_assert_write()
111 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_fail_write() local
113 if (fd < 0) { in sysctl_fail_write()
118 if (write(fd, val, strlen(val)) >= 0) { in sysctl_fail_write()
128 int fd = open("/proc/sys/vm/memfd_noexec", O_RDONLY | O_CLOEXEC); in sysctl_assert_equal() local
130 if (fd < 0) { in sysctl_assert_equal()
135 if (read(fd, buf, sizeof(buf)) < 0) { in sysctl_assert_equal()
154 int fd; in mfd_assert_reopen_fd() local
159 fd = open(path, O_RDWR); in mfd_assert_reopen_fd()
160 if (fd < 0) { in mfd_assert_reopen_fd()
165 return fd; in mfd_assert_reopen_fd()
181 static unsigned int mfd_assert_get_seals(int fd) in mfd_assert_get_seals() argument
185 r = fcntl(fd, F_GET_SEALS); in mfd_assert_get_seals()
187 printf("GET_SEALS(%d) failed: %m\n", fd); in mfd_assert_get_seals()
194 static void mfd_assert_has_seals(int fd, unsigned int seals) in mfd_assert_has_seals() argument
199 fd2name(fd, buf, PATH_MAX); in mfd_assert_has_seals()
201 s = mfd_assert_get_seals(fd); in mfd_assert_has_seals()
208 static void mfd_assert_add_seals(int fd, unsigned int seals) in mfd_assert_add_seals() argument
213 s = mfd_assert_get_seals(fd); in mfd_assert_add_seals()
214 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_assert_add_seals()
216 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals); in mfd_assert_add_seals()
221 static void mfd_fail_add_seals(int fd, unsigned int seals) in mfd_fail_add_seals() argument
226 r = fcntl(fd, F_GET_SEALS); in mfd_fail_add_seals()
232 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_fail_add_seals()
235 fd, s, seals); in mfd_fail_add_seals()
240 static void mfd_assert_size(int fd, size_t size) in mfd_assert_size() argument
245 r = fstat(fd, &st); in mfd_assert_size()
247 printf("fstat(%d) failed: %m\n", fd); in mfd_assert_size()
256 static int mfd_assert_dup(int fd) in mfd_assert_dup() argument
260 r = dup(fd); in mfd_assert_dup()
262 printf("dup(%d) failed: %m\n", fd); in mfd_assert_dup()
269 static void *mfd_assert_mmap_shared(int fd) in mfd_assert_mmap_shared() argument
277 fd, in mfd_assert_mmap_shared()
287 static void *mfd_assert_mmap_private(int fd) in mfd_assert_mmap_private() argument
295 fd, in mfd_assert_mmap_private()
305 static int mfd_assert_open(int fd, int flags, mode_t mode) in mfd_assert_open() argument
310 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_assert_open()
320 static void mfd_fail_open(int fd, int flags, mode_t mode) in mfd_fail_open() argument
325 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_fail_open()
333 static void mfd_assert_read(int fd) in mfd_assert_read() argument
339 l = read(fd, buf, sizeof(buf)); in mfd_assert_read()
350 fd, in mfd_assert_read()
363 fd, in mfd_assert_read()
373 static void mfd_assert_read_shared(int fd) in mfd_assert_read_shared() argument
382 fd, in mfd_assert_read_shared()
391 static void mfd_assert_fork_private_write(int fd) in mfd_assert_fork_private_write() argument
400 fd, in mfd_assert_fork_private_write()
425 static void mfd_assert_write(int fd) in mfd_assert_write() argument
437 l = write(fd, "\0\0\0\0", 4); in mfd_assert_write()
449 fd, in mfd_assert_write()
463 fd, in mfd_assert_write()
478 fd, in mfd_assert_write()
495 r = fallocate(fd, in mfd_assert_write()
505 static void mfd_fail_write(int fd) in mfd_fail_write() argument
512 l = write(fd, "data", 4); in mfd_fail_write()
523 fd, in mfd_fail_write()
535 fd, in mfd_fail_write()
548 fd, in mfd_fail_write()
560 r = fallocate(fd, in mfd_fail_write()
570 static void mfd_assert_shrink(int fd) in mfd_assert_shrink() argument
574 r = ftruncate(fd, mfd_def_size / 2); in mfd_assert_shrink()
580 mfd_assert_size(fd, mfd_def_size / 2); in mfd_assert_shrink()
582 fd2 = mfd_assert_open(fd, in mfd_assert_shrink()
587 mfd_assert_size(fd, 0); in mfd_assert_shrink()
590 static void mfd_fail_shrink(int fd) in mfd_fail_shrink() argument
594 r = ftruncate(fd, mfd_def_size / 2); in mfd_fail_shrink()
600 mfd_fail_open(fd, in mfd_fail_shrink()
605 static void mfd_assert_grow(int fd) in mfd_assert_grow() argument
609 r = ftruncate(fd, mfd_def_size * 2); in mfd_assert_grow()
615 mfd_assert_size(fd, mfd_def_size * 2); in mfd_assert_grow()
617 r = fallocate(fd, in mfd_assert_grow()
626 mfd_assert_size(fd, mfd_def_size * 4); in mfd_assert_grow()
629 static void mfd_fail_grow(int fd) in mfd_fail_grow() argument
633 r = ftruncate(fd, mfd_def_size * 2); in mfd_fail_grow()
639 r = fallocate(fd, in mfd_fail_grow()
649 static void mfd_assert_grow_write(int fd) in mfd_assert_grow_write() argument
664 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_assert_grow_write()
670 mfd_assert_size(fd, mfd_def_size * 8); in mfd_assert_grow_write()
673 static void mfd_fail_grow_write(int fd) in mfd_fail_grow_write() argument
688 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_fail_grow_write()
695 static void mfd_assert_mode(int fd, int mode) in mfd_assert_mode() argument
701 fd2name(fd, buf, PATH_MAX); in mfd_assert_mode()
703 if (fstat(fd, &st) < 0) { in mfd_assert_mode()
715 static void mfd_assert_chmod(int fd, int mode) in mfd_assert_chmod() argument
720 fd2name(fd, buf, PATH_MAX); in mfd_assert_chmod()
722 if (fchmod(fd, mode) < 0) { in mfd_assert_chmod()
727 mfd_assert_mode(fd, mode); in mfd_assert_chmod()
730 static void mfd_fail_chmod(int fd, int mode) in mfd_fail_chmod() argument
736 fd2name(fd, buf, PATH_MAX); in mfd_fail_chmod()
738 if (fstat(fd, &st) < 0) { in mfd_fail_chmod()
743 if (fchmod(fd, mode) == 0) { in mfd_fail_chmod()
750 mfd_assert_mode(fd, st.st_mode & 07777); in mfd_fail_chmod()
827 int fd; in test_create() local
844 fd = mfd_assert_new("", 0, 0); in test_create()
845 close(fd); in test_create()
858 fd = mfd_assert_new("", 0, MFD_CLOEXEC); in test_create()
859 close(fd); in test_create()
862 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING); in test_create()
863 close(fd); in test_create()
866 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC); in test_create()
867 close(fd); in test_create()
876 int fd; in test_basic() local
880 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
885 mfd_assert_has_seals(fd, 0); in test_basic()
886 mfd_assert_add_seals(fd, F_SEAL_SHRINK | in test_basic()
888 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
892 mfd_assert_add_seals(fd, F_SEAL_SHRINK | in test_basic()
894 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
898 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL); in test_basic()
899 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
905 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_basic()
906 mfd_fail_add_seals(fd, 0); in test_basic()
908 close(fd); in test_basic()
911 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
914 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
915 mfd_fail_add_seals(fd, F_SEAL_SHRINK | in test_basic()
918 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
919 close(fd); in test_basic()
928 int fd; in test_seal_write() local
932 fd = mfd_assert_new("kern_memfd_seal_write", in test_seal_write()
935 mfd_assert_has_seals(fd, 0); in test_seal_write()
936 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_seal_write()
937 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_seal_write()
939 mfd_assert_read(fd); in test_seal_write()
940 mfd_fail_write(fd); in test_seal_write()
941 mfd_assert_shrink(fd); in test_seal_write()
942 mfd_assert_grow(fd); in test_seal_write()
943 mfd_fail_grow_write(fd); in test_seal_write()
945 close(fd); in test_seal_write()
954 int fd, fd2; in test_seal_future_write() local
959 fd = mfd_assert_new("kern_memfd_seal_future_write", in test_seal_future_write()
963 p = mfd_assert_mmap_shared(fd); in test_seal_future_write()
965 mfd_assert_has_seals(fd, 0); in test_seal_future_write()
967 mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
968 mfd_assert_has_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
971 mfd_assert_read(fd); in test_seal_future_write()
972 mfd_assert_read_shared(fd); in test_seal_future_write()
973 mfd_fail_write(fd); in test_seal_future_write()
975 fd2 = mfd_assert_reopen_fd(fd); in test_seal_future_write()
981 mfd_assert_fork_private_write(fd); in test_seal_future_write()
985 close(fd); in test_seal_future_write()
994 int fd; in test_seal_shrink() local
998 fd = mfd_assert_new("kern_memfd_seal_shrink", in test_seal_shrink()
1001 mfd_assert_has_seals(fd, 0); in test_seal_shrink()
1002 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
1003 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
1005 mfd_assert_read(fd); in test_seal_shrink()
1006 mfd_assert_write(fd); in test_seal_shrink()
1007 mfd_fail_shrink(fd); in test_seal_shrink()
1008 mfd_assert_grow(fd); in test_seal_shrink()
1009 mfd_assert_grow_write(fd); in test_seal_shrink()
1011 close(fd); in test_seal_shrink()
1020 int fd; in test_seal_grow() local
1024 fd = mfd_assert_new("kern_memfd_seal_grow", in test_seal_grow()
1027 mfd_assert_has_seals(fd, 0); in test_seal_grow()
1028 mfd_assert_add_seals(fd, F_SEAL_GROW); in test_seal_grow()
1029 mfd_assert_has_seals(fd, F_SEAL_GROW); in test_seal_grow()
1031 mfd_assert_read(fd); in test_seal_grow()
1032 mfd_assert_write(fd); in test_seal_grow()
1033 mfd_assert_shrink(fd); in test_seal_grow()
1034 mfd_fail_grow(fd); in test_seal_grow()
1035 mfd_fail_grow_write(fd); in test_seal_grow()
1037 close(fd); in test_seal_grow()
1046 int fd; in test_seal_resize() local
1050 fd = mfd_assert_new("kern_memfd_seal_resize", in test_seal_resize()
1053 mfd_assert_has_seals(fd, 0); in test_seal_resize()
1054 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1055 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1057 mfd_assert_read(fd); in test_seal_resize()
1058 mfd_assert_write(fd); in test_seal_resize()
1059 mfd_fail_shrink(fd); in test_seal_resize()
1060 mfd_fail_grow(fd); in test_seal_resize()
1061 mfd_fail_grow_write(fd); in test_seal_resize()
1063 close(fd); in test_seal_resize()
1073 int fd; in test_exec_seal() local
1078 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1082 mfd_assert_mode(fd, 0777); in test_exec_seal()
1083 mfd_assert_chmod(fd, 0644); in test_exec_seal()
1085 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1086 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1087 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1089 mfd_assert_chmod(fd, 0600); in test_exec_seal()
1090 mfd_fail_chmod(fd, 0777); in test_exec_seal()
1091 mfd_fail_chmod(fd, 0670); in test_exec_seal()
1092 mfd_fail_chmod(fd, 0605); in test_exec_seal()
1093 mfd_fail_chmod(fd, 0700); in test_exec_seal()
1094 mfd_fail_chmod(fd, 0100); in test_exec_seal()
1095 mfd_assert_chmod(fd, 0666); in test_exec_seal()
1096 mfd_assert_write(fd); in test_exec_seal()
1097 close(fd); in test_exec_seal()
1100 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1104 mfd_assert_mode(fd, 0777); in test_exec_seal()
1105 mfd_assert_chmod(fd, 0700); in test_exec_seal()
1107 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1108 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1109 mfd_assert_has_seals(fd, F_WX_SEALS); in test_exec_seal()
1111 mfd_fail_chmod(fd, 0711); in test_exec_seal()
1112 mfd_fail_chmod(fd, 0600); in test_exec_seal()
1113 mfd_fail_write(fd); in test_exec_seal()
1114 close(fd); in test_exec_seal()
1123 int fd; in test_exec_no_seal() local
1128 fd = mfd_assert_new("kern_memfd_exec_no_sealing", in test_exec_no_seal()
1131 mfd_assert_mode(fd, 0777); in test_exec_no_seal()
1132 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_exec_no_seal()
1133 mfd_assert_chmod(fd, 0666); in test_exec_no_seal()
1134 close(fd); in test_exec_no_seal()
1142 int fd; in test_noexec_seal() local
1147 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1150 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1151 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1152 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1153 close(fd); in test_noexec_seal()
1156 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1159 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1160 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1161 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1162 close(fd); in test_noexec_seal()
1167 int fd; in test_sysctl_sysctl0() local
1171 fd = mfd_assert_new("kern_memfd_sysctl_0_dfl", in test_sysctl_sysctl0()
1174 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl0()
1175 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl0()
1176 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl0()
1177 close(fd); in test_sysctl_sysctl0()
1188 int fd; in test_sysctl_sysctl1() local
1192 fd = mfd_assert_new("kern_memfd_sysctl_1_dfl", in test_sysctl_sysctl1()
1195 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1196 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1197 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1198 close(fd); in test_sysctl_sysctl1()
1200 fd = mfd_assert_new("kern_memfd_sysctl_1_exec", in test_sysctl_sysctl1()
1203 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl1()
1204 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl1()
1205 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl1()
1206 close(fd); in test_sysctl_sysctl1()
1208 fd = mfd_assert_new("kern_memfd_sysctl_1_noexec", in test_sysctl_sysctl1()
1211 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1212 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1213 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1214 close(fd); in test_sysctl_sysctl1()
1225 int fd; in test_sysctl_sysctl2() local
1229 fd = mfd_assert_new("kern_memfd_sysctl_2_dfl", in test_sysctl_sysctl2()
1232 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1233 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1234 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1235 close(fd); in test_sysctl_sysctl2()
1240 fd = mfd_assert_new("kern_memfd_sysctl_2_noexec", in test_sysctl_sysctl2()
1243 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1244 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1245 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1246 close(fd); in test_sysctl_sysctl2()
1257 int fd; in sysctl_simple_child() local
1324 int fd; in sysctl_nested_child() local
1427 int fd, fd2; in test_share_dup() local
1431 fd = mfd_assert_new("kern_memfd_share_dup", in test_share_dup()
1434 mfd_assert_has_seals(fd, 0); in test_share_dup()
1436 fd2 = mfd_assert_dup(fd); in test_share_dup()
1439 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_dup()
1440 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_dup()
1444 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_dup()
1447 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1448 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_dup()
1451 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1453 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1458 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1459 close(fd); in test_share_dup()
1468 int fd; in test_share_mmap() local
1473 fd = mfd_assert_new("kern_memfd_share_mmap", in test_share_mmap()
1476 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1479 p = mfd_assert_mmap_shared(fd); in test_share_mmap()
1480 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1481 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1482 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1483 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1487 p = mfd_assert_mmap_private(fd); in test_share_mmap()
1488 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1489 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_mmap()
1492 close(fd); in test_share_mmap()
1503 int fd, fd2; in test_share_open() local
1507 fd = mfd_assert_new("kern_memfd_share_open", in test_share_open()
1510 mfd_assert_has_seals(fd, 0); in test_share_open()
1512 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1513 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_open()
1514 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_open()
1518 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1521 close(fd); in test_share_open()
1522 fd = mfd_assert_open(fd2, O_RDONLY, 0); in test_share_open()
1524 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_open()
1525 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1529 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1532 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_open()
1536 close(fd); in test_share_open()
1545 int fd; in test_share_fork() local
1550 fd = mfd_assert_new("kern_memfd_share_fork", in test_share_fork()
1553 mfd_assert_has_seals(fd, 0); in test_share_fork()
1556 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_fork()
1557 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1559 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1560 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1564 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1565 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1567 close(fd); in test_share_fork()