The patch titled Subject: mm: enforce the mapping_map_writable() check after call_mmap() has been added to the -mm mm-unstable branch. Its filename is mm-enforce-the-mapping_map_writable-check-after-call_mmap.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-enforce-the-mapping_map_writable-check-after-call_mmap.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Lorenzo Stoakes <lstoakes@xxxxxxxxx> Subject: mm: enforce the mapping_map_writable() check after call_mmap() Date: Sat, 7 Oct 2023 21:51:01 +0100 In order for an F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE in seal_check_write() we must be able to invoke either the shmem_mmap() or hugetlbfs_file_mmap() f_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to clear VM_MAYWRITE. However, the existing logic in mmap_region() performs this check BEFORE calling call_mmap() (which invokes file->f_ops->mmap()). We must enforce this check AFTER the function call. In order to avoid any risk of breaking call_mmap() handlers which assume this will have been done first, we continue to mark the file writable first, simply deferring enforcement of it failing until afterwards. This enables mmap(..., PROT_READ, MAP_SHARED, fd, 0) mappings for memfd's sealed via F_SEAL_WRITE to succeed, whereas previously they were not permitted. Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Link: https://lkml.kernel.org/r/d2748bc4077b53c60bcb06fccaf976cb2afee345.1696709413.git.lstoakes@xxxxxxxxx Signed-off-by: Lorenzo Stoakes <lstoakes@xxxxxxxxx> Cc: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx> Cc: Andy Lutomirski <luto@xxxxxxxxxx> Cc: Christian Brauner <brauner@xxxxxxxxxx> Cc: Hugh Dickins <hughd@xxxxxxxxxx> Cc: Jan Kara <jack@xxxxxxx> Cc: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx> Cc: Mike Kravetz <mike.kravetz@xxxxxxxxxx> Cc: Muchun Song <muchun.song@xxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/mmap.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) --- a/mm/mmap.c~mm-enforce-the-mapping_map_writable-check-after-call_mmap +++ a/mm/mmap.c @@ -2845,11 +2845,10 @@ cannot_expand: vma->vm_pgoff = pgoff; if (file) { - if (is_shared_maywrite(vm_flags)) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + int writable_error = 0; + + if (vma_is_shared_maywrite(vma)) + writable_error = mapping_map_writable(file->f_mapping); vma->vm_file = get_file(file); error = call_mmap(file, vma); @@ -2857,6 +2856,15 @@ cannot_expand: goto unmap_and_free_vma; /* + * call_mmap() may have changed VMA flags, so retry this check + * if it failed before. + */ + if (writable_error && vma_is_shared_maywrite(vma)) { + error = writable_error; + goto close_and_free_vma; + } + + /* * Expansion is handled above, merging is handled below. * Drivers should not alter the address of the VMA. */ _ Patches currently in -mm which might be from lstoakes@xxxxxxxxx are mm-filemap-clarify-filemap_fault-comments-for-not-uptodate-case.patch mm-filemap-clarify-filemap_fault-comments-for-not-uptodate-case-fix.patch mm-make-__access_remote_vm-static.patch mm-gup-explicitly-define-and-check-internal-gup-flags-disallow-foll_touch.patch mm-gup-make-failure-to-pin-an-error-if-foll_nowait-not-specified.patch mm-gup-adapt-get_user_page_vma_remote-to-never-return-null.patch mm-move-vma_policy-and-anon_vma_name-decls-to-mm_typesh.patch mm-abstract-the-vma_merge-split_vma-pattern-for-mprotect-et-al.patch mm-make-vma_merge-and-split_vma-internal.patch mm-abstract-merge-for-new-vmas-into-vma_merge_new_vma.patch mm-abstract-vma-merge-and-extend-into-vma_merge_extend-helper.patch mm-drop-the-assumption-that-vm_shared-always-implies-writable.patch mm-update-memfd-seal-write-check-to-include-f_seal_write.patch mm-enforce-the-mapping_map_writable-check-after-call_mmap.patch