GNU Linux-libre 4.19.281-gnu1
[releases.git] / tools / testing / selftests / memfd / memfd_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #define __EXPORTED_HEADERS__
4
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>
21
22 #include "common.h"
23
24 #define MEMFD_STR       "memfd:"
25 #define MEMFD_HUGE_STR  "memfd-hugetlb:"
26 #define SHARED_FT_STR   "(shared file-table)"
27
28 #define MFD_DEF_SIZE 8192
29 #define STACK_SIZE 65536
30
31 /*
32  * Default is not to test hugetlbfs
33  */
34 static size_t mfd_def_size = MFD_DEF_SIZE;
35 static const char *memfd_str = MEMFD_STR;
36
37 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
38 {
39         int r, fd;
40
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();
46         }
47
48         r = ftruncate(fd, sz);
49         if (r < 0) {
50                 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
51                 abort();
52         }
53
54         return fd;
55 }
56
57 static void mfd_fail_new(const char *name, unsigned int flags)
58 {
59         int r;
60
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();
67         }
68 }
69
70 static unsigned int mfd_assert_get_seals(int fd)
71 {
72         int r;
73
74         r = fcntl(fd, F_GET_SEALS);
75         if (r < 0) {
76                 printf("GET_SEALS(%d) failed: %m\n", fd);
77                 abort();
78         }
79
80         return (unsigned int)r;
81 }
82
83 static void mfd_assert_has_seals(int fd, unsigned int seals)
84 {
85         unsigned int s;
86
87         s = mfd_assert_get_seals(fd);
88         if (s != seals) {
89                 printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
90                 abort();
91         }
92 }
93
94 static void mfd_assert_add_seals(int fd, unsigned int seals)
95 {
96         int r;
97         unsigned int s;
98
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();
104         }
105 }
106
107 static void mfd_fail_add_seals(int fd, unsigned int seals)
108 {
109         int r;
110         unsigned int s;
111
112         r = fcntl(fd, F_GET_SEALS);
113         if (r < 0)
114                 s = 0;
115         else
116                 s = (unsigned int)r;
117
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();
123         }
124 }
125
126 static void mfd_assert_size(int fd, size_t size)
127 {
128         struct stat st;
129         int r;
130
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();
139         }
140 }
141
142 static int mfd_assert_dup(int fd)
143 {
144         int r;
145
146         r = dup(fd);
147         if (r < 0) {
148                 printf("dup(%d) failed: %m\n", fd);
149                 abort();
150         }
151
152         return r;
153 }
154
155 static void *mfd_assert_mmap_shared(int fd)
156 {
157         void *p;
158
159         p = mmap(NULL,
160                  mfd_def_size,
161                  PROT_READ | PROT_WRITE,
162                  MAP_SHARED,
163                  fd,
164                  0);
165         if (p == MAP_FAILED) {
166                 printf("mmap() failed: %m\n");
167                 abort();
168         }
169
170         return p;
171 }
172
173 static void *mfd_assert_mmap_private(int fd)
174 {
175         void *p;
176
177         p = mmap(NULL,
178                  mfd_def_size,
179                  PROT_READ,
180                  MAP_PRIVATE,
181                  fd,
182                  0);
183         if (p == MAP_FAILED) {
184                 printf("mmap() failed: %m\n");
185                 abort();
186         }
187
188         return p;
189 }
190
191 static int mfd_assert_open(int fd, int flags, mode_t mode)
192 {
193         char buf[512];
194         int r;
195
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();
201         }
202
203         return r;
204 }
205
206 static void mfd_fail_open(int fd, int flags, mode_t mode)
207 {
208         char buf[512];
209         int r;
210
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();
216         }
217 }
218
219 static void mfd_assert_read(int fd)
220 {
221         char buf[16];
222         void *p;
223         ssize_t l;
224
225         l = read(fd, buf, sizeof(buf));
226         if (l != sizeof(buf)) {
227                 printf("read() failed: %m\n");
228                 abort();
229         }
230
231         /* verify PROT_READ *is* allowed */
232         p = mmap(NULL,
233                  mfd_def_size,
234                  PROT_READ,
235                  MAP_PRIVATE,
236                  fd,
237                  0);
238         if (p == MAP_FAILED) {
239                 printf("mmap() failed: %m\n");
240                 abort();
241         }
242         munmap(p, mfd_def_size);
243
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,
249                  fd,
250                  0);
251         if (p == MAP_FAILED) {
252                 printf("mmap() failed: %m\n");
253                 abort();
254         }
255         munmap(p, mfd_def_size);
256 }
257
258 static void mfd_assert_write(int fd)
259 {
260         ssize_t l;
261         void *p;
262         int r;
263
264         /*
265          * huegtlbfs does not support write, but we want to
266          * verify everything else here.
267          */
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();
274                 }
275         }
276
277         /* verify PROT_READ | PROT_WRITE is allowed */
278         p = mmap(NULL,
279                  mfd_def_size,
280                  PROT_READ | PROT_WRITE,
281                  MAP_SHARED,
282                  fd,
283                  0);
284         if (p == MAP_FAILED) {
285                 printf("mmap() failed: %m\n");
286                 abort();
287         }
288         *(char *)p = 0;
289         munmap(p, mfd_def_size);
290
291         /* verify PROT_WRITE is allowed */
292         p = mmap(NULL,
293                  mfd_def_size,
294                  PROT_WRITE,
295                  MAP_SHARED,
296                  fd,
297                  0);
298         if (p == MAP_FAILED) {
299                 printf("mmap() failed: %m\n");
300                 abort();
301         }
302         *(char *)p = 0;
303         munmap(p, mfd_def_size);
304
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,
311                  fd,
312                  0);
313         if (p == MAP_FAILED) {
314                 printf("mmap() failed: %m\n");
315                 abort();
316         }
317
318         r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
319         if (r < 0) {
320                 printf("mprotect() failed: %m\n");
321                 abort();
322         }
323
324         *(char *)p = 0;
325         munmap(p, mfd_def_size);
326
327         /* verify PUNCH_HOLE works */
328         r = fallocate(fd,
329                       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
330                       0,
331                       mfd_def_size);
332         if (r < 0) {
333                 printf("fallocate(PUNCH_HOLE) failed: %m\n");
334                 abort();
335         }
336 }
337
338 static void mfd_fail_write(int fd)
339 {
340         ssize_t l;
341         void *p;
342         int r;
343
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();
349         }
350
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,
356                  fd,
357                  0);
358         if (p != MAP_FAILED) {
359                 printf("mmap() didn't fail as expected\n");
360                 abort();
361         }
362
363         /* verify PROT_WRITE is not allowed */
364         p = mmap(NULL,
365                  mfd_def_size,
366                  PROT_WRITE,
367                  MAP_SHARED,
368                  fd,
369                  0);
370         if (p != MAP_FAILED) {
371                 printf("mmap() didn't fail as expected\n");
372                 abort();
373         }
374
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,
381                  fd,
382                  0);
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();
388                 }
389                 munmap(p, mfd_def_size);
390         }
391
392         /* verify PUNCH_HOLE fails */
393         r = fallocate(fd,
394                       FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
395                       0,
396                       mfd_def_size);
397         if (r >= 0) {
398                 printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
399                 abort();
400         }
401 }
402
403 static void mfd_assert_shrink(int fd)
404 {
405         int r, fd2;
406
407         r = ftruncate(fd, mfd_def_size / 2);
408         if (r < 0) {
409                 printf("ftruncate(SHRINK) failed: %m\n");
410                 abort();
411         }
412
413         mfd_assert_size(fd, mfd_def_size / 2);
414
415         fd2 = mfd_assert_open(fd,
416                               O_RDWR | O_CREAT | O_TRUNC,
417                               S_IRUSR | S_IWUSR);
418         close(fd2);
419
420         mfd_assert_size(fd, 0);
421 }
422
423 static void mfd_fail_shrink(int fd)
424 {
425         int r;
426
427         r = ftruncate(fd, mfd_def_size / 2);
428         if (r >= 0) {
429                 printf("ftruncate(SHRINK) didn't fail as expected\n");
430                 abort();
431         }
432
433         mfd_fail_open(fd,
434                       O_RDWR | O_CREAT | O_TRUNC,
435                       S_IRUSR | S_IWUSR);
436 }
437
438 static void mfd_assert_grow(int fd)
439 {
440         int r;
441
442         r = ftruncate(fd, mfd_def_size * 2);
443         if (r < 0) {
444                 printf("ftruncate(GROW) failed: %m\n");
445                 abort();
446         }
447
448         mfd_assert_size(fd, mfd_def_size * 2);
449
450         r = fallocate(fd,
451                       0,
452                       0,
453                       mfd_def_size * 4);
454         if (r < 0) {
455                 printf("fallocate(ALLOC) failed: %m\n");
456                 abort();
457         }
458
459         mfd_assert_size(fd, mfd_def_size * 4);
460 }
461
462 static void mfd_fail_grow(int fd)
463 {
464         int r;
465
466         r = ftruncate(fd, mfd_def_size * 2);
467         if (r >= 0) {
468                 printf("ftruncate(GROW) didn't fail as expected\n");
469                 abort();
470         }
471
472         r = fallocate(fd,
473                       0,
474                       0,
475                       mfd_def_size * 4);
476         if (r >= 0) {
477                 printf("fallocate(ALLOC) didn't fail as expected\n");
478                 abort();
479         }
480 }
481
482 static void mfd_assert_grow_write(int fd)
483 {
484         static char *buf;
485         ssize_t l;
486
487         /* hugetlbfs does not support write */
488         if (hugetlbfs_test)
489                 return;
490
491         buf = malloc(mfd_def_size * 8);
492         if (!buf) {
493                 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
494                 abort();
495         }
496
497         l = pwrite(fd, buf, mfd_def_size * 8, 0);
498         if (l != (mfd_def_size * 8)) {
499                 printf("pwrite() failed: %m\n");
500                 abort();
501         }
502
503         mfd_assert_size(fd, mfd_def_size * 8);
504 }
505
506 static void mfd_fail_grow_write(int fd)
507 {
508         static char *buf;
509         ssize_t l;
510
511         /* hugetlbfs does not support write */
512         if (hugetlbfs_test)
513                 return;
514
515         buf = malloc(mfd_def_size * 8);
516         if (!buf) {
517                 printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
518                 abort();
519         }
520
521         l = pwrite(fd, buf, mfd_def_size * 8, 0);
522         if (l == (mfd_def_size * 8)) {
523                 printf("pwrite() didn't fail as expected\n");
524                 abort();
525         }
526 }
527
528 static int idle_thread_fn(void *arg)
529 {
530         sigset_t set;
531         int sig;
532
533         /* dummy waiter; SIGTERM terminates us anyway */
534         sigemptyset(&set);
535         sigaddset(&set, SIGTERM);
536         sigwait(&set, &sig);
537
538         return 0;
539 }
540
541 static pid_t spawn_idle_thread(unsigned int flags)
542 {
543         uint8_t *stack;
544         pid_t pid;
545
546         stack = malloc(STACK_SIZE);
547         if (!stack) {
548                 printf("malloc(STACK_SIZE) failed: %m\n");
549                 abort();
550         }
551
552         pid = clone(idle_thread_fn,
553                     stack + STACK_SIZE,
554                     SIGCHLD | flags,
555                     NULL);
556         if (pid < 0) {
557                 printf("clone() failed: %m\n");
558                 abort();
559         }
560
561         return pid;
562 }
563
564 static void join_idle_thread(pid_t pid)
565 {
566         kill(pid, SIGTERM);
567         waitpid(pid, NULL, 0);
568 }
569
570 /*
571  * Test memfd_create() syscall
572  * Verify syscall-argument validation, including name checks, flag validation
573  * and more.
574  */
575 static void test_create(void)
576 {
577         char buf[2048];
578         int fd;
579
580         printf("%s CREATE\n", memfd_str);
581
582         /* test NULL name */
583         mfd_fail_new(NULL, 0);
584
585         /* test over-long name (not zero-terminated) */
586         memset(buf, 0xff, sizeof(buf));
587         mfd_fail_new(buf, 0);
588
589         /* test over-long zero-terminated name */
590         memset(buf, 0xff, sizeof(buf));
591         buf[sizeof(buf) - 1] = 0;
592         mfd_fail_new(buf, 0);
593
594         /* verify "" is a valid name */
595         fd = mfd_assert_new("", 0, 0);
596         close(fd);
597
598         /* verify invalid O_* open flags */
599         mfd_fail_new("", 0x0100);
600         mfd_fail_new("", ~MFD_CLOEXEC);
601         mfd_fail_new("", ~MFD_ALLOW_SEALING);
602         mfd_fail_new("", ~0);
603         mfd_fail_new("", 0x80000000U);
604
605         /* verify MFD_CLOEXEC is allowed */
606         fd = mfd_assert_new("", 0, MFD_CLOEXEC);
607         close(fd);
608
609         /* verify MFD_ALLOW_SEALING is allowed */
610         fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
611         close(fd);
612
613         /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
614         fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
615         close(fd);
616 }
617
618 /*
619  * Test basic sealing
620  * A very basic sealing test to see whether setting/retrieving seals works.
621  */
622 static void test_basic(void)
623 {
624         int fd;
625
626         printf("%s BASIC\n", memfd_str);
627
628         fd = mfd_assert_new("kern_memfd_basic",
629                             mfd_def_size,
630                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
631
632         /* add basic seals */
633         mfd_assert_has_seals(fd, 0);
634         mfd_assert_add_seals(fd, F_SEAL_SHRINK |
635                                  F_SEAL_WRITE);
636         mfd_assert_has_seals(fd, F_SEAL_SHRINK |
637                                  F_SEAL_WRITE);
638
639         /* add them again */
640         mfd_assert_add_seals(fd, F_SEAL_SHRINK |
641                                  F_SEAL_WRITE);
642         mfd_assert_has_seals(fd, F_SEAL_SHRINK |
643                                  F_SEAL_WRITE);
644
645         /* add more seals and seal against sealing */
646         mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
647         mfd_assert_has_seals(fd, F_SEAL_SHRINK |
648                                  F_SEAL_GROW |
649                                  F_SEAL_WRITE |
650                                  F_SEAL_SEAL);
651
652         /* verify that sealing no longer works */
653         mfd_fail_add_seals(fd, F_SEAL_GROW);
654         mfd_fail_add_seals(fd, 0);
655
656         close(fd);
657
658         /* verify sealing does not work without MFD_ALLOW_SEALING */
659         fd = mfd_assert_new("kern_memfd_basic",
660                             mfd_def_size,
661                             MFD_CLOEXEC);
662         mfd_assert_has_seals(fd, F_SEAL_SEAL);
663         mfd_fail_add_seals(fd, F_SEAL_SHRINK |
664                                F_SEAL_GROW |
665                                F_SEAL_WRITE);
666         mfd_assert_has_seals(fd, F_SEAL_SEAL);
667         close(fd);
668 }
669
670 /*
671  * Test SEAL_WRITE
672  * Test whether SEAL_WRITE actually prevents modifications.
673  */
674 static void test_seal_write(void)
675 {
676         int fd;
677
678         printf("%s SEAL-WRITE\n", memfd_str);
679
680         fd = mfd_assert_new("kern_memfd_seal_write",
681                             mfd_def_size,
682                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
683         mfd_assert_has_seals(fd, 0);
684         mfd_assert_add_seals(fd, F_SEAL_WRITE);
685         mfd_assert_has_seals(fd, F_SEAL_WRITE);
686
687         mfd_assert_read(fd);
688         mfd_fail_write(fd);
689         mfd_assert_shrink(fd);
690         mfd_assert_grow(fd);
691         mfd_fail_grow_write(fd);
692
693         close(fd);
694 }
695
696 /*
697  * Test SEAL_SHRINK
698  * Test whether SEAL_SHRINK actually prevents shrinking
699  */
700 static void test_seal_shrink(void)
701 {
702         int fd;
703
704         printf("%s SEAL-SHRINK\n", memfd_str);
705
706         fd = mfd_assert_new("kern_memfd_seal_shrink",
707                             mfd_def_size,
708                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
709         mfd_assert_has_seals(fd, 0);
710         mfd_assert_add_seals(fd, F_SEAL_SHRINK);
711         mfd_assert_has_seals(fd, F_SEAL_SHRINK);
712
713         mfd_assert_read(fd);
714         mfd_assert_write(fd);
715         mfd_fail_shrink(fd);
716         mfd_assert_grow(fd);
717         mfd_assert_grow_write(fd);
718
719         close(fd);
720 }
721
722 /*
723  * Test SEAL_GROW
724  * Test whether SEAL_GROW actually prevents growing
725  */
726 static void test_seal_grow(void)
727 {
728         int fd;
729
730         printf("%s SEAL-GROW\n", memfd_str);
731
732         fd = mfd_assert_new("kern_memfd_seal_grow",
733                             mfd_def_size,
734                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
735         mfd_assert_has_seals(fd, 0);
736         mfd_assert_add_seals(fd, F_SEAL_GROW);
737         mfd_assert_has_seals(fd, F_SEAL_GROW);
738
739         mfd_assert_read(fd);
740         mfd_assert_write(fd);
741         mfd_assert_shrink(fd);
742         mfd_fail_grow(fd);
743         mfd_fail_grow_write(fd);
744
745         close(fd);
746 }
747
748 /*
749  * Test SEAL_SHRINK | SEAL_GROW
750  * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
751  */
752 static void test_seal_resize(void)
753 {
754         int fd;
755
756         printf("%s SEAL-RESIZE\n", memfd_str);
757
758         fd = mfd_assert_new("kern_memfd_seal_resize",
759                             mfd_def_size,
760                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
761         mfd_assert_has_seals(fd, 0);
762         mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
763         mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
764
765         mfd_assert_read(fd);
766         mfd_assert_write(fd);
767         mfd_fail_shrink(fd);
768         mfd_fail_grow(fd);
769         mfd_fail_grow_write(fd);
770
771         close(fd);
772 }
773
774 /*
775  * Test sharing via dup()
776  * Test that seals are shared between dupped FDs and they're all equal.
777  */
778 static void test_share_dup(char *banner, char *b_suffix)
779 {
780         int fd, fd2;
781
782         printf("%s %s %s\n", memfd_str, banner, b_suffix);
783
784         fd = mfd_assert_new("kern_memfd_share_dup",
785                             mfd_def_size,
786                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
787         mfd_assert_has_seals(fd, 0);
788
789         fd2 = mfd_assert_dup(fd);
790         mfd_assert_has_seals(fd2, 0);
791
792         mfd_assert_add_seals(fd, F_SEAL_WRITE);
793         mfd_assert_has_seals(fd, F_SEAL_WRITE);
794         mfd_assert_has_seals(fd2, F_SEAL_WRITE);
795
796         mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
797         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
798         mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
799
800         mfd_assert_add_seals(fd, F_SEAL_SEAL);
801         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
802         mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
803
804         mfd_fail_add_seals(fd, F_SEAL_GROW);
805         mfd_fail_add_seals(fd2, F_SEAL_GROW);
806         mfd_fail_add_seals(fd, F_SEAL_SEAL);
807         mfd_fail_add_seals(fd2, F_SEAL_SEAL);
808
809         close(fd2);
810
811         mfd_fail_add_seals(fd, F_SEAL_GROW);
812         close(fd);
813 }
814
815 /*
816  * Test sealing with active mmap()s
817  * Modifying seals is only allowed if no other mmap() refs exist.
818  */
819 static void test_share_mmap(char *banner, char *b_suffix)
820 {
821         int fd;
822         void *p;
823
824         printf("%s %s %s\n", memfd_str,  banner, b_suffix);
825
826         fd = mfd_assert_new("kern_memfd_share_mmap",
827                             mfd_def_size,
828                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
829         mfd_assert_has_seals(fd, 0);
830
831         /* shared/writable ref prevents sealing WRITE, but allows others */
832         p = mfd_assert_mmap_shared(fd);
833         mfd_fail_add_seals(fd, F_SEAL_WRITE);
834         mfd_assert_has_seals(fd, 0);
835         mfd_assert_add_seals(fd, F_SEAL_SHRINK);
836         mfd_assert_has_seals(fd, F_SEAL_SHRINK);
837         munmap(p, mfd_def_size);
838
839         /* readable ref allows sealing */
840         p = mfd_assert_mmap_private(fd);
841         mfd_assert_add_seals(fd, F_SEAL_WRITE);
842         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
843         munmap(p, mfd_def_size);
844
845         close(fd);
846 }
847
848 /*
849  * Test sealing with open(/proc/self/fd/%d)
850  * Via /proc we can get access to a separate file-context for the same memfd.
851  * This is *not* like dup(), but like a real separate open(). Make sure the
852  * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
853  */
854 static void test_share_open(char *banner, char *b_suffix)
855 {
856         int fd, fd2;
857
858         printf("%s %s %s\n", memfd_str, banner, b_suffix);
859
860         fd = mfd_assert_new("kern_memfd_share_open",
861                             mfd_def_size,
862                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
863         mfd_assert_has_seals(fd, 0);
864
865         fd2 = mfd_assert_open(fd, O_RDWR, 0);
866         mfd_assert_add_seals(fd, F_SEAL_WRITE);
867         mfd_assert_has_seals(fd, F_SEAL_WRITE);
868         mfd_assert_has_seals(fd2, F_SEAL_WRITE);
869
870         mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
871         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
872         mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
873
874         close(fd);
875         fd = mfd_assert_open(fd2, O_RDONLY, 0);
876
877         mfd_fail_add_seals(fd, F_SEAL_SEAL);
878         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
879         mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
880
881         close(fd2);
882         fd2 = mfd_assert_open(fd, O_RDWR, 0);
883
884         mfd_assert_add_seals(fd2, F_SEAL_SEAL);
885         mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
886         mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
887
888         close(fd2);
889         close(fd);
890 }
891
892 /*
893  * Test sharing via fork()
894  * Test whether seal-modifications work as expected with forked childs.
895  */
896 static void test_share_fork(char *banner, char *b_suffix)
897 {
898         int fd;
899         pid_t pid;
900
901         printf("%s %s %s\n", memfd_str, banner, b_suffix);
902
903         fd = mfd_assert_new("kern_memfd_share_fork",
904                             mfd_def_size,
905                             MFD_CLOEXEC | MFD_ALLOW_SEALING);
906         mfd_assert_has_seals(fd, 0);
907
908         pid = spawn_idle_thread(0);
909         mfd_assert_add_seals(fd, F_SEAL_SEAL);
910         mfd_assert_has_seals(fd, F_SEAL_SEAL);
911
912         mfd_fail_add_seals(fd, F_SEAL_WRITE);
913         mfd_assert_has_seals(fd, F_SEAL_SEAL);
914
915         join_idle_thread(pid);
916
917         mfd_fail_add_seals(fd, F_SEAL_WRITE);
918         mfd_assert_has_seals(fd, F_SEAL_SEAL);
919
920         close(fd);
921 }
922
923 int main(int argc, char **argv)
924 {
925         pid_t pid;
926
927         if (argc == 2) {
928                 if (!strcmp(argv[1], "hugetlbfs")) {
929                         unsigned long hpage_size = default_huge_page_size();
930
931                         if (!hpage_size) {
932                                 printf("Unable to determine huge page size\n");
933                                 abort();
934                         }
935
936                         hugetlbfs_test = 1;
937                         memfd_str = MEMFD_HUGE_STR;
938                         mfd_def_size = hpage_size * 2;
939                 } else {
940                         printf("Unknown option: %s\n", argv[1]);
941                         abort();
942                 }
943         }
944
945         test_create();
946         test_basic();
947
948         test_seal_write();
949         test_seal_shrink();
950         test_seal_grow();
951         test_seal_resize();
952
953         test_share_dup("SHARE-DUP", "");
954         test_share_mmap("SHARE-MMAP", "");
955         test_share_open("SHARE-OPEN", "");
956         test_share_fork("SHARE-FORK", "");
957
958         /* Run test-suite in a multi-threaded environment with a shared
959          * file-table. */
960         pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
961         test_share_dup("SHARE-DUP", SHARED_FT_STR);
962         test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
963         test_share_open("SHARE-OPEN", SHARED_FT_STR);
964         test_share_fork("SHARE-FORK", SHARED_FT_STR);
965         join_idle_thread(pid);
966
967         printf("memfd: DONE\n");
968
969         return 0;
970 }