]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
fs: consistently use can_mmap_file() helper
authorLorenzo Stoakes <lorenzo.stoakes@oracle.com>
Mon, 16 Jun 2025 19:33:22 +0000 (20:33 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 17 Jun 2025 11:47:22 +0000 (13:47 +0200)
Since commit c84bf6dd2b83 ("mm: introduce new .mmap_prepare() file
callback"), the f_op->mmap() hook has been deprecated in favour of
f_op->mmap_prepare().

Additionally, commit bb666b7c2707 ("mm: add mmap_prepare() compatibility
layer for nested file systems") permits the use of the .mmap_prepare() hook
even in nested filesystems like overlayfs.

There are a number of places where we check only for f_op->mmap - this is
incorrect now mmap_prepare exists, so update all of these to use the
general helper can_mmap_file().

Most notably, this updates the elf logic to allow for the ability to
execute binaries on filesystems which have the .mmap_prepare hook, but
additionally we update nested filesystems.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Link: https://lore.kernel.org/b68145b609532e62bab603dd9686faa6562046ec.1750099179.git.lorenzo.stoakes@oracle.com
Acked-by: Kees Cook <kees@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/backing-file.c
fs/binfmt_elf.c
fs/binfmt_elf_fdpic.c
fs/coda/file.c
fs/ecryptfs/file.c
include/linux/fs.h
mm/mmap.c
mm/nommu.c
tools/testing/vma/vma_internal.h

index 04018679bf69bc265a56152c2e4b2212eff62609..29748953a851c0126f75682098dac23b5df1b1f5 100644 (file)
@@ -333,7 +333,7 @@ int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
        if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
                return -EIO;
 
-       if (!file->f_op->mmap)
+       if (!can_mmap_file(file))
                return -ENODEV;
 
        vma_set_file(vma, file);
index a43363d593e5b9bf1ad81b5f63564858b6435677..e3b56b603192d45df4410be8dacc003f0cb34106 100644 (file)
@@ -646,7 +646,7 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
        if (!elf_check_arch(interp_elf_ex) ||
            elf_check_fdpic(interp_elf_ex))
                goto out;
-       if (!interpreter->f_op->mmap)
+       if (!can_mmap_file(interpreter))
                goto out;
 
        total_size = total_mapping_size(interp_elf_phdata,
@@ -848,7 +848,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
                goto out;
        if (elf_check_fdpic(elf_ex))
                goto out;
-       if (!bprm->file->f_op->mmap)
+       if (!can_mmap_file(bprm->file))
                goto out;
 
        elf_phdata = load_elf_phdrs(elf_ex, bprm->file);
index 9133f3827f90c14716aa1850b3efd5adbcd04842..59b138062352276547f530846c65514eb10ff674 100644 (file)
@@ -109,7 +109,7 @@ static int is_elf(struct elfhdr *hdr, struct file *file)
                return 0;
        if (!elf_check_arch(hdr))
                return 0;
-       if (!file->f_op->mmap)
+       if (!can_mmap_file(file))
                return 0;
        return 1;
 }
index 2e6ea9319b353e5fdc4c5829983c98998ec6237c..a390b5d21196c2ca77101ff5507b3127a26e843b 100644 (file)
@@ -160,7 +160,7 @@ coda_file_mmap(struct file *coda_file, struct vm_area_struct *vma)
        size_t count;
        int ret;
 
-       if (!host_file->f_op->mmap)
+       if (!can_mmap_file(host_file))
                return -ENODEV;
 
        if (WARN_ON(coda_file != vma->vm_file))
index ce0a3c5ed0ca9b84efab7d8a9d59c839549991e7..5f8f96da09fe9d99446d875d6ed13ea64af6d2eb 100644 (file)
@@ -193,7 +193,7 @@ static int ecryptfs_mmap(struct file *file, struct vm_area_struct *vma)
         * natively.  If FILESYSTEM_MAX_STACK_DEPTH > 2 or ecryptfs
         * allows recursive mounting, this will need to be extended.
         */
-       if (!lower_file->f_op->mmap)
+       if (!can_mmap_file(lower_file))
                return -ENODEV;
        return generic_file_mmap(file, vma);
 }
index c66f235f9e4de77857dce3d45be0dd1822bb0376..d4fa1cb0755af0bee25221944c9d517e79e907ce 100644 (file)
@@ -2260,7 +2260,7 @@ struct inode_operations {
 } ____cacheline_aligned;
 
 /* Did the driver provide valid mmap hook configuration? */
-static inline bool file_has_valid_mmap_hooks(struct file *file)
+static inline bool can_mmap_file(struct file *file)
 {
        bool has_mmap = file->f_op->mmap;
        bool has_mmap_prepare = file->f_op->mmap_prepare;
index 09c563c9511238ca22b2768a5ae82c25df2deff6..12c1d060f10462cb37a3fbf25e960f04e6aaae46 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -475,7 +475,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,
                                vm_flags &= ~VM_MAYEXEC;
                        }
 
-                       if (!file_has_valid_mmap_hooks(file))
+                       if (!can_mmap_file(file))
                                return -ENODEV;
                        if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
                                return -EINVAL;
index 38c22ea0a95c663fe99ad935490cdd6eaf63061c..56a53de101668bcd2cf93ffa15b2738fe43f0a17 100644 (file)
@@ -719,7 +719,7 @@ static int validate_mmap_request(struct file *file,
 
        if (file) {
                /* files must support mmap */
-               if (!file_has_valid_mmap_hooks(file))
+               if (!can_mmap_file(file))
                        return -ENODEV;
 
                /* work out if what we've got could possibly be shared
index 7ab04700470ff51262f8a995082d4f3fcca14cea..816e7e057585d9c4dfbc3fd80a225b73e45c33ac 100644 (file)
@@ -1464,7 +1464,7 @@ static int compat_vma_mmap_prepare(struct file *file,
 }
 
 /* Did the driver provide valid mmap hook configuration? */
-static inline bool file_has_valid_mmap_hooks(struct file *file)
+static inline bool can_mmap_file(struct file *file)
 {
        bool has_mmap = file->f_op->mmap;
        bool has_mmap_prepare = file->f_op->mmap_prepare;