Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap
@ 2026-11-19 19:22 Dominik Maier
  0 siblings, 0 replies; only message in thread
From: Dominik Maier @ 2026-11-19 19:22 UTC (permalink / raw)
  To: Muchun Song, Oscar Salvador, Andrew Morton
  Cc: David Hildenbrand, linux-mm, dmnk+artist, Dominik Maier

hugetlbfs_file_mmap() expands inode->i_size whenever VMA_WRITE_BIT is set
and len > inode->i_size, without checking whether a shared mapping
violates F_SEAL_GROW or inode_newsize_ok(), or whether a private mapping
holds write access to the underlying file and has no active write/growth
seals.

Because do_mmap() and memfd_check_seals_mmap() allow PROT_WRITE with
MAP_PRIVATE | MAP_NORESERVE on O_RDONLY descriptors and sealed
MFD_HUGETLB memfds, an untrusted IPC recipient holding a read-only or
F_SEAL_GROW-sealed hugetlb descriptor can inflate the shared
inode->i_size to multi-terabyte values even when vm.nr_hugepages is 0,
causing trusted peers relying on F_SEAL_GROW to hit SIGBUS past the
allocated pages. Similarly, MAP_SHARED | PROT_WRITE beyond i_size on an
F_SEAL_GROW memfd (without F_SEAL_WRITE) bypasses the growth seal.

For MAP_SHARED | PROT_WRITE mappings with len > inode->i_size, reject
F_SEAL_GROW with -EPERM and enforce inode_newsize_ok() before reserving
huge pages. For MAP_PRIVATE | PROT_WRITE mappings, keep allowing the
private VMA itself while only expanding inode->i_size when FMODE_WRITE
is set, no growth/write seals are active, and inode_newsize_ok()
succeeds.

This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available upon maintainers' request. The fix was verified against the
reproducer in QEMU/KVM, compile-tested with -Werror, and checked with
checkpatch.pl --strict.

Fixes: ff62a3421044 ("hugetlb: implement memfd sealing")
Fixes: 045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
 fs/hugetlbfs/inode.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 02e0a65..de416fc 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -100,10 +100,12 @@ static const struct fs_parameter_spec hugetlb_fs_parameters[] = {
 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct inode *inode = file_inode(file);
+	struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
 	loff_t len, vma_len;
 	int ret;
 	struct hstate *h = hstate_file(file);
 	vma_flags_t vma_flags;
+	bool grow_inode = false;
 
 	/*
 	 * vma address alignment (but not the pgoff alignment) has
@@ -139,6 +141,24 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 	inode_lock(inode);
 	file_accessed(file);
 
+	if (vma_test(vma, VMA_WRITE_BIT) && inode->i_size < len) {
+		if (vma_test(vma, VMA_SHARED_BIT)) {
+			if (info->seals & F_SEAL_GROW) {
+				ret = -EPERM;
+				goto out;
+			}
+			ret = inode_newsize_ok(inode, len);
+			if (ret)
+				goto out;
+			grow_inode = true;
+		} else if ((file->f_mode & FMODE_WRITE) &&
+			   !(info->seals & (F_SEAL_GROW | F_SEAL_WRITE |
+					    F_SEAL_FUTURE_WRITE)) &&
+			   !inode_newsize_ok(inode, len)) {
+			grow_inode = true;
+		}
+	}
+
 	ret = -ENOMEM;
 
 	vma_flags = vma->flags;
@@ -157,7 +177,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 		goto out;
 
 	ret = 0;
-	if (vma_test(vma, VMA_WRITE_BIT) && inode->i_size < len)
+	if (grow_inode)
 		i_size_write(inode, len);
 out:
 	inode_unlock(inode);
-- 
2.56.1.750.gf70dc682cf-goog



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19 19:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:22 [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap Dominik Maier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox