Skip to contentSkip to conversation

hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap

PATCH In review Dominik Maier 3 participants 4 messages Nov 19 – Nov 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap

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")
---
 fs/hugetlbfs/inode.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
fs/hugetlbfs/inode.c +21 -1
index 02e0a65..de416fc 100644
@@ -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);
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
#1 lore raw
Quoted 29 lines · 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.

Quoted 4 lines
Fixes ff62a3421044 ("hugetlb: implement memfd sealing")
Fixes 045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap")

Thanks for your report.

Your patch does various things, and I had AI fixes for all of these issues already. I accepted some, I rejected some.

Take a look at:

https://lore.kernel.org/all/[email protected]/

That was already merged, and, in particular, my description in it.

"We currently also allow CoW mappings to grow a file. Disallowing this, maybe even only for read-only file descriptors, could break existing userspace, so we have to be more careful. QEMU's file backend is an example user that might rely on such things, and would suddenly start getting SIGBUS when faulting in memory outside EOF.

So be careful and focus on F_SEAL_GROW for now."

And in my private notes I have to a bug fix called "hugetlbfs: grow a file in mmap() only if open for write"

"There is a real chance to break user-space: for example, supply an empty hugetlb file to QEMU, which might open it read-only and then SIGBUS on access. It would only warn that fallocate() fails and continue. This is long-standing behavior. Read-only users can already fill hugetlb files holes through read-faults ..."

So I don't think there is anything left to fix here that would be worth changing long-standing behavior and possibly breaking user space.

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 59 lines · David Hildenbrand (Arm)
Quoted 29 lines · 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.

Quoted 4 lines
Fixes ff62a3421044 ("hugetlb: implement memfd sealing")
Fixes 045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap")

Thanks for your report.

Your patch does various things, and I had AI fixes for all of these issues already. I accepted some, I rejected some.

Take a look at:

https://lore.kernel.org/all/[email protected]/

That was already merged, and, in particular, my description in it.

"We currently also allow CoW mappings to grow a file. Disallowing this, maybe even only for read-only file descriptors, could break existing userspace, so we have to be more careful. QEMU's file backend is an example user that might rely on such things, and would suddenly start getting SIGBUS when faulting in memory outside EOF.

So be careful and focus on F_SEAL_GROW for now."

And in my private notes I have to a bug fix called "hugetlbfs: grow a file in mmap() only if open for write"

Quoted 8 lines

"There is a real chance to break user-space: for example, supply an empty hugetlb file to QEMU, which might open it read-only and then SIGBUS on access. It would only warn that fallocate() fails and continue. This is long-standing behavior. Read-only users can already fill hugetlb files holes through read-faults ..."

So I don't think there is anything left to fix here that would be worth changing long-standing behavior and possibly breaking user space.

Thinking again, I assume we could also let F_SEAL_GROW stop reserving beyond EOF. But everything else just has the chance to break user-space.

Signature · 3 lines
Cheers,

David
#3 lore raw
In reply to Dominik Maier
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] hugetlbfs: non-fatal inode_newsize_ok() in hugetlbfs_file_mmap() sends SIGXFSZ - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] hugetlbfs: non-fatal inode_newsize_ok() in hugetlbfs_file_mmap() sends SIGXFSZ
- [Medium] hugetlbfs: missing write-seal check in hugetlbfs_file_mmap() shared branch
- [Low] hugetlbfs: inaccurate Fixes trailer 045c7a3f53d9 for MAP_PRIVATE i_size growth
--

commit 9d1ca8a773e4ed12af22f0ee1ea69a706da80ecd
Author: Dominik Maier <[email protected]>

Quoted 31 lines

hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap

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.

Quoted 4 lines
Fixes ff62a3421044 ("hugetlb: implement memfd sealing")
Fixes 045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap")

[Severity: Low]
045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap") only converted inode->i_size = len to i_size_write(inode, len). The commit that allowed MAP_PRIVATE | PROT_WRITE mappings on O_RDONLY descriptors to reach the i_size expansion was 1e8f889b10d8 ("[PATCH] Hugetlb: Copy on Write support"), which removed the early check rejecting writable private mappings.

Quoted 18 lines
fs/hugetlbfs/inode.c +2 -0
index ca63a530a26a1..c75f9ea4c9043 100644
@@ -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

fs/hugetlbfs/inode.c static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) · line 149

Quoted 9 lines
fs/hugetlbfs/inode.c +6 -0
@@ -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;
+			}

[Severity: Medium]
Should the VMA_SHARED_BIT check also reject F_SEAL_WRITE and F_SEAL_FUTURE_WRITE under inode_lock(inode), like the MAP_PRIVATE branch below?

memfd_check_seals_mmap() in do_mmap() checks seals without holding inode_lock(inode), and memfd_add_seals() only calls mapping_deny_writable() for F_SEAL_WRITE, not F_SEAL_FUTURE_WRITE:

CPU0 CPU1
do_mmap()
memfd_check_seals_mmap()

                                      memfd_add_seals(..., F_SEAL_FUTURE_WRITE)
                                        inode_lock(inode)
                                        *file_seals |= F_SEAL_FUTURE_WRITE
                                        inode_unlock(inode)
  ...
  hugetlbfs_file_mmap()
    inode_lock(inode)
    if (info->seals & F_SEAL_GROW)
    ...
    i_size_write(inode, len)  <-- bypasses F_SEAL_FUTURE_WRITE

A concurrent MAP_SHARED | PROT_WRITE mmap() can therefore bypass F_SEAL_FUTURE_WRITE after the seal has been committed, growing inode->i_size and creating a writable shared mapping.

fs/hugetlbfs/inode.c static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) · line 160

Quoted 11 lines
fs/hugetlbfs/inode.c +11 -0
+			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;
+		}
+	}

[Severity: Medium]
Does calling inode_newsize_ok(inode, len) as a non-fatal condition in the MAP_PRIVATE branch send SIGXFSZ to current while still succeeding?

When len exceeds rlimit(RLIMIT_FSIZE), inode_newsize_ok() calls send_sig(SIGXFSZ, current, 0) before returning -EFBIG. Because this branch ignores the error and continues, a MAP_PRIVATE | PROT_WRITE mmap() on a writable, unsealed file delivers a fatal-by-default SIGXFSZ even though mmap() returns 0. If SIGXFSZ is caught or ignored, hugetlb_reserve_pages() still reserves private huge pages for the full range up to len without growing inode->i_size, and faulting those pages later fails with VM_FAULT_SIGBUS in hugetlb_no_page() because vmf->pgoff is past i_size.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1