All of 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
  2026-11-20  9:15 ` David Hildenbrand (Arm)
  2026-11-20 19:22 ` sashiko-bot
  0 siblings, 2 replies; 4+ messages 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] 4+ messages in thread

* Re: [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap
  2026-11-19 19:22 [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap Dominik Maier
@ 2026-11-20  9:15 ` David Hildenbrand (Arm)
  2026-11-20  9:21   ` David Hildenbrand (Arm)
  2026-11-20 19:22 ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-20  9:15 UTC (permalink / raw)
  To: Dominik Maier, Muchun Song, Oscar Salvador, Andrew Morton
  Cc: linux-mm, dmnk+artist

On 11/19/26 20:22, Dominik Maier wrote:
> 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>

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/20261028-fixes_hugetlb-v1-11-a0a033db180f@kernel.org/

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.


-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap
  2026-11-20  9:15 ` David Hildenbrand (Arm)
@ 2026-11-20  9:21   ` David Hildenbrand (Arm)
  0 siblings, 0 replies; 4+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-20  9:21 UTC (permalink / raw)
  To: Dominik Maier, Muchun Song, Oscar Salvador, Andrew Morton
  Cc: linux-mm, dmnk+artist

On 11/20/26 10:15, David Hildenbrand (Arm) wrote:
> On 11/19/26 20:22, Dominik Maier wrote:
>> 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>
> 
> 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/20261028-fixes_hugetlb-v1-11-a0a033db180f@kernel.org/
> 
> 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.

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.

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap
  2026-11-19 19:22 [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap Dominik Maier
  2026-11-20  9:15 ` David Hildenbrand (Arm)
@ 2026-11-20 19:22 ` sashiko-bot
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-20 19:22 UTC (permalink / raw)
  To: Dominik Maier, Muchun Song, Oscar Salvador, Andrew Morton
  Cc: David Hildenbrand, linux-mm, dmnk+artist

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 <dmnk@google.com>

> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dominik Maier <dmnk@google.com>

[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.

> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index ca63a530a26a1..c75f9ea4c9043 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;
> +			}

[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.

> +			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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119192209.2651562-1-dmnk@google.com?part=1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-20 19:22 UTC | newest]

Thread overview: 4+ messages (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
2026-11-20  9:15 ` David Hildenbrand (Arm)
2026-11-20  9:21   ` David Hildenbrand (Arm)
2026-11-20 19:22 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.