linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Liam Howlett <liam.howlett@oracle.com>
To: Jann Horn <jannh@google.com>
Cc: "linux-mm@kvack.org" <linux-mm@kvack.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Yu Zhao <yuzhao@google.com>, Jason Donenfeld <Jason@zx2c4.com>,
	Matthew Wilcox <willy@infradead.org>,
	SeongJae Park <sj@kernel.org>, Vlastimil Babka <vbabka@suse.cz>
Subject: Re: [PATCH] mmap: Fix do_brk_flags() modifying obviously incorrect VMAs
Date: Mon, 5 Dec 2022 17:27:35 +0000	[thread overview]
Message-ID: <20221205172718.vewypps6pe2qev4c@revolver> (raw)
In-Reply-To: <CAG48ez2XrTRqvLBdfXuU=V9xbRxA4jiJgGvMb2xFy2tcjnhr_A@mail.gmail.com>

* Jann Horn <jannh@google.com> [221205 12:16]:
> On Mon, Dec 5, 2022 at 5:53 PM Liam Howlett <liam.howlett@oracle.com> wrote:
> > 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:
> 
> > Fixes: 2e7ce7d354f2 ("mm/mmap: change do_brk_flags() to expand existing VMA and add do_brk_munmap()")
> > Suggested-by: Jann Horn <jannh@google.com>
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> > ---
> >  mm/mmap.c | 11 +++--------
> >  1 file changed, 3 insertions(+), 8 deletions(-)
> >
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index a5eb2f175da0..41a2c42593e8 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -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?

That was not 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))".

Yes, I will do that and send a v2.

> 
> > +           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

      reply	other threads:[~2022-12-05 17:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-05 16:53 Liam Howlett
2022-12-05 17:15 ` Jann Horn
2022-12-05 17:27   ` Liam Howlett [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221205172718.vewypps6pe2qev4c@revolver \
    --to=liam.howlett@oracle.com \
    --cc=Jason@zx2c4.com \
    --cc=akpm@linux-foundation.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=sj@kernel.org \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    --cc=yuzhao@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox