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=1796388736; b=W8itYIwxvqR5qqfOgsZh5El35+zJT0h56DWyC8YA4McmuNpIz4HlMRqOZlPcN+T7Y1JX UJIn3Fc0oua2nMJa/o5wJ+3PSBugDt5/7ke9MG2/UIW++Glw/3WPbPV3c0RGKFDnqL9t/ 7znAh4WE52KINHyJWYKDUpAJ0gKDNvOzOj+bs+1HtzywAz9xLPBp5BkuGrrF4Zrxsp7x/ qOX97J8LIo85wupnbMT597acGW02GMpNLckt4aSd/OQhJMOKuZmNvRGLdiLofEPaXavgU It+Lh5vRfq+dowzNN/kE3tJ4AwfbEwfdG1rDNSE2vowxM9s+dFb+Te++Ep7Ih3maSfw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388736; 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=tlNSNCCbgom+VUF3icEaPcMn7xuoANhCGHcVu65sXZ0=; b=LRwDeIEwhTtLee6adnfgIGUTMBaRdJebU3DkgayUx+xIqeVBAdmJWnI/twZGpDvsabWg FpArnzTu833KnnabI7UCZVQz/Fu2pRNnvcr/SvrZNqwvFkS9u0npSd9oHNL9DwmOfazVe BsW8xvvCGiqhL+IVahFYW82CiN+oTlBRt848GO5I9rN45+bftXmyg/dNTeFJm3mv5Ahuz 7UCGtb17VZrrbTyKpJ8GjFXZ4nlzz6DXj8hvI1vuf0zrecRGJVfcVIjMFWMXh0Mifezmy u2AiauxcGW8iQBJtdSmzBeLw52sjbrPuYxq+QWSUfQSLbYjmgXTtZHJYO/hkqaLATew== 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=aWhXuIE1; 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 C8E78606A32C; Fri, 4 Dec 2026 12:52:16 +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=aWhXuIE1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8926B1F000C2; Fri, 4 Dec 2026 12:51:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388736; bh=tlNSNCCbgom+VUF3icEaPcMn7xuoANhCGHcVu65sXZ0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=aWhXuIE1rv3BFbGR2T4onIkVFvb4Xa+PkCxmB+hhprTmePvSlYDiQKxPDR9cwj/R3 x66qkZAZ42iszFrQAI24urIsVHBwOVZ7VTyhOsZxQH2xmlkIi3ZZbe0U7QqJGas6sO WqlM9VlFM2h2S05TEUE9G4w/9zUF6DjCeeGXsEjA2u1Bys16KklCw8B1tduFKXYYn3 vK8Kavawmq2PwWrNJyDkeNJFa2+JKyHkjtBLZ+NaAINUUNSekTkDGbGVb5G+FbWsyh viMpnXH3QZSs7WvlO/Kv7FAqPsfWnWLp9Oy5i1t+zEJSVvMTEqsdPmMFMJvWCWVsmu ya3O5WaWy/O8w== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:49:57 +0100 Subject: [PATCH v2 06/31] mm/shmem: seal the size of SysV and shared anon files X-Mailing-List: stable@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: <20261204-fixes_core_3-v2-6-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@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 , Christian Brauner X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4746; i=david@kernel.org; h=from:subject:message-id; bh=sxi9K2SZuuV4s8axUyyiSY/3w44i0dbrNK8RI2QZemI=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv3L2vzk1aqzc+se3uD8IBYtIpQgW/ya8Uf/tBsLW 6KC7C8c7ShlYRDjYJAVU2QJCr5v8PLlutpqNz5mmDmsTCBDGLg4BWAiTv8Y/kdMjltYvZX3UvSb J5liEzc2nr+wu84g7/3hXN7VbcJtq+YyMhzsbNz45r7Haa3YzX0vBCdsyNnsvz7hy2Z77sy3zzo OlnIAAA== 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 Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Baolin Wang Acked-by: Christian Brauner 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 798581db29853..5536371e344ac 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -6290,6 +6290,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 @@ -6297,7 +6298,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