Lines Matching full:fd

48 static ssize_t fd2name(int fd, char *buf, size_t bufsize)  in fd2name()  argument
54 size = snprintf(buf1, PATH_MAX, "/proc/self/fd/%d", fd); in fd2name()
56 printf("snprintf(%d) failed on %m\n", fd); in fd2name()
74 int r, fd; in mfd_assert_new() local
76 fd = sys_memfd_create(name, flags); in mfd_assert_new()
77 if (fd < 0) { in mfd_assert_new()
83 r = ftruncate(fd, sz); in mfd_assert_new()
89 return fd; in mfd_assert_new()
94 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_assert_write() local
96 if (fd < 0) { in sysctl_assert_write()
101 if (write(fd, val, strlen(val)) < 0) { in sysctl_assert_write()
109 int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC); in sysctl_fail_write() local
111 if (fd < 0) { in sysctl_fail_write()
116 if (write(fd, val, strlen(val)) >= 0) { in sysctl_fail_write()
126 int fd = open("/proc/sys/vm/memfd_noexec", O_RDONLY | O_CLOEXEC); in sysctl_assert_equal() local
128 if (fd < 0) { in sysctl_assert_equal()
133 if (read(fd, buf, sizeof(buf)) < 0) { in sysctl_assert_equal()
152 int fd; in mfd_assert_reopen_fd() local
155 sprintf(path, "/proc/self/fd/%d", fd_in); in mfd_assert_reopen_fd()
157 fd = open(path, O_RDWR); in mfd_assert_reopen_fd()
158 if (fd < 0) { in mfd_assert_reopen_fd()
159 printf("re-open of existing fd %d failed\n", fd_in); in mfd_assert_reopen_fd()
163 return fd; in mfd_assert_reopen_fd()
179 static unsigned int mfd_assert_get_seals(int fd) in mfd_assert_get_seals() argument
183 r = fcntl(fd, F_GET_SEALS); in mfd_assert_get_seals()
185 printf("GET_SEALS(%d) failed: %m\n", fd); in mfd_assert_get_seals()
192 static void mfd_assert_has_seals(int fd, unsigned int seals) in mfd_assert_has_seals() argument
196 fd2name(fd, buf, PATH_MAX); in mfd_assert_has_seals()
198 s = mfd_assert_get_seals(fd); in mfd_assert_has_seals()
205 static void mfd_assert_add_seals(int fd, unsigned int seals) in mfd_assert_add_seals() argument
210 s = mfd_assert_get_seals(fd); in mfd_assert_add_seals()
211 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_assert_add_seals()
213 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals); in mfd_assert_add_seals()
218 static void mfd_fail_add_seals(int fd, unsigned int seals) in mfd_fail_add_seals() argument
223 r = fcntl(fd, F_GET_SEALS); in mfd_fail_add_seals()
229 r = fcntl(fd, F_ADD_SEALS, seals); in mfd_fail_add_seals()
232 fd, s, seals); in mfd_fail_add_seals()
237 static void mfd_assert_size(int fd, size_t size) in mfd_assert_size() argument
242 r = fstat(fd, &st); in mfd_assert_size()
244 printf("fstat(%d) failed: %m\n", fd); in mfd_assert_size()
253 static int mfd_assert_dup(int fd) in mfd_assert_dup() argument
257 r = dup(fd); in mfd_assert_dup()
259 printf("dup(%d) failed: %m\n", fd); in mfd_assert_dup()
266 static void *mfd_assert_mmap_shared(int fd) in mfd_assert_mmap_shared() argument
274 fd, in mfd_assert_mmap_shared()
284 static void *mfd_assert_mmap_private(int fd) in mfd_assert_mmap_private() argument
292 fd, in mfd_assert_mmap_private()
302 static int mfd_assert_open(int fd, int flags, mode_t mode) in mfd_assert_open() argument
307 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_assert_open()
317 static void mfd_fail_open(int fd, int flags, mode_t mode) in mfd_fail_open() argument
322 sprintf(buf, "/proc/self/fd/%d", fd); in mfd_fail_open()
330 static void mfd_assert_read(int fd) in mfd_assert_read() argument
336 l = read(fd, buf, sizeof(buf)); in mfd_assert_read()
347 fd, in mfd_assert_read()
360 fd, in mfd_assert_read()
370 static void mfd_assert_read_shared(int fd) in mfd_assert_read_shared() argument
379 fd, in mfd_assert_read_shared()
388 static void mfd_assert_fork_private_write(int fd) in mfd_assert_fork_private_write() argument
397 fd, in mfd_assert_fork_private_write()
422 static void mfd_assert_write(int fd) in mfd_assert_write() argument
434 l = write(fd, "\0\0\0\0", 4); in mfd_assert_write()
446 fd, in mfd_assert_write()
460 fd, in mfd_assert_write()
475 fd, in mfd_assert_write()
492 r = fallocate(fd, in mfd_assert_write()
502 static void mfd_fail_write(int fd) in mfd_fail_write() argument
509 l = write(fd, "data", 4); in mfd_fail_write()
520 fd, in mfd_fail_write()
532 fd, in mfd_fail_write()
545 fd, in mfd_fail_write()
557 r = fallocate(fd, in mfd_fail_write()
567 static void mfd_assert_shrink(int fd) in mfd_assert_shrink() argument
571 r = ftruncate(fd, mfd_def_size / 2); in mfd_assert_shrink()
577 mfd_assert_size(fd, mfd_def_size / 2); in mfd_assert_shrink()
579 fd2 = mfd_assert_open(fd, in mfd_assert_shrink()
584 mfd_assert_size(fd, 0); in mfd_assert_shrink()
587 static void mfd_fail_shrink(int fd) in mfd_fail_shrink() argument
591 r = ftruncate(fd, mfd_def_size / 2); in mfd_fail_shrink()
597 mfd_fail_open(fd, in mfd_fail_shrink()
602 static void mfd_assert_grow(int fd) in mfd_assert_grow() argument
606 r = ftruncate(fd, mfd_def_size * 2); in mfd_assert_grow()
612 mfd_assert_size(fd, mfd_def_size * 2); in mfd_assert_grow()
614 r = fallocate(fd, in mfd_assert_grow()
623 mfd_assert_size(fd, mfd_def_size * 4); in mfd_assert_grow()
626 static void mfd_fail_grow(int fd) in mfd_fail_grow() argument
630 r = ftruncate(fd, mfd_def_size * 2); in mfd_fail_grow()
636 r = fallocate(fd, in mfd_fail_grow()
646 static void mfd_assert_grow_write(int fd) in mfd_assert_grow_write() argument
661 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_assert_grow_write()
667 mfd_assert_size(fd, mfd_def_size * 8); in mfd_assert_grow_write()
670 static void mfd_fail_grow_write(int fd) in mfd_fail_grow_write() argument
685 l = pwrite(fd, buf, mfd_def_size * 8, 0); in mfd_fail_grow_write()
692 static void mfd_assert_mode(int fd, int mode) in mfd_assert_mode() argument
697 fd2name(fd, buf, PATH_MAX); in mfd_assert_mode()
699 if (fstat(fd, &st) < 0) { in mfd_assert_mode()
711 static void mfd_assert_chmod(int fd, int mode) in mfd_assert_chmod() argument
715 fd2name(fd, buf, PATH_MAX); in mfd_assert_chmod()
717 if (fchmod(fd, mode) < 0) { in mfd_assert_chmod()
722 mfd_assert_mode(fd, mode); in mfd_assert_chmod()
725 static void mfd_fail_chmod(int fd, int mode) in mfd_fail_chmod() argument
730 fd2name(fd, buf, PATH_MAX); in mfd_fail_chmod()
732 if (fstat(fd, &st) < 0) { in mfd_fail_chmod()
737 if (fchmod(fd, mode) == 0) { in mfd_fail_chmod()
744 mfd_assert_mode(fd, st.st_mode & 07777); in mfd_fail_chmod()
821 int fd; in test_create() local
838 fd = mfd_assert_new("", 0, 0); in test_create()
839 close(fd); in test_create()
852 fd = mfd_assert_new("", 0, MFD_CLOEXEC); in test_create()
853 close(fd); in test_create()
856 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING); in test_create()
857 close(fd); in test_create()
860 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC); in test_create()
861 close(fd); in test_create()
870 int fd; in test_basic() local
874 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
879 mfd_assert_has_seals(fd, 0); in test_basic()
880 mfd_assert_add_seals(fd, F_SEAL_SHRINK | in test_basic()
882 mfd_assert_has_seals(fd, F_SEAL_SHRINK | 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_GROW | F_SEAL_SEAL); in test_basic()
893 mfd_assert_has_seals(fd, F_SEAL_SHRINK | in test_basic()
899 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_basic()
900 mfd_fail_add_seals(fd, 0); in test_basic()
902 close(fd); in test_basic()
905 fd = mfd_assert_new("kern_memfd_basic", in test_basic()
908 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
909 mfd_fail_add_seals(fd, F_SEAL_SHRINK | in test_basic()
912 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_basic()
913 close(fd); in test_basic()
922 int fd; in test_seal_write() local
926 fd = mfd_assert_new("kern_memfd_seal_write", in test_seal_write()
929 mfd_assert_has_seals(fd, 0); in test_seal_write()
930 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_seal_write()
931 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_seal_write()
933 mfd_assert_read(fd); in test_seal_write()
934 mfd_fail_write(fd); in test_seal_write()
935 mfd_assert_shrink(fd); in test_seal_write()
936 mfd_assert_grow(fd); in test_seal_write()
937 mfd_fail_grow_write(fd); in test_seal_write()
939 close(fd); in test_seal_write()
948 int fd, fd2; in test_seal_future_write() local
953 fd = mfd_assert_new("kern_memfd_seal_future_write", in test_seal_future_write()
957 p = mfd_assert_mmap_shared(fd); in test_seal_future_write()
959 mfd_assert_has_seals(fd, 0); in test_seal_future_write()
961 mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
962 mfd_assert_has_seals(fd, F_SEAL_FUTURE_WRITE); in test_seal_future_write()
965 mfd_assert_read(fd); in test_seal_future_write()
966 mfd_assert_read_shared(fd); in test_seal_future_write()
967 mfd_fail_write(fd); in test_seal_future_write()
969 fd2 = mfd_assert_reopen_fd(fd); in test_seal_future_write()
975 mfd_assert_fork_private_write(fd); in test_seal_future_write()
979 close(fd); in test_seal_future_write()
988 int fd; in test_seal_shrink() local
992 fd = mfd_assert_new("kern_memfd_seal_shrink", in test_seal_shrink()
995 mfd_assert_has_seals(fd, 0); in test_seal_shrink()
996 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
997 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_seal_shrink()
999 mfd_assert_read(fd); in test_seal_shrink()
1000 mfd_assert_write(fd); in test_seal_shrink()
1001 mfd_fail_shrink(fd); in test_seal_shrink()
1002 mfd_assert_grow(fd); in test_seal_shrink()
1003 mfd_assert_grow_write(fd); in test_seal_shrink()
1005 close(fd); in test_seal_shrink()
1014 int fd; in test_seal_grow() local
1018 fd = mfd_assert_new("kern_memfd_seal_grow", in test_seal_grow()
1021 mfd_assert_has_seals(fd, 0); in test_seal_grow()
1022 mfd_assert_add_seals(fd, F_SEAL_GROW); in test_seal_grow()
1023 mfd_assert_has_seals(fd, F_SEAL_GROW); in test_seal_grow()
1025 mfd_assert_read(fd); in test_seal_grow()
1026 mfd_assert_write(fd); in test_seal_grow()
1027 mfd_assert_shrink(fd); in test_seal_grow()
1028 mfd_fail_grow(fd); in test_seal_grow()
1029 mfd_fail_grow_write(fd); in test_seal_grow()
1031 close(fd); in test_seal_grow()
1040 int fd; in test_seal_resize() local
1044 fd = mfd_assert_new("kern_memfd_seal_resize", in test_seal_resize()
1047 mfd_assert_has_seals(fd, 0); in test_seal_resize()
1048 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1049 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW); in test_seal_resize()
1051 mfd_assert_read(fd); in test_seal_resize()
1052 mfd_assert_write(fd); in test_seal_resize()
1053 mfd_fail_shrink(fd); in test_seal_resize()
1054 mfd_fail_grow(fd); in test_seal_resize()
1055 mfd_fail_grow_write(fd); in test_seal_resize()
1057 close(fd); in test_seal_resize()
1062 * Test fd is created with exec and allow sealing.
1067 int fd; in test_exec_seal() local
1072 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1076 mfd_assert_mode(fd, 0777); in test_exec_seal()
1077 mfd_assert_chmod(fd, 0644); in test_exec_seal()
1079 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1080 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1081 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1083 mfd_assert_chmod(fd, 0600); in test_exec_seal()
1084 mfd_fail_chmod(fd, 0777); in test_exec_seal()
1085 mfd_fail_chmod(fd, 0670); in test_exec_seal()
1086 mfd_fail_chmod(fd, 0605); in test_exec_seal()
1087 mfd_fail_chmod(fd, 0700); in test_exec_seal()
1088 mfd_fail_chmod(fd, 0100); in test_exec_seal()
1089 mfd_assert_chmod(fd, 0666); in test_exec_seal()
1090 mfd_assert_write(fd); in test_exec_seal()
1091 close(fd); in test_exec_seal()
1094 fd = mfd_assert_new("kern_memfd_seal_exec", in test_exec_seal()
1098 mfd_assert_mode(fd, 0777); in test_exec_seal()
1099 mfd_assert_chmod(fd, 0700); in test_exec_seal()
1101 mfd_assert_has_seals(fd, 0); in test_exec_seal()
1102 mfd_assert_add_seals(fd, F_SEAL_EXEC); in test_exec_seal()
1103 mfd_assert_has_seals(fd, F_WX_SEALS); in test_exec_seal()
1105 mfd_fail_chmod(fd, 0711); in test_exec_seal()
1106 mfd_fail_chmod(fd, 0600); in test_exec_seal()
1107 mfd_fail_write(fd); in test_exec_seal()
1108 close(fd); in test_exec_seal()
1113 * Test fd is created with exec and not allow sealing.
1117 int fd; in test_exec_no_seal() local
1122 fd = mfd_assert_new("kern_memfd_exec_no_sealing", in test_exec_no_seal()
1125 mfd_assert_mode(fd, 0777); in test_exec_no_seal()
1126 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_exec_no_seal()
1127 mfd_assert_chmod(fd, 0666); in test_exec_no_seal()
1128 close(fd); in test_exec_no_seal()
1136 int fd; in test_noexec_seal() local
1141 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1144 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1145 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1146 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1147 close(fd); in test_noexec_seal()
1150 fd = mfd_assert_new("kern_memfd_noexec", in test_noexec_seal()
1153 mfd_assert_mode(fd, 0666); in test_noexec_seal()
1154 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_noexec_seal()
1155 mfd_fail_chmod(fd, 0777); in test_noexec_seal()
1156 close(fd); in test_noexec_seal()
1161 int fd; in test_sysctl_sysctl0() local
1165 fd = mfd_assert_new("kern_memfd_sysctl_0_dfl", in test_sysctl_sysctl0()
1168 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl0()
1169 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl0()
1170 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl0()
1171 close(fd); in test_sysctl_sysctl0()
1182 int fd; in test_sysctl_sysctl1() local
1186 fd = mfd_assert_new("kern_memfd_sysctl_1_dfl", in test_sysctl_sysctl1()
1189 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1190 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1191 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1192 close(fd); in test_sysctl_sysctl1()
1194 fd = mfd_assert_new("kern_memfd_sysctl_1_exec", in test_sysctl_sysctl1()
1197 mfd_assert_mode(fd, 0777); in test_sysctl_sysctl1()
1198 mfd_assert_has_seals(fd, 0); in test_sysctl_sysctl1()
1199 mfd_assert_chmod(fd, 0644); in test_sysctl_sysctl1()
1200 close(fd); in test_sysctl_sysctl1()
1202 fd = mfd_assert_new("kern_memfd_sysctl_1_noexec", in test_sysctl_sysctl1()
1205 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl1()
1206 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl1()
1207 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl1()
1208 close(fd); in test_sysctl_sysctl1()
1219 int fd; in test_sysctl_sysctl2() local
1223 fd = mfd_assert_new("kern_memfd_sysctl_2_dfl", in test_sysctl_sysctl2()
1226 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1227 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1228 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1229 close(fd); in test_sysctl_sysctl2()
1234 fd = mfd_assert_new("kern_memfd_sysctl_2_noexec", in test_sysctl_sysctl2()
1237 mfd_assert_mode(fd, 0666); in test_sysctl_sysctl2()
1238 mfd_assert_has_seals(fd, F_SEAL_EXEC); in test_sysctl_sysctl2()
1239 mfd_fail_chmod(fd, 0777); in test_sysctl_sysctl2()
1240 close(fd); in test_sysctl_sysctl2()
1417 int fd, fd2; in test_share_dup() local
1421 fd = mfd_assert_new("kern_memfd_share_dup", in test_share_dup()
1424 mfd_assert_has_seals(fd, 0); in test_share_dup()
1426 fd2 = mfd_assert_dup(fd); in test_share_dup()
1429 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_dup()
1430 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_dup()
1434 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_dup()
1437 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1438 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_dup()
1441 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1443 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_dup()
1448 mfd_fail_add_seals(fd, F_SEAL_GROW); in test_share_dup()
1449 close(fd); in test_share_dup()
1458 int fd; in test_share_mmap() local
1463 fd = mfd_assert_new("kern_memfd_share_mmap", in test_share_mmap()
1466 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1469 p = mfd_assert_mmap_shared(fd); in test_share_mmap()
1470 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1471 mfd_assert_has_seals(fd, 0); in test_share_mmap()
1472 mfd_assert_add_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1473 mfd_assert_has_seals(fd, F_SEAL_SHRINK); in test_share_mmap()
1477 p = mfd_assert_mmap_private(fd); in test_share_mmap()
1478 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_mmap()
1479 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_mmap()
1482 close(fd); in test_share_mmap()
1486 * Test sealing with open(/proc/self/fd/%d)
1493 int fd, fd2; in test_share_open() local
1497 fd = mfd_assert_new("kern_memfd_share_open", in test_share_open()
1500 mfd_assert_has_seals(fd, 0); in test_share_open()
1502 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1503 mfd_assert_add_seals(fd, F_SEAL_WRITE); in test_share_open()
1504 mfd_assert_has_seals(fd, F_SEAL_WRITE); in test_share_open()
1508 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1511 close(fd); in test_share_open()
1512 fd = mfd_assert_open(fd2, O_RDONLY, 0); in test_share_open()
1514 mfd_fail_add_seals(fd, F_SEAL_SEAL); in test_share_open()
1515 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK); in test_share_open()
1519 fd2 = mfd_assert_open(fd, O_RDWR, 0); in test_share_open()
1522 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL); in test_share_open()
1526 close(fd); in test_share_open()
1535 int fd; in test_share_fork() local
1540 fd = mfd_assert_new("kern_memfd_share_fork", in test_share_fork()
1543 mfd_assert_has_seals(fd, 0); in test_share_fork()
1546 mfd_assert_add_seals(fd, F_SEAL_SEAL); in test_share_fork()
1547 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1549 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1550 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1554 mfd_fail_add_seals(fd, F_SEAL_WRITE); in test_share_fork()
1555 mfd_assert_has_seals(fd, F_SEAL_SEAL); in test_share_fork()
1557 close(fd); in test_share_fork()