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 int mfd_assert_reopen_fd(int fd_in
)
62 sprintf(path
, "/proc/self/fd/%d", fd_in
);
64 fd
= open(path
, O_RDWR
);
66 printf("re-open of existing fd %d failed\n", fd_in
);
73 static void mfd_fail_new(const char *name
, unsigned int flags
)
77 r
= sys_memfd_create(name
, flags
);
79 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
86 static unsigned int mfd_assert_get_seals(int fd
)
90 r
= fcntl(fd
, F_GET_SEALS
);
92 printf("GET_SEALS(%d) failed: %m\n", fd
);
96 return (unsigned int)r
;
99 static void mfd_assert_has_seals(int fd
, unsigned int seals
)
103 s
= mfd_assert_get_seals(fd
);
105 printf("%u != %u = GET_SEALS(%d)\n", seals
, s
, fd
);
110 static void mfd_assert_add_seals(int fd
, unsigned int seals
)
115 s
= mfd_assert_get_seals(fd
);
116 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
118 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd
, s
, seals
);
123 static void mfd_fail_add_seals(int fd
, unsigned int seals
)
128 r
= fcntl(fd
, F_GET_SEALS
);
134 r
= fcntl(fd
, F_ADD_SEALS
, seals
);
136 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
142 static void mfd_assert_size(int fd
, size_t size
)
149 printf("fstat(%d) failed: %m\n", fd
);
151 } else if (st
.st_size
!= size
) {
152 printf("wrong file size %lld, but expected %lld\n",
153 (long long)st
.st_size
, (long long)size
);
158 static int mfd_assert_dup(int fd
)
164 printf("dup(%d) failed: %m\n", fd
);
171 static void *mfd_assert_mmap_shared(int fd
)
177 PROT_READ
| PROT_WRITE
,
181 if (p
== MAP_FAILED
) {
182 printf("mmap() failed: %m\n");
189 static void *mfd_assert_mmap_private(int fd
)
199 if (p
== MAP_FAILED
) {
200 printf("mmap() failed: %m\n");
207 static int mfd_assert_open(int fd
, int flags
, mode_t mode
)
212 sprintf(buf
, "/proc/self/fd/%d", fd
);
213 r
= open(buf
, flags
, mode
);
215 printf("open(%s) failed: %m\n", buf
);
222 static void mfd_fail_open(int fd
, int flags
, mode_t mode
)
227 sprintf(buf
, "/proc/self/fd/%d", fd
);
228 r
= open(buf
, flags
, mode
);
230 printf("open(%s) didn't fail as expected\n", buf
);
235 static void mfd_assert_read(int fd
)
241 l
= read(fd
, buf
, sizeof(buf
));
242 if (l
!= sizeof(buf
)) {
243 printf("read() failed: %m\n");
247 /* verify PROT_READ *is* allowed */
254 if (p
== MAP_FAILED
) {
255 printf("mmap() failed: %m\n");
258 munmap(p
, mfd_def_size
);
260 /* verify MAP_PRIVATE is *always* allowed (even writable) */
263 PROT_READ
| PROT_WRITE
,
267 if (p
== MAP_FAILED
) {
268 printf("mmap() failed: %m\n");
271 munmap(p
, mfd_def_size
);
274 /* Test that PROT_READ + MAP_SHARED mappings work. */
275 static void mfd_assert_read_shared(int fd
)
279 /* verify PROT_READ and MAP_SHARED *is* allowed */
286 if (p
== MAP_FAILED
) {
287 printf("mmap() failed: %m\n");
290 munmap(p
, mfd_def_size
);
293 static void mfd_assert_write(int fd
)
300 * huegtlbfs does not support write, but we want to
301 * verify everything else here.
303 if (!hugetlbfs_test
) {
304 /* verify write() succeeds */
305 l
= write(fd
, "\0\0\0\0", 4);
307 printf("write() failed: %m\n");
312 /* verify PROT_READ | PROT_WRITE is allowed */
315 PROT_READ
| PROT_WRITE
,
319 if (p
== MAP_FAILED
) {
320 printf("mmap() failed: %m\n");
324 munmap(p
, mfd_def_size
);
326 /* verify PROT_WRITE is allowed */
333 if (p
== MAP_FAILED
) {
334 printf("mmap() failed: %m\n");
338 munmap(p
, mfd_def_size
);
340 /* verify PROT_READ with MAP_SHARED is allowed and a following
341 * mprotect(PROT_WRITE) allows writing */
348 if (p
== MAP_FAILED
) {
349 printf("mmap() failed: %m\n");
353 r
= mprotect(p
, mfd_def_size
, PROT_READ
| PROT_WRITE
);
355 printf("mprotect() failed: %m\n");
360 munmap(p
, mfd_def_size
);
362 /* verify PUNCH_HOLE works */
364 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
368 printf("fallocate(PUNCH_HOLE) failed: %m\n");
373 static void mfd_fail_write(int fd
)
379 /* verify write() fails */
380 l
= write(fd
, "data", 4);
382 printf("expected EPERM on write(), but got %d: %m\n", (int)l
);
386 /* verify PROT_READ | PROT_WRITE is not allowed */
389 PROT_READ
| PROT_WRITE
,
393 if (p
!= MAP_FAILED
) {
394 printf("mmap() didn't fail as expected\n");
398 /* verify PROT_WRITE is not allowed */
405 if (p
!= MAP_FAILED
) {
406 printf("mmap() didn't fail as expected\n");
410 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
411 * allowed. Note that for r/w the kernel already prevents the mmap. */
418 if (p
!= MAP_FAILED
) {
419 r
= mprotect(p
, mfd_def_size
, PROT_READ
| PROT_WRITE
);
421 printf("mmap()+mprotect() didn't fail as expected\n");
426 /* verify PUNCH_HOLE fails */
428 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
432 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
437 static void mfd_assert_shrink(int fd
)
441 r
= ftruncate(fd
, mfd_def_size
/ 2);
443 printf("ftruncate(SHRINK) failed: %m\n");
447 mfd_assert_size(fd
, mfd_def_size
/ 2);
449 fd2
= mfd_assert_open(fd
,
450 O_RDWR
| O_CREAT
| O_TRUNC
,
454 mfd_assert_size(fd
, 0);
457 static void mfd_fail_shrink(int fd
)
461 r
= ftruncate(fd
, mfd_def_size
/ 2);
463 printf("ftruncate(SHRINK) didn't fail as expected\n");
468 O_RDWR
| O_CREAT
| O_TRUNC
,
472 static void mfd_assert_grow(int fd
)
476 r
= ftruncate(fd
, mfd_def_size
* 2);
478 printf("ftruncate(GROW) failed: %m\n");
482 mfd_assert_size(fd
, mfd_def_size
* 2);
489 printf("fallocate(ALLOC) failed: %m\n");
493 mfd_assert_size(fd
, mfd_def_size
* 4);
496 static void mfd_fail_grow(int fd
)
500 r
= ftruncate(fd
, mfd_def_size
* 2);
502 printf("ftruncate(GROW) didn't fail as expected\n");
511 printf("fallocate(ALLOC) didn't fail as expected\n");
516 static void mfd_assert_grow_write(int fd
)
521 /* hugetlbfs does not support write */
525 buf
= malloc(mfd_def_size
* 8);
527 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
531 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
532 if (l
!= (mfd_def_size
* 8)) {
533 printf("pwrite() failed: %m\n");
537 mfd_assert_size(fd
, mfd_def_size
* 8);
540 static void mfd_fail_grow_write(int fd
)
545 /* hugetlbfs does not support write */
549 buf
= malloc(mfd_def_size
* 8);
551 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
555 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
556 if (l
== (mfd_def_size
* 8)) {
557 printf("pwrite() didn't fail as expected\n");
562 static int idle_thread_fn(void *arg
)
567 /* dummy waiter; SIGTERM terminates us anyway */
569 sigaddset(&set
, SIGTERM
);
575 static pid_t
spawn_idle_thread(unsigned int flags
)
580 stack
= malloc(STACK_SIZE
);
582 printf("malloc(STACK_SIZE) failed: %m\n");
586 pid
= clone(idle_thread_fn
,
591 printf("clone() failed: %m\n");
598 static void join_idle_thread(pid_t pid
)
601 waitpid(pid
, NULL
, 0);
605 * Test memfd_create() syscall
606 * Verify syscall-argument validation, including name checks, flag validation
609 static void test_create(void)
614 printf("%s CREATE\n", memfd_str
);
617 mfd_fail_new(NULL
, 0);
619 /* test over-long name (not zero-terminated) */
620 memset(buf
, 0xff, sizeof(buf
));
621 mfd_fail_new(buf
, 0);
623 /* test over-long zero-terminated name */
624 memset(buf
, 0xff, sizeof(buf
));
625 buf
[sizeof(buf
) - 1] = 0;
626 mfd_fail_new(buf
, 0);
628 /* verify "" is a valid name */
629 fd
= mfd_assert_new("", 0, 0);
632 /* verify invalid O_* open flags */
633 mfd_fail_new("", 0x0100);
634 mfd_fail_new("", ~MFD_CLOEXEC
);
635 mfd_fail_new("", ~MFD_ALLOW_SEALING
);
636 mfd_fail_new("", ~0);
637 mfd_fail_new("", 0x80000000U
);
639 /* verify MFD_CLOEXEC is allowed */
640 fd
= mfd_assert_new("", 0, MFD_CLOEXEC
);
643 /* verify MFD_ALLOW_SEALING is allowed */
644 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
);
647 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
648 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
| MFD_CLOEXEC
);
654 * A very basic sealing test to see whether setting/retrieving seals works.
656 static void test_basic(void)
660 printf("%s BASIC\n", memfd_str
);
662 fd
= mfd_assert_new("kern_memfd_basic",
664 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
666 /* add basic seals */
667 mfd_assert_has_seals(fd
, 0);
668 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
670 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
674 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
676 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
679 /* add more seals and seal against sealing */
680 mfd_assert_add_seals(fd
, F_SEAL_GROW
| F_SEAL_SEAL
);
681 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
686 /* verify that sealing no longer works */
687 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
688 mfd_fail_add_seals(fd
, 0);
692 /* verify sealing does not work without MFD_ALLOW_SEALING */
693 fd
= mfd_assert_new("kern_memfd_basic",
696 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
697 mfd_fail_add_seals(fd
, F_SEAL_SHRINK
|
700 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
706 * Test whether SEAL_WRITE actually prevents modifications.
708 static void test_seal_write(void)
712 printf("%s SEAL-WRITE\n", memfd_str
);
714 fd
= mfd_assert_new("kern_memfd_seal_write",
716 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
717 mfd_assert_has_seals(fd
, 0);
718 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
719 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
723 mfd_assert_shrink(fd
);
725 mfd_fail_grow_write(fd
);
731 * Test SEAL_FUTURE_WRITE
732 * Test whether SEAL_FUTURE_WRITE actually prevents modifications.
734 static void test_seal_future_write(void)
739 printf("%s SEAL-FUTURE-WRITE\n", memfd_str
);
741 fd
= mfd_assert_new("kern_memfd_seal_future_write",
743 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
745 p
= mfd_assert_mmap_shared(fd
);
747 mfd_assert_has_seals(fd
, 0);
749 mfd_assert_add_seals(fd
, F_SEAL_FUTURE_WRITE
);
750 mfd_assert_has_seals(fd
, F_SEAL_FUTURE_WRITE
);
752 /* read should pass, writes should fail */
754 mfd_assert_read_shared(fd
);
757 fd2
= mfd_assert_reopen_fd(fd
);
758 /* read should pass, writes should still fail */
759 mfd_assert_read(fd2
);
760 mfd_assert_read_shared(fd2
);
763 munmap(p
, mfd_def_size
);
770 * Test whether SEAL_SHRINK actually prevents shrinking
772 static void test_seal_shrink(void)
776 printf("%s SEAL-SHRINK\n", memfd_str
);
778 fd
= mfd_assert_new("kern_memfd_seal_shrink",
780 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
781 mfd_assert_has_seals(fd
, 0);
782 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
783 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
786 mfd_assert_write(fd
);
789 mfd_assert_grow_write(fd
);
796 * Test whether SEAL_GROW actually prevents growing
798 static void test_seal_grow(void)
802 printf("%s SEAL-GROW\n", memfd_str
);
804 fd
= mfd_assert_new("kern_memfd_seal_grow",
806 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
807 mfd_assert_has_seals(fd
, 0);
808 mfd_assert_add_seals(fd
, F_SEAL_GROW
);
809 mfd_assert_has_seals(fd
, F_SEAL_GROW
);
812 mfd_assert_write(fd
);
813 mfd_assert_shrink(fd
);
815 mfd_fail_grow_write(fd
);
821 * Test SEAL_SHRINK | SEAL_GROW
822 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
824 static void test_seal_resize(void)
828 printf("%s SEAL-RESIZE\n", memfd_str
);
830 fd
= mfd_assert_new("kern_memfd_seal_resize",
832 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
833 mfd_assert_has_seals(fd
, 0);
834 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
835 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
838 mfd_assert_write(fd
);
841 mfd_fail_grow_write(fd
);
847 * Test sharing via dup()
848 * Test that seals are shared between dupped FDs and they're all equal.
850 static void test_share_dup(char *banner
, char *b_suffix
)
854 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
856 fd
= mfd_assert_new("kern_memfd_share_dup",
858 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
859 mfd_assert_has_seals(fd
, 0);
861 fd2
= mfd_assert_dup(fd
);
862 mfd_assert_has_seals(fd2
, 0);
864 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
865 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
866 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
868 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
869 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
870 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
872 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
873 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
874 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
876 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
877 mfd_fail_add_seals(fd2
, F_SEAL_GROW
);
878 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
879 mfd_fail_add_seals(fd2
, F_SEAL_SEAL
);
883 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
888 * Test sealing with active mmap()s
889 * Modifying seals is only allowed if no other mmap() refs exist.
891 static void test_share_mmap(char *banner
, char *b_suffix
)
896 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
898 fd
= mfd_assert_new("kern_memfd_share_mmap",
900 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
901 mfd_assert_has_seals(fd
, 0);
903 /* shared/writable ref prevents sealing WRITE, but allows others */
904 p
= mfd_assert_mmap_shared(fd
);
905 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
906 mfd_assert_has_seals(fd
, 0);
907 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
908 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
909 munmap(p
, mfd_def_size
);
911 /* readable ref allows sealing */
912 p
= mfd_assert_mmap_private(fd
);
913 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
914 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
915 munmap(p
, mfd_def_size
);
921 * Test sealing with open(/proc/self/fd/%d)
922 * Via /proc we can get access to a separate file-context for the same memfd.
923 * This is *not* like dup(), but like a real separate open(). Make sure the
924 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
926 static void test_share_open(char *banner
, char *b_suffix
)
930 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
932 fd
= mfd_assert_new("kern_memfd_share_open",
934 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
935 mfd_assert_has_seals(fd
, 0);
937 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
938 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
939 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
940 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
942 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
943 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
944 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
947 fd
= mfd_assert_open(fd2
, O_RDONLY
, 0);
949 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
950 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
951 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
954 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
956 mfd_assert_add_seals(fd2
, F_SEAL_SEAL
);
957 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
958 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
965 * Test sharing via fork()
966 * Test whether seal-modifications work as expected with forked childs.
968 static void test_share_fork(char *banner
, char *b_suffix
)
973 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
975 fd
= mfd_assert_new("kern_memfd_share_fork",
977 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
978 mfd_assert_has_seals(fd
, 0);
980 pid
= spawn_idle_thread(0);
981 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
982 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
984 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
985 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
987 join_idle_thread(pid
);
989 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
990 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
995 int main(int argc
, char **argv
)
1000 if (!strcmp(argv
[1], "hugetlbfs")) {
1001 unsigned long hpage_size
= default_huge_page_size();
1004 printf("Unable to determine huge page size\n");
1009 memfd_str
= MEMFD_HUGE_STR
;
1010 mfd_def_size
= hpage_size
* 2;
1012 printf("Unknown option: %s\n", argv
[1]);
1021 test_seal_future_write();
1026 test_share_dup("SHARE-DUP", "");
1027 test_share_mmap("SHARE-MMAP", "");
1028 test_share_open("SHARE-OPEN", "");
1029 test_share_fork("SHARE-FORK", "");
1031 /* Run test-suite in a multi-threaded environment with a shared
1033 pid
= spawn_idle_thread(CLONE_FILES
| CLONE_FS
| CLONE_VM
);
1034 test_share_dup("SHARE-DUP", SHARED_FT_STR
);
1035 test_share_mmap("SHARE-MMAP", SHARED_FT_STR
);
1036 test_share_open("SHARE-OPEN", SHARED_FT_STR
);
1037 test_share_fork("SHARE-FORK", SHARED_FT_STR
);
1038 join_idle_thread(pid
);
1040 printf("memfd: DONE\n");