2 #define __EXPORTED_HEADERS__
7 #include <linux/falloc.h>
8 #include <linux/fcntl.h>
9 #include <linux/memfd.h>
17 #include <sys/syscall.h>
21 #define MFD_DEF_SIZE 8192
22 #define STACK_SIZE 65536
24 static int sys_memfd_create(const char *name,
27 return syscall(__NR_memfd_create, name, flags);
30 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
34 fd = sys_memfd_create(name, flags);
36 printf("memfd_create(\"%s\", %u) failed: %m\n",
41 r = ftruncate(fd, sz);
43 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
50 static void mfd_fail_new(const char *name, unsigned int flags)
54 r = sys_memfd_create(name, flags);
56 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
63 static unsigned int mfd_assert_get_seals(int fd)
67 r = fcntl(fd, F_GET_SEALS);
69 printf("GET_SEALS(%d) failed: %m\n", fd);
73 return (unsigned int)r;
76 static void mfd_assert_has_seals(int fd, unsigned int seals)
80 s = mfd_assert_get_seals(fd);
82 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
87 static void mfd_assert_add_seals(int fd, unsigned int seals)
92 s = mfd_assert_get_seals(fd);
93 r = fcntl(fd, F_ADD_SEALS, seals);
95 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
100 static void mfd_fail_add_seals(int fd, unsigned int seals)
105 r = fcntl(fd, F_GET_SEALS);
111 r = fcntl(fd, F_ADD_SEALS, seals);
113 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
119 static void mfd_assert_size(int fd, size_t size)
126 printf("fstat(%d) failed: %m\n", fd);
128 } else if (st.st_size != size) {
129 printf("wrong file size %lld, but expected %lld\n",
130 (long long)st.st_size, (long long)size);
135 static int mfd_assert_dup(int fd)
141 printf("dup(%d) failed: %m\n", fd);
148 static void *mfd_assert_mmap_shared(int fd)
154 PROT_READ | PROT_WRITE,
158 if (p == MAP_FAILED) {
159 printf("mmap() failed: %m\n");
166 static void *mfd_assert_mmap_private(int fd)
176 if (p == MAP_FAILED) {
177 printf("mmap() failed: %m\n");
184 static int mfd_assert_open(int fd, int flags, mode_t mode)
189 sprintf(buf, "/proc/self/fd/%d", fd);
190 r = open(buf, flags, mode);
192 printf("open(%s) failed: %m\n", buf);
199 static void mfd_fail_open(int fd, int flags, mode_t mode)
204 sprintf(buf, "/proc/self/fd/%d", fd);
205 r = open(buf, flags, mode);
207 printf("open(%s) didn't fail as expected\n", buf);
212 static void mfd_assert_read(int fd)
218 l = read(fd, buf, sizeof(buf));
219 if (l != sizeof(buf)) {
220 printf("read() failed: %m\n");
224 /* verify PROT_READ *is* allowed */
231 if (p == MAP_FAILED) {
232 printf("mmap() failed: %m\n");
235 munmap(p, MFD_DEF_SIZE);
237 /* verify MAP_PRIVATE is *always* allowed (even writable) */
240 PROT_READ | PROT_WRITE,
244 if (p == MAP_FAILED) {
245 printf("mmap() failed: %m\n");
248 munmap(p, MFD_DEF_SIZE);
251 static void mfd_assert_write(int fd)
257 /* verify write() succeeds */
258 l = write(fd, "\0\0\0\0", 4);
260 printf("write() failed: %m\n");
264 /* verify PROT_READ | PROT_WRITE is allowed */
267 PROT_READ | PROT_WRITE,
271 if (p == MAP_FAILED) {
272 printf("mmap() failed: %m\n");
276 munmap(p, MFD_DEF_SIZE);
278 /* verify PROT_WRITE is allowed */
285 if (p == MAP_FAILED) {
286 printf("mmap() failed: %m\n");
290 munmap(p, MFD_DEF_SIZE);
292 /* verify PROT_READ with MAP_SHARED is allowed and a following
293 * mprotect(PROT_WRITE) allows writing */
300 if (p == MAP_FAILED) {
301 printf("mmap() failed: %m\n");
305 r = mprotect(p, MFD_DEF_SIZE, PROT_READ | PROT_WRITE);
307 printf("mprotect() failed: %m\n");
312 munmap(p, MFD_DEF_SIZE);
314 /* verify PUNCH_HOLE works */
316 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
320 printf("fallocate(PUNCH_HOLE) failed: %m\n");
325 static void mfd_fail_write(int fd)
331 /* verify write() fails */
332 l = write(fd, "data", 4);
334 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
338 /* verify PROT_READ | PROT_WRITE is not allowed */
341 PROT_READ | PROT_WRITE,
345 if (p != MAP_FAILED) {
346 printf("mmap() didn't fail as expected\n");
350 /* verify PROT_WRITE is not allowed */
357 if (p != MAP_FAILED) {
358 printf("mmap() didn't fail as expected\n");
362 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
363 * allowed. Note that for r/w the kernel already prevents the mmap. */
370 if (p != MAP_FAILED) {
371 r = mprotect(p, MFD_DEF_SIZE, PROT_READ | PROT_WRITE);
373 printf("mmap()+mprotect() didn't fail as expected\n");
378 /* verify PUNCH_HOLE fails */
380 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
384 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
389 static void mfd_assert_shrink(int fd)
393 r = ftruncate(fd, MFD_DEF_SIZE / 2);
395 printf("ftruncate(SHRINK) failed: %m\n");
399 mfd_assert_size(fd, MFD_DEF_SIZE / 2);
401 fd2 = mfd_assert_open(fd,
402 O_RDWR | O_CREAT | O_TRUNC,
406 mfd_assert_size(fd, 0);
409 static void mfd_fail_shrink(int fd)
413 r = ftruncate(fd, MFD_DEF_SIZE / 2);
415 printf("ftruncate(SHRINK) didn't fail as expected\n");
420 O_RDWR | O_CREAT | O_TRUNC,
424 static void mfd_assert_grow(int fd)
428 r = ftruncate(fd, MFD_DEF_SIZE * 2);
430 printf("ftruncate(GROW) failed: %m\n");
434 mfd_assert_size(fd, MFD_DEF_SIZE * 2);
441 printf("fallocate(ALLOC) failed: %m\n");
445 mfd_assert_size(fd, MFD_DEF_SIZE * 4);
448 static void mfd_fail_grow(int fd)
452 r = ftruncate(fd, MFD_DEF_SIZE * 2);
454 printf("ftruncate(GROW) didn't fail as expected\n");
463 printf("fallocate(ALLOC) didn't fail as expected\n");
468 static void mfd_assert_grow_write(int fd)
470 static char buf[MFD_DEF_SIZE * 8];
473 l = pwrite(fd, buf, sizeof(buf), 0);
474 if (l != sizeof(buf)) {
475 printf("pwrite() failed: %m\n");
479 mfd_assert_size(fd, MFD_DEF_SIZE * 8);
482 static void mfd_fail_grow_write(int fd)
484 static char buf[MFD_DEF_SIZE * 8];
487 l = pwrite(fd, buf, sizeof(buf), 0);
488 if (l == sizeof(buf)) {
489 printf("pwrite() didn't fail as expected\n");
494 static int idle_thread_fn(void *arg)
499 /* dummy waiter; SIGTERM terminates us anyway */
501 sigaddset(&set, SIGTERM);
507 static pid_t spawn_idle_thread(unsigned int flags)
512 stack = malloc(STACK_SIZE);
514 printf("malloc(STACK_SIZE) failed: %m\n");
518 pid = clone(idle_thread_fn,
523 printf("clone() failed: %m\n");
530 static void join_idle_thread(pid_t pid)
533 waitpid(pid, NULL, 0);
537 * Test memfd_create() syscall
538 * Verify syscall-argument validation, including name checks, flag validation
541 static void test_create(void)
547 mfd_fail_new(NULL, 0);
549 /* test over-long name (not zero-terminated) */
550 memset(buf, 0xff, sizeof(buf));
551 mfd_fail_new(buf, 0);
553 /* test over-long zero-terminated name */
554 memset(buf, 0xff, sizeof(buf));
555 buf[sizeof(buf) - 1] = 0;
556 mfd_fail_new(buf, 0);
558 /* verify "" is a valid name */
559 fd = mfd_assert_new("", 0, 0);
562 /* verify invalid O_* open flags */
563 mfd_fail_new("", 0x0100);
564 mfd_fail_new("", ~MFD_CLOEXEC);
565 mfd_fail_new("", ~MFD_ALLOW_SEALING);
566 mfd_fail_new("", ~0);
567 mfd_fail_new("", 0x80000000U);
569 /* verify MFD_CLOEXEC is allowed */
570 fd = mfd_assert_new("", 0, MFD_CLOEXEC);
573 /* verify MFD_ALLOW_SEALING is allowed */
574 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
577 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
578 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
584 * A very basic sealing test to see whether setting/retrieving seals works.
586 static void test_basic(void)
590 fd = mfd_assert_new("kern_memfd_basic",
592 MFD_CLOEXEC | MFD_ALLOW_SEALING);
594 /* add basic seals */
595 mfd_assert_has_seals(fd, 0);
596 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
598 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
602 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
604 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
607 /* add more seals and seal against sealing */
608 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
609 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
614 /* verify that sealing no longer works */
615 mfd_fail_add_seals(fd, F_SEAL_GROW);
616 mfd_fail_add_seals(fd, 0);
620 /* verify sealing does not work without MFD_ALLOW_SEALING */
621 fd = mfd_assert_new("kern_memfd_basic",
624 mfd_assert_has_seals(fd, F_SEAL_SEAL);
625 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
628 mfd_assert_has_seals(fd, F_SEAL_SEAL);
634 * Test whether SEAL_WRITE actually prevents modifications.
636 static void test_seal_write(void)
640 fd = mfd_assert_new("kern_memfd_seal_write",
642 MFD_CLOEXEC | MFD_ALLOW_SEALING);
643 mfd_assert_has_seals(fd, 0);
644 mfd_assert_add_seals(fd, F_SEAL_WRITE);
645 mfd_assert_has_seals(fd, F_SEAL_WRITE);
649 mfd_assert_shrink(fd);
651 mfd_fail_grow_write(fd);
658 * Test whether SEAL_SHRINK actually prevents shrinking
660 static void test_seal_shrink(void)
664 fd = mfd_assert_new("kern_memfd_seal_shrink",
666 MFD_CLOEXEC | MFD_ALLOW_SEALING);
667 mfd_assert_has_seals(fd, 0);
668 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
669 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
672 mfd_assert_write(fd);
675 mfd_assert_grow_write(fd);
682 * Test whether SEAL_GROW actually prevents growing
684 static void test_seal_grow(void)
688 fd = mfd_assert_new("kern_memfd_seal_grow",
690 MFD_CLOEXEC | MFD_ALLOW_SEALING);
691 mfd_assert_has_seals(fd, 0);
692 mfd_assert_add_seals(fd, F_SEAL_GROW);
693 mfd_assert_has_seals(fd, F_SEAL_GROW);
696 mfd_assert_write(fd);
697 mfd_assert_shrink(fd);
699 mfd_fail_grow_write(fd);
705 * Test SEAL_SHRINK | SEAL_GROW
706 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
708 static void test_seal_resize(void)
712 fd = mfd_assert_new("kern_memfd_seal_resize",
714 MFD_CLOEXEC | MFD_ALLOW_SEALING);
715 mfd_assert_has_seals(fd, 0);
716 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
717 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
720 mfd_assert_write(fd);
723 mfd_fail_grow_write(fd);
729 * Test sharing via dup()
730 * Test that seals are shared between dupped FDs and they're all equal.
732 static void test_share_dup(void)
736 fd = mfd_assert_new("kern_memfd_share_dup",
738 MFD_CLOEXEC | MFD_ALLOW_SEALING);
739 mfd_assert_has_seals(fd, 0);
741 fd2 = mfd_assert_dup(fd);
742 mfd_assert_has_seals(fd2, 0);
744 mfd_assert_add_seals(fd, F_SEAL_WRITE);
745 mfd_assert_has_seals(fd, F_SEAL_WRITE);
746 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
748 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
749 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
750 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
752 mfd_assert_add_seals(fd, F_SEAL_SEAL);
753 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
754 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
756 mfd_fail_add_seals(fd, F_SEAL_GROW);
757 mfd_fail_add_seals(fd2, F_SEAL_GROW);
758 mfd_fail_add_seals(fd, F_SEAL_SEAL);
759 mfd_fail_add_seals(fd2, F_SEAL_SEAL);
763 mfd_fail_add_seals(fd, F_SEAL_GROW);
768 * Test sealing with active mmap()s
769 * Modifying seals is only allowed if no other mmap() refs exist.
771 static void test_share_mmap(void)
776 fd = mfd_assert_new("kern_memfd_share_mmap",
778 MFD_CLOEXEC | MFD_ALLOW_SEALING);
779 mfd_assert_has_seals(fd, 0);
781 /* shared/writable ref prevents sealing WRITE, but allows others */
782 p = mfd_assert_mmap_shared(fd);
783 mfd_fail_add_seals(fd, F_SEAL_WRITE);
784 mfd_assert_has_seals(fd, 0);
785 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
786 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
787 munmap(p, MFD_DEF_SIZE);
789 /* readable ref allows sealing */
790 p = mfd_assert_mmap_private(fd);
791 mfd_assert_add_seals(fd, F_SEAL_WRITE);
792 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
793 munmap(p, MFD_DEF_SIZE);
799 * Test sealing with open(/proc/self/fd/%d)
800 * Via /proc we can get access to a separate file-context for the same memfd.
801 * This is *not* like dup(), but like a real separate open(). Make sure the
802 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
804 static void test_share_open(void)
808 fd = mfd_assert_new("kern_memfd_share_open",
810 MFD_CLOEXEC | MFD_ALLOW_SEALING);
811 mfd_assert_has_seals(fd, 0);
813 fd2 = mfd_assert_open(fd, O_RDWR, 0);
814 mfd_assert_add_seals(fd, F_SEAL_WRITE);
815 mfd_assert_has_seals(fd, F_SEAL_WRITE);
816 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
818 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
819 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
820 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
823 fd = mfd_assert_open(fd2, O_RDONLY, 0);
825 mfd_fail_add_seals(fd, F_SEAL_SEAL);
826 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
827 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
830 fd2 = mfd_assert_open(fd, O_RDWR, 0);
832 mfd_assert_add_seals(fd2, F_SEAL_SEAL);
833 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
834 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
841 * Test sharing via fork()
842 * Test whether seal-modifications work as expected with forked childs.
844 static void test_share_fork(void)
849 fd = mfd_assert_new("kern_memfd_share_fork",
851 MFD_CLOEXEC | MFD_ALLOW_SEALING);
852 mfd_assert_has_seals(fd, 0);
854 pid = spawn_idle_thread(0);
855 mfd_assert_add_seals(fd, F_SEAL_SEAL);
856 mfd_assert_has_seals(fd, F_SEAL_SEAL);
858 mfd_fail_add_seals(fd, F_SEAL_WRITE);
859 mfd_assert_has_seals(fd, F_SEAL_SEAL);
861 join_idle_thread(pid);
863 mfd_fail_add_seals(fd, F_SEAL_WRITE);
864 mfd_assert_has_seals(fd, F_SEAL_SEAL);
869 int main(int argc, char **argv)
873 printf("memfd: CREATE\n");
875 printf("memfd: BASIC\n");
878 printf("memfd: SEAL-WRITE\n");
880 printf("memfd: SEAL-SHRINK\n");
882 printf("memfd: SEAL-GROW\n");
884 printf("memfd: SEAL-RESIZE\n");
887 printf("memfd: SHARE-DUP\n");
889 printf("memfd: SHARE-MMAP\n");
891 printf("memfd: SHARE-OPEN\n");
893 printf("memfd: SHARE-FORK\n");
896 /* Run test-suite in a multi-threaded environment with a shared
898 pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
899 printf("memfd: SHARE-DUP (shared file-table)\n");
901 printf("memfd: SHARE-MMAP (shared file-table)\n");
903 printf("memfd: SHARE-OPEN (shared file-table)\n");
905 printf("memfd: SHARE-FORK (shared file-table)\n");
907 join_idle_thread(pid);
909 printf("memfd: DONE\n");