@@ -54,6 +54,22 @@ static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
54
54
return fd ;
55
55
}
56
56
57
+ static int mfd_assert_reopen_fd (int fd_in )
58
+ {
59
+ int r , fd ;
60
+ char path [100 ];
61
+
62
+ sprintf (path , "/proc/self/fd/%d" , fd_in );
63
+
64
+ fd = open (path , O_RDWR );
65
+ if (fd < 0 ) {
66
+ printf ("re-open of existing fd %d failed\n" , fd_in );
67
+ abort ();
68
+ }
69
+
70
+ return fd ;
71
+ }
72
+
57
73
static void mfd_fail_new (const char * name , unsigned int flags )
58
74
{
59
75
int r ;
@@ -255,6 +271,25 @@ static void mfd_assert_read(int fd)
255
271
munmap (p , mfd_def_size );
256
272
}
257
273
274
+ /* Test that PROT_READ + MAP_SHARED mappings work. */
275
+ static void mfd_assert_read_shared (int fd )
276
+ {
277
+ void * p ;
278
+
279
+ /* verify PROT_READ and MAP_SHARED *is* allowed */
280
+ p = mmap (NULL ,
281
+ mfd_def_size ,
282
+ PROT_READ ,
283
+ MAP_SHARED ,
284
+ fd ,
285
+ 0 );
286
+ if (p == MAP_FAILED ) {
287
+ printf ("mmap() failed: %m\n" );
288
+ abort ();
289
+ }
290
+ munmap (p , mfd_def_size );
291
+ }
292
+
258
293
static void mfd_assert_write (int fd )
259
294
{
260
295
ssize_t l ;
@@ -692,6 +727,44 @@ static void test_seal_write(void)
692
727
close (fd );
693
728
}
694
729
730
+ /*
731
+ * Test SEAL_FUTURE_WRITE
732
+ * Test whether SEAL_FUTURE_WRITE actually prevents modifications.
733
+ */
734
+ static void test_seal_future_write (void )
735
+ {
736
+ int fd , fd2 ;
737
+ void * p ;
738
+
739
+ printf ("%s SEAL-FUTURE-WRITE\n" , memfd_str );
740
+
741
+ fd = mfd_assert_new ("kern_memfd_seal_future_write" ,
742
+ mfd_def_size ,
743
+ MFD_CLOEXEC | MFD_ALLOW_SEALING );
744
+
745
+ p = mfd_assert_mmap_shared (fd );
746
+
747
+ mfd_assert_has_seals (fd , 0 );
748
+
749
+ mfd_assert_add_seals (fd , F_SEAL_FUTURE_WRITE );
750
+ mfd_assert_has_seals (fd , F_SEAL_FUTURE_WRITE );
751
+
752
+ /* read should pass, writes should fail */
753
+ mfd_assert_read (fd );
754
+ mfd_assert_read_shared (fd );
755
+ mfd_fail_write (fd );
756
+
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 );
762
+
763
+ munmap (p , mfd_def_size );
764
+ close (fd2 );
765
+ close (fd );
766
+ }
767
+
695
768
/*
696
769
* Test SEAL_SHRINK
697
770
* Test whether SEAL_SHRINK actually prevents shrinking
@@ -945,6 +1018,7 @@ int main(int argc, char **argv)
945
1018
test_basic ();
946
1019
947
1020
test_seal_write ();
1021
+ test_seal_future_write ();
948
1022
test_seal_shrink ();
949
1023
test_seal_grow ();
950
1024
test_seal_resize ();
0 commit comments