Re: [PATCH v2 24/35] hugetlbfs: seal SHM_HUGETLB files against growth

From: Jose A. Perez de Azpillaga

Date: Fri Dec 04 2026 - 06:27:58 EST


On 12/3/26 12:37 PM, David Hildenbrand (Arm) wrote:
> 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>

LGTM:

Reviewed-by: Jose A. Perez de Azpillaga <azpijr@xxxxxxxxx>

--
cheers,
jose a. p-a