[PATCH 17/21] hugetlbfs: seal SHM_HUGETLB files against growth
From: David Hildenbrand (Arm)
Date: Wed Nov 25 2026 - 17:38:07 EST
From: Chris Mason <mason@xxxxxxxxxx>
The size of a SysV shared memory segment is fixed at shmget(), and
do_shmat() relies on that by mapping i_size bytes of the backing file.
However, with SHM_HUGETLB, the hugetlbfs file can be grown through an
fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate().
Also, remap_file_pages() with a pgoff beyond the segment can grow it,
because hugetlbfs_file_mmap() grows the file to the end of a writable
mapping:
shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0)
remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)
i_size = 8M
shmat(id, NULL, 0) maps 8M
Later calls to shmat() will map the bigger size.
Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but
remap_file_pages() does not require any privileges, so we focus on the
latter.
Let's fix it by setting F_SEAL_GROW on the inode when creating it for a
segment. remap_file_pages() beyond the segment still succeeds, like for
shmem segments, but accessing pages there now correctly results in
SIGBUS.
This has low urgency, and likely nobody ever ran into this in practice.
Let's not CC stable.
Fixes: c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Signed-off-by: Chris Mason <mason@xxxxxxxxxx>
[ david: adjust description ]
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
fs/hugetlbfs/inode.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 0b96fdc3ad9d8..f1a8208ffc818 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -1559,8 +1559,11 @@ struct file *hugetlb_file_setup(const char *name, size_t size,
S_IFREG | S_IRWXUGO, 0);
if (!inode)
goto out;
- if (creat_flags == HUGETLB_SHMFS_INODE)
+ if (creat_flags == HUGETLB_SHMFS_INODE) {
inode->i_flags |= S_PRIVATE;
+ /* The size of a segment is fixed at shmget(). */
+ HUGETLBFS_I(inode)->seals |= F_SEAL_GROW;
+ }
inode->i_size = size;
clear_nlink(inode);
--
2.56.1