Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Muchun Song <muchun.song@linux.dev>,
	Oscar Salvador <osalvador@suse.de>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: David Hildenbrand <david@kernel.org>,
	linux-mm@kvack.org, dmnk+artist@google.com,
	 Dominik Maier <dmnk@google.com>
Subject: [PATCH] hugetlbfs: respect F_SEAL_GROW and FMODE_WRITE before growing i_size in mmap
Date: Thu, 19 Nov 2026 20:22:09 +0100	[thread overview]
Message-ID: <20261119192209.2651562-1-dmnk@google.com> (raw)

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



                 reply	other threads:[~2026-11-19 19:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119192209.2651562-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=dmnk+artist@google.com \
    --cc=linux-mm@kvack.org \
    --cc=muchun.song@linux.dev \
    --cc=osalvador@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox