]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/testing/selftests/memfd/memfd_test.c
1 // SPDX-License-Identifier: GPL-2.0
3 #define __EXPORTED_HEADERS__
8 #include <linux/falloc.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_fork_private_write(int fd
)
300 PROT_READ
| PROT_WRITE
,
304 if (p
== MAP_FAILED
) {
305 printf("mmap() failed: %m\n");
316 waitpid(pid
, NULL
, 0);
319 printf("MAP_PRIVATE copy-on-write failed: %m\n");
324 munmap(p
, mfd_def_size
);
327 static void mfd_assert_write(int fd
)
334 * huegtlbfs does not support write, but we want to
335 * verify everything else here.
337 if (!hugetlbfs_test
) {
338 /* verify write() succeeds */
339 l
= write(fd
, "\0\0\0\0", 4);
341 printf("write() failed: %m\n");
346 /* verify PROT_READ | PROT_WRITE is allowed */
349 PROT_READ
| PROT_WRITE
,
353 if (p
== MAP_FAILED
) {
354 printf("mmap() failed: %m\n");
358 munmap(p
, mfd_def_size
);
360 /* verify PROT_WRITE is allowed */
367 if (p
== MAP_FAILED
) {
368 printf("mmap() failed: %m\n");
372 munmap(p
, mfd_def_size
);
374 /* verify PROT_READ with MAP_SHARED is allowed and a following
375 * mprotect(PROT_WRITE) allows writing */
382 if (p
== MAP_FAILED
) {
383 printf("mmap() failed: %m\n");
387 r
= mprotect(p
, mfd_def_size
, PROT_READ
| PROT_WRITE
);
389 printf("mprotect() failed: %m\n");
394 munmap(p
, mfd_def_size
);
396 /* verify PUNCH_HOLE works */
398 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
402 printf("fallocate(PUNCH_HOLE) failed: %m\n");
407 static void mfd_fail_write(int fd
)
413 /* verify write() fails */
414 l
= write(fd
, "data", 4);
416 printf("expected EPERM on write(), but got %d: %m\n", (int)l
);
420 /* verify PROT_READ | PROT_WRITE is not allowed */
423 PROT_READ
| PROT_WRITE
,
427 if (p
!= MAP_FAILED
) {
428 printf("mmap() didn't fail as expected\n");
432 /* verify PROT_WRITE is not allowed */
439 if (p
!= MAP_FAILED
) {
440 printf("mmap() didn't fail as expected\n");
444 /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
445 * allowed. Note that for r/w the kernel already prevents the mmap. */
452 if (p
!= MAP_FAILED
) {
453 r
= mprotect(p
, mfd_def_size
, PROT_READ
| PROT_WRITE
);
455 printf("mmap()+mprotect() didn't fail as expected\n");
458 munmap(p
, mfd_def_size
);
461 /* verify PUNCH_HOLE fails */
463 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
467 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
472 static void mfd_assert_shrink(int fd
)
476 r
= ftruncate(fd
, mfd_def_size
/ 2);
478 printf("ftruncate(SHRINK) failed: %m\n");
482 mfd_assert_size(fd
, mfd_def_size
/ 2);
484 fd2
= mfd_assert_open(fd
,
485 O_RDWR
| O_CREAT
| O_TRUNC
,
489 mfd_assert_size(fd
, 0);
492 static void mfd_fail_shrink(int fd
)
496 r
= ftruncate(fd
, mfd_def_size
/ 2);
498 printf("ftruncate(SHRINK) didn't fail as expected\n");
503 O_RDWR
| O_CREAT
| O_TRUNC
,
507 static void mfd_assert_grow(int fd
)
511 r
= ftruncate(fd
, mfd_def_size
* 2);
513 printf("ftruncate(GROW) failed: %m\n");
517 mfd_assert_size(fd
, mfd_def_size
* 2);
524 printf("fallocate(ALLOC) failed: %m\n");
528 mfd_assert_size(fd
, mfd_def_size
* 4);
531 static void mfd_fail_grow(int fd
)
535 r
= ftruncate(fd
, mfd_def_size
* 2);
537 printf("ftruncate(GROW) didn't fail as expected\n");
546 printf("fallocate(ALLOC) didn't fail as expected\n");
551 static void mfd_assert_grow_write(int fd
)
556 /* hugetlbfs does not support write */
560 buf
= malloc(mfd_def_size
* 8);
562 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
566 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
567 if (l
!= (mfd_def_size
* 8)) {
568 printf("pwrite() failed: %m\n");
572 mfd_assert_size(fd
, mfd_def_size
* 8);
575 static void mfd_fail_grow_write(int fd
)
580 /* hugetlbfs does not support write */
584 buf
= malloc(mfd_def_size
* 8);
586 printf("malloc(%zu) failed: %m\n", mfd_def_size
* 8);
590 l
= pwrite(fd
, buf
, mfd_def_size
* 8, 0);
591 if (l
== (mfd_def_size
* 8)) {
592 printf("pwrite() didn't fail as expected\n");
597 static int idle_thread_fn(void *arg
)
602 /* dummy waiter; SIGTERM terminates us anyway */
604 sigaddset(&set
, SIGTERM
);
610 static pid_t
spawn_idle_thread(unsigned int flags
)
615 stack
= malloc(STACK_SIZE
);
617 printf("malloc(STACK_SIZE) failed: %m\n");
621 pid
= clone(idle_thread_fn
,
626 printf("clone() failed: %m\n");
633 static void join_idle_thread(pid_t pid
)
636 waitpid(pid
, NULL
, 0);
640 * Test memfd_create() syscall
641 * Verify syscall-argument validation, including name checks, flag validation
644 static void test_create(void)
649 printf("%s CREATE\n", memfd_str
);
652 mfd_fail_new(NULL
, 0);
654 /* test over-long name (not zero-terminated) */
655 memset(buf
, 0xff, sizeof(buf
));
656 mfd_fail_new(buf
, 0);
658 /* test over-long zero-terminated name */
659 memset(buf
, 0xff, sizeof(buf
));
660 buf
[sizeof(buf
) - 1] = 0;
661 mfd_fail_new(buf
, 0);
663 /* verify "" is a valid name */
664 fd
= mfd_assert_new("", 0, 0);
667 /* verify invalid O_* open flags */
668 mfd_fail_new("", 0x0100);
669 mfd_fail_new("", ~MFD_CLOEXEC
);
670 mfd_fail_new("", ~MFD_ALLOW_SEALING
);
671 mfd_fail_new("", ~0);
672 mfd_fail_new("", 0x80000000U
);
674 /* verify MFD_CLOEXEC is allowed */
675 fd
= mfd_assert_new("", 0, MFD_CLOEXEC
);
678 /* verify MFD_ALLOW_SEALING is allowed */
679 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
);
682 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
683 fd
= mfd_assert_new("", 0, MFD_ALLOW_SEALING
| MFD_CLOEXEC
);
689 * A very basic sealing test to see whether setting/retrieving seals works.
691 static void test_basic(void)
695 printf("%s BASIC\n", memfd_str
);
697 fd
= mfd_assert_new("kern_memfd_basic",
699 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
701 /* add basic seals */
702 mfd_assert_has_seals(fd
, 0);
703 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
705 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
709 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
|
711 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
714 /* add more seals and seal against sealing */
715 mfd_assert_add_seals(fd
, F_SEAL_GROW
| F_SEAL_SEAL
);
716 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
|
721 /* verify that sealing no longer works */
722 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
723 mfd_fail_add_seals(fd
, 0);
727 /* verify sealing does not work without MFD_ALLOW_SEALING */
728 fd
= mfd_assert_new("kern_memfd_basic",
731 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
732 mfd_fail_add_seals(fd
, F_SEAL_SHRINK
|
735 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
741 * Test whether SEAL_WRITE actually prevents modifications.
743 static void test_seal_write(void)
747 printf("%s SEAL-WRITE\n", memfd_str
);
749 fd
= mfd_assert_new("kern_memfd_seal_write",
751 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
752 mfd_assert_has_seals(fd
, 0);
753 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
754 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
758 mfd_assert_shrink(fd
);
760 mfd_fail_grow_write(fd
);
766 * Test SEAL_FUTURE_WRITE
767 * Test whether SEAL_FUTURE_WRITE actually prevents modifications.
769 static void test_seal_future_write(void)
774 printf("%s SEAL-FUTURE-WRITE\n", memfd_str
);
776 fd
= mfd_assert_new("kern_memfd_seal_future_write",
778 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
780 p
= mfd_assert_mmap_shared(fd
);
782 mfd_assert_has_seals(fd
, 0);
784 mfd_assert_add_seals(fd
, F_SEAL_FUTURE_WRITE
);
785 mfd_assert_has_seals(fd
, F_SEAL_FUTURE_WRITE
);
787 /* read should pass, writes should fail */
789 mfd_assert_read_shared(fd
);
792 fd2
= mfd_assert_reopen_fd(fd
);
793 /* read should pass, writes should still fail */
794 mfd_assert_read(fd2
);
795 mfd_assert_read_shared(fd2
);
798 mfd_assert_fork_private_write(fd
);
800 munmap(p
, mfd_def_size
);
807 * Test whether SEAL_SHRINK actually prevents shrinking
809 static void test_seal_shrink(void)
813 printf("%s SEAL-SHRINK\n", memfd_str
);
815 fd
= mfd_assert_new("kern_memfd_seal_shrink",
817 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
818 mfd_assert_has_seals(fd
, 0);
819 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
820 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
823 mfd_assert_write(fd
);
826 mfd_assert_grow_write(fd
);
833 * Test whether SEAL_GROW actually prevents growing
835 static void test_seal_grow(void)
839 printf("%s SEAL-GROW\n", memfd_str
);
841 fd
= mfd_assert_new("kern_memfd_seal_grow",
843 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
844 mfd_assert_has_seals(fd
, 0);
845 mfd_assert_add_seals(fd
, F_SEAL_GROW
);
846 mfd_assert_has_seals(fd
, F_SEAL_GROW
);
849 mfd_assert_write(fd
);
850 mfd_assert_shrink(fd
);
852 mfd_fail_grow_write(fd
);
858 * Test SEAL_SHRINK | SEAL_GROW
859 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
861 static void test_seal_resize(void)
865 printf("%s SEAL-RESIZE\n", memfd_str
);
867 fd
= mfd_assert_new("kern_memfd_seal_resize",
869 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
870 mfd_assert_has_seals(fd
, 0);
871 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
872 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
| F_SEAL_GROW
);
875 mfd_assert_write(fd
);
878 mfd_fail_grow_write(fd
);
884 * Test sharing via dup()
885 * Test that seals are shared between dupped FDs and they're all equal.
887 static void test_share_dup(char *banner
, char *b_suffix
)
891 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
893 fd
= mfd_assert_new("kern_memfd_share_dup",
895 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
896 mfd_assert_has_seals(fd
, 0);
898 fd2
= mfd_assert_dup(fd
);
899 mfd_assert_has_seals(fd2
, 0);
901 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
902 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
903 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
905 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
906 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
907 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
909 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
910 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
911 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
913 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
914 mfd_fail_add_seals(fd2
, F_SEAL_GROW
);
915 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
916 mfd_fail_add_seals(fd2
, F_SEAL_SEAL
);
920 mfd_fail_add_seals(fd
, F_SEAL_GROW
);
925 * Test sealing with active mmap()s
926 * Modifying seals is only allowed if no other mmap() refs exist.
928 static void test_share_mmap(char *banner
, char *b_suffix
)
933 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
935 fd
= mfd_assert_new("kern_memfd_share_mmap",
937 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
938 mfd_assert_has_seals(fd
, 0);
940 /* shared/writable ref prevents sealing WRITE, but allows others */
941 p
= mfd_assert_mmap_shared(fd
);
942 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
943 mfd_assert_has_seals(fd
, 0);
944 mfd_assert_add_seals(fd
, F_SEAL_SHRINK
);
945 mfd_assert_has_seals(fd
, F_SEAL_SHRINK
);
946 munmap(p
, mfd_def_size
);
948 /* readable ref allows sealing */
949 p
= mfd_assert_mmap_private(fd
);
950 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
951 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
952 munmap(p
, mfd_def_size
);
958 * Test sealing with open(/proc/self/fd/%d)
959 * Via /proc we can get access to a separate file-context for the same memfd.
960 * This is *not* like dup(), but like a real separate open(). Make sure the
961 * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
963 static void test_share_open(char *banner
, char *b_suffix
)
967 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
969 fd
= mfd_assert_new("kern_memfd_share_open",
971 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
972 mfd_assert_has_seals(fd
, 0);
974 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
975 mfd_assert_add_seals(fd
, F_SEAL_WRITE
);
976 mfd_assert_has_seals(fd
, F_SEAL_WRITE
);
977 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
);
979 mfd_assert_add_seals(fd2
, F_SEAL_SHRINK
);
980 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
981 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
984 fd
= mfd_assert_open(fd2
, O_RDONLY
, 0);
986 mfd_fail_add_seals(fd
, F_SEAL_SEAL
);
987 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
988 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
);
991 fd2
= mfd_assert_open(fd
, O_RDWR
, 0);
993 mfd_assert_add_seals(fd2
, F_SEAL_SEAL
);
994 mfd_assert_has_seals(fd
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
995 mfd_assert_has_seals(fd2
, F_SEAL_WRITE
| F_SEAL_SHRINK
| F_SEAL_SEAL
);
1002 * Test sharing via fork()
1003 * Test whether seal-modifications work as expected with forked childs.
1005 static void test_share_fork(char *banner
, char *b_suffix
)
1010 printf("%s %s %s\n", memfd_str
, banner
, b_suffix
);
1012 fd
= mfd_assert_new("kern_memfd_share_fork",
1014 MFD_CLOEXEC
| MFD_ALLOW_SEALING
);
1015 mfd_assert_has_seals(fd
, 0);
1017 pid
= spawn_idle_thread(0);
1018 mfd_assert_add_seals(fd
, F_SEAL_SEAL
);
1019 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
1021 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
1022 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
1024 join_idle_thread(pid
);
1026 mfd_fail_add_seals(fd
, F_SEAL_WRITE
);
1027 mfd_assert_has_seals(fd
, F_SEAL_SEAL
);
1032 int main(int argc
, char **argv
)
1037 if (!strcmp(argv
[1], "hugetlbfs")) {
1038 unsigned long hpage_size
= default_huge_page_size();
1041 printf("Unable to determine huge page size\n");
1046 memfd_str
= MEMFD_HUGE_STR
;
1047 mfd_def_size
= hpage_size
* 2;
1049 printf("Unknown option: %s\n", argv
[1]);
1058 test_seal_future_write();
1063 test_share_dup("SHARE-DUP", "");
1064 test_share_mmap("SHARE-MMAP", "");
1065 test_share_open("SHARE-OPEN", "");
1066 test_share_fork("SHARE-FORK", "");
1068 /* Run test-suite in a multi-threaded environment with a shared
1070 pid
= spawn_idle_thread(CLONE_FILES
| CLONE_FS
| CLONE_VM
);
1071 test_share_dup("SHARE-DUP", SHARED_FT_STR
);
1072 test_share_mmap("SHARE-MMAP", SHARED_FT_STR
);
1073 test_share_open("SHARE-OPEN", SHARED_FT_STR
);
1074 test_share_fork("SHARE-FORK", SHARED_FT_STR
);
1075 join_idle_thread(pid
);
1077 printf("memfd: DONE\n");