FAILED: patch "[PATCH] memfd: deny writeable mappings when implying SEAL_WRITE" failed to apply to 6.12-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.12-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.12.y
git checkout FETCH_HEAD
git cherry-pick -x 3b041514cb6eae45869b020f743c14d983363222
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026060408-corrosive-dental-a22b@gregkh' --subject-prefix 'PATCH 6.12.y' HEAD^..
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 3b041514cb6eae45869b020f743c14d983363222 Mon Sep 17 00:00:00 2001
From: "Pratyush Yadav (Google)" <[email protected]>
Date: Tue, 5 May 2026 15:39:20 +0200
Subject: [PATCH] memfd: deny writeable mappings when implying SEAL_WRITE
When SEAL_EXEC is added, SEAL_WRITE is implied to make W^X. But the implied seal is set after the check that makes sure the memfd can not have any writable mappings. This means one can use SEAL_EXEC to apply SEAL_WRITE while having writeable mappings.
This breaks the contract that SEAL_WRITE provides and can be used by an attacker to pass a memfd that appears to be write sealed but can still be modified arbitrarily.
Fix this by adding the implied seals before the call for mapping_deny_writable() is done.
mm/memfd.c +6 -6
@@ -283,6 +283,12 @@ int memfd_add_seals(struct file *file, unsigned int seals) goto unlock; } +/* +* SEAL_EXEC implies SEAL_WRITE, making W^X from the start. +*/ +if (seals &F_SEAL_EXEC && inode->i_mode &0111 ) + seals |=F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE |F_SEAL_FUTURE_WRITE ; +if ((seals &F_SEAL_WRITE ) && !(*file_seals &F_SEAL_WRITE )) { error =mapping_deny_writable (file->f_mapping);if (error)
@@ -295,12 +301,6 @@ int memfd_add_seals(struct file *file, unsigned int seals) } } -/* -* SEAL_EXEC implies SEAL_WRITE, making W^X from the start. -*/ -if (seals &F_SEAL_EXEC && inode->i_mode &0111 ) - seals |=F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE |F_SEAL_FUTURE_WRITE ; - *file_seals |= seals; error =0 ;
Sasha Levin
maintainerPATCH 1/3 mm: perform all memfd seal checks in a single place
From: Lorenzo Stoakes <[email protected]>
[ Upstream commit fa00b8ef1803fe133b4897c25227aa0d298dd093 ]
We no longer actually need to perform these checks in the f_op->mmap() hook any longer.
We already moved the operation which clears VM_MAYWRITE on a read-only mapping of a write-sealed memfd in order to work around the restrictions imposed by commit 5de195060b2e ("mm: resolve faulty mmap_region() error path behaviour").
There is no reason for us not to simply go ahead and additionally check to see if any pre-existing seals are in place here rather than defer this to the f_op->mmap() hook.
By doing this we remove more logic from shmem_mmap() which doesn't belong there, as well as doing the same for hugetlbfs_file_mmap(). We also remove dubious shared logic in mm.h which simply does not belong there either.
It makes sense to do these checks at the earliest opportunity, we know these are shmem (or hugetlbfs) mappings whose relevant VMA flags will not change from the invoking do_mmap() so there is simply no need to wait.
This also means the implementation of further memfd seal flags can be done within mm/memfd.c and also have the opportunity to modify VMA flags as necessary early in the mapping logic.
Stable-dep-of: 3b041514cb6e ("memfd: deny writeable mappings when implying SEAL_WRITE")
--- fs/hugetlbfs/inode.c | 5 ---- include/linux/memfd.h | 23 +++++++++--------- include/linux/mm.h | 55 ------------------------------------------- mm/memfd.c | 44 +++++++++++++++++++++++++++++++++- mm/mmap.c | 12 +++++++--- mm/shmem.c | 6 ----- 6 files changed, 63 insertions(+), 82 deletions(-)
fs/hugetlbfs/inode.c +0 -5
@@ -96,7 +96,6 @@ static const struct fs_parameter_spec hugetlb_fs_parameters[] = {static int hugetlbfs_file_mmap (struct file *file,struct vm_area_struct *vma) {struct inode *inode =file_inode (file); -struct hugetlbfs_inode_info *info =HUGETLBFS_I (inode);loff_t len, vma_len;int ret;struct hstate *h =hstate_file (file);
@@ -113,10 +112,6 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)vm_flags_set (vma,VM_HUGETLB |VM_DONTEXPAND ); vma->vm_ops = &hugetlb_vm_ops; - ret =seal_check_write (info->seals, vma); -if (ret) -return ret; -/* * page based offset in vm_pgoff could be sufficiently large to * overflow a loff_t when converted to byte offset. This can
include/linux/memfd.h +11 -12
@@ -7,7 +7,14 @@#ifdef CONFIG_MEMFD_CREATE extern long memfd_fcntl (struct file *file,unsigned int cmd,unsigned int arg);struct folio *memfd_alloc_folio (struct file *memfd,pgoff_t idx); -unsigned int *memfd_file_seals_ptr (struct file *file); +/* +* Check for any existing seals on mmap, return an error if access is denied due +* to sealing, or 0 otherwise. +* +* We also update VMA flags if appropriate by manipulating the VMA flags pointed +* to by vm_flags_ptr. +*/ +int memfd_check_seals_mmap (struct file *file,unsigned long *vm_flags_ptr);#else static inline long memfd_fcntl (struct file *f,unsigned int c,unsigned int a) {
@@ -17,19 +24,11 @@ static inline struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx) {return ERR_PTR (-EINVAL ); } - -static inline unsigned int *memfd_file_seals_ptr (struct file *file) +static inline int memfd_check_seals_mmap (struct file *file, +unsigned long *vm_flags_ptr) { -return NULL ; +return 0 ; }#endif -/* Retrieve memfd seals associated with the file, if any. */ -static inline unsigned int memfd_file_seals (struct file *file) -{ -unsigned int *sealsp =memfd_file_seals_ptr (file); - -return sealsp ? *sealsp :0 ; -} -#endif /* __LINUX_MEMFD_H */
include/linux/mm.h +0 -55
61 lines not on this page · raw message
mm/memfd.c +43 -1
56 lines not on this page · raw message
mm/mmap.c +9 -3
@@ -369,8 +369,8 @@ unsigned long do_mmap(struct file *file, unsigned long addr,if (file) {struct inode *inode =file_inode (file); -unsigned int seals =memfd_file_seals (file);unsigned long flags_mask; +int err;if (!file_mmap_ok (file, inode, pgoff, len))return -EOVERFLOW ;
@@ -410,8 +410,6 @@ unsigned long do_mmap(struct file *file, unsigned long addr, vm_flags |=VM_SHARED |VM_MAYSHARE ;if (!(file->f_mode &FMODE_WRITE )) vm_flags &= ~(VM_MAYWRITE |VM_SHARED ); -else if (is_readonly_sealed (seals, vm_flags)) - vm_flags &= ~VM_MAYWRITE ; fallthrough;case MAP_PRIVATE :if (!(file->f_mode &FMODE_READ ))
@@ -431,6 +429,14 @@ unsigned long do_mmap(struct file *file, unsigned long addr,default :return -EINVAL ; } + +/* +* Check to see if we are violating any seals and update VMA +* flags if necessary to avoid future seal violations. +*/ + err =memfd_check_seals_mmap (file, &vm_flags); +if (err) +return (unsigned long )err; }else {switch (flags &MAP_TYPE ) {case MAP_SHARED :
mm/shmem.c +0 -6
@@ -2820,12 +2820,6 @@ int shmem_lock(struct file *file, int lock, struct ucounts *ucounts)static int shmem_mmap (struct file *file,struct vm_area_struct *vma) {struct inode *inode =file_inode (file); -struct shmem_inode_info *info =SHMEM_I (inode); -int ret; - - ret =seal_check_write (info->seals, vma); -if (ret) -return ret;file_accessed (file);/* This is anonymous shared memory if it is unlinked at the time of mmap */
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/3 mm/memfd: fix spelling and grammatical issues
From: Liu Ye <[email protected]>
[ Upstream commit 33c9b01ed2fcbc101cdfeb497f4581e981e7c1e7 ]
The comment "If a private mapping then writability is irrelevant" contains a typo. It should be "If a private mapping then writability is irrelevant". The comment "SEAL_EXEC implys SEAL_WRITE, making W^X from the start." contains a typo. It should be "SEAL_EXEC implies SEAL_WRITE, making W^X from the start."
Stable-dep-of: 3b041514cb6e ("memfd: deny writeable mappings when implying SEAL_WRITE")
--- mm/memfd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/memfd.c +2 -2
@@ -286,7 +286,7 @@ static int memfd_add_seals(struct file *file, unsigned int seals) }/* -* SEAL_EXEC implys SEAL_WRITE, making W^X from the start. +* SEAL_EXEC implies SEAL_WRITE, making W^X from the start. */ if (seals &F_SEAL_EXEC && inode->i_mode &0111 ) seals |=F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE |F_SEAL_FUTURE_WRITE ;
@@ -364,7 +364,7 @@ static int check_write_seal(unsigned long *vm_flags_ptr)unsigned long vm_flags = *vm_flags_ptr;unsigned long mask = vm_flags & (VM_SHARED |VM_WRITE ); -/* If a private matting then writability is irrelevant. */ +/* If a private mapping then writability is irrelevant. */ if (!(mask &VM_SHARED ))return 0 ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/3 memfd: deny writeable mappings when implying SEAL_WRITE
In reply to Sasha LevinFrom: "Pratyush Yadav (Google)" <[email protected]>
[ Upstream commit 3b041514cb6eae45869b020f743c14d983363222 ]
When SEAL_EXEC is added, SEAL_WRITE is implied to make W^X. But the implied seal is set after the check that makes sure the memfd can not have any writable mappings. This means one can use SEAL_EXEC to apply SEAL_WRITE while having writeable mappings.
This breaks the contract that SEAL_WRITE provides and can be used by an attacker to pass a memfd that appears to be write sealed but can still be modified arbitrarily.
Fix this by adding the implied seals before the call for mapping_deny_writable() is done.
--- mm/memfd.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
mm/memfd.c +6 -6
@@ -273,6 +273,12 @@ static int memfd_add_seals(struct file *file, unsigned int seals) goto unlock; } +/* +* SEAL_EXEC implies SEAL_WRITE, making W^X from the start. +*/ +if (seals &F_SEAL_EXEC && inode->i_mode &0111 ) + seals |=F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE |F_SEAL_FUTURE_WRITE ; +if ((seals &F_SEAL_WRITE ) && !(*file_seals &F_SEAL_WRITE )) { error =mapping_deny_writable (file->f_mapping);if (error)
@@ -285,12 +291,6 @@ static int memfd_add_seals(struct file *file, unsigned int seals) } } -/* -* SEAL_EXEC implies SEAL_WRITE, making W^X from the start. -*/ -if (seals &F_SEAL_EXEC && inode->i_mode &0111 ) - seals |=F_SEAL_SHRINK |F_SEAL_GROW |F_SEAL_WRITE |F_SEAL_FUTURE_WRITE ; - *file_seals |= seals; error =0 ;
Signature · 1 line
2.53.0