]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
mm: update memfd seal write check to include F_SEAL_WRITE
authorLorenzo Stoakes <lstoakes@gmail.com>
Wed, 30 Jul 2025 01:54:00 +0000 (18:54 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 28 Aug 2025 14:22:55 +0000 (16:22 +0200)
[ Upstream commit 28464bbb2ddc199433383994bcb9600c8034afa1 ]

The seal_check_future_write() function is called by shmem_mmap() or
hugetlbfs_file_mmap() to disallow any future writable mappings of an memfd
sealed this way.

The F_SEAL_WRITE flag is not checked here, as that is handled via the
mapping->i_mmap_writable mechanism and so any attempt at a mapping would
fail before this could be run.

However we intend to change this, meaning this check can be performed for
F_SEAL_WRITE mappings also.

The logic here is equally applicable to both flags, so update this
function to accommodate both and rename it accordingly.

Link: https://lkml.kernel.org/r/913628168ce6cce77df7d13a63970bae06a526e0.1697116581.git.lstoakes@gmail.com
Signed-off-by: Lorenzo Stoakes <lstoakes@gmail.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Hugh Dickins <hughd@google.com>
Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Muchun Song <muchun.song@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: stable@vger.kernel.org
Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/hugetlbfs/inode.c
include/linux/mm.h
mm/shmem.c

index bf3cda4989623cd97c4d47aadc173dfe8c886e6c..6e97a54ffda124a33cb42c1635d6cc32b59dde28 100644 (file)
@@ -148,7 +148,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
        vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
        vma->vm_ops = &hugetlb_vm_ops;
 
-       ret = seal_check_future_write(info->seals, vma);
+       ret = seal_check_write(info->seals, vma);
        if (ret)
                return ret;
 
index 2bedc9940c472c62ec67ed338f03f820093e7da0..130d53f0bd66ece0b69a803d34ef11167729207b 100644 (file)
@@ -3201,25 +3201,26 @@ unsigned long wp_shared_mapping_range(struct address_space *mapping,
 extern int sysctl_nr_trim_pages;
 
 /**
- * seal_check_future_write - Check for F_SEAL_FUTURE_WRITE flag and handle it
+ * seal_check_write - Check for F_SEAL_WRITE or F_SEAL_FUTURE_WRITE flags and
+ *                    handle them.
  * @seals: the seals to check
  * @vma: the vma to operate on
  *
- * Check whether F_SEAL_FUTURE_WRITE is set; if so, do proper check/handling on
- * the vma flags.  Return 0 if check pass, or <0 for errors.
+ * Check whether F_SEAL_WRITE or F_SEAL_FUTURE_WRITE are set; if so, do proper
+ * check/handling on the vma flags.  Return 0 if check pass, or <0 for errors.
  */
-static inline int seal_check_future_write(int seals, struct vm_area_struct *vma)
+static inline int seal_check_write(int seals, struct vm_area_struct *vma)
 {
-       if (seals & F_SEAL_FUTURE_WRITE) {
+       if (seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
                /*
                 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
-                * "future write" seal active.
+                * write seals are active.
                 */
                if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
                        return -EPERM;
 
                /*
-                * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
+                * Since an F_SEAL_[FUTURE_]WRITE sealed memfd can be mapped as
                 * MAP_SHARED and read-only, take care to not allow mprotect to
                 * revert protections on such mappings. Do this only for shared
                 * mappings. For private mappings, don't need to mask
index 6666114ed53bbe48bdea843f6f3ac6fb7f3e58f1..5f8d8899bd0e7571e3a421f34671f1db4ef6041a 100644 (file)
@@ -2263,7 +2263,7 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
        struct shmem_inode_info *info = SHMEM_I(file_inode(file));
        int ret;
 
-       ret = seal_check_future_write(info->seals, vma);
+       ret = seal_check_write(info->seals, vma);
        if (ret)
                return ret;