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");
391 /* verify PUNCH_HOLE fails */
393 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
397 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
402 static void mfd_assert_shrink(int fd
)
406 r
= ftruncate(fd
, mfd_def_size
/ 2);
408 printf("ftruncate(SHRINK) failed: %m\n");
412 mfd_assert_size(fd
, mfd_def_size
/ 2);
414 fd2
= mfd_assert_open(fd
,
415 O_RDWR
| O_CREAT
| O_TRUNC
,
419 mfd_assert_size(fd
, 0);
422 static void mfd_fail_shrink(int fd
)
426 r
= ftruncate(fd
, mfd_def_size
/ 2);
428 printf("ftruncate(SHRINK) didn't fail as expected\n");
433 O_RDWR
| O_CREAT
| O_TRUNC
,
437 static void mfd_assert_grow(int fd
)
441 r
= ftruncate(fd
, mfd_def_size
* 2);
443 printf("ftruncate(GROW) failed: %m\n");
447 mfd_assert_size(fd
, mfd_def_size
* 2);
454 printf("fallocate(ALLOC) failed: %m\n");
458 mfd_assert_size(fd
, mfd_def_size
* 4);
461 static void mfd_fail_grow(int fd
)
465 r
= ftruncate(fd
, mfd_def_size
* 2);
467 printf("ftruncate(GROW) didn't fail as expected\n");
476 printf("fallocate(ALLOC) didn't fail as expected\n");
481 static void mfd_assert_grow_write(int fd
)
486 /* hugetlbfs does not support write */
490 buf
= malloc(mfd_def_size
* 8);
492 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
496 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
497 if (l
!= (mfd_def_size
* 8)) {
498 printf("pwrite() failed: %m\n");
502 mfd_assert_size(fd
, mfd_def_size
* 8);
505 static void mfd_fail_grow_write(int fd
)
510 /* hugetlbfs does not support write */
514 buf
= malloc(mfd_def_size
* 8);
516 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
520 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
521 if (l
== (mfd_def_size
* 8)) {
522 printf("pwrite() didn't fail as expected\n");
527 static int idle_thread_fn(void *arg
)
532 /* dummy waiter; SIGTERM terminates us anyway */
534 sigaddset(&set
, SIGTERM
);
540 static pid_t
spawn_idle_thread(unsigned int flags
)
545 stack
= malloc(STACK_SIZE
);
547 printf("malloc(STACK_SIZE) failed: %m\n");
551 pid
= clone(idle_thread_fn
,
556 printf("clone() failed: %m\n");
563 static void join_idle_thread(pid_t pid
)
566 waitpid(pid
, NULL
, 0);
570 * Test memfd_create() syscall
571 * Verify syscall-argument validation, including name checks, flag validation
574 static void test_create(void)
579 printf("%s CREATE\n", memfd_str
);
582 mfd_fail_new(NULL
, 0);
584 /* test over-long name (not zero-terminated) */
585 memset(buf
, 0xff, sizeof(buf
));
586 mfd_fail_new(buf
, 0);
588 /* test over-long zero-terminated name */
589 memset(buf
, 0xff, sizeof(buf
));
590 buf
[sizeof(buf
) - 1] = 0;
591 mfd_fail_new(buf
, 0);
593 /* verify "" is a valid name */
594 fd
= mfd_assert_new("", 0, 0);
597 /* verify invalid O_* open flags */
598 mfd_fail_new("", 0x0100);
599 mfd_fail_new("", ~MFD_CLOEXEC
);
600 mfd_fail_new("", ~MFD_ALLOW_SEALING
);
601 mfd_fail_new("", ~0);
602 mfd_fail_new("", 0x80000000U
);
604 /* verify MFD_CLOEXEC is allowed */
605 fd
= mfd_assert_new("", 0, MFD_CLOEXEC
);
608 /* verify MFD_ALLOW_SEALING is allowed */
609 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
);
612 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
613 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
| MFD_CLOEXEC
);
619 * A very basic sealing test to see whether setting/retrieving seals works.
621 static void test_basic(void)
625 printf("%s BASIC\n", memfd_str
);
627 fd
= mfd_assert_new("kern_memfd_basic",
629 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
631 /* add basic seals */
632 mfd_assert_has_seals(fd
, 0);
633 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
635 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
639 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
641 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
644 /* add more seals and seal against sealing */
645 mfd_assert_add_seals(fd
, F_SEAL_GROW
| F_SEAL_SEAL
);
646 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
651 /* verify that sealing no longer works */
652 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
653 mfd_fail_add_seals(fd
, 0);
657 /* verify sealing does not work without MFD_ALLOW_SEALING */
658 fd
= mfd_assert_new("kern_memfd_basic",
661 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
662 mfd_fail_add_seals(fd
, F_SEAL_SHRINK
|
665 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
671 * Test whether SEAL_WRITE actually prevents modifications.
673 static void test_seal_write(void)
677 printf("%s SEAL-WRITE\n", memfd_str
);
679 fd
= mfd_assert_new("kern_memfd_seal_write",
681 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
682 mfd_assert_has_seals(fd
, 0);
683 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
684 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
688 mfd_assert_shrink(fd
);
690 mfd_fail_grow_write(fd
);
697 * Test whether SEAL_SHRINK actually prevents shrinking
699 static void test_seal_shrink(void)
703 printf("%s SEAL-SHRINK\n", memfd_str
);
705 fd
= mfd_assert_new("kern_memfd_seal_shrink",
707 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
708 mfd_assert_has_seals(fd
, 0);
709 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
710 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
713 mfd_assert_write(fd
);
716 mfd_assert_grow_write(fd
);
723 * Test whether SEAL_GROW actually prevents growing
725 static void test_seal_grow(void)
729 printf("%s SEAL-GROW\n", memfd_str
);
731 fd
= mfd_assert_new("kern_memfd_seal_grow",
733 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
734 mfd_assert_has_seals(fd
, 0);
735 mfd_assert_add_seals(fd
, F_SEAL_GROW
);
736 mfd_assert_has_seals(fd
, F_SEAL_GROW
);
739 mfd_assert_write(fd
);
740 mfd_assert_shrink(fd
);
742 mfd_fail_grow_write(fd
);
748 * Test SEAL_SHRINK | SEAL_GROW
749 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
751 static void test_seal_resize(void)
755 printf("%s SEAL-RESIZE\n", memfd_str
);
757 fd
= mfd_assert_new("kern_memfd_seal_resize",
759 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
760 mfd_assert_has_seals(fd
, 0);
761 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
762 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
765 mfd_assert_write(fd
);
768 mfd_fail_grow_write(fd
);
774 * Test sharing via dup()
775 * Test that seals are shared between dupped FDs and they're all equal.
777 static void test_share_dup(char *banner
, char *b_suffix
)
781 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
783 fd
= mfd_assert_new("kern_memfd_share_dup",
785 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
786 mfd_assert_has_seals(fd
, 0);
788 fd2
= mfd_assert_dup(fd
);
789 mfd_assert_has_seals(fd2
, 0);
791 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
792 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
793 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
795 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
796 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
797 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
799 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
800 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
801 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
803 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
804 mfd_fail_add_seals(fd2
, F_SEAL_GROW
);
805 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
806 mfd_fail_add_seals(fd2
, F_SEAL_SEAL
);
810 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
815 * Test sealing with active mmap()s
816 * Modifying seals is only allowed if no other mmap() refs exist.
818 static void test_share_mmap(char *banner
, char *b_suffix
)
823 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
825 fd
= mfd_assert_new("kern_memfd_share_mmap",
827 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
828 mfd_assert_has_seals(fd
, 0);
830 /* shared/writable ref prevents sealing WRITE, but allows others */
831 p
= mfd_assert_mmap_shared(fd
);
832 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
833 mfd_assert_has_seals(fd
, 0);
834 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
835 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
836 munmap(p
, mfd_def_size
);
838 /* readable ref allows sealing */
839 p
= mfd_assert_mmap_private(fd
);
840 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
841 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
842 munmap(p
, mfd_def_size
);
848 * Test sealing with open(/proc/self/fd/%d)
849 * Via /proc we can get access to a separate file-context for the same memfd.
850 * This is *not* like dup(), but like a real separate open(). Make sure the
851 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
853 static void test_share_open(char *banner
, char *b_suffix
)
857 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
859 fd
= mfd_assert_new("kern_memfd_share_open",
861 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
862 mfd_assert_has_seals(fd
, 0);
864 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
865 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
866 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
867 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
869 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
870 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
871 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
874 fd
= mfd_assert_open(fd2
, O_RDONLY
, 0);
876 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
877 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
878 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
881 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
883 mfd_assert_add_seals(fd2
, F_SEAL_SEAL
);
884 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
885 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
892 * Test sharing via fork()
893 * Test whether seal-modifications work as expected with forked childs.
895 static void test_share_fork(char *banner
, char *b_suffix
)
900 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
902 fd
= mfd_assert_new("kern_memfd_share_fork",
904 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
905 mfd_assert_has_seals(fd
, 0);
907 pid
= spawn_idle_thread(0);
908 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
909 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
911 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
912 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
914 join_idle_thread(pid
);
916 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
917 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
922 int main(int argc
, char **argv
)
927 if (!strcmp(argv
[1], "hugetlbfs")) {
928 unsigned long hpage_size
= default_huge_page_size();
931 printf("Unable to determine huge page size\n");
936 memfd_str
= MEMFD_HUGE_STR
;
937 mfd_def_size
= hpage_size
* 2;
939 printf("Unknown option: %s\n", argv
[1]);
952 test_share_dup("SHARE-DUP", "");
953 test_share_mmap("SHARE-MMAP", "");
954 test_share_open("SHARE-OPEN", "");
955 test_share_fork("SHARE-FORK", "");
957 /* Run test-suite in a multi-threaded environment with a shared
959 pid
= spawn_idle_thread(CLONE_FILES
| CLONE_FS
| CLONE_VM
);
960 test_share_dup("SHARE-DUP", SHARED_FT_STR
);
961 test_share_mmap("SHARE-MMAP", SHARED_FT_STR
);
962 test_share_open("SHARE-OPEN", SHARED_FT_STR
);
963 test_share_fork("SHARE-FORK", SHARED_FT_STR
);
964 join_idle_thread(pid
);
966 printf("memfd: DONE\n");