Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / memfd / memfd_test.c
blob10baa1652fc2af92de1f5aee3e84bc36d9e46c7f
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #define __EXPORTED_HEADERS__
5 #include <errno.h>
6 #include <inttypes.h>
7 #include <limits.h>
8 #include <linux/falloc.h>
9 #include <linux/fcntl.h>
10 #include <linux/memfd.h>
11 #include <sched.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <signal.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <sys/stat.h>
18 #include <sys/syscall.h>
19 #include <sys/wait.h>
20 #include <unistd.h>
22 #include "common.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)
39 int r, fd;
41 fd = sys_memfd_create(name, flags);
42 if (fd < 0) {
43 printf("memfd_create(\"%s\", %u) failed: %m\n",
44 name, flags);
45 abort();
48 r = ftruncate(fd, sz);
49 if (r < 0) {
50 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
51 abort();
54 return fd;
57 static void mfd_fail_new(const char *name, unsigned int flags)
59 int r;
61 r = sys_memfd_create(name, flags);
62 if (r >= 0) {
63 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
64 name, flags);
65 close(r);
66 abort();
70 static unsigned int mfd_assert_get_seals(int fd)
72 int r;
74 r = fcntl(fd, F_GET_SEALS);
75 if (r < 0) {
76 printf("GET_SEALS(%d) failed: %m\n", fd);
77 abort();
80 return (unsigned int)r;
83 static void mfd_assert_has_seals(int fd, unsigned int seals)
85 unsigned int s;
87 s = mfd_assert_get_seals(fd);
88 if (s != seals) {
89 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
90 abort();
94 static void mfd_assert_add_seals(int fd, unsigned int seals)
96 int r;
97 unsigned int s;
99 s = mfd_assert_get_seals(fd);
100 r = fcntl(fd, F_ADD_SEALS, seals);
101 if (r < 0) {
102 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
103 abort();
107 static void mfd_fail_add_seals(int fd, unsigned int seals)
109 int r;
110 unsigned int s;
112 r = fcntl(fd, F_GET_SEALS);
113 if (r < 0)
114 s = 0;
115 else
116 s = (unsigned int)r;
118 r = fcntl(fd, F_ADD_SEALS, seals);
119 if (r >= 0) {
120 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
121 fd, s, seals);
122 abort();
126 static void mfd_assert_size(int fd, size_t size)
128 struct stat st;
129 int r;
131 r = fstat(fd, &st);
132 if (r < 0) {
133 printf("fstat(%d) failed: %m\n", fd);
134 abort();
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);
138 abort();
142 static int mfd_assert_dup(int fd)
144 int r;
146 r = dup(fd);
147 if (r < 0) {
148 printf("dup(%d) failed: %m\n", fd);
149 abort();
152 return r;
155 static void *mfd_assert_mmap_shared(int fd)
157 void *p;
159 p = mmap(NULL,
160 mfd_def_size,
161 PROT_READ | PROT_WRITE,
162 MAP_SHARED,
165 if (p == MAP_FAILED) {
166 printf("mmap() failed: %m\n");
167 abort();
170 return p;
173 static void *mfd_assert_mmap_private(int fd)
175 void *p;
177 p = mmap(NULL,
178 mfd_def_size,
179 PROT_READ,
180 MAP_PRIVATE,
183 if (p == MAP_FAILED) {
184 printf("mmap() failed: %m\n");
185 abort();
188 return p;
191 static int mfd_assert_open(int fd, int flags, mode_t mode)
193 char buf[512];
194 int r;
196 sprintf(buf, "/proc/self/fd/%d", fd);
197 r = open(buf, flags, mode);
198 if (r < 0) {
199 printf("open(%s) failed: %m\n", buf);
200 abort();
203 return r;
206 static void mfd_fail_open(int fd, int flags, mode_t mode)
208 char buf[512];
209 int r;
211 sprintf(buf, "/proc/self/fd/%d", fd);
212 r = open(buf, flags, mode);
213 if (r >= 0) {
214 printf("open(%s) didn't fail as expected\n", buf);
215 abort();
219 static void mfd_assert_read(int fd)
221 char buf[16];
222 void *p;
223 ssize_t l;
225 l = read(fd, buf, sizeof(buf));
226 if (l != sizeof(buf)) {
227 printf("read() failed: %m\n");
228 abort();
231 /* verify PROT_READ *is* allowed */
232 p = mmap(NULL,
233 mfd_def_size,
234 PROT_READ,
235 MAP_PRIVATE,
238 if (p == MAP_FAILED) {
239 printf("mmap() failed: %m\n");
240 abort();
242 munmap(p, mfd_def_size);
244 /* verify MAP_PRIVATE is *always* allowed (even writable) */
245 p = mmap(NULL,
246 mfd_def_size,
247 PROT_READ | PROT_WRITE,
248 MAP_PRIVATE,
251 if (p == MAP_FAILED) {
252 printf("mmap() failed: %m\n");
253 abort();
255 munmap(p, mfd_def_size);
258 static void mfd_assert_write(int fd)
260 ssize_t l;
261 void *p;
262 int r;
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);
271 if (l != 4) {
272 printf("write() failed: %m\n");
273 abort();
277 /* verify PROT_READ | PROT_WRITE is allowed */
278 p = mmap(NULL,
279 mfd_def_size,
280 PROT_READ | PROT_WRITE,
281 MAP_SHARED,
284 if (p == MAP_FAILED) {
285 printf("mmap() failed: %m\n");
286 abort();
288 *(char *)p = 0;
289 munmap(p, mfd_def_size);
291 /* verify PROT_WRITE is allowed */
292 p = mmap(NULL,
293 mfd_def_size,
294 PROT_WRITE,
295 MAP_SHARED,
298 if (p == MAP_FAILED) {
299 printf("mmap() failed: %m\n");
300 abort();
302 *(char *)p = 0;
303 munmap(p, mfd_def_size);
305 /* verify PROT_READ with MAP_SHARED is allowed and a following
306 * mprotect(PROT_WRITE) allows writing */
307 p = mmap(NULL,
308 mfd_def_size,
309 PROT_READ,
310 MAP_SHARED,
313 if (p == MAP_FAILED) {
314 printf("mmap() failed: %m\n");
315 abort();
318 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
319 if (r < 0) {
320 printf("mprotect() failed: %m\n");
321 abort();
324 *(char *)p = 0;
325 munmap(p, mfd_def_size);
327 /* verify PUNCH_HOLE works */
328 r = fallocate(fd,
329 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
331 mfd_def_size);
332 if (r < 0) {
333 printf("fallocate(PUNCH_HOLE) failed: %m\n");
334 abort();
338 static void mfd_fail_write(int fd)
340 ssize_t l;
341 void *p;
342 int r;
344 /* verify write() fails */
345 l = write(fd, "data", 4);
346 if (l != -EPERM) {
347 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
348 abort();
351 /* verify PROT_READ | PROT_WRITE is not allowed */
352 p = mmap(NULL,
353 mfd_def_size,
354 PROT_READ | PROT_WRITE,
355 MAP_SHARED,
358 if (p != MAP_FAILED) {
359 printf("mmap() didn't fail as expected\n");
360 abort();
363 /* verify PROT_WRITE is not allowed */
364 p = mmap(NULL,
365 mfd_def_size,
366 PROT_WRITE,
367 MAP_SHARED,
370 if (p != MAP_FAILED) {
371 printf("mmap() didn't fail as expected\n");
372 abort();
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. */
377 p = mmap(NULL,
378 mfd_def_size,
379 PROT_READ,
380 MAP_SHARED,
383 if (p != MAP_FAILED) {
384 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
385 if (r >= 0) {
386 printf("mmap()+mprotect() didn't fail as expected\n");
387 abort();
391 /* verify PUNCH_HOLE fails */
392 r = fallocate(fd,
393 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
395 mfd_def_size);
396 if (r >= 0) {
397 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
398 abort();
402 static void mfd_assert_shrink(int fd)
404 int r, fd2;
406 r = ftruncate(fd, mfd_def_size / 2);
407 if (r < 0) {
408 printf("ftruncate(SHRINK) failed: %m\n");
409 abort();
412 mfd_assert_size(fd, mfd_def_size / 2);
414 fd2 = mfd_assert_open(fd,
415 O_RDWR | O_CREAT | O_TRUNC,
416 S_IRUSR | S_IWUSR);
417 close(fd2);
419 mfd_assert_size(fd, 0);
422 static void mfd_fail_shrink(int fd)
424 int r;
426 r = ftruncate(fd, mfd_def_size / 2);
427 if (r >= 0) {
428 printf("ftruncate(SHRINK) didn't fail as expected\n");
429 abort();
432 mfd_fail_open(fd,
433 O_RDWR | O_CREAT | O_TRUNC,
434 S_IRUSR | S_IWUSR);
437 static void mfd_assert_grow(int fd)
439 int r;
441 r = ftruncate(fd, mfd_def_size * 2);
442 if (r < 0) {
443 printf("ftruncate(GROW) failed: %m\n");
444 abort();
447 mfd_assert_size(fd, mfd_def_size * 2);
449 r = fallocate(fd,
452 mfd_def_size * 4);
453 if (r < 0) {
454 printf("fallocate(ALLOC) failed: %m\n");
455 abort();
458 mfd_assert_size(fd, mfd_def_size * 4);
461 static void mfd_fail_grow(int fd)
463 int r;
465 r = ftruncate(fd, mfd_def_size * 2);
466 if (r >= 0) {
467 printf("ftruncate(GROW) didn't fail as expected\n");
468 abort();
471 r = fallocate(fd,
474 mfd_def_size * 4);
475 if (r >= 0) {
476 printf("fallocate(ALLOC) didn't fail as expected\n");
477 abort();
481 static void mfd_assert_grow_write(int fd)
483 static char *buf;
484 ssize_t l;
486 /* hugetlbfs does not support write */
487 if (hugetlbfs_test)
488 return;
490 buf = malloc(mfd_def_size * 8);
491 if (!buf) {
492 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
493 abort();
496 l = pwrite(fd, buf, mfd_def_size * 8, 0);
497 if (l != (mfd_def_size * 8)) {
498 printf("pwrite() failed: %m\n");
499 abort();
502 mfd_assert_size(fd, mfd_def_size * 8);
505 static void mfd_fail_grow_write(int fd)
507 static char *buf;
508 ssize_t l;
510 /* hugetlbfs does not support write */
511 if (hugetlbfs_test)
512 return;
514 buf = malloc(mfd_def_size * 8);
515 if (!buf) {
516 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
517 abort();
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");
523 abort();
527 static int idle_thread_fn(void *arg)
529 sigset_t set;
530 int sig;
532 /* dummy waiter; SIGTERM terminates us anyway */
533 sigemptyset(&set);
534 sigaddset(&set, SIGTERM);
535 sigwait(&set, &sig);
537 return 0;
540 static pid_t spawn_idle_thread(unsigned int flags)
542 uint8_t *stack;
543 pid_t pid;
545 stack = malloc(STACK_SIZE);
546 if (!stack) {
547 printf("malloc(STACK_SIZE) failed: %m\n");
548 abort();
551 pid = clone(idle_thread_fn,
552 stack + STACK_SIZE,
553 SIGCHLD | flags,
554 NULL);
555 if (pid < 0) {
556 printf("clone() failed: %m\n");
557 abort();
560 return pid;
563 static void join_idle_thread(pid_t pid)
565 kill(pid, SIGTERM);
566 waitpid(pid, NULL, 0);
570 * Test memfd_create() syscall
571 * Verify syscall-argument validation, including name checks, flag validation
572 * and more.
574 static void test_create(void)
576 char buf[2048];
577 int fd;
579 printf("%s CREATE\n", memfd_str);
581 /* test NULL name */
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);
595 close(fd);
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);
606 close(fd);
608 /* verify MFD_ALLOW_SEALING is allowed */
609 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
610 close(fd);
612 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
613 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
614 close(fd);
618 * Test basic sealing
619 * A very basic sealing test to see whether setting/retrieving seals works.
621 static void test_basic(void)
623 int fd;
625 printf("%s BASIC\n", memfd_str);
627 fd = mfd_assert_new("kern_memfd_basic",
628 mfd_def_size,
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 |
634 F_SEAL_WRITE);
635 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
636 F_SEAL_WRITE);
638 /* add them again */
639 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
640 F_SEAL_WRITE);
641 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
642 F_SEAL_WRITE);
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 |
647 F_SEAL_GROW |
648 F_SEAL_WRITE |
649 F_SEAL_SEAL);
651 /* verify that sealing no longer works */
652 mfd_fail_add_seals(fd, F_SEAL_GROW);
653 mfd_fail_add_seals(fd, 0);
655 close(fd);
657 /* verify sealing does not work without MFD_ALLOW_SEALING */
658 fd = mfd_assert_new("kern_memfd_basic",
659 mfd_def_size,
660 MFD_CLOEXEC);
661 mfd_assert_has_seals(fd, F_SEAL_SEAL);
662 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
663 F_SEAL_GROW |
664 F_SEAL_WRITE);
665 mfd_assert_has_seals(fd, F_SEAL_SEAL);
666 close(fd);
670 * Test SEAL_WRITE
671 * Test whether SEAL_WRITE actually prevents modifications.
673 static void test_seal_write(void)
675 int fd;
677 printf("%s SEAL-WRITE\n", memfd_str);
679 fd = mfd_assert_new("kern_memfd_seal_write",
680 mfd_def_size,
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);
686 mfd_assert_read(fd);
687 mfd_fail_write(fd);
688 mfd_assert_shrink(fd);
689 mfd_assert_grow(fd);
690 mfd_fail_grow_write(fd);
692 close(fd);
696 * Test SEAL_SHRINK
697 * Test whether SEAL_SHRINK actually prevents shrinking
699 static void test_seal_shrink(void)
701 int fd;
703 printf("%s SEAL-SHRINK\n", memfd_str);
705 fd = mfd_assert_new("kern_memfd_seal_shrink",
706 mfd_def_size,
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);
712 mfd_assert_read(fd);
713 mfd_assert_write(fd);
714 mfd_fail_shrink(fd);
715 mfd_assert_grow(fd);
716 mfd_assert_grow_write(fd);
718 close(fd);
722 * Test SEAL_GROW
723 * Test whether SEAL_GROW actually prevents growing
725 static void test_seal_grow(void)
727 int fd;
729 printf("%s SEAL-GROW\n", memfd_str);
731 fd = mfd_assert_new("kern_memfd_seal_grow",
732 mfd_def_size,
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);
738 mfd_assert_read(fd);
739 mfd_assert_write(fd);
740 mfd_assert_shrink(fd);
741 mfd_fail_grow(fd);
742 mfd_fail_grow_write(fd);
744 close(fd);
748 * Test SEAL_SHRINK | SEAL_GROW
749 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
751 static void test_seal_resize(void)
753 int fd;
755 printf("%s SEAL-RESIZE\n", memfd_str);
757 fd = mfd_assert_new("kern_memfd_seal_resize",
758 mfd_def_size,
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);
764 mfd_assert_read(fd);
765 mfd_assert_write(fd);
766 mfd_fail_shrink(fd);
767 mfd_fail_grow(fd);
768 mfd_fail_grow_write(fd);
770 close(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)
779 int fd, fd2;
781 printf("%s %s %s\n", memfd_str, banner, b_suffix);
783 fd = mfd_assert_new("kern_memfd_share_dup",
784 mfd_def_size,
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);
808 close(fd2);
810 mfd_fail_add_seals(fd, F_SEAL_GROW);
811 close(fd);
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)
820 int fd;
821 void *p;
823 printf("%s %s %s\n", memfd_str, banner, b_suffix);
825 fd = mfd_assert_new("kern_memfd_share_mmap",
826 mfd_def_size,
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);
844 close(fd);
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)
855 int fd, fd2;
857 printf("%s %s %s\n", memfd_str, banner, b_suffix);
859 fd = mfd_assert_new("kern_memfd_share_open",
860 mfd_def_size,
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);
873 close(fd);
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);
880 close(fd2);
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);
887 close(fd2);
888 close(fd);
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)
897 int fd;
898 pid_t pid;
900 printf("%s %s %s\n", memfd_str, banner, b_suffix);
902 fd = mfd_assert_new("kern_memfd_share_fork",
903 mfd_def_size,
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);
919 close(fd);
922 int main(int argc, char **argv)
924 pid_t pid;
926 if (argc == 2) {
927 if (!strcmp(argv[1], "hugetlbfs")) {
928 unsigned long hpage_size = default_huge_page_size();
930 if (!hpage_size) {
931 printf("Unable to determine huge page size\n");
932 abort();
935 hugetlbfs_test = 1;
936 memfd_str = MEMFD_HUGE_STR;
937 mfd_def_size = hpage_size * 2;
938 } else {
939 printf("Unknown option: %s\n", argv[1]);
940 abort();
944 test_create();
945 test_basic();
947 test_seal_write();
948 test_seal_shrink();
949 test_seal_grow();
950 test_seal_resize();
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
958 * file-table. */
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");
968 return 0;