staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / memfd / memfd_test.c
blobc67d32eeb668e3a145e4eacbb5c7add68ee8b894
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 int mfd_assert_reopen_fd(int fd_in)
59 int r, fd;
60 char path[100];
62 sprintf(path, "/proc/self/fd/%d", fd_in);
64 fd = open(path, O_RDWR);
65 if (fd < 0) {
66 printf("re-open of existing fd %d failed\n", fd_in);
67 abort();
70 return fd;
73 static void mfd_fail_new(const char *name, unsigned int flags)
75 int r;
77 r = sys_memfd_create(name, flags);
78 if (r >= 0) {
79 printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
80 name, flags);
81 close(r);
82 abort();
86 static unsigned int mfd_assert_get_seals(int fd)
88 int r;
90 r = fcntl(fd, F_GET_SEALS);
91 if (r < 0) {
92 printf("GET_SEALS(%d) failed: %m\n", fd);
93 abort();
96 return (unsigned int)r;
99 static void mfd_assert_has_seals(int fd, unsigned int seals)
101 unsigned int s;
103 s = mfd_assert_get_seals(fd);
104 if (s != seals) {
105 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
106 abort();
110 static void mfd_assert_add_seals(int fd, unsigned int seals)
112 int r;
113 unsigned int s;
115 s = mfd_assert_get_seals(fd);
116 r = fcntl(fd, F_ADD_SEALS, seals);
117 if (r < 0) {
118 printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
119 abort();
123 static void mfd_fail_add_seals(int fd, unsigned int seals)
125 int r;
126 unsigned int s;
128 r = fcntl(fd, F_GET_SEALS);
129 if (r < 0)
130 s = 0;
131 else
132 s = (unsigned int)r;
134 r = fcntl(fd, F_ADD_SEALS, seals);
135 if (r >= 0) {
136 printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
137 fd, s, seals);
138 abort();
142 static void mfd_assert_size(int fd, size_t size)
144 struct stat st;
145 int r;
147 r = fstat(fd, &st);
148 if (r < 0) {
149 printf("fstat(%d) failed: %m\n", fd);
150 abort();
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);
154 abort();
158 static int mfd_assert_dup(int fd)
160 int r;
162 r = dup(fd);
163 if (r < 0) {
164 printf("dup(%d) failed: %m\n", fd);
165 abort();
168 return r;
171 static void *mfd_assert_mmap_shared(int fd)
173 void *p;
175 p = mmap(NULL,
176 mfd_def_size,
177 PROT_READ | PROT_WRITE,
178 MAP_SHARED,
181 if (p == MAP_FAILED) {
182 printf("mmap() failed: %m\n");
183 abort();
186 return p;
189 static void *mfd_assert_mmap_private(int fd)
191 void *p;
193 p = mmap(NULL,
194 mfd_def_size,
195 PROT_READ,
196 MAP_PRIVATE,
199 if (p == MAP_FAILED) {
200 printf("mmap() failed: %m\n");
201 abort();
204 return p;
207 static int mfd_assert_open(int fd, int flags, mode_t mode)
209 char buf[512];
210 int r;
212 sprintf(buf, "/proc/self/fd/%d", fd);
213 r = open(buf, flags, mode);
214 if (r < 0) {
215 printf("open(%s) failed: %m\n", buf);
216 abort();
219 return r;
222 static void mfd_fail_open(int fd, int flags, mode_t mode)
224 char buf[512];
225 int r;
227 sprintf(buf, "/proc/self/fd/%d", fd);
228 r = open(buf, flags, mode);
229 if (r >= 0) {
230 printf("open(%s) didn't fail as expected\n", buf);
231 abort();
235 static void mfd_assert_read(int fd)
237 char buf[16];
238 void *p;
239 ssize_t l;
241 l = read(fd, buf, sizeof(buf));
242 if (l != sizeof(buf)) {
243 printf("read() failed: %m\n");
244 abort();
247 /* verify PROT_READ *is* allowed */
248 p = mmap(NULL,
249 mfd_def_size,
250 PROT_READ,
251 MAP_PRIVATE,
254 if (p == MAP_FAILED) {
255 printf("mmap() failed: %m\n");
256 abort();
258 munmap(p, mfd_def_size);
260 /* verify MAP_PRIVATE is *always* allowed (even writable) */
261 p = mmap(NULL,
262 mfd_def_size,
263 PROT_READ | PROT_WRITE,
264 MAP_PRIVATE,
267 if (p == MAP_FAILED) {
268 printf("mmap() failed: %m\n");
269 abort();
271 munmap(p, mfd_def_size);
274 /* Test that PROT_READ + MAP_SHARED mappings work. */
275 static void mfd_assert_read_shared(int fd)
277 void *p;
279 /* verify PROT_READ and MAP_SHARED *is* allowed */
280 p = mmap(NULL,
281 mfd_def_size,
282 PROT_READ,
283 MAP_SHARED,
286 if (p == MAP_FAILED) {
287 printf("mmap() failed: %m\n");
288 abort();
290 munmap(p, mfd_def_size);
293 static void mfd_assert_write(int fd)
295 ssize_t l;
296 void *p;
297 int r;
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);
306 if (l != 4) {
307 printf("write() failed: %m\n");
308 abort();
312 /* verify PROT_READ | PROT_WRITE is allowed */
313 p = mmap(NULL,
314 mfd_def_size,
315 PROT_READ | PROT_WRITE,
316 MAP_SHARED,
319 if (p == MAP_FAILED) {
320 printf("mmap() failed: %m\n");
321 abort();
323 *(char *)p = 0;
324 munmap(p, mfd_def_size);
326 /* verify PROT_WRITE is allowed */
327 p = mmap(NULL,
328 mfd_def_size,
329 PROT_WRITE,
330 MAP_SHARED,
333 if (p == MAP_FAILED) {
334 printf("mmap() failed: %m\n");
335 abort();
337 *(char *)p = 0;
338 munmap(p, mfd_def_size);
340 /* verify PROT_READ with MAP_SHARED is allowed and a following
341 * mprotect(PROT_WRITE) allows writing */
342 p = mmap(NULL,
343 mfd_def_size,
344 PROT_READ,
345 MAP_SHARED,
348 if (p == MAP_FAILED) {
349 printf("mmap() failed: %m\n");
350 abort();
353 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
354 if (r < 0) {
355 printf("mprotect() failed: %m\n");
356 abort();
359 *(char *)p = 0;
360 munmap(p, mfd_def_size);
362 /* verify PUNCH_HOLE works */
363 r = fallocate(fd,
364 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
366 mfd_def_size);
367 if (r < 0) {
368 printf("fallocate(PUNCH_HOLE) failed: %m\n");
369 abort();
373 static void mfd_fail_write(int fd)
375 ssize_t l;
376 void *p;
377 int r;
379 /* verify write() fails */
380 l = write(fd, "data", 4);
381 if (l != -EPERM) {
382 printf("expected EPERM on write(), but got %d: %m\n", (int)l);
383 abort();
386 /* verify PROT_READ | PROT_WRITE is not allowed */
387 p = mmap(NULL,
388 mfd_def_size,
389 PROT_READ | PROT_WRITE,
390 MAP_SHARED,
393 if (p != MAP_FAILED) {
394 printf("mmap() didn't fail as expected\n");
395 abort();
398 /* verify PROT_WRITE is not allowed */
399 p = mmap(NULL,
400 mfd_def_size,
401 PROT_WRITE,
402 MAP_SHARED,
405 if (p != MAP_FAILED) {
406 printf("mmap() didn't fail as expected\n");
407 abort();
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. */
412 p = mmap(NULL,
413 mfd_def_size,
414 PROT_READ,
415 MAP_SHARED,
418 if (p != MAP_FAILED) {
419 r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
420 if (r >= 0) {
421 printf("mmap()+mprotect() didn't fail as expected\n");
422 abort();
426 /* verify PUNCH_HOLE fails */
427 r = fallocate(fd,
428 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
430 mfd_def_size);
431 if (r >= 0) {
432 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
433 abort();
437 static void mfd_assert_shrink(int fd)
439 int r, fd2;
441 r = ftruncate(fd, mfd_def_size / 2);
442 if (r < 0) {
443 printf("ftruncate(SHRINK) failed: %m\n");
444 abort();
447 mfd_assert_size(fd, mfd_def_size / 2);
449 fd2 = mfd_assert_open(fd,
450 O_RDWR | O_CREAT | O_TRUNC,
451 S_IRUSR | S_IWUSR);
452 close(fd2);
454 mfd_assert_size(fd, 0);
457 static void mfd_fail_shrink(int fd)
459 int r;
461 r = ftruncate(fd, mfd_def_size / 2);
462 if (r >= 0) {
463 printf("ftruncate(SHRINK) didn't fail as expected\n");
464 abort();
467 mfd_fail_open(fd,
468 O_RDWR | O_CREAT | O_TRUNC,
469 S_IRUSR | S_IWUSR);
472 static void mfd_assert_grow(int fd)
474 int r;
476 r = ftruncate(fd, mfd_def_size * 2);
477 if (r < 0) {
478 printf("ftruncate(GROW) failed: %m\n");
479 abort();
482 mfd_assert_size(fd, mfd_def_size * 2);
484 r = fallocate(fd,
487 mfd_def_size * 4);
488 if (r < 0) {
489 printf("fallocate(ALLOC) failed: %m\n");
490 abort();
493 mfd_assert_size(fd, mfd_def_size * 4);
496 static void mfd_fail_grow(int fd)
498 int r;
500 r = ftruncate(fd, mfd_def_size * 2);
501 if (r >= 0) {
502 printf("ftruncate(GROW) didn't fail as expected\n");
503 abort();
506 r = fallocate(fd,
509 mfd_def_size * 4);
510 if (r >= 0) {
511 printf("fallocate(ALLOC) didn't fail as expected\n");
512 abort();
516 static void mfd_assert_grow_write(int fd)
518 static char *buf;
519 ssize_t l;
521 /* hugetlbfs does not support write */
522 if (hugetlbfs_test)
523 return;
525 buf = malloc(mfd_def_size * 8);
526 if (!buf) {
527 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
528 abort();
531 l = pwrite(fd, buf, mfd_def_size * 8, 0);
532 if (l != (mfd_def_size * 8)) {
533 printf("pwrite() failed: %m\n");
534 abort();
537 mfd_assert_size(fd, mfd_def_size * 8);
540 static void mfd_fail_grow_write(int fd)
542 static char *buf;
543 ssize_t l;
545 /* hugetlbfs does not support write */
546 if (hugetlbfs_test)
547 return;
549 buf = malloc(mfd_def_size * 8);
550 if (!buf) {
551 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
552 abort();
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");
558 abort();
562 static int idle_thread_fn(void *arg)
564 sigset_t set;
565 int sig;
567 /* dummy waiter; SIGTERM terminates us anyway */
568 sigemptyset(&set);
569 sigaddset(&set, SIGTERM);
570 sigwait(&set, &sig);
572 return 0;
575 static pid_t spawn_idle_thread(unsigned int flags)
577 uint8_t *stack;
578 pid_t pid;
580 stack = malloc(STACK_SIZE);
581 if (!stack) {
582 printf("malloc(STACK_SIZE) failed: %m\n");
583 abort();
586 pid = clone(idle_thread_fn,
587 stack + STACK_SIZE,
588 SIGCHLD | flags,
589 NULL);
590 if (pid < 0) {
591 printf("clone() failed: %m\n");
592 abort();
595 return pid;
598 static void join_idle_thread(pid_t pid)
600 kill(pid, SIGTERM);
601 waitpid(pid, NULL, 0);
605 * Test memfd_create() syscall
606 * Verify syscall-argument validation, including name checks, flag validation
607 * and more.
609 static void test_create(void)
611 char buf[2048];
612 int fd;
614 printf("%s CREATE\n", memfd_str);
616 /* test NULL name */
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);
630 close(fd);
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);
641 close(fd);
643 /* verify MFD_ALLOW_SEALING is allowed */
644 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
645 close(fd);
647 /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
648 fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
649 close(fd);
653 * Test basic sealing
654 * A very basic sealing test to see whether setting/retrieving seals works.
656 static void test_basic(void)
658 int fd;
660 printf("%s BASIC\n", memfd_str);
662 fd = mfd_assert_new("kern_memfd_basic",
663 mfd_def_size,
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 |
669 F_SEAL_WRITE);
670 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
671 F_SEAL_WRITE);
673 /* add them again */
674 mfd_assert_add_seals(fd, F_SEAL_SHRINK |
675 F_SEAL_WRITE);
676 mfd_assert_has_seals(fd, F_SEAL_SHRINK |
677 F_SEAL_WRITE);
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 |
682 F_SEAL_GROW |
683 F_SEAL_WRITE |
684 F_SEAL_SEAL);
686 /* verify that sealing no longer works */
687 mfd_fail_add_seals(fd, F_SEAL_GROW);
688 mfd_fail_add_seals(fd, 0);
690 close(fd);
692 /* verify sealing does not work without MFD_ALLOW_SEALING */
693 fd = mfd_assert_new("kern_memfd_basic",
694 mfd_def_size,
695 MFD_CLOEXEC);
696 mfd_assert_has_seals(fd, F_SEAL_SEAL);
697 mfd_fail_add_seals(fd, F_SEAL_SHRINK |
698 F_SEAL_GROW |
699 F_SEAL_WRITE);
700 mfd_assert_has_seals(fd, F_SEAL_SEAL);
701 close(fd);
705 * Test SEAL_WRITE
706 * Test whether SEAL_WRITE actually prevents modifications.
708 static void test_seal_write(void)
710 int fd;
712 printf("%s SEAL-WRITE\n", memfd_str);
714 fd = mfd_assert_new("kern_memfd_seal_write",
715 mfd_def_size,
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);
721 mfd_assert_read(fd);
722 mfd_fail_write(fd);
723 mfd_assert_shrink(fd);
724 mfd_assert_grow(fd);
725 mfd_fail_grow_write(fd);
727 close(fd);
731 * Test SEAL_FUTURE_WRITE
732 * Test whether SEAL_FUTURE_WRITE actually prevents modifications.
734 static void test_seal_future_write(void)
736 int fd, fd2;
737 void *p;
739 printf("%s SEAL-FUTURE-WRITE\n", memfd_str);
741 fd = mfd_assert_new("kern_memfd_seal_future_write",
742 mfd_def_size,
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 */
753 mfd_assert_read(fd);
754 mfd_assert_read_shared(fd);
755 mfd_fail_write(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);
761 mfd_fail_write(fd2);
763 munmap(p, mfd_def_size);
764 close(fd2);
765 close(fd);
769 * Test SEAL_SHRINK
770 * Test whether SEAL_SHRINK actually prevents shrinking
772 static void test_seal_shrink(void)
774 int fd;
776 printf("%s SEAL-SHRINK\n", memfd_str);
778 fd = mfd_assert_new("kern_memfd_seal_shrink",
779 mfd_def_size,
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);
785 mfd_assert_read(fd);
786 mfd_assert_write(fd);
787 mfd_fail_shrink(fd);
788 mfd_assert_grow(fd);
789 mfd_assert_grow_write(fd);
791 close(fd);
795 * Test SEAL_GROW
796 * Test whether SEAL_GROW actually prevents growing
798 static void test_seal_grow(void)
800 int fd;
802 printf("%s SEAL-GROW\n", memfd_str);
804 fd = mfd_assert_new("kern_memfd_seal_grow",
805 mfd_def_size,
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);
811 mfd_assert_read(fd);
812 mfd_assert_write(fd);
813 mfd_assert_shrink(fd);
814 mfd_fail_grow(fd);
815 mfd_fail_grow_write(fd);
817 close(fd);
821 * Test SEAL_SHRINK | SEAL_GROW
822 * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
824 static void test_seal_resize(void)
826 int fd;
828 printf("%s SEAL-RESIZE\n", memfd_str);
830 fd = mfd_assert_new("kern_memfd_seal_resize",
831 mfd_def_size,
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);
837 mfd_assert_read(fd);
838 mfd_assert_write(fd);
839 mfd_fail_shrink(fd);
840 mfd_fail_grow(fd);
841 mfd_fail_grow_write(fd);
843 close(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)
852 int fd, fd2;
854 printf("%s %s %s\n", memfd_str, banner, b_suffix);
856 fd = mfd_assert_new("kern_memfd_share_dup",
857 mfd_def_size,
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);
881 close(fd2);
883 mfd_fail_add_seals(fd, F_SEAL_GROW);
884 close(fd);
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)
893 int fd;
894 void *p;
896 printf("%s %s %s\n", memfd_str, banner, b_suffix);
898 fd = mfd_assert_new("kern_memfd_share_mmap",
899 mfd_def_size,
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);
917 close(fd);
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)
928 int fd, fd2;
930 printf("%s %s %s\n", memfd_str, banner, b_suffix);
932 fd = mfd_assert_new("kern_memfd_share_open",
933 mfd_def_size,
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);
946 close(fd);
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);
953 close(fd2);
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);
960 close(fd2);
961 close(fd);
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)
970 int fd;
971 pid_t pid;
973 printf("%s %s %s\n", memfd_str, banner, b_suffix);
975 fd = mfd_assert_new("kern_memfd_share_fork",
976 mfd_def_size,
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);
992 close(fd);
995 int main(int argc, char **argv)
997 pid_t pid;
999 if (argc == 2) {
1000 if (!strcmp(argv[1], "hugetlbfs")) {
1001 unsigned long hpage_size = default_huge_page_size();
1003 if (!hpage_size) {
1004 printf("Unable to determine huge page size\n");
1005 abort();
1008 hugetlbfs_test = 1;
1009 memfd_str = MEMFD_HUGE_STR;
1010 mfd_def_size = hpage_size * 2;
1011 } else {
1012 printf("Unknown option: %s\n", argv[1]);
1013 abort();
1017 test_create();
1018 test_basic();
1020 test_seal_write();
1021 test_seal_future_write();
1022 test_seal_shrink();
1023 test_seal_grow();
1024 test_seal_resize();
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
1032 * file-table. */
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");
1042 return 0;