Skip to contentSkip to conversation

mmap: Fix do_brk_flags() modifying obviously incorrect VMAs

PATCH Superseded Liam Howlett 2 participants 3 messages Dec 5, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmap: Fix do_brk_flags() modifying obviously incorrect VMAs

Add more sanity checks to the VMA that do_brk_flags() will expand. Ensure the VMA matches basic merge requirements within the function before calling can_vma_merge_after().

Drop the duplicate checks from vm_brk_flags() since they will be enforced later.

Fixes 2e7ce7d354f2 ("mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()")
---
 mm/mmap.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
mm/mmap.c +3 -8
index a5eb2f175da0..41a2c42593e8 100644
@@ -2946,9 +2946,9 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma,
 	 * Expand the existing vma if possible; Note that singular lists do not
 	 * occur after forking, so the expand will only happen on new VMAs.
 	 */
-	if (vma &&
-	    (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) &&
-	    ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) {
+	if (vma && vma->vm_end == addr && !vma_policy(vma) && vma->anon_vma &&
+	    can_vma_merge_after(vma, flags, NULL, NULL,
+				addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) {
 		mas_set_range(mas, vma->vm_start, addr + len - 1);
 		if (mas_preallocate(mas, vma, GFP_KERNEL))
 			return -ENOMEM;
@@ -3035,11 +3035,6 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)
 		goto munmap_failed;
 
 	vma = mas_prev(&mas, 0);
-	if (!vma || vma->vm_end != addr || vma_policy(vma) ||
-	    !can_vma_merge_after(vma, flags, NULL, NULL,
-				 addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL))
-		vma = NULL;
-
 	ret = do_brk_flags(&mas, vma, addr, len, flags);
 	populate = ((mm->def_flags & VM_LOCKED) != 0);
 	mmap_write_unlock(mm);
Signature · 1 line
2.35.1
reviewer
#1 lore raw
Quoted 6 lines · Liam Howlett

Add more sanity checks to the VMA that do_brk_flags() will expand. Ensure the VMA matches basic merge requirements within the function before calling can_vma_merge_after().

Drop the duplicate checks from vm_brk_flags() since they will be enforced later.

Looks good to me, with one note:

Quoted 12 lines
Fixes 2e7ce7d354f2 ("mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()")
---
 mm/mmap.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
mm/mmap.c +0 -0
index a5eb2f175da0..41a2c42593e8 100644

mm/mmap.c static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, · line 2949

Quoted 7 lines
mm/mmap.c +1 -3
@@ -2946,9 +2946,9 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma,
         * Expand the existing vma if possible; Note that singular lists do not
         * occur after forking, so the expand will only happen on new VMAs.
         */
-       if (vma &&
-           (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) &&
-           ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) {
+       if (vma && vma->vm_end == addr && !vma_policy(vma) && vma->anon_vma &&

Why the "vma->anon_vma" check here? The old code was checking that the existing VMA is not attached to more than one anon_vma; but the new code instead checks that the existing VMA is attached to at least one anon_vma, and then is_mergeable_anon_vma() checks that the VMA is not attached to more than one anon_vma, so in effect the VMA has to be attached to exactly one anon_vma. Is that intentional?

If not, maybe delete the "vma->anon_vma &&" - can_vma_merge_after() already does the equivalent check of the old "(!vma->anon_vma || list_is_singular(&vma->anon_vma_chain))".

Quoted 19 lines
mm/mmap.c +2 -6
+           can_vma_merge_after(vma, flags, NULL, NULL,
+                               addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) {
                mas_set_range(mas, vma->vm_start, addr + len - 1);
                if (mas_preallocate(mas, vma, GFP_KERNEL))
                        return -ENOMEM;
@@ -3035,11 +3035,6 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)
                goto munmap_failed;
 
        vma = mas_prev(&mas, 0);
-       if (!vma || vma->vm_end != addr || vma_policy(vma) ||
-           !can_vma_merge_after(vma, flags, NULL, NULL,
-                                addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL))
-               vma = NULL;
-
        ret = do_brk_flags(&mas, vma, addr, len, flags);
        populate = ((mm->def_flags & VM_LOCKED) != 0);
        mmap_write_unlock(mm);
--

2.35.1

#2 lore raw
Quoted 28 lines · Jann Horn
Quoted 6 lines · Liam Howlett

Add more sanity checks to the VMA that do_brk_flags() will expand. Ensure the VMA matches basic merge requirements within the function before calling can_vma_merge_after().

Drop the duplicate checks from vm_brk_flags() since they will be enforced later.

Looks good to me, with one note:

Quoted 18 lines
Fixes 2e7ce7d354f2 ("mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()")
---
 mm/mmap.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
mm/mmap.c +0 -3
index a5eb2f175da0..41a2c42593e8 100644
@@ -2946,9 +2946,9 @@ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma,
         * Expand the existing vma if possible; Note that singular lists do not
         * occur after forking, so the expand will only happen on new VMAs.
         */
-       if (vma &&
-           (!vma->anon_vma || list_is_singular(&vma->anon_vma_chain)) &&
-           ((vma->vm_flags & ~VM_SOFTDIRTY) == flags)) {
Quoted 8 lines

mm/mmap.c static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, · line 2949

Quoted 1 line
mm/mmap.c +1 -0
+       if (vma && vma->vm_end == addr && !vma_policy(vma) && vma->anon_vma &&

Why the "vma->anon_vma" check here? The old code was checking that the existing VMA is not attached to more than one anon_vma; but the new code instead checks that the existing VMA is attached to at least one anon_vma, and then is_mergeable_anon_vma() checks that the VMA is not attached to more than one anon_vma, so in effect the VMA has to be attached to exactly one anon_vma. Is that intentional?

That was not intentional.

Quoted 4 lines

If not, maybe delete the "vma->anon_vma &&" - can_vma_merge_after() already does the equivalent check of the old "(!vma->anon_vma || list_is_singular(&vma->anon_vma_chain))".

Yes, I will do that and send a v2.

Quoted 20 lines
Quoted 20 lines
mm/mmap.c +2 -6
+           can_vma_merge_after(vma, flags, NULL, NULL,
+                               addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL)) {
                mas_set_range(mas, vma->vm_start, addr + len - 1);
                if (mas_preallocate(mas, vma, GFP_KERNEL))
                        return -ENOMEM;
@@ -3035,11 +3035,6 @@ int vm_brk_flags(unsigned long addr, unsigned long request, unsigned long flags)
                goto munmap_failed;
 
        vma = mas_prev(&mas, 0);
-       if (!vma || vma->vm_end != addr || vma_policy(vma) ||
-           !can_vma_merge_after(vma, flags, NULL, NULL,
-                                addr >> PAGE_SHIFT, NULL_VM_UFFD_CTX, NULL))
-               vma = NULL;
-
        ret = do_brk_flags(&mas, vma, addr, len, flags);
        populate = ((mm->def_flags & VM_LOCKED) != 0);
        mmap_write_unlock(mm);
--

2.35.1