mmap denywrite mess (Was: [GIT PULL] overlayfs fixes for 5.14-rc6)
Subsystems (5)
Miklos Szeredi
maintainerQuoted 2 lines · Linus Torvalds
Ugh. Th edances with denywrite and
mapping_unmap_writableare really really annoying.
Attached version has error and success paths separated. Was that your complaint?
Quoted 4 lines
I get the feeling that the whole thing with
deny_write_accessandmapping_map_writablecould possibly be done after-the-fact somehow as part of actually inserting the vma in the vma tree, rather than done as the vma is prepared.
I don't know if that's doable or not. The final denywrite count is obtained in __vma_link_file(), called after __vma_link(). The questions are:
- does the order of those helper calls matter?
- if it does, could the __vma_link() be safely undone after an unsuccessful __vmal_link_file()?
Quoted 3 lines
And most users of
vma_set_file()probably really don't want that whole thing at all (ie the DRM stuff that just switches out a local thing. They also don't check for the new error cases you've added.
Christian König wants to follow up with those checks (which should be asserts, if the code wasn't buggy in the first place).
Quoted 3 lines
So I really think this is quite questionable, and those cases should probably have been done entirely inside ovlfs rather than polluting the cases that don't care and don't check.
I don't get that. mmap_region() currently drops the deny counts from the original file. That doesn't work for overlayfs since it needs to take new temp counts on the override file.
So mmap_region() is changed to drop the counts on vma->vm_file, but then all callers of vma_set_file() will need to do that switch of temp counts, there's no way around that.
Thanks,
Miklos
For reference, here's the previous discussion:
https://lore.kernel.org/linux-mm/[email protected]/
--- fs/overlayfs/file.c | 4 +++- include/linux/mm.h | 2 +- mm/mmap.c | 2 +- mm/util.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-)
fs/overlayfs/file.c +3 -1
@@ -475,7 +475,9 @@ static int ovl_mmap(struct file *file, sif (WARN_ON (file != vma->vm_file))return -EIO ; -vma_set_file (vma, realfile); + ret =vma_set_file (vma, realfile); +if (ret) +return ret; old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =call_mmap (vma->vm_file, vma);
include/linux/mm.h +1 -1
@@ -2780,7 +2780,7 @@ static inline void vma_set_page_prot(str }#endif -void vma_set_file (struct vm_area_struct *vma,struct file *file); +int /* __must_check */ vma_set_file (struct vm_area_struct *vma,struct file *file);#ifdef CONFIG_NUMA_BALANCING unsigned long change_prot_numa(struct vm_area_struct *vma,
mm/mmap.c +1 -1
@@ -1806,6 +1806,7 @@ unsigned long mmap_region(struct file *f*/ vma->vm_file =get_file (file); error =call_mmap (file, vma); + file = vma->vm_file;if (error) goto unmap_and_free_vma;
@@ -1867,7 +1868,6 @@ unsigned long mmap_region(struct file *fif (vm_flags &VM_DENYWRITE )allow_write_access (file); } - file = vma->vm_file; out:perf_event_mmap (vma);
mm/util.c +30 -1
@@ -314,12 +314,41 @@ int vma_is_stack_for_current(struct vm_a/* * Change backing file, only valid to use during initial VMA setup. */ -void vma_set_file (struct vm_area_struct *vma,struct file *file) +int vma_set_file (struct vm_area_struct *vma,struct file *file) { +vm_flags_t vm_flags = vma->vm_flags; +int err; + +/* Get temporary denial counts on replacement */ +if (vm_flags &VM_DENYWRITE ) { + err =deny_write_access (file); +if (err) +return err; + } +if (vm_flags &VM_SHARED ) { + err =mapping_map_writable (file->f_mapping); +if (err) + goto undo_denywrite; + } +/* Changing an anonymous vma with this is illegal */ get_file (file);swap (vma->vm_file, file); + +/* Undo temporary denial counts on replaced */ +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (file->f_mapping); + +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +fput (file); +return 0 ; + +undo_denywrite: +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +return err; }EXPORT_SYMBOL (vma_set_file);
Quoted 132 lines · Miklos Szeredi
Quoted 2 lines · Linus Torvalds
Ugh. Th edances with denywrite and
mapping_unmap_writableare really really annoying.Attached version has error and success paths separated. Was that your complaint?
Quoted 4 lines
I get the feeling that the whole thing with
deny_write_accessandmapping_map_writablecould possibly be done after-the-fact somehow as part of actually inserting the vma in the vma tree, rather than done as the vma is prepared.I don't know if that's doable or not. The final denywrite count is obtained in
__vma_link_file(), called after__vma_link(). The questions are:- does the order of those helper calls matter?
- if it does, could the
__vma_link()be safely undone after an unsuccessful__vmal_link_file()?Quoted 3 lines
And most users of
vma_set_file()probably really don't want that whole thing at all (ie the DRM stuff that just switches out a local thing. They also don't check for the new error cases you've added.Christian König wants to follow up with those checks (which should be asserts, if the code wasn't buggy in the first place).
Quoted 3 lines
So I really think this is quite questionable, and those cases should probably have been done entirely inside ovlfs rather than polluting the cases that don't care and don't check.
I don't get that.
mmap_region()currently drops the deny counts from the original file. That doesn't work for overlayfs since it needs to take new temp counts on the override file.So
mmap_region()is changed to drop the counts onvma->vm_file, but then all callers ofvma_set_file()will need to do that switch of temp counts, there's no way around that.Thanks,
MiklosFor reference, here's the previous discussion:
https://lore.kernel.org/linux-mm/[email protected]/
--- fs/overlayfs/file.c | 4 +++- include/linux/mm.h | 2 +- mm/mmap.c | 2 +- mm/util.c | 31 ++++++++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 4 deletions(-)fs/overlayfs/file.c +3
-1@@ -475,7 +475,9 @@ static int ovl_mmap(struct file *file, sif (WARN_ON (file != vma->vm_file))return -EIO ; -vma_set_file (vma, realfile); + ret =vma_set_file (vma, realfile); +if (ret) +return ret; old_cred =ovl_override_creds (file_inode (file)->i_sb); ret =call_mmap (vma->vm_file, vma);include/linux/mm.h +1
-1@@ -2780,7 +2780,7 @@ static inline void vma_set_page_prot(str }#endif -void vma_set_file (struct vm_area_struct *vma,struct file *file); +int /* __must_check */ vma_set_file (struct vm_area_struct *vma,struct file *file);#ifdef CONFIG_NUMA_BALANCING unsigned long change_prot_numa(struct vm_area_struct *vma,mm/mmap.c +1
-1@@ -1806,6 +1806,7 @@ unsigned long mmap_region(struct file *f*/ vma->vm_file =get_file (file); error =call_mmap (file, vma); + file = vma->vm_file;if (error) goto unmap_and_free_vma;@@ -1867,7 +1868,6 @@ unsigned long mmap_region(struct file *fif (vm_flags &VM_DENYWRITE )allow_write_access (file); } - file = vma->vm_file; out:perf_event_mmap (vma);mm/util.c +24
-1@@ -314,12 +314,41 @@ int vma_is_stack_for_current(struct vm_a/* * Change backing file, only valid to use during initial VMA setup. */ -void vma_set_file (struct vm_area_struct *vma,struct file *file) +int vma_set_file (struct vm_area_struct *vma,struct file *file) { +vm_flags_t vm_flags = vma->vm_flags; +int err; + +/* Get temporary denial counts on replacement */ +if (vm_flags &VM_DENYWRITE ) { + err =deny_write_access (file); +if (err) +return err; + } +if (vm_flags &VM_SHARED ) { + err =mapping_map_writable (file->f_mapping); +if (err) + goto undo_denywrite; + } +/* Changing an anonymous vma with this is illegal */ get_file (file);swap (vma->vm_file, file); + +/* Undo temporary denial counts on replaced */ +if (vm_flags &VM_SHARED ) +mapping_unmap_writable (file->f_mapping); + +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +
mm/util.c int vma_is_stack_for_current(struct vm_a · line 354
Quoted 10 lines
mm/util.c +6
-0fput (file); +return 0 ; + +undo_denywrite: +if (vm_flags &VM_DENYWRITE ) +allow_write_access (file); +return err; }EXPORT_SYMBOL (vma_set_file);
I proposed a while ago to get rid of VM_DENYWRITE completely:
https://lkml.kernel.org/r/[email protected]
I haven't looked how much it still applies to current upstream, but maybe that might help cleaning up that code.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 7 lines · David Hildenbrand
I proposed a while ago to get rid of
VM_DENYWRITEcompletely:https://lkml.kernel.org/r/[email protected]
I haven't looked how much it still applies to current upstream, but maybe that might help cleaning up that code.
I like it.
I agree that we could - and probably should - just do it this way.
We don't expose MAP_DENYWRITE to user space any more - and the old legacy library loading code certainly isn't worth it - and so effectively the only way to set it is with execve().
And yes, it gets rid of all the silly games with the per-mapping flags.
Linus
Quoted 11 lines · Linus Torvalds
Quoted 7 lines · David Hildenbrand
I proposed a while ago to get rid of
VM_DENYWRITEcompletely:https://lkml.kernel.org/r/[email protected]
I haven't looked how much it still applies to current upstream, but maybe that might help cleaning up that code.
I like it.
Quoted 7 lines
I agree that we could - and probably should - just do it this way.
We don't expose
MAP_DENYWRITEto user space any more - and the old legacy library loading code certainly isn't worth it - and so effectively the only way to set it is withexecve().And yes, it gets rid of all the silly games with the per-mapping flags.
I'll rebase, retest and resend, putting you on cc. Then we can discuss if/how/when we might want to go that path.
Signature · 3 lines
Thanks, David / dhildenb