2019-03-01 23:06:54

by Todd Kjos

[permalink] [raw]
Subject: [PATCH] binder: fix race between munmap() and direct reclaim

An munmap() on a binder device causes binder_vma_close() to be called
which clears the alloc->vma pointer.

If direct reclaim causes binder_alloc_free_page() to be called, there
is a race where alloc->vma is read into a local vma pointer and then
used later after the mm->mmap_sem is acquired. This can result in
calling zap_page_range() with an invalid vma which manifests as a
use-after-free in zap_page_range().

The fix is to check alloc->vma after acquiring the mmap_sem (which we
were acquiring anyway) and skip zap_page_range() if it has changed
to NULL.

Signed-off-by: Todd Kjos <[email protected]>
---
drivers/android/binder_alloc.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index 6389467670a0b..195f120c4e8c9 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -927,14 +927,13 @@ enum lru_status binder_alloc_free_page(struct list_head *item,

index = page - alloc->pages;
page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
+
+ mm = alloc->vma_vm_mm;
+ if (!mmget_not_zero(mm))
+ goto err_mmget;
+ if (!down_write_trylock(&mm->mmap_sem))
+ goto err_down_write_mmap_sem_failed;
vma = binder_alloc_get_vma(alloc);
- if (vma) {
- if (!mmget_not_zero(alloc->vma_vm_mm))
- goto err_mmget;
- mm = alloc->vma_vm_mm;
- if (!down_read_trylock(&mm->mmap_sem))
- goto err_down_write_mmap_sem_failed;
- }

list_lru_isolate(lru, item);
spin_unlock(lock);
@@ -945,10 +944,9 @@ enum lru_status binder_alloc_free_page(struct list_head *item,
zap_page_range(vma, page_addr, PAGE_SIZE);

trace_binder_unmap_user_end(alloc, index);
-
- up_read(&mm->mmap_sem);
- mmput(mm);
}
+ up_write(&mm->mmap_sem);
+ mmput(mm);

trace_binder_unmap_kernel_start(alloc, index);

--
2.21.0.352.gf09ad66450-goog



2019-03-02 07:58:55

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH] binder: fix race between munmap() and direct reclaim

On Fri, Mar 01, 2019 at 03:06:06PM -0800, Todd Kjos wrote:
> An munmap() on a binder device causes binder_vma_close() to be called
> which clears the alloc->vma pointer.
>
> If direct reclaim causes binder_alloc_free_page() to be called, there
> is a race where alloc->vma is read into a local vma pointer and then
> used later after the mm->mmap_sem is acquired. This can result in
> calling zap_page_range() with an invalid vma which manifests as a
> use-after-free in zap_page_range().
>
> The fix is to check alloc->vma after acquiring the mmap_sem (which we
> were acquiring anyway) and skip zap_page_range() if it has changed
> to NULL.
>
> Signed-off-by: Todd Kjos <[email protected]>
> ---

Any specific commit that this fixes? And should it be marked for
stable releases?

thanks,

greg k-h

2019-03-02 16:28:34

by Todd Kjos

[permalink] [raw]
Subject: Re: [PATCH] binder: fix race between munmap() and direct reclaim

On Fri, Mar 1, 2019 at 11:57 PM Greg KH <[email protected]> wrote:
>
> On Fri, Mar 01, 2019 at 03:06:06PM -0800, Todd Kjos wrote:
> > An munmap() on a binder device causes binder_vma_close() to be called
> > which clears the alloc->vma pointer.
> >
> > If direct reclaim causes binder_alloc_free_page() to be called, there
> > is a race where alloc->vma is read into a local vma pointer and then
> > used later after the mm->mmap_sem is acquired. This can result in
> > calling zap_page_range() with an invalid vma which manifests as a
> > use-after-free in zap_page_range().
> >
> > The fix is to check alloc->vma after acquiring the mmap_sem (which we
> > were acquiring anyway) and skip zap_page_range() if it has changed
> > to NULL.
> >
> > Signed-off-by: Todd Kjos <[email protected]>
> > ---
>
> Any specific commit that this fixes?

No, it's been there a long time.

> And should it be marked for stable releases?

It is needed in stable (back to 4.4), but will need to be backported.
Should I post backported versions targeting the specific releases now?
I was thinking we'd wait for this one to land. I think we'll need 1
patch for 4.4/4.9 and a second one for 4.14/4.19 (and some of those
backported patches will have conflicts when merged down to android-4.X
-- I think the 4.14/4.19 version will apply to all the android
branches). Let me know how you want to handle this.

>
> thanks,
>
> greg k-h

2019-03-05 20:06:42

by Joel Fernandes

[permalink] [raw]
Subject: Re: [PATCH] binder: fix race between munmap() and direct reclaim

On Sat, Mar 02, 2019 at 08:27:44AM -0800, Todd Kjos wrote:
> On Fri, Mar 1, 2019 at 11:57 PM Greg KH <[email protected]> wrote:
> >
> > On Fri, Mar 01, 2019 at 03:06:06PM -0800, Todd Kjos wrote:
> > > An munmap() on a binder device causes binder_vma_close() to be called
> > > which clears the alloc->vma pointer.
> > >
> > > If direct reclaim causes binder_alloc_free_page() to be called, there
> > > is a race where alloc->vma is read into a local vma pointer and then
> > > used later after the mm->mmap_sem is acquired. This can result in
> > > calling zap_page_range() with an invalid vma which manifests as a
> > > use-after-free in zap_page_range().
> > >
> > > The fix is to check alloc->vma after acquiring the mmap_sem (which we
> > > were acquiring anyway) and skip zap_page_range() if it has changed
> > > to NULL.
> > >
> > > Signed-off-by: Todd Kjos <[email protected]>

Awesome patch,
Reviewed-by: Joel Fernandes (Google) <[email protected]>

thanks!

- Joel

> > > ---
> >
> > Any specific commit that this fixes?
>
> No, it's been there a long time.
>
> > And should it be marked for stable releases?
>
> It is needed in stable (back to 4.4), but will need to be backported.
> Should I post backported versions targeting the specific releases now?
> I was thinking we'd wait for this one to land. I think we'll need 1
> patch for 4.4/4.9 and a second one for 4.14/4.19 (and some of those
> backported patches will have conflicts when merged down to android-4.X
> -- I think the 4.14/4.19 version will apply to all the android
> branches). Let me know how you want to handle this.
>
> >
> > thanks,
> >
> > greg k-h