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>
22 #define MEMFD_STR "memfd:"
23 #define SHARED_FT_STR "(shared file-table)"
25 #define MFD_DEF_SIZE 8192
26 #define STACK_SIZE 65536
29 * Default is not to test hugetlbfs
31 static int hugetlbfs_test;
32 static size_t mfd_def_size = MFD_DEF_SIZE;
35 * Copied from mlock2-tests.c
37 static unsigned long default_huge_page_size(void)
39 unsigned long hps = 0;
42 FILE *f = fopen("/proc/meminfo", "r");
46 while (getline(&line, &linelen, f) > 0) {
47 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
58 static int sys_memfd_create(const char *name,
64 return syscall(__NR_memfd_create, name, flags);
67 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
71 fd = sys_memfd_create(name, flags);
73 printf("memfd_create(\"%s\", %u) failed: %m\n",
78 r = ftruncate(fd, sz);
80 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
87 static void mfd_fail_new(const char *name, unsigned int flags)
91 r = sys_memfd_create(name, flags);
93 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
100 static unsigned int mfd_assert_get_seals(int fd)
104 r = fcntl(fd, F_GET_SEALS);
106 printf("GET_SEALS(%d) failed: %m\n", fd);
110 return (unsigned int)r;
113 static void mfd_assert_has_seals(int fd, unsigned int seals)
117 s = mfd_assert_get_seals(fd);
119 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
124 static void mfd_assert_add_seals(int fd, unsigned int seals)
129 s = mfd_assert_get_seals(fd);
130 r = fcntl(fd, F_ADD_SEALS, seals);
132 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
137 static void mfd_fail_add_seals(int fd, unsigned int seals)
142 r = fcntl(fd, F_GET_SEALS);
148 r = fcntl(fd, F_ADD_SEALS, seals);
150 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
156 static void mfd_assert_size(int fd, size_t size)
163 printf("fstat(%d) failed: %m\n", fd);
165 } else if (st.st_size != size) {
166 printf("wrong file size %lld, but expected %lld\n",
167 (long long)st.st_size, (long long)size);
172 static int mfd_assert_dup(int fd)
178 printf("dup(%d) failed: %m\n", fd);
185 static void *mfd_assert_mmap_shared(int fd)
191 PROT_READ | PROT_WRITE,
195 if (p == MAP_FAILED) {
196 printf("mmap() failed: %m\n");
203 static void *mfd_assert_mmap_private(int fd)
213 if (p == MAP_FAILED) {
214 printf("mmap() failed: %m\n");
221 static int mfd_assert_open(int fd, int flags, mode_t mode)
226 sprintf(buf, "/proc/self/fd/%d", fd);
227 r = open(buf, flags, mode);
229 printf("open(%s) failed: %m\n", buf);
236 static void mfd_fail_open(int fd, int flags, mode_t mode)
241 sprintf(buf, "/proc/self/fd/%d", fd);
242 r = open(buf, flags, mode);
244 printf("open(%s) didn't fail as expected\n", buf);
249 static void mfd_assert_read(int fd)
255 l = read(fd, buf, sizeof(buf));
256 if (l != sizeof(buf)) {
257 printf("read() failed: %m\n");
261 /* verify PROT_READ *is* allowed */
268 if (p == MAP_FAILED) {
269 printf("mmap() failed: %m\n");
272 munmap(p, mfd_def_size);
274 /* verify MAP_PRIVATE is *always* allowed (even writable) */
277 PROT_READ | PROT_WRITE,
281 if (p == MAP_FAILED) {
282 printf("mmap() failed: %m\n");
285 munmap(p, mfd_def_size);
288 static void mfd_assert_write(int fd)
295 * huegtlbfs does not support write, but we want to
296 * verify everything else here.
298 if (!hugetlbfs_test) {
299 /* verify write() succeeds */
300 l = write(fd, "\0\0\0\0", 4);
302 printf("write() failed: %m\n");
307 /* verify PROT_READ | PROT_WRITE is allowed */
310 PROT_READ | PROT_WRITE,
314 if (p == MAP_FAILED) {
315 printf("mmap() failed: %m\n");
319 munmap(p, mfd_def_size);
321 /* verify PROT_WRITE is allowed */
328 if (p == MAP_FAILED) {
329 printf("mmap() failed: %m\n");
333 munmap(p, mfd_def_size);
335 /* verify PROT_READ with MAP_SHARED is allowed and a following
336 * mprotect(PROT_WRITE) allows writing */
343 if (p == MAP_FAILED) {
344 printf("mmap() failed: %m\n");
348 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
350 printf("mprotect() failed: %m\n");
355 munmap(p, mfd_def_size);
357 /* verify PUNCH_HOLE works */
359 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
363 printf("fallocate(PUNCH_HOLE) failed: %m\n");
368 static void mfd_fail_write(int fd)
374 /* verify write() fails */
375 l = write(fd, "data", 4);
377 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
381 /* verify PROT_READ | PROT_WRITE is not allowed */
384 PROT_READ | PROT_WRITE,
388 if (p != MAP_FAILED) {
389 printf("mmap() didn't fail as expected\n");
393 /* verify PROT_WRITE is not allowed */
400 if (p != MAP_FAILED) {
401 printf("mmap() didn't fail as expected\n");
405 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
406 * allowed. Note that for r/w the kernel already prevents the mmap. */
413 if (p != MAP_FAILED) {
414 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
416 printf("mmap()+mprotect() didn't fail as expected\n");
421 /* verify PUNCH_HOLE fails */
423 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
427 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
432 static void mfd_assert_shrink(int fd)
436 r = ftruncate(fd, mfd_def_size / 2);
438 printf("ftruncate(SHRINK) failed: %m\n");
442 mfd_assert_size(fd, mfd_def_size / 2);
444 fd2 = mfd_assert_open(fd,
445 O_RDWR | O_CREAT | O_TRUNC,
449 mfd_assert_size(fd, 0);
452 static void mfd_fail_shrink(int fd)
456 r = ftruncate(fd, mfd_def_size / 2);
458 printf("ftruncate(SHRINK) didn't fail as expected\n");
463 O_RDWR | O_CREAT | O_TRUNC,
467 static void mfd_assert_grow(int fd)
471 r = ftruncate(fd, mfd_def_size * 2);
473 printf("ftruncate(GROW) failed: %m\n");
477 mfd_assert_size(fd, mfd_def_size * 2);
484 printf("fallocate(ALLOC) failed: %m\n");
488 mfd_assert_size(fd, mfd_def_size * 4);
491 static void mfd_fail_grow(int fd)
495 r = ftruncate(fd, mfd_def_size * 2);
497 printf("ftruncate(GROW) didn't fail as expected\n");
506 printf("fallocate(ALLOC) didn't fail as expected\n");
511 static void mfd_assert_grow_write(int fd)
516 buf = malloc(mfd_def_size * 8);
518 printf("malloc(%d) failed: %m\n", mfd_def_size * 8);
522 l = pwrite(fd, buf, mfd_def_size * 8, 0);
523 if (l != (mfd_def_size * 8)) {
524 printf("pwrite() failed: %m\n");
528 mfd_assert_size(fd, mfd_def_size * 8);
531 static void mfd_fail_grow_write(int fd)
536 buf = malloc(mfd_def_size * 8);
538 printf("malloc(%d) failed: %m\n", mfd_def_size * 8);
542 l = pwrite(fd, buf, mfd_def_size * 8, 0);
543 if (l == (mfd_def_size * 8)) {
544 printf("pwrite() didn't fail as expected\n");
549 static int idle_thread_fn(void *arg)
554 /* dummy waiter; SIGTERM terminates us anyway */
556 sigaddset(&set, SIGTERM);
562 static pid_t spawn_idle_thread(unsigned int flags)
567 stack = malloc(STACK_SIZE);
569 printf("malloc(STACK_SIZE) failed: %m\n");
573 pid = clone(idle_thread_fn,
578 printf("clone() failed: %m\n");
585 static void join_idle_thread(pid_t pid)
588 waitpid(pid, NULL, 0);
592 * Test memfd_create() syscall
593 * Verify syscall-argument validation, including name checks, flag validation
596 static void test_create(void)
601 printf("%s CREATE\n", MEMFD_STR);
604 mfd_fail_new(NULL, 0);
606 /* test over-long name (not zero-terminated) */
607 memset(buf, 0xff, sizeof(buf));
608 mfd_fail_new(buf, 0);
610 /* test over-long zero-terminated name */
611 memset(buf, 0xff, sizeof(buf));
612 buf[sizeof(buf) - 1] = 0;
613 mfd_fail_new(buf, 0);
615 /* verify "" is a valid name */
616 fd = mfd_assert_new("", 0, 0);
619 /* verify invalid O_* open flags */
620 mfd_fail_new("", 0x0100);
621 mfd_fail_new("", ~MFD_CLOEXEC);
622 mfd_fail_new("", ~MFD_ALLOW_SEALING);
623 mfd_fail_new("", ~0);
624 mfd_fail_new("", 0x80000000U);
626 /* verify MFD_CLOEXEC is allowed */
627 fd = mfd_assert_new("", 0, MFD_CLOEXEC);
630 if (!hugetlbfs_test) {
631 /* verify MFD_ALLOW_SEALING is allowed */
632 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
635 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
636 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
639 /* sealing is not supported on hugetlbfs */
640 mfd_fail_new("", MFD_ALLOW_SEALING);
646 * A very basic sealing test to see whether setting/retrieving seals works.
648 static void test_basic(void)
652 /* hugetlbfs does not contain sealing support */
656 printf("%s BASIC\n", MEMFD_STR);
658 fd = mfd_assert_new("kern_memfd_basic",
660 MFD_CLOEXEC | MFD_ALLOW_SEALING);
662 /* add basic seals */
663 mfd_assert_has_seals(fd, 0);
664 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
666 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
670 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
672 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
675 /* add more seals and seal against sealing */
676 mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
677 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
682 /* verify that sealing no longer works */
683 mfd_fail_add_seals(fd, F_SEAL_GROW);
684 mfd_fail_add_seals(fd, 0);
688 /* verify sealing does not work without MFD_ALLOW_SEALING */
689 fd = mfd_assert_new("kern_memfd_basic",
692 mfd_assert_has_seals(fd, F_SEAL_SEAL);
693 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
696 mfd_assert_has_seals(fd, F_SEAL_SEAL);
701 * hugetlbfs doesn't support seals or write, so just verify grow and shrink
702 * on a hugetlbfs file created via memfd_create.
704 static void test_hugetlbfs_grow_shrink(void)
708 printf("%s HUGETLBFS-GROW-SHRINK\n", MEMFD_STR);
710 fd = mfd_assert_new("kern_memfd_seal_write",
715 mfd_assert_write(fd);
716 mfd_assert_shrink(fd);
724 * Test whether SEAL_WRITE actually prevents modifications.
726 static void test_seal_write(void)
731 * hugetlbfs does not contain sealing or write support. Just test
732 * basic grow and shrink via test_hugetlbfs_grow_shrink.
735 return test_hugetlbfs_grow_shrink();
737 printf("%s SEAL-WRITE\n", MEMFD_STR);
739 fd = mfd_assert_new("kern_memfd_seal_write",
741 MFD_CLOEXEC | MFD_ALLOW_SEALING);
742 mfd_assert_has_seals(fd, 0);
743 mfd_assert_add_seals(fd, F_SEAL_WRITE);
744 mfd_assert_has_seals(fd, F_SEAL_WRITE);
748 mfd_assert_shrink(fd);
750 mfd_fail_grow_write(fd);
757 * Test whether SEAL_SHRINK actually prevents shrinking
759 static void test_seal_shrink(void)
763 /* hugetlbfs does not contain sealing support */
767 printf("%s SEAL-SHRINK\n", MEMFD_STR);
769 fd = mfd_assert_new("kern_memfd_seal_shrink",
771 MFD_CLOEXEC | MFD_ALLOW_SEALING);
772 mfd_assert_has_seals(fd, 0);
773 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
774 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
777 mfd_assert_write(fd);
780 mfd_assert_grow_write(fd);
787 * Test whether SEAL_GROW actually prevents growing
789 static void test_seal_grow(void)
793 /* hugetlbfs does not contain sealing support */
797 printf("%s SEAL-GROW\n", MEMFD_STR);
799 fd = mfd_assert_new("kern_memfd_seal_grow",
801 MFD_CLOEXEC | MFD_ALLOW_SEALING);
802 mfd_assert_has_seals(fd, 0);
803 mfd_assert_add_seals(fd, F_SEAL_GROW);
804 mfd_assert_has_seals(fd, F_SEAL_GROW);
807 mfd_assert_write(fd);
808 mfd_assert_shrink(fd);
810 mfd_fail_grow_write(fd);
816 * Test SEAL_SHRINK | SEAL_GROW
817 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
819 static void test_seal_resize(void)
823 /* hugetlbfs does not contain sealing support */
827 printf("%s SEAL-RESIZE\n", MEMFD_STR);
829 fd = mfd_assert_new("kern_memfd_seal_resize",
831 MFD_CLOEXEC | MFD_ALLOW_SEALING);
832 mfd_assert_has_seals(fd, 0);
833 mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
834 mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
837 mfd_assert_write(fd);
840 mfd_fail_grow_write(fd);
846 * hugetlbfs does not support seals. Basic test to dup the memfd created
847 * fd and perform some basic operations on it.
849 static void hugetlbfs_dup(char *b_suffix)
853 printf("%s HUGETLBFS-DUP %s\n", MEMFD_STR, b_suffix);
855 fd = mfd_assert_new("kern_memfd_share_dup",
859 fd2 = mfd_assert_dup(fd);
862 mfd_assert_write(fd);
864 mfd_assert_shrink(fd2);
865 mfd_assert_grow(fd2);
872 * Test sharing via dup()
873 * Test that seals are shared between dupped FDs and they're all equal.
875 static void test_share_dup(char *banner, char *b_suffix)
880 * hugetlbfs does not contain sealing support. Perform some
881 * basic testing on dup'ed fd instead via hugetlbfs_dup.
883 if (hugetlbfs_test) {
884 hugetlbfs_dup(b_suffix);
888 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
890 fd = mfd_assert_new("kern_memfd_share_dup",
892 MFD_CLOEXEC | MFD_ALLOW_SEALING);
893 mfd_assert_has_seals(fd, 0);
895 fd2 = mfd_assert_dup(fd);
896 mfd_assert_has_seals(fd2, 0);
898 mfd_assert_add_seals(fd, F_SEAL_WRITE);
899 mfd_assert_has_seals(fd, F_SEAL_WRITE);
900 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
902 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
903 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
904 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
906 mfd_assert_add_seals(fd, F_SEAL_SEAL);
907 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
908 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
910 mfd_fail_add_seals(fd, F_SEAL_GROW);
911 mfd_fail_add_seals(fd2, F_SEAL_GROW);
912 mfd_fail_add_seals(fd, F_SEAL_SEAL);
913 mfd_fail_add_seals(fd2, F_SEAL_SEAL);
917 mfd_fail_add_seals(fd, F_SEAL_GROW);
922 * Test sealing with active mmap()s
923 * Modifying seals is only allowed if no other mmap() refs exist.
925 static void test_share_mmap(char *banner, char *b_suffix)
930 /* hugetlbfs does not contain sealing support */
934 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
936 fd = mfd_assert_new("kern_memfd_share_mmap",
938 MFD_CLOEXEC | MFD_ALLOW_SEALING);
939 mfd_assert_has_seals(fd, 0);
941 /* shared/writable ref prevents sealing WRITE, but allows others */
942 p = mfd_assert_mmap_shared(fd);
943 mfd_fail_add_seals(fd, F_SEAL_WRITE);
944 mfd_assert_has_seals(fd, 0);
945 mfd_assert_add_seals(fd, F_SEAL_SHRINK);
946 mfd_assert_has_seals(fd, F_SEAL_SHRINK);
947 munmap(p, mfd_def_size);
949 /* readable ref allows sealing */
950 p = mfd_assert_mmap_private(fd);
951 mfd_assert_add_seals(fd, F_SEAL_WRITE);
952 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
953 munmap(p, mfd_def_size);
959 * Basic test to make sure we can open the hugetlbfs fd via /proc and
960 * perform some simple operations on it.
962 static void hugetlbfs_proc_open(char *b_suffix)
966 printf("%s HUGETLBFS-PROC-OPEN %s\n", MEMFD_STR, b_suffix);
968 fd = mfd_assert_new("kern_memfd_share_open",
972 fd2 = mfd_assert_open(fd, O_RDWR, 0);
975 mfd_assert_write(fd);
977 mfd_assert_shrink(fd2);
978 mfd_assert_grow(fd2);
985 * Test sealing with open(/proc/self/fd/%d)
986 * Via /proc we can get access to a separate file-context for the same memfd.
987 * This is *not* like dup(), but like a real separate open(). Make sure the
988 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
990 static void test_share_open(char *banner, char *b_suffix)
995 * hugetlbfs does not contain sealing support. So test basic
996 * functionality of using /proc fd via hugetlbfs_proc_open
998 if (hugetlbfs_test) {
999 hugetlbfs_proc_open(b_suffix);
1003 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
1005 fd = mfd_assert_new("kern_memfd_share_open",
1007 MFD_CLOEXEC | MFD_ALLOW_SEALING);
1008 mfd_assert_has_seals(fd, 0);
1010 fd2 = mfd_assert_open(fd, O_RDWR, 0);
1011 mfd_assert_add_seals(fd, F_SEAL_WRITE);
1012 mfd_assert_has_seals(fd, F_SEAL_WRITE);
1013 mfd_assert_has_seals(fd2, F_SEAL_WRITE);
1015 mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
1016 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
1017 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
1020 fd = mfd_assert_open(fd2, O_RDONLY, 0);
1022 mfd_fail_add_seals(fd, F_SEAL_SEAL);
1023 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
1024 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
1027 fd2 = mfd_assert_open(fd, O_RDWR, 0);
1029 mfd_assert_add_seals(fd2, F_SEAL_SEAL);
1030 mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
1031 mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
1038 * Test sharing via fork()
1039 * Test whether seal-modifications work as expected with forked childs.
1041 static void test_share_fork(char *banner, char *b_suffix)
1046 /* hugetlbfs does not contain sealing support */
1050 printf("%s %s %s\n", MEMFD_STR, banner, b_suffix);
1052 fd = mfd_assert_new("kern_memfd_share_fork",
1054 MFD_CLOEXEC | MFD_ALLOW_SEALING);
1055 mfd_assert_has_seals(fd, 0);
1057 pid = spawn_idle_thread(0);
1058 mfd_assert_add_seals(fd, F_SEAL_SEAL);
1059 mfd_assert_has_seals(fd, F_SEAL_SEAL);
1061 mfd_fail_add_seals(fd, F_SEAL_WRITE);
1062 mfd_assert_has_seals(fd, F_SEAL_SEAL);
1064 join_idle_thread(pid);
1066 mfd_fail_add_seals(fd, F_SEAL_WRITE);
1067 mfd_assert_has_seals(fd, F_SEAL_SEAL);
1072 int main(int argc, char **argv)
1077 if (!strcmp(argv[1], "hugetlbfs")) {
1078 unsigned long hpage_size = default_huge_page_size();
1081 printf("Unable to determine huge page size\n");
1086 mfd_def_size = hpage_size * 2;
1098 test_share_dup("SHARE-DUP", "");
1099 test_share_mmap("SHARE-MMAP", "");
1100 test_share_open("SHARE-OPEN", "");
1101 test_share_fork("SHARE-FORK", "");
1103 /* Run test-suite in a multi-threaded environment with a shared
1105 pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
1106 test_share_dup("SHARE-DUP", SHARED_FT_STR);
1107 test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
1108 test_share_open("SHARE-OPEN", SHARED_FT_STR);
1109 test_share_fork("SHARE-FORK", SHARED_FT_STR);
1110 join_idle_thread(pid);
1112 printf("memfd: DONE\n");