From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795946932; b=o1tz80cH5l4+DygBgCCHRP8SqqzeSl1SH9dJr+jJ6QWfdt0f7pHfSQLauoPz6WvISkPD +oAX15uIbOKTuKl1YDEJxZ+VZ2CsNcW7jHBenQEDlSxMiLfNh9PrNqyEwN9iXcHWWgrI7 eX8Nyp3nmrYzdfWoScTxi+WWZvYX6Zt0s3DumvXB4uRHXFXjxf2wRD5SDrTP9oj8mXb/v ulLCT7dDQbQy3C6u/n+6naGK1XnrI2nazhmScXd8Sbq58s00qJw45fOIYMBut8Lu9vXDq Sbu9396E6PtvxpSdvXM8EVEx111dONHtaBwdNhZguTh8f3uqxIKIAUlZkYQqoWA2LdA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795946932; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=qvLSv1NTXAFggz4j8PHXZYEt8BBQNjDaYkDx4EeSutQ=; b=LoBLSloFMvau17kOtC2LyEANpOOXKfF1BU86hlLRNFXEzkBUyuE4xueTl0KVEsJZ7A6u A+LgqoAgrFUYqJkNfowd4CqUgFxFGkR2UaNIvlfnEE+erLbgrHUQx3OziTU9sMNno7j3r UTZl1OaBL3XfnvHDIUL0TUtESnt7GQsy6RB/+wL7gBSj1eEl3ZPSF4WK5LQU3VS3gxN+n JJf9aLSdBA5tcmdiIpUan6uudSdjH4zZxHUCU7GyETaAjFXTHlYfaaufH0uISjJLPGtpB wWNTp7IHAnBVtcorW+H/EVPpiSihS+8y7VhrCJWt3BYaoGDuH7Z94liAgwTj0flhLaQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NBE1j1tU; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A3E6606A329; Sun, 29 Nov 2026 10:08:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NBE1j1tU Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF04D1F0089B; Sun, 29 Nov 2026 10:08:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795946932; bh=qvLSv1NTXAFggz4j8PHXZYEt8BBQNjDaYkDx4EeSutQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=NBE1j1tUhNEqvS2eTeXS7lIgAmnLgJx49HaQS+yeZGzP9fgTzvlJk/HHOcVLU40a6 sCpnJpJPzZVWjg8ar/eWo9uKw0NnWdYo3aEreAtzznxyuh7YipZAgZd+K8dKzpHXuk givTOhvySKV8Z1Ab0Qp4mY2LlctwChY6gkTfxsjrkizUe3ICHPFwAUSXuTngZfjlC6 eslNr8ceP7pVceJLfMX3zYiy/N4F8FTmOPPdgZJHlly2WYGeaunN/8Uz8nJ+kCEjN8 z7E3l4USJ+/sUrTN18/VQ5Ce+4SyYf1bhG3cV4v/aWA/jxWjlUHTm11loPlLW/xeC2 0W2CPncxPQwoQ== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:37 +0100 Subject: [PATCH 06/32] mm/shmem: seal the size of SysV and shared anon files X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-6-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4530; i=david@kernel.org; h=from:subject:message-id; bh=OhqsDAGNlc8d7N/FtP/ProE+CMbrtMbuBWdWCnf9ZRI=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f2qv79A+yPqxJeTfjihZdxbDrbdOPfnV9Jjh78+tX fmSs2YWd5SyMIhxMMiKKbIEBd83ePlyXW21Gx8zzBxWJpAhDFycAjARIwOG/6kbZnqt9PJX9Us8 e2RfXMgjxemRjEFSMdI7rv9ZcFfzwk9GhqkV3Ed/5X19qG7sUX58za+yN68eTQvj89mjI3O+Pzy Ymw0A X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A A process can resize the underlying shmem file of a SysV shm segment or of a shared anonymous mapping through /proc//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//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@kernel.org/ Fixes: 640708a2cff7 ("procfs: introduce the /proc//map_files/ directory") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- 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 +#include #include #include #include @@ -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