Skip to content

Commit a7a74d7

Browse files
thejhgregkh
authored andcommitted
binder: Prevent repeated use of ->mmap() via NULL mapping
binder_alloc_mmap_handler() attempts to detect the use of ->mmap() on a binder_proc whose binder_alloc has already been initialized by checking whether alloc->buffer is non-zero. Before commit 8802116 ("binder: remove kernel vm_area for buffer space"), alloc->buffer was a kernel mapping address, which is always non-zero, but since that commit, it is a userspace mapping address. A sufficiently privileged user can map /dev/binder at NULL, tricking binder_alloc_mmap_handler() into assuming that the binder_proc has not been mapped yet. This leads to memory unsafety. Luckily, no context on Android has such privileges, and on a typical Linux desktop system, you need to be root to do that. Fix it by using the mapping size instead of the mapping address to distinguish the mapped case. A valid VMA can't have size zero. Fixes: 8802116 ("binder: remove kernel vm_area for buffer space") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn <jannh@google.com> Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Link: https://lore.kernel.org/r/20191018205631.248274-2-jannh@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8eb52a1 commit a7a74d7

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

‎drivers/android/binder_alloc.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,17 +680,17 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
680680
struct binder_buffer *buffer;
681681

682682
mutex_lock(&binder_alloc_mmap_lock);
683-
if (alloc->buffer) {
683+
if (alloc->buffer_size) {
684684
ret = -EBUSY;
685685
failure_string = "already mapped";
686686
goto err_already_mapped;
687687
}
688+
alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
689+
SZ_4M);
690+
mutex_unlock(&binder_alloc_mmap_lock);
688691

689692
alloc->buffer = (void __user *)vma->vm_start;
690-
mutex_unlock(&binder_alloc_mmap_lock);
691693

692-
alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
693-
SZ_4M);
694694
alloc->pages = kcalloc(alloc->buffer_size / PAGE_SIZE,
695695
sizeof(alloc->pages[0]),
696696
GFP_KERNEL);
@@ -721,8 +721,9 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
721721
kfree(alloc->pages);
722722
alloc->pages = NULL;
723723
err_alloc_pages_failed:
724-
mutex_lock(&binder_alloc_mmap_lock);
725724
alloc->buffer = NULL;
725+
mutex_lock(&binder_alloc_mmap_lock);
726+
alloc->buffer_size = 0;
726727
err_already_mapped:
727728
mutex_unlock(&binder_alloc_mmap_lock);
728729
binder_alloc_debug(BINDER_DEBUG_USER_ERROR,

0 commit comments

Comments
 (0)