Users of memfd_pin_folios(), such as udmabuf and iommufd (IOMMU_IOAS_MAP_FILE), pin the folios backing a memfd range and keep using them for DMA. A pin keeps a folio alive, but it does not keep the folio in the file. If userspace later punches a hole with fallocate(FALLOC_FL_PUNCH_HOLE) or madvise(MADV_REMOVE), the folio is removed from the page cache while the pin still holds it, and the pin holder is not told. From then on the device and the file no longer share memory: data written by the device is not visible through the file, and data written through the file never reaches the device. udmabuf already requires F_SEAL_SHRINK so that truncation cannot remove pages, but there is no seal that prevents punching holes without also preventing writes. F_SEAL_WRITE and F_SEAL_FUTURE_WRITE do block hole punching, but they cannot be used for memory that has to stay writable, and udmabuf rejects memfds that carry them. Add F_SEAL_PUNCH_HOLE, which makes fallocate(FALLOC_FL_PUNCH_HOLE) fail with -EPERM on shmem and hugetlbfs memfds. MADV_REMOVE is covered as well, because it is implemented with vfs_fallocate() and FALLOC_FL_PUNCH_HOLE. Signed-off-by: Samiullah Khawaja --- fs/hugetlbfs/inode.c | 3 ++- include/uapi/linux/fcntl.h | 1 + mm/memfd.c | 4 +++- mm/shmem.c | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index e8a05d3de327..65034b72b98e 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -660,7 +660,8 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) inode_lock(inode); /* protected by i_rwsem */ - if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { + if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE | + F_SEAL_PUNCH_HOLE)) { inode_unlock(inode); return -EPERM; } diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index e43e3de3e9ee..f9dd929adaf4 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -50,6 +50,7 @@ #define F_SEAL_WRITE 0x0008 /* prevent writes */ #define F_SEAL_FUTURE_WRITE 0x0010 /* prevent future writes while mapped */ #define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */ +#define F_SEAL_PUNCH_HOLE 0x0040 /* prevent punching holes */ /* (1U << 31) is reserved for signed error codes */ /* diff --git a/mm/memfd.c b/mm/memfd.c index 0f6fff004f5e..3f0ef72fb301 100644 --- a/mm/memfd.c +++ b/mm/memfd.c @@ -227,7 +227,8 @@ static unsigned int *memfd_file_seals_ptr(struct file *file) F_SEAL_SHRINK | \ F_SEAL_GROW | \ F_SEAL_WRITE | \ - F_SEAL_FUTURE_WRITE) + F_SEAL_FUTURE_WRITE | \ + F_SEAL_PUNCH_HOLE) int memfd_add_seals(struct file *file, unsigned int seals) { @@ -253,6 +254,7 @@ int memfd_add_seals(struct file *file, unsigned int seals) * SEAL_GROW: Prevent the file from growing * SEAL_WRITE: Prevent write access to the file * SEAL_EXEC: Prevent modification of the exec bits in the file mode + * SEAL_PUNCH_HOLE: Prevent punching holes into the file * * As we don't require any trust relationship between two parties, we * must prevent seals from being removed. Therefore, sealing a file diff --git a/mm/shmem.c b/mm/shmem.c index fd0953d55fd6..e79a298d6292 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3853,7 +3853,8 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); /* protected by i_rwsem */ - if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { + if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE | + F_SEAL_PUNCH_HOLE)) { error = -EPERM; goto out; } -- 2.56.0.307.g5fe7bd680c-goog Add tests for the new F_SEAL_PUNCH_HOLE seal. Like the rest of memfd_test, they run on shmem memfds by default and on hugetlbfs memfds when memfd_test is invoked with the "hugetlbfs" argument. mfd_assert_write() checks that the file can be written, and then also checks that a hole can be punched into it. The second check no longer holds once F_SEAL_PUNCH_HOLE is set. Move the write checks into __mfd_assert_write() and the punch check into mfd_assert_punch_hole(), and have mfd_assert_write() call both, so the existing tests keep doing exactly what they did before. Add mfd_fail_punch_hole(), which checks that both fallocate(FALLOC_FL_PUNCH_HOLE) and madvise(MADV_REMOVE) fail with EPERM, and use it in two new tests. SEAL-PUNCH-HOLE sets only the new seal and checks that punching a hole fails, while writing, shrinking and growing the file still work. SEAL-PUNCH-HOLE-SHRINK adds F_SEAL_SHRINK as well and checks that the file can neither have holes punched into it nor be shrunk, while it can still grow. Signed-off-by: Samiullah Khawaja --- tools/testing/selftests/memfd/memfd_test.c | 111 ++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c index cdab3a837624..095c5eb255af 100644 --- a/tools/testing/selftests/memfd/memfd_test.c +++ b/tools/testing/selftests/memfd/memfd_test.c @@ -35,6 +35,10 @@ #define F_SEAL_EXEC 0x0020 +#ifndef F_SEAL_PUNCH_HOLE +#define F_SEAL_PUNCH_HOLE 0x0040 +#endif + #define F_WX_SEALS (F_SEAL_SHRINK | \ F_SEAL_GROW | \ F_SEAL_WRITE | \ @@ -455,7 +459,7 @@ static void mfd_assert_fork_private_write(int fd) munmap(p, mfd_def_size); } -static void mfd_assert_write(int fd) +static void __mfd_assert_write(int fd) { ssize_t l; void *p; @@ -523,6 +527,11 @@ static void mfd_assert_write(int fd) *(char *)p = 0; munmap(p, mfd_def_size); +} + +static void mfd_assert_punch_hole(int fd) +{ + int r; /* verify PUNCH_HOLE works */ r = fallocate(fd, @@ -535,6 +544,49 @@ static void mfd_assert_write(int fd) } } +static void mfd_fail_punch_hole(int fd) +{ + void *p; + int r; + + /* verify PUNCH_HOLE fails */ + r = fallocate(fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + 0, + mfd_def_size); + if (r >= 0 || errno != EPERM) { + printf("expected EPERM on fallocate(PUNCH_HOLE), but got %d: %m\n", + r); + abort(); + } + + /* verify MADV_REMOVE fails, it punches a hole as well */ + p = mmap(NULL, + mfd_def_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + fd, + 0); + if (p == MAP_FAILED) { + printf("mmap() failed: %m\n"); + abort(); + } + + r = madvise(p, mfd_def_size, MADV_REMOVE); + if (r >= 0 || errno != EPERM) { + printf("expected EPERM on madvise(MADV_REMOVE), but got %d: %m\n", + r); + abort(); + } + munmap(p, mfd_def_size); +} + +static void mfd_assert_write(int fd) +{ + __mfd_assert_write(fd); + mfd_assert_punch_hole(fd); +} + static void mfd_fail_write(int fd) { ssize_t l; @@ -1117,6 +1169,61 @@ static void test_seal_resize(void) close(fd); } +/* + * Test SEAL_PUNCH_HOLE + * Test whether SEAL_PUNCH_HOLE actually prevents punching holes, but still + * allows writing and resizing + */ +static void test_seal_punch_hole(void) +{ + int fd; + + printf("%s SEAL-PUNCH-HOLE\n", memfd_str); + + fd = mfd_assert_new("kern_memfd_seal_punch_hole", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + mfd_assert_has_seals(fd, 0); + mfd_assert_add_seals(fd, F_SEAL_PUNCH_HOLE); + mfd_assert_has_seals(fd, F_SEAL_PUNCH_HOLE); + + mfd_assert_read(fd); + __mfd_assert_write(fd); + mfd_fail_punch_hole(fd); + mfd_assert_shrink(fd); + mfd_assert_grow(fd); + mfd_assert_grow_write(fd); + + close(fd); +} + +/* + * Test SEAL_PUNCH_HOLE | SEAL_SHRINK + * Test whether SEAL_PUNCH_HOLE | SEAL_SHRINK prevents removing pages from + * the file + */ +static void test_seal_punch_hole_shrink(void) +{ + int fd; + + printf("%s SEAL-PUNCH-HOLE-SHRINK\n", memfd_str); + + fd = mfd_assert_new("kern_memfd_seal_punch_hole_shrink", + mfd_def_size, + MFD_CLOEXEC | MFD_ALLOW_SEALING); + mfd_assert_has_seals(fd, 0); + mfd_assert_add_seals(fd, F_SEAL_PUNCH_HOLE | F_SEAL_SHRINK); + mfd_assert_has_seals(fd, F_SEAL_PUNCH_HOLE | F_SEAL_SHRINK); + + mfd_assert_read(fd); + __mfd_assert_write(fd); + mfd_fail_punch_hole(fd); + mfd_fail_shrink(fd); + mfd_assert_grow(fd); + + close(fd); +} + /* * Test SEAL_EXEC * Test fd is created with exec and allow sealing. @@ -1736,6 +1843,8 @@ int main(int argc, char **argv) test_seal_shrink(); test_seal_grow(); test_seal_resize(); + test_seal_punch_hole(); + test_seal_punch_hole_shrink(); if (pid_ns_supported()) { test_sysctl_simple(); -- 2.56.0.307.g5fe7bd680c-goog