1 // SPDX-License-Identifier: GPL-2.0
3 #define __EXPORTED_HEADERS__
8 #include <linux/falloc.h>
9 #include <linux/fcntl.h>
10 #include <linux/memfd.h>
18 #include <sys/syscall.h>
24 #define MEMFD_STR "memfd:"
25 #define MEMFD_HUGE_STR "memfd-hugetlb:"
26 #define SHARED_FT_STR "(shared file-table)"
28 #define MFD_DEF_SIZE 8192
29 #define STACK_SIZE 65536
32 * Default is not to test hugetlbfs
34 static size_t mfd_def_size = MFD_DEF_SIZE;
35 static const char *memfd_str = MEMFD_STR;
37 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
41 fd = sys_memfd_create(name, flags);
43 printf("memfd_create(\"%s\", %u) failed: %m\n",
48 r = ftruncate(fd, sz);
50 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
57 static void mfd_fail_new(const char *name, unsigned int flags)
61 r = sys_memfd_create(name, flags);
63 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
70 static unsigned int mfd_assert_get_seals(int fd)
74 r = fcntl(fd, F_GET_SEALS);
76 printf("GET_SEALS(%d) failed: %m\n", fd);
80 return (unsigned int)r;
83 static void mfd_assert_has_seals(int fd, unsigned int seals)
87 s = mfd_assert_get_seals(fd);
89 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
94 static void mfd_assert_add_seals(int fd, unsigned int seals)
99 s = mfd_assert_get_seals(fd);
100 r = fcntl(fd, F_ADD_SEALS, seals);
102 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
107 static void mfd_fail_add_seals(int fd, unsigned int seals)
112 r = fcntl(fd, F_GET_SEALS);
118 r = fcntl(fd, F_ADD_SEALS, seals);
120 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
126 static void mfd_assert_size(int fd, size_t size)
133 printf("fstat(%d) failed: %m\n", fd);
135 } else if (st.st_size != size) {
136 printf("wrong file size %lld, but expected %lld\n",
137 (long long)st.st_size, (long long)size);
142 static int mfd_assert_dup(int fd)
148 printf("dup(%d) failed: %m\n", fd);
155 static void *mfd_assert_mmap_shared(int fd)
161 PROT_READ | PROT_WRITE,
165 if (p == MAP_FAILED) {
166 printf("mmap() failed: %m\n");
173 static void *mfd_assert_mmap_private(int fd)
183 if (p == MAP_FAILED) {
184 printf("mmap() failed: %m\n");
191 static int mfd_assert_open(int fd, int flags, mode_t mode)
196 sprintf(buf, "/proc/self/fd/%d", fd);
197 r = open(buf, flags, mode);
199 printf("open(%s) failed: %m\n", buf);
206 static void mfd_fail_open(int fd, int flags, mode_t mode)
211 sprintf(buf, "/proc/self/fd/%d", fd);
212 r = open(buf, flags, mode);
214 printf("open(%s) didn't fail as expected\n", buf);
219 static void mfd_assert_read(int fd)
225 l = read(fd, buf, sizeof(buf));
226 if (l != sizeof(buf)) {
227 printf("read() failed: %m\n");
231 /* verify PROT_READ *is* allowed */
238 if (p == MAP_FAILED) {
239 printf("mmap() failed: %m\n");
242 munmap(p, mfd_def_size);
244 /* verify MAP_PRIVATE is *always* allowed (even writable) */
247 PROT_READ | PROT_WRITE,
251 if (p == MAP_FAILED) {
252 printf("mmap() failed: %m\n");
255 munmap(p, mfd_def_size);
258 static void mfd_assert_write(int fd)
265 * huegtlbfs does not support write, but we want to
266 * verify everything else here.
268 if (!hugetlbfs_test) {
269 /* verify write() succeeds */
270 l = write(fd, "\0\0\0\0", 4);
272 printf("write() failed: %m\n");
277 /* verify PROT_READ | PROT_WRITE is allowed */
280 PROT_READ | PROT_WRITE,
284 if (p == MAP_FAILED) {
285 printf("mmap() failed: %m\n");
289 munmap(p, mfd_def_size);
291 /* verify PROT_WRITE is allowed */
298 if (p == MAP_FAILED) {
299 printf("mmap() failed: %m\n");
303 munmap(p, mfd_def_size);
305 /* verify PROT_READ with MAP_SHARED is allowed and a following
306 * mprotect(PROT_WRITE) allows writing */
313 if (p == MAP_FAILED) {
314 printf("mmap() failed: %m\n");
318 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
320 printf("mprotect() failed: %m\n");
325 munmap(p, mfd_def_size);
327 /* verify PUNCH_HOLE works */
329 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
333 printf("fallocate(PUNCH_HOLE) failed: %m\n");
338 static void mfd_fail_write(int fd)
344 /* verify write() fails */
345 l = write(fd, "data", 4);
347 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
351 /* verify PROT_READ | PROT_WRITE is not allowed */
354 PROT_READ | PROT_WRITE,
358 if (p != MAP_FAILED) {
359 printf("mmap() didn't fail as expected\n");
363 /* verify PROT_WRITE is not allowed */
370 if (p != MAP_FAILED) {
371 printf("mmap() didn't fail as expected\n");
375 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
376 * allowed. Note that for r/w the kernel already prevents the mmap. */
383 if (p != MAP_FAILED) {
384 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
386 printf("mmap()+mprotect() didn't fail as expected\n");
389 munmap(p, mfd_def_size);
392 /* verify PUNCH_HOLE fails */
394 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
398 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
403 static void mfd_assert_shrink(int fd)
407 r = ftruncate(fd, mfd_def_size / 2);
409 printf("ftruncate(SHRINK) failed: %m\n");
413 mfd_assert_size(fd, mfd_def_size / 2);
415 fd2 = mfd_assert_open(fd,
416 O_RDWR | O_CREAT | O_TRUNC,
420 mfd_assert_size(fd, 0);
423 static void mfd_fail_shrink(int fd)
427 r = ftruncate(fd, mfd_def_size / 2);
429 printf("ftruncate(SHRINK) didn't fail as expected\n");
434 O_RDWR | O_CREAT | O_TRUNC,
438 static void mfd_assert_grow(int fd)
442 r = ftruncate(fd, mfd_def_size * 2);
444 printf("ftruncate(GROW) failed: %m\n");
448 mfd_assert_size(fd, mfd_def_size * 2);
455 printf("fallocate(ALLOC) failed: %m\n");
459 mfd_assert_size(fd, mfd_def_size * 4);
462 static void mfd_fail_grow(int fd)
466 r = ftruncate(fd, mfd_def_size * 2);
468 printf("ftruncate(GROW) didn't fail as expected\n");
477 printf("fallocate(ALLOC) didn't fail as expected\n");
482 static void mfd_assert_grow_write(int fd)
487 /* hugetlbfs does not support write */
491 buf = malloc(mfd_def_size * 8);
493 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
497 l = pwrite(fd, buf, mfd_def_size * 8, 0);
498 if (l != (mfd_def_size * 8)) {
499 printf("pwrite() failed: %m\n");
503 mfd_assert_size(fd, mfd_def_size * 8);
506 static void mfd_fail_grow_write(int fd)
511 /* hugetlbfs does not support write */
515 buf = malloc(mfd_def_size * 8);
517 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
521 l = pwrite(fd, buf, mfd_def_size * 8, 0);
522 if (l == (mfd_def_size * 8)) {
523 printf("pwrite() didn't fail as expected\n");
528 static int idle_thread_fn(void *arg)
533 /* dummy waiter; SIGTERM terminates us anyway */
535 sigaddset(&set, SIGTERM);
541 static pid_t spawn_idle_thread(unsigned int flags)
546 stack = malloc(STACK_SIZE);
548 printf("malloc(STACK_SIZE) failed: %m\n");
552 pid = clone(idle_thread_fn,
557 printf("clone() failed: %m\n");
564 static void join_idle_thread(pid_t pid)
567 waitpid(pid, NULL, 0);
571 * Test memfd_create() syscall
572 * Verify syscall-argument validation, including name checks, flag validation
575 static void test_create(void)
580 printf("%s CREATE\n", memfd_str);
583 mfd_fail_new(NULL, 0);
585 /* test over-long name (not zero-terminated) */
586 memset(buf, 0xff, sizeof(buf));
587 mfd_fail_new(buf, 0);
589 /* test over-long zero-terminated name */
590 memset(buf, 0xff, sizeof(buf));
591 buf[sizeof(buf) - 1] = 0;
592 mfd_fail_new(buf, 0);
594 /* verify "" is a valid name */
595 fd = mfd_assert_new("", 0, 0);
598 /* verify invalid O_* open flags */
599 mfd_fail_new("", 0x0100);
600 mfd_fail_new("", ~MFD_CLOEXEC);
601 mfd_fail_new("", ~MFD_ALLOW_SEALING);
602 mfd_fail_new("", ~0);
603 mfd_fail_new("", 0x80000000U);
605 /* verify MFD_CLOEXEC is allowed */
606 fd = mfd_assert_new("", 0, MFD_CLOEXEC);
609 /* verify MFD_ALLOW_SEALING is allowed */
610 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
613 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
614 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
620 * A very basic sealing test to see whether setting/retrieving seals works.
622 static void test_basic(void)
626 printf("%s BASIC\n", memfd_str);
628 fd = mfd_assert_new("kern_memfd_basic",
630 MFD_CLOEXEC | MFD_ALLOW_SEALING);
632 /* add basic seals */
633 mfd_assert_has_seals(fd, 0);
634 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
636 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
640 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
642 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
645 /* add more seals and seal against sealing */
646 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
647 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
652 /* verify that sealing no longer works */
653 mfd_fail_add_seals(fd, F_SEAL_GROW);
654 mfd_fail_add_seals(fd, 0);
658 /* verify sealing does not work without MFD_ALLOW_SEALING */
659 fd = mfd_assert_new("kern_memfd_basic",
662 mfd_assert_has_seals(fd, F_SEAL_SEAL);
663 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
666 mfd_assert_has_seals(fd, F_SEAL_SEAL);
672 * Test whether SEAL_WRITE actually prevents modifications.
674 static void test_seal_write(void)
678 printf("%s SEAL-WRITE\n", memfd_str);
680 fd = mfd_assert_new("kern_memfd_seal_write",
682 MFD_CLOEXEC | MFD_ALLOW_SEALING);
683 mfd_assert_has_seals(fd, 0);
684 mfd_assert_add_seals(fd, F_SEAL_WRITE);
685 mfd_assert_has_seals(fd, F_SEAL_WRITE);
689 mfd_assert_shrink(fd);
691 mfd_fail_grow_write(fd);
698 * Test whether SEAL_SHRINK actually prevents shrinking
700 static void test_seal_shrink(void)
704 printf("%s SEAL-SHRINK\n", memfd_str);
706 fd = mfd_assert_new("kern_memfd_seal_shrink",
708 MFD_CLOEXEC | MFD_ALLOW_SEALING);
709 mfd_assert_has_seals(fd, 0);
710 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
711 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
714 mfd_assert_write(fd);
717 mfd_assert_grow_write(fd);
724 * Test whether SEAL_GROW actually prevents growing
726 static void test_seal_grow(void)
730 printf("%s SEAL-GROW\n", memfd_str);
732 fd = mfd_assert_new("kern_memfd_seal_grow",
734 MFD_CLOEXEC | MFD_ALLOW_SEALING);
735 mfd_assert_has_seals(fd, 0);
736 mfd_assert_add_seals(fd, F_SEAL_GROW);
737 mfd_assert_has_seals(fd, F_SEAL_GROW);
740 mfd_assert_write(fd);
741 mfd_assert_shrink(fd);
743 mfd_fail_grow_write(fd);
749 * Test SEAL_SHRINK | SEAL_GROW
750 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
752 static void test_seal_resize(void)
756 printf("%s SEAL-RESIZE\n", memfd_str);
758 fd = mfd_assert_new("kern_memfd_seal_resize",
760 MFD_CLOEXEC | MFD_ALLOW_SEALING);
761 mfd_assert_has_seals(fd, 0);
762 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
763 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
766 mfd_assert_write(fd);
769 mfd_fail_grow_write(fd);
775 * Test sharing via dup()
776 * Test that seals are shared between dupped FDs and they're all equal.
778 static void test_share_dup(char *banner, char *b_suffix)
782 printf("%s %s %s\n", memfd_str, banner, b_suffix);
784 fd = mfd_assert_new("kern_memfd_share_dup",
786 MFD_CLOEXEC | MFD_ALLOW_SEALING);
787 mfd_assert_has_seals(fd, 0);
789 fd2 = mfd_assert_dup(fd);
790 mfd_assert_has_seals(fd2, 0);
792 mfd_assert_add_seals(fd, F_SEAL_WRITE);
793 mfd_assert_has_seals(fd, F_SEAL_WRITE);
794 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
796 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
797 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
798 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
800 mfd_assert_add_seals(fd, F_SEAL_SEAL);
801 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
802 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
804 mfd_fail_add_seals(fd, F_SEAL_GROW);
805 mfd_fail_add_seals(fd2, F_SEAL_GROW);
806 mfd_fail_add_seals(fd, F_SEAL_SEAL);
807 mfd_fail_add_seals(fd2, F_SEAL_SEAL);
811 mfd_fail_add_seals(fd, F_SEAL_GROW);
816 * Test sealing with active mmap()s
817 * Modifying seals is only allowed if no other mmap() refs exist.
819 static void test_share_mmap(char *banner, char *b_suffix)
824 printf("%s %s %s\n", memfd_str, banner, b_suffix);
826 fd = mfd_assert_new("kern_memfd_share_mmap",
828 MFD_CLOEXEC | MFD_ALLOW_SEALING);
829 mfd_assert_has_seals(fd, 0);
831 /* shared/writable ref prevents sealing WRITE, but allows others */
832 p = mfd_assert_mmap_shared(fd);
833 mfd_fail_add_seals(fd, F_SEAL_WRITE);
834 mfd_assert_has_seals(fd, 0);
835 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
836 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
837 munmap(p, mfd_def_size);
839 /* readable ref allows sealing */
840 p = mfd_assert_mmap_private(fd);
841 mfd_assert_add_seals(fd, F_SEAL_WRITE);
842 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
843 munmap(p, mfd_def_size);
849 * Test sealing with open(/proc/self/fd/%d)
850 * Via /proc we can get access to a separate file-context for the same memfd.
851 * This is *not* like dup(), but like a real separate open(). Make sure the
852 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
854 static void test_share_open(char *banner, char *b_suffix)
858 printf("%s %s %s\n", memfd_str, banner, b_suffix);
860 fd = mfd_assert_new("kern_memfd_share_open",
862 MFD_CLOEXEC | MFD_ALLOW_SEALING);
863 mfd_assert_has_seals(fd, 0);
865 fd2 = mfd_assert_open(fd, O_RDWR, 0);
866 mfd_assert_add_seals(fd, F_SEAL_WRITE);
867 mfd_assert_has_seals(fd, F_SEAL_WRITE);
868 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
870 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
871 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
872 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
875 fd = mfd_assert_open(fd2, O_RDONLY, 0);
877 mfd_fail_add_seals(fd, F_SEAL_SEAL);
878 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
879 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
882 fd2 = mfd_assert_open(fd, O_RDWR, 0);
884 mfd_assert_add_seals(fd2, F_SEAL_SEAL);
885 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
886 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
893 * Test sharing via fork()
894 * Test whether seal-modifications work as expected with forked childs.
896 static void test_share_fork(char *banner, char *b_suffix)
901 printf("%s %s %s\n", memfd_str, banner, b_suffix);
903 fd = mfd_assert_new("kern_memfd_share_fork",
905 MFD_CLOEXEC | MFD_ALLOW_SEALING);
906 mfd_assert_has_seals(fd, 0);
908 pid = spawn_idle_thread(0);
909 mfd_assert_add_seals(fd, F_SEAL_SEAL);
910 mfd_assert_has_seals(fd, F_SEAL_SEAL);
912 mfd_fail_add_seals(fd, F_SEAL_WRITE);
913 mfd_assert_has_seals(fd, F_SEAL_SEAL);
915 join_idle_thread(pid);
917 mfd_fail_add_seals(fd, F_SEAL_WRITE);
918 mfd_assert_has_seals(fd, F_SEAL_SEAL);
923 int main(int argc, char **argv)
928 if (!strcmp(argv[1], "hugetlbfs")) {
929 unsigned long hpage_size = default_huge_page_size();
932 printf("Unable to determine huge page size\n");
937 memfd_str = MEMFD_HUGE_STR;
938 mfd_def_size = hpage_size * 2;
940 printf("Unknown option: %s\n", argv[1]);
953 test_share_dup("SHARE-DUP", "");
954 test_share_mmap("SHARE-MMAP", "");
955 test_share_open("SHARE-OPEN", "");
956 test_share_fork("SHARE-FORK", "");
958 /* Run test-suite in a multi-threaded environment with a shared
960 pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
961 test_share_dup("SHARE-DUP", SHARED_FT_STR);
962 test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
963 test_share_open("SHARE-OPEN", SHARED_FT_STR);
964 test_share_fork("SHARE-FORK", SHARED_FT_STR);
965 join_idle_thread(pid);
967 printf("memfd: DONE\n");