Skip to content

Commit 1589789

Browse files
lorenzo-stoakesakpm00
authored andcommitted
mm: perform the mapping_map_writable() check after call_mmap()
In order for a F_SEAL_WRITE sealed memfd mapping to have an opportunity to clear VM_MAYWRITE, we must be able to invoke the appropriate vm_ops->mmap() handler to do so. We would otherwise fail the mapping_map_writable() check before we had the opportunity to avoid it. This patch moves this check after the call_mmap() invocation. Only memfd actively denies write access causing a potential failure here (in memfd_add_seals()), so there should be no impact on non-memfd cases. This patch makes the userland-visible change that MAP_SHARED, PROT_READ mappings of an F_SEAL_WRITE sealed memfd mapping will now succeed. There is a delicate situation with cleanup paths assuming that a writable mapping must have occurred in circumstances where it may now not have. In order to ensure we do not accidentally mark a writable file unwritable by mistake, we explicitly track whether we have a writable mapping and unmap only if we do. [[email protected]: do not set writable_file_mapping in inappropriate case] Link: https://lkml.kernel.org/r/[email protected] Link: https://bugzilla.kernel.org/show_bug.cgi?id=217238 Link: https://lkml.kernel.org/r/55e413d20678a1bb4c7cce889062bbb07b0df892.1697116581.git.lstoakes@gmail.com Signed-off-by: Lorenzo Stoakes <[email protected]> Reviewed-by: Jan Kara <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Matthew Wilcox (Oracle) <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Muchun Song <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 28464bb commit 1589789

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

mm/mmap.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
27522752
unsigned long charged = 0;
27532753
unsigned long end = addr + len;
27542754
unsigned long merge_start = addr, merge_end = end;
2755+
bool writable_file_mapping = false;
27552756
pgoff_t vm_pgoff;
27562757
int error;
27572758
VMA_ITERATOR(vmi, mm, addr);
@@ -2846,17 +2847,19 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
28462847
vma->vm_pgoff = pgoff;
28472848

28482849
if (file) {
2849-
if (is_shared_maywrite(vm_flags)) {
2850-
error = mapping_map_writable(file->f_mapping);
2851-
if (error)
2852-
goto free_vma;
2853-
}
2854-
28552850
vma->vm_file = get_file(file);
28562851
error = call_mmap(file, vma);
28572852
if (error)
28582853
goto unmap_and_free_vma;
28592854

2855+
if (vma_is_shared_maywrite(vma)) {
2856+
error = mapping_map_writable(file->f_mapping);
2857+
if (error)
2858+
goto close_and_free_vma;
2859+
2860+
writable_file_mapping = true;
2861+
}
2862+
28602863
/*
28612864
* Expansion is handled above, merging is handled below.
28622865
* Drivers should not alter the address of the VMA.
@@ -2937,7 +2940,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
29372940

29382941
/* Once vma denies write, undo our temporary denial count */
29392942
unmap_writable:
2940-
if (file && is_shared_maywrite(vm_flags))
2943+
if (writable_file_mapping)
29412944
mapping_unmap_writable(file->f_mapping);
29422945
file = vma->vm_file;
29432946
ksm_add_vma(vma);
@@ -2985,7 +2988,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
29852988
unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start,
29862989
vma->vm_end, vma->vm_end, true);
29872990
}
2988-
if (file && is_shared_maywrite(vm_flags))
2991+
if (writable_file_mapping)
29892992
mapping_unmap_writable(file->f_mapping);
29902993
free_vma:
29912994
vm_area_free(vma);

0 commit comments

Comments
 (0)