[PATCH 06/32] mm/shmem: seal the size of SysV and shared anon files
From: David Hildenbrand (Arm)
Date: Sun Nov 29 2026 - 05:08:56 EST
A process can resize the underlying shmem file of a SysV shm segment or of
a shared anonymous mapping through /proc/<pid>/map_files.
We account these files by their size when creating them and unaccount
them by their size when destroying them, so resizing them can underflow
vm_committed_as and the RLIMIT_MEMLOCK count of SHM_LOCK, and add pages
that are charged to neither.
Consequently, an underflowed vm_committed_as lets the whole system commit
memory beyond the limit of vm.overcommit_memory=2, and the RLIMIT_MEMLOCK
underflow triggers a WARN_ON_ONCE().
Since commit 77142517990f ("shmem: update memory reservation on truncate")
we adjust the commit charge on truncation, but not when writing or
fallocating beyond EOF, and we never adjust the RLIMIT_MEMLOCK count.
Let's fix it by sealing these files against growing and shrinking when
creating them instead, similar to what is being done for SysV segments on
hugetlbfs [1].
Truncating such files and writing or fallocating to them beyond EOF will
now fail with EPERM, and F_GET_SEALS will now report these two seals.
As udmabuf requires F_SEAL_SHRINK, it will now accept these files, which
shouldn't matter, as obtaining an fd for them requires
CAP_CHECKPOINT_RESTORE.
It's highly unlikely that any user would rely on this: e.g., CRIU only
maps and reads these files through /proc/<pid>/map_files, and only
resizes memfds. A search didn't reveal any other relevant cases.
This has low urgency, as it requires CAP_CHECKPOINT_RESTORE.
[1] https://lore.kernel.org/all/20261125-hugetlb_fixes_2-v1-17-db27fd2f71a8@xxxxxxxxxx/
Fixes: 640708a2cff7 ("procfs: introduce the /proc/<pid>/map_files/ directory")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/shmem_fs.h | 11 +++++++++++
ipc/shm.c | 2 ++
mm/shmem.c | 6 +++++-
3 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h
index f89b4b1511332..9dc9956852dac 100644
--- a/include/linux/shmem_fs.h
+++ b/include/linux/shmem_fs.h
@@ -3,6 +3,7 @@
#define __SHMEM_FS_H
#include <linux/file.h>
+#include <linux/fcntl.h>
#include <linux/swap.h>
#include <linux/mempolicy.h>
#include <linux/pagemap.h>
@@ -119,6 +120,11 @@ extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
#ifdef CONFIG_SHMEM
bool shmem_file(struct file *file);
bool shmem_mapping(const struct address_space *mapping);
+
+static inline void shmem_seal_file_size(struct file *file)
+{
+ SHMEM_I(file_inode(file))->seals |= F_SEAL_GROW | F_SEAL_SHRINK;
+}
#else
static inline bool shmem_file(struct file *file)
{
@@ -129,7 +135,12 @@ static inline bool shmem_mapping(const struct address_space *mapping)
{
return false;
}
+
+static inline void shmem_seal_file_size(struct file *file)
+{
+}
#endif /* CONFIG_SHMEM */
+
void shmem_unlock_mapping(struct address_space *mapping);
struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
pgoff_t index, gfp_t gfp_mask);
diff --git a/ipc/shm.c b/ipc/shm.c
index 83ac223352ba5..d868d1daa45d9 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -762,6 +762,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
if (has_no_reserve && sysctl_overcommit_memory != OVERCOMMIT_NEVER)
vma_flags_set(&acctflag, VMA_NORESERVE_BIT);
file = shmem_kernel_file_setup(name, size, acctflag);
+ if (!IS_ERR(file))
+ shmem_seal_file_size(file);
}
error = PTR_ERR(file);
if (IS_ERR(file))
diff --git a/mm/shmem.c b/mm/shmem.c
index eb224ee26c685..feae93e05b27b 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -6288,6 +6288,7 @@ static struct file *__shmem_zero_setup(unsigned long start, unsigned long end,
vma_flags_t flags)
{
loff_t size = end - start;
+ struct file *file;
/*
* Cloning a new file under mmap_lock leads to a lock ordering conflict
@@ -6295,7 +6296,10 @@ static struct file *__shmem_zero_setup(unsigned long start, unsigned long end,
* accessible to the user through its mapping, use S_PRIVATE flag to
* bypass file security, in the same way as shmem_kernel_file_setup().
*/
- return shmem_kernel_file_setup("dev/zero", size, flags);
+ file = shmem_kernel_file_setup("dev/zero", size, flags);
+ if (!IS_ERR(file))
+ shmem_seal_file_size(file);
+ return file;
}
/**
--
2.56.1