2 #define __EXPORTED_HEADERS__
7 #include <linux/falloc.h>
8 #include <linux/fcntl.h>
9 #include <linux/memfd.h>
17 #include <sys/syscall.h>
20 #define MFD_DEF_SIZE 8192
21 #define STACK_SIZE 65535
23 static int sys_memfd_create(const char *name,
26 return syscall(__NR_memfd_create, name, flags);
29 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
33 fd = sys_memfd_create(name, flags);
35 printf("memfd_create(\"%s\", %u) failed: %m\n",
40 r = ftruncate(fd, sz);
42 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
49 static void mfd_fail_new(const char *name, unsigned int flags)
53 r = sys_memfd_create(name, flags);
55 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
62 static unsigned int mfd_assert_get_seals(int fd)
66 r = fcntl(fd, F_GET_SEALS);
68 printf("GET_SEALS(%d) failed: %m\n", fd);
72 return (unsigned int)r;
75 static void mfd_assert_has_seals(int fd, unsigned int seals)
79 s = mfd_assert_get_seals(fd);
81 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
86 static void mfd_assert_add_seals(int fd, unsigned int seals)
91 s = mfd_assert_get_seals(fd);
92 r = fcntl(fd, F_ADD_SEALS, seals);
94 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
99 static void mfd_fail_add_seals(int fd, unsigned int seals)
104 r = fcntl(fd, F_GET_SEALS);
110 r = fcntl(fd, F_ADD_SEALS, seals);
112 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
118 static void mfd_assert_size(int fd, size_t size)
125 printf("fstat(%d) failed: %m\n", fd);
127 } else if (st.st_size != size) {
128 printf("wrong file size %lld, but expected %lld\n",
129 (long long)st.st_size, (long long)size);
134 static int mfd_assert_dup(int fd)
140 printf("dup(%d) failed: %m\n", fd);
147 static void *mfd_assert_mmap_shared(int fd)
153 PROT_READ | PROT_WRITE,
157 if (p == MAP_FAILED) {
158 printf("mmap() failed: %m\n");
165 static void *mfd_assert_mmap_private(int fd)
175 if (p == MAP_FAILED) {
176 printf("mmap() failed: %m\n");
183 static int mfd_assert_open(int fd, int flags, mode_t mode)
188 sprintf(buf, "/proc/self/fd/%d", fd);
189 r = open(buf, flags, mode);
191 printf("open(%s) failed: %m\n", buf);
198 static void mfd_fail_open(int fd, int flags, mode_t mode)
203 sprintf(buf, "/proc/self/fd/%d", fd);
204 r = open(buf, flags, mode);
206 printf("open(%s) didn't fail as expected\n", buf);
211 static void mfd_assert_read(int fd)
217 l = read(fd, buf, sizeof(buf));
218 if (l != sizeof(buf)) {
219 printf("read() failed: %m\n");
223 /* verify PROT_READ *is* allowed */
230 if (p == MAP_FAILED) {
231 printf("mmap() failed: %m\n");
234 munmap(p, MFD_DEF_SIZE);
236 /* verify MAP_PRIVATE is *always* allowed (even writable) */
239 PROT_READ | PROT_WRITE,
243 if (p == MAP_FAILED) {
244 printf("mmap() failed: %m\n");
247 munmap(p, MFD_DEF_SIZE);
250 static void mfd_assert_write(int fd)
256 /* verify write() succeeds */
257 l = write(fd, "\0\0\0\0", 4);
259 printf("write() failed: %m\n");
263 /* verify PROT_READ | PROT_WRITE is allowed */
266 PROT_READ | PROT_WRITE,
270 if (p == MAP_FAILED) {
271 printf("mmap() failed: %m\n");
275 munmap(p, MFD_DEF_SIZE);
277 /* verify PROT_WRITE is allowed */
284 if (p == MAP_FAILED) {
285 printf("mmap() failed: %m\n");
289 munmap(p, MFD_DEF_SIZE);
291 /* verify PROT_READ with MAP_SHARED is allowed and a following
292 * mprotect(PROT_WRITE) allows writing */
299 if (p == MAP_FAILED) {
300 printf("mmap() failed: %m\n");
304 r = mprotect(p, MFD_DEF_SIZE, PROT_READ | PROT_WRITE);
306 printf("mprotect() failed: %m\n");
311 munmap(p, MFD_DEF_SIZE);
313 /* verify PUNCH_HOLE works */
315 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
319 printf("fallocate(PUNCH_HOLE) failed: %m\n");
324 static void mfd_fail_write(int fd)
330 /* verify write() fails */
331 l = write(fd, "data", 4);
333 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
337 /* verify PROT_READ | PROT_WRITE is not allowed */
340 PROT_READ | PROT_WRITE,
344 if (p != MAP_FAILED) {
345 printf("mmap() didn't fail as expected\n");
349 /* verify PROT_WRITE is not allowed */
356 if (p != MAP_FAILED) {
357 printf("mmap() didn't fail as expected\n");
361 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
362 * allowed. Note that for r/w the kernel already prevents the mmap. */
369 if (p != MAP_FAILED) {
370 r = mprotect(p, MFD_DEF_SIZE, PROT_READ | PROT_WRITE);
372 printf("mmap()+mprotect() didn't fail as expected\n");
377 /* verify PUNCH_HOLE fails */
379 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
383 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
388 static void mfd_assert_shrink(int fd)
392 r = ftruncate(fd, MFD_DEF_SIZE / 2);
394 printf("ftruncate(SHRINK) failed: %m\n");
398 mfd_assert_size(fd, MFD_DEF_SIZE / 2);
400 fd2 = mfd_assert_open(fd,
401 O_RDWR | O_CREAT | O_TRUNC,
405 mfd_assert_size(fd, 0);
408 static void mfd_fail_shrink(int fd)
412 r = ftruncate(fd, MFD_DEF_SIZE / 2);
414 printf("ftruncate(SHRINK) didn't fail as expected\n");
419 O_RDWR | O_CREAT | O_TRUNC,
423 static void mfd_assert_grow(int fd)
427 r = ftruncate(fd, MFD_DEF_SIZE * 2);
429 printf("ftruncate(GROW) failed: %m\n");
433 mfd_assert_size(fd, MFD_DEF_SIZE * 2);
440 printf("fallocate(ALLOC) failed: %m\n");
444 mfd_assert_size(fd, MFD_DEF_SIZE * 4);
447 static void mfd_fail_grow(int fd)
451 r = ftruncate(fd, MFD_DEF_SIZE * 2);
453 printf("ftruncate(GROW) didn't fail as expected\n");
462 printf("fallocate(ALLOC) didn't fail as expected\n");
467 static void mfd_assert_grow_write(int fd)
469 static char buf[MFD_DEF_SIZE * 8];
472 l = pwrite(fd, buf, sizeof(buf), 0);
473 if (l != sizeof(buf)) {
474 printf("pwrite() failed: %m\n");
478 mfd_assert_size(fd, MFD_DEF_SIZE * 8);
481 static void mfd_fail_grow_write(int fd)
483 static char buf[MFD_DEF_SIZE * 8];
486 l = pwrite(fd, buf, sizeof(buf), 0);
487 if (l == sizeof(buf)) {
488 printf("pwrite() didn't fail as expected\n");
493 static int idle_thread_fn(void *arg)
498 /* dummy waiter; SIGTERM terminates us anyway */
500 sigaddset(&set, SIGTERM);
506 static pid_t spawn_idle_thread(unsigned int flags)
511 stack = malloc(STACK_SIZE);
513 printf("malloc(STACK_SIZE) failed: %m\n");
517 pid = clone(idle_thread_fn,
522 printf("clone() failed: %m\n");
529 static void join_idle_thread(pid_t pid)
532 waitpid(pid, NULL, 0);
536 * Test memfd_create() syscall
537 * Verify syscall-argument validation, including name checks, flag validation
540 static void test_create(void)
546 mfd_fail_new(NULL, 0);
548 /* test over-long name (not zero-terminated) */
549 memset(buf, 0xff, sizeof(buf));
550 mfd_fail_new(buf, 0);
552 /* test over-long zero-terminated name */
553 memset(buf, 0xff, sizeof(buf));
554 buf[sizeof(buf) - 1] = 0;
555 mfd_fail_new(buf, 0);
557 /* verify "" is a valid name */
558 fd = mfd_assert_new("", 0, 0);
561 /* verify invalid O_* open flags */
562 mfd_fail_new("", 0x0100);
563 mfd_fail_new("", ~MFD_CLOEXEC);
564 mfd_fail_new("", ~MFD_ALLOW_SEALING);
565 mfd_fail_new("", ~0);
566 mfd_fail_new("", 0x80000000U);
568 /* verify MFD_CLOEXEC is allowed */
569 fd = mfd_assert_new("", 0, MFD_CLOEXEC);
572 /* verify MFD_ALLOW_SEALING is allowed */
573 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
576 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
577 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
583 * A very basic sealing test to see whether setting/retrieving seals works.
585 static void test_basic(void)
589 fd = mfd_assert_new("kern_memfd_basic",
591 MFD_CLOEXEC | MFD_ALLOW_SEALING);
593 /* add basic seals */
594 mfd_assert_has_seals(fd, 0);
595 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
597 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
601 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
603 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
606 /* add more seals and seal against sealing */
607 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
608 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
613 /* verify that sealing no longer works */
614 mfd_fail_add_seals(fd, F_SEAL_GROW);
615 mfd_fail_add_seals(fd, 0);
619 /* verify sealing does not work without MFD_ALLOW_SEALING */
620 fd = mfd_assert_new("kern_memfd_basic",
623 mfd_assert_has_seals(fd, F_SEAL_SEAL);
624 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
627 mfd_assert_has_seals(fd, F_SEAL_SEAL);
633 * Test whether SEAL_WRITE actually prevents modifications.
635 static void test_seal_write(void)
639 fd = mfd_assert_new("kern_memfd_seal_write",
641 MFD_CLOEXEC | MFD_ALLOW_SEALING);
642 mfd_assert_has_seals(fd, 0);
643 mfd_assert_add_seals(fd, F_SEAL_WRITE);
644 mfd_assert_has_seals(fd, F_SEAL_WRITE);
648 mfd_assert_shrink(fd);
650 mfd_fail_grow_write(fd);
657 * Test whether SEAL_SHRINK actually prevents shrinking
659 static void test_seal_shrink(void)
663 fd = mfd_assert_new("kern_memfd_seal_shrink",
665 MFD_CLOEXEC | MFD_ALLOW_SEALING);
666 mfd_assert_has_seals(fd, 0);
667 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
668 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
671 mfd_assert_write(fd);
674 mfd_assert_grow_write(fd);
681 * Test whether SEAL_GROW actually prevents growing
683 static void test_seal_grow(void)
687 fd = mfd_assert_new("kern_memfd_seal_grow",
689 MFD_CLOEXEC | MFD_ALLOW_SEALING);
690 mfd_assert_has_seals(fd, 0);
691 mfd_assert_add_seals(fd, F_SEAL_GROW);
692 mfd_assert_has_seals(fd, F_SEAL_GROW);
695 mfd_assert_write(fd);
696 mfd_assert_shrink(fd);
698 mfd_fail_grow_write(fd);
704 * Test SEAL_SHRINK | SEAL_GROW
705 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
707 static void test_seal_resize(void)
711 fd = mfd_assert_new("kern_memfd_seal_resize",
713 MFD_CLOEXEC | MFD_ALLOW_SEALING);
714 mfd_assert_has_seals(fd, 0);
715 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
716 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
719 mfd_assert_write(fd);
722 mfd_fail_grow_write(fd);
728 * Test sharing via dup()
729 * Test that seals are shared between dupped FDs and they're all equal.
731 static void test_share_dup(void)
735 fd = mfd_assert_new("kern_memfd_share_dup",
737 MFD_CLOEXEC | MFD_ALLOW_SEALING);
738 mfd_assert_has_seals(fd, 0);
740 fd2 = mfd_assert_dup(fd);
741 mfd_assert_has_seals(fd2, 0);
743 mfd_assert_add_seals(fd, F_SEAL_WRITE);
744 mfd_assert_has_seals(fd, F_SEAL_WRITE);
745 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
747 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
748 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
749 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
751 mfd_assert_add_seals(fd, F_SEAL_SEAL);
752 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
753 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
755 mfd_fail_add_seals(fd, F_SEAL_GROW);
756 mfd_fail_add_seals(fd2, F_SEAL_GROW);
757 mfd_fail_add_seals(fd, F_SEAL_SEAL);
758 mfd_fail_add_seals(fd2, F_SEAL_SEAL);
762 mfd_fail_add_seals(fd, F_SEAL_GROW);
767 * Test sealing with active mmap()s
768 * Modifying seals is only allowed if no other mmap() refs exist.
770 static void test_share_mmap(void)
775 fd = mfd_assert_new("kern_memfd_share_mmap",
777 MFD_CLOEXEC | MFD_ALLOW_SEALING);
778 mfd_assert_has_seals(fd, 0);
780 /* shared/writable ref prevents sealing WRITE, but allows others */
781 p = mfd_assert_mmap_shared(fd);
782 mfd_fail_add_seals(fd, F_SEAL_WRITE);
783 mfd_assert_has_seals(fd, 0);
784 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
785 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
786 munmap(p, MFD_DEF_SIZE);
788 /* readable ref allows sealing */
789 p = mfd_assert_mmap_private(fd);
790 mfd_assert_add_seals(fd, F_SEAL_WRITE);
791 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
792 munmap(p, MFD_DEF_SIZE);
798 * Test sealing with open(/proc/self/fd/%d)
799 * Via /proc we can get access to a separate file-context for the same memfd.
800 * This is *not* like dup(), but like a real separate open(). Make sure the
801 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
803 static void test_share_open(void)
807 fd = mfd_assert_new("kern_memfd_share_open",
809 MFD_CLOEXEC | MFD_ALLOW_SEALING);
810 mfd_assert_has_seals(fd, 0);
812 fd2 = mfd_assert_open(fd, O_RDWR, 0);
813 mfd_assert_add_seals(fd, F_SEAL_WRITE);
814 mfd_assert_has_seals(fd, F_SEAL_WRITE);
815 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
817 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
818 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
819 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
822 fd = mfd_assert_open(fd2, O_RDONLY, 0);
824 mfd_fail_add_seals(fd, F_SEAL_SEAL);
825 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
826 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
829 fd2 = mfd_assert_open(fd, O_RDWR, 0);
831 mfd_assert_add_seals(fd2, F_SEAL_SEAL);
832 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
833 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
840 * Test sharing via fork()
841 * Test whether seal-modifications work as expected with forked childs.
843 static void test_share_fork(void)
848 fd = mfd_assert_new("kern_memfd_share_fork",
850 MFD_CLOEXEC | MFD_ALLOW_SEALING);
851 mfd_assert_has_seals(fd, 0);
853 pid = spawn_idle_thread(0);
854 mfd_assert_add_seals(fd, F_SEAL_SEAL);
855 mfd_assert_has_seals(fd, F_SEAL_SEAL);
857 mfd_fail_add_seals(fd, F_SEAL_WRITE);
858 mfd_assert_has_seals(fd, F_SEAL_SEAL);
860 join_idle_thread(pid);
862 mfd_fail_add_seals(fd, F_SEAL_WRITE);
863 mfd_assert_has_seals(fd, F_SEAL_SEAL);
868 int main(int argc, char **argv)
872 printf("memfd: CREATE\n");
874 printf("memfd: BASIC\n");
877 printf("memfd: SEAL-WRITE\n");
879 printf("memfd: SEAL-SHRINK\n");
881 printf("memfd: SEAL-GROW\n");
883 printf("memfd: SEAL-RESIZE\n");
886 printf("memfd: SHARE-DUP\n");
888 printf("memfd: SHARE-MMAP\n");
890 printf("memfd: SHARE-OPEN\n");
892 printf("memfd: SHARE-FORK\n");
895 /* Run test-suite in a multi-threaded environment with a shared
897 pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
898 printf("memfd: SHARE-DUP (shared file-table)\n");
900 printf("memfd: SHARE-MMAP (shared file-table)\n");
902 printf("memfd: SHARE-OPEN (shared file-table)\n");
904 printf("memfd: SHARE-FORK (shared file-table)\n");
906 join_idle_thread(pid);
908 printf("memfd: DONE\n");