public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Carlos Llamas <cmllamas@google.com>
Cc: "Arve Hjønnevåg" <arve@android.com>,
	"Todd Kjos" <tkjos@android.com>,
	"Martijn Coenen" <maco@android.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>,
	"Michal Hocko" <mhocko@kernel.org>,
	"Vlastimil Babka" <vbabka@suse.cz>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org, kernel-team@android.com,
	"Jann Horn" <jannh@google.com>,
	stable@vger.kernel.org, "Minchan Kim" <minchan@kernel.org>,
	"Yang Shi" <yang.shi@linux.alibaba.com>,
	"Liam Howlett" <liam.howlett@oracle.com>,
	"Todd Kjos" <tkjos@google.com>
Subject: Re: [PATCH] binder: fix UAF of alloc->vma in race with munmap()
Date: Sat, 20 May 2023 17:29:23 +0100	[thread overview]
Message-ID: <2023052002-nervy-juggling-c7a1@gregkh> (raw)
In-Reply-To: <ZGjCWI6kpaNuNiya@google.com>

On Sat, May 20, 2023 at 12:51:36PM +0000, Carlos Llamas wrote:
> On Sat, May 20, 2023 at 10:55:02AM +0100, Greg Kroah-Hartman wrote:
> > On Fri, May 19, 2023 at 07:59:49PM +0000, Carlos Llamas wrote:
> > > [ cmllamas: clean forward port from commit 015ac18be7de ("binder: fix
> > >   UAF of alloc->vma in race with munmap()") in 5.10 stable. It is needed
> > >   in mainline after the revert of commit a43cfc87caaf ("android: binder:
> > >   stop saving a pointer to the VMA") as pointed out by Liam. The commit
> > >   log and tags have been tweaked to reflect this. ]
> > > 
> > > In commit 720c24192404 ("ANDROID: binder: change down_write to
> > > down_read") binder assumed the mmap read lock is sufficient to protect
> > > alloc->vma inside binder_update_page_range(). This used to be accurate
> > > until commit dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in
> > > munmap"), which now downgrades the mmap_lock after detaching the vma
> > > from the rbtree in munmap(). Then it proceeds to teardown and free the
> > > vma with only the read lock held.
> > > 
> > > This means that accesses to alloc->vma in binder_update_page_range() now
> > > will race with vm_area_free() in munmap() and can cause a UAF as shown
> > > in the following KASAN trace:
> > > 
> > >   ==================================================================
> > >   BUG: KASAN: use-after-free in vm_insert_page+0x7c/0x1f0
> > >   Read of size 8 at addr ffff16204ad00600 by task server/558
> > > 
> > >   CPU: 3 PID: 558 Comm: server Not tainted 5.10.150-00001-gdc8dcf942daa #1
> > >   Hardware name: linux,dummy-virt (DT)
> > >   Call trace:
> > >    dump_backtrace+0x0/0x2a0
> > >    show_stack+0x18/0x2c
> > >    dump_stack+0xf8/0x164
> > >    print_address_description.constprop.0+0x9c/0x538
> > >    kasan_report+0x120/0x200
> > >    __asan_load8+0xa0/0xc4
> > >    vm_insert_page+0x7c/0x1f0
> > >    binder_update_page_range+0x278/0x50c
> > >    binder_alloc_new_buf+0x3f0/0xba0
> > >    binder_transaction+0x64c/0x3040
> > >    binder_thread_write+0x924/0x2020
> > >    binder_ioctl+0x1610/0x2e5c
> > >    __arm64_sys_ioctl+0xd4/0x120
> > >    el0_svc_common.constprop.0+0xac/0x270
> > >    do_el0_svc+0x38/0xa0
> > >    el0_svc+0x1c/0x2c
> > >    el0_sync_handler+0xe8/0x114
> > >    el0_sync+0x180/0x1c0
> > > 
> > >   Allocated by task 559:
> > >    kasan_save_stack+0x38/0x6c
> > >    __kasan_kmalloc.constprop.0+0xe4/0xf0
> > >    kasan_slab_alloc+0x18/0x2c
> > >    kmem_cache_alloc+0x1b0/0x2d0
> > >    vm_area_alloc+0x28/0x94
> > >    mmap_region+0x378/0x920
> > >    do_mmap+0x3f0/0x600
> > >    vm_mmap_pgoff+0x150/0x17c
> > >    ksys_mmap_pgoff+0x284/0x2dc
> > >    __arm64_sys_mmap+0x84/0xa4
> > >    el0_svc_common.constprop.0+0xac/0x270
> > >    do_el0_svc+0x38/0xa0
> > >    el0_svc+0x1c/0x2c
> > >    el0_sync_handler+0xe8/0x114
> > >    el0_sync+0x180/0x1c0
> > > 
> > >   Freed by task 560:
> > >    kasan_save_stack+0x38/0x6c
> > >    kasan_set_track+0x28/0x40
> > >    kasan_set_free_info+0x24/0x4c
> > >    __kasan_slab_free+0x100/0x164
> > >    kasan_slab_free+0x14/0x20
> > >    kmem_cache_free+0xc4/0x34c
> > >    vm_area_free+0x1c/0x2c
> > >    remove_vma+0x7c/0x94
> > >    __do_munmap+0x358/0x710
> > >    __vm_munmap+0xbc/0x130
> > >    __arm64_sys_munmap+0x4c/0x64
> > >    el0_svc_common.constprop.0+0xac/0x270
> > >    do_el0_svc+0x38/0xa0
> > >    el0_svc+0x1c/0x2c
> > >    el0_sync_handler+0xe8/0x114
> > >    el0_sync+0x180/0x1c0
> > > 
> > >   [...]
> > >   ==================================================================
> > > 
> > > To prevent the race above, revert back to taking the mmap write lock
> > > inside binder_update_page_range(). One might expect an increase of mmap
> > > lock contention. However, binder already serializes these calls via top
> > > level alloc->mutex. Also, there was no performance impact shown when
> > > running the binder benchmark tests.
> > > 
> > > Fixes: c0fd2101781e ("Revert "android: binder: stop saving a pointer to the VMA"")
> > 
> > I can't find this commit in any tree, are you sure it's correct?
> 
> The commit comes from your char-misc-linus branch, it hasn't really
> landed in mainline yet. I added this tag to make sure this fix is
> bounded to the revert otherwise it exposes the UAF. I know I'm relying
> on a merge, so let me know if I should drop the tag instead.

Ah, no, that works, thanks, I'll queue it up.

greg k-h

  reply	other threads:[~2023-05-20 16:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19 19:59 [PATCH] binder: fix UAF of alloc->vma in race with munmap() Carlos Llamas
2023-05-20  9:54 ` Greg Kroah-Hartman
2023-05-20 12:35   ` Carlos Llamas
2023-05-20  9:55 ` Greg Kroah-Hartman
2023-05-20 12:51   ` Carlos Llamas
2023-05-20 16:29     ` Greg Kroah-Hartman [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-05-20 10:22 Fred Lewis

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=2023052002-nervy-juggling-c7a1@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=jannh@google.com \
    --cc=joel@joelfernandes.org \
    --cc=kernel-team@android.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=liam.howlett@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=mhocko@kernel.org \
    --cc=minchan@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=surenb@google.com \
    --cc=tkjos@android.com \
    --cc=tkjos@google.com \
    --cc=vbabka@suse.cz \
    --cc=willy@infradead.org \
    --cc=yang.shi@linux.alibaba.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