Skip to content

Commit ea0916e

Browse files
lorenzo-stoakesakpm00
authored andcommitted
selftests/memfd: add test for mapping write-sealed memfd read-only
Now we have reinstated the ability to map F_SEAL_WRITE mappings read-only, assert that we are able to do this in a test to ensure that we do not regress this again. Link: https://lkml.kernel.org/r/a6377ec470b14c0539b4600cf8fa24bf2e4858ae.1732804776.git.lorenzo.stoakes@oracle.com Signed-off-by: Lorenzo Stoakes <[email protected]> Cc: Jann Horn <[email protected]> Cc: Julian Orth <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8ec396d commit ea0916e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tools/testing/selftests/memfd/memfd_test.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,24 @@ static void *mfd_assert_mmap_shared(int fd)
282282
return p;
283283
}
284284

285+
static void *mfd_assert_mmap_read_shared(int fd)
286+
{
287+
void *p;
288+
289+
p = mmap(NULL,
290+
mfd_def_size,
291+
PROT_READ,
292+
MAP_SHARED,
293+
fd,
294+
0);
295+
if (p == MAP_FAILED) {
296+
printf("mmap() failed: %m\n");
297+
abort();
298+
}
299+
300+
return p;
301+
}
302+
285303
static void *mfd_assert_mmap_private(int fd)
286304
{
287305
void *p;
@@ -980,6 +998,30 @@ static void test_seal_future_write(void)
980998
close(fd);
981999
}
9821000

1001+
static void test_seal_write_map_read_shared(void)
1002+
{
1003+
int fd;
1004+
void *p;
1005+
1006+
printf("%s SEAL-WRITE-MAP-READ\n", memfd_str);
1007+
1008+
fd = mfd_assert_new("kern_memfd_seal_write_map_read",
1009+
mfd_def_size,
1010+
MFD_CLOEXEC | MFD_ALLOW_SEALING);
1011+
1012+
mfd_assert_add_seals(fd, F_SEAL_WRITE);
1013+
mfd_assert_has_seals(fd, F_SEAL_WRITE);
1014+
1015+
p = mfd_assert_mmap_read_shared(fd);
1016+
1017+
mfd_assert_read(fd);
1018+
mfd_assert_read_shared(fd);
1019+
mfd_fail_write(fd);
1020+
1021+
munmap(p, mfd_def_size);
1022+
close(fd);
1023+
}
1024+
9831025
/*
9841026
* Test SEAL_SHRINK
9851027
* Test whether SEAL_SHRINK actually prevents shrinking
@@ -1593,6 +1635,7 @@ int main(int argc, char **argv)
15931635

15941636
test_seal_write();
15951637
test_seal_future_write();
1638+
test_seal_write_map_read_shared();
15961639
test_seal_shrink();
15971640
test_seal_grow();
15981641
test_seal_resize();

0 commit comments

Comments
 (0)