Re: [PATCHv2 1/2] x86/mm: Do not allow non-MAP_FIXED mapping across DEFAULT_MAP_WINDOW border

From: Kirill A. Shutemov
Date: Wed Nov 15 2017 - 07:10:57 EST


On Wed, Nov 15, 2017 at 12:39:40PM +0100, Thomas Gleixner wrote:
> On Wed, 15 Nov 2017, Kirill A. Shutemov wrote:
> > On Wed, Nov 15, 2017 at 12:00:46AM +0100, Thomas Gleixner wrote:
> > > On Wed, 15 Nov 2017, Kirill A. Shutemov wrote:
> > > > On Tue, Nov 14, 2017 at 09:54:52PM +0100, Thomas Gleixner wrote:
> > > > > On Tue, 14 Nov 2017, Kirill A. Shutemov wrote:
> > > > >
> > > > > > On Tue, Nov 14, 2017 at 05:01:50PM +0100, Thomas Gleixner wrote:
> > > > > > > @@ -198,11 +199,14 @@ arch_get_unmapped_area_topdown(struct fi
> > > > > > > /* requesting a specific address */
> > > > > > > if (addr) {
> > > > > > > addr = PAGE_ALIGN(addr);
> > > > > > > + if (!mmap_address_hint_valid(addr, len))
> > > > > > > + goto get_unmapped_area;
> > > > > > > +
> > > > > >
> > > > > > Here and in hugetlb_get_unmapped_area(), we should align the addr after
> > > > > > the check, not before. Otherwise the alignment itself can bring us over
> > > > > > the borderline as we align up.
> > > > >
> > > > > Hmm, then I wonder whether the next check against vm_start_gap() which
> > > > > checks against the aligned address is correct:
> > > > >
> > > > > addr = PAGE_ALIGN(addr);
> > > > > vma = find_vma(mm, addr);
> > > > >
> > > > > if (end - len >= addr &&
> > > > > (!vma || addr + len <= vm_start_gap(vma)))
> > > > > return addr;
> > > >
> > > > I think the check is correct. The check is against resulting addresses
> > > > that end up in vm_start/vm_end. In our case we want to figure out what
> > > > user asked for.
> > >
> > > Well, but then checking just against the user supplied addr is only half of
> > > the story.
> > >
> > > addr = boundary - PAGE_SIZE - PAGE_SIZE / 2;
> > > len = PAGE_SIZE - PAGE_SIZE / 2;
> > >
> > > That fits, but then after alignment we end up with
> > >
> > > addr = boudary - PAGE_SIZE;
> > >
> > > and due to len > PAGE_SIZE this will result in a mapping which crosses the
> > > boundary, right? So checking against the PAGE_ALIGN(addr) should be the
> > > right thing to do.
> >
> > IIUC, this is only the case if 'len' is not aligned, right?
> >
> > >From what I see we expect caller to align it (and mm/mmap.c does this, I
> > haven't checked other callers).
> >
> > And hugetlb would actively reject non-aligned len.
> >
> > I *think* we should be fine with checking unaligned 'addr'.
>
> I think we should keep it consistent for the normal and the huge case and
> just check aligned and be done with it.

Aligned 'addr'? Or 'len'? Both?

We would have problem with checking aligned addr. I steped it in hugetlb
case:

- User asks for mmap((1UL << 47) - PAGE_SIZE, 2 << 20, MAP_HUGETLB);

- On 4-level paging machine this gives us invalid hint address as
'TASK_SIZE - len' is more than 'addr'. Goto get_unmapped_area.

- On 5-level paging machine hint address gets rounded up to next 2MB
boundary that is exactly 1UL << 47 and we happily allocate from full
address space which may lead to trouble.

--
Kirill A. Shutemov