Skip to content

Commit 5440298

Browse files
joelagneltorvalds
authored andcommitted
selftests/memfd: add tests for F_SEAL_FUTURE_WRITE seal
Add tests to verify sealing memfds with the F_SEAL_FUTURE_WRITE works as expected. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Joel Fernandes (Google) <[email protected]> Reviewed-by: Shuah Khan <[email protected]> Cc: Al Viro <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jann Horn <[email protected]> Cc: J. Bruce Fields <[email protected]> Cc: Jeff Layton <[email protected]> Cc: John Stultz <[email protected]> Cc: Marc-Andr Lureau <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Minchan Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent ab3948f commit 5440298

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

tools/testing/selftests/memfd/memfd_test.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
5454
return fd;
5555
}
5656

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+
5773
static void mfd_fail_new(const char *name, unsigned int flags)
5874
{
5975
int r;
@@ -255,6 +271,25 @@ static void mfd_assert_read(int fd)
255271
munmap(p, mfd_def_size);
256272
}
257273

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+
258293
static void mfd_assert_write(int fd)
259294
{
260295
ssize_t l;
@@ -692,6 +727,44 @@ static void test_seal_write(void)
692727
close(fd);
693728
}
694729

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+
695768
/*
696769
* Test SEAL_SHRINK
697770
* Test whether SEAL_SHRINK actually prevents shrinking
@@ -945,6 +1018,7 @@ int main(int argc, char **argv)
9451018
test_basic();
9461019

9471020
test_seal_write();
1021+
test_seal_future_write();
9481022
test_seal_shrink();
9491023
test_seal_grow();
9501024
test_seal_resize();

0 commit comments

Comments
 (0)