Skip to contentSkip to conversation

binder: validate alloc->mm in ->mmap() handler

PATCH In review Carlos Llamas 2 participants 2 messages Nov 4, 2022

Subsystems (2)
lore
Share & tools
lore

Carlos Llamas

author maintainer
#0 lore raw

PATCH 1/1 binder: validate alloc->mm in ->mmap() handler

Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference") binder caches a pointer to the current->mm during open(). This fixes a null-ptr dereference reported by syzkaller. Unfortunately, it also opens the door for a process to update its mm after the open(), (e.g. via execve) making the cached alloc->mm pointer invalid.

Things get worse when the process continues to mmap() a vma. From this point forward, binder will attempt to find this vma using an obsolete alloc->mm reference. Such as in binder_update_page_range(), where the wrong vma is obtained via vma_lookup(), yet binder proceeds to happily insert new pages into it.

To avoid this issue fail the ->mmap() callback if we detect a mismatch between the vma->vm_mm and the original alloc->mm pointer. This prevents alloc->vm_addr from getting set, so that any subsequent vma_lookup() calls fail as expected.

Fixes 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference")
Cc <[email protected]> # 5.15+
---
 drivers/android/binder_alloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/android/binder_alloc.c +7 -0
index 1c39cfce32fa..4ad42b0f75cd 100644
@@ -739,6 +739,12 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	const char *failure_string;
 	struct binder_buffer *buffer;
 
+	if (unlikely(vma->vm_mm != alloc->mm)) {
+		ret = -EINVAL;
+		failure_string = "invalid vma->vm_mm";
+		goto err_invalid_mm;
+	}
+
 	mutex_lock(&binder_alloc_mmap_lock);
 	if (alloc->buffer_size) {
 		ret = -EBUSY;
@@ -785,6 +791,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	alloc->buffer_size = 0;
 err_already_mapped:
 	mutex_unlock(&binder_alloc_mmap_lock);
+err_invalid_mm:
 	binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
 			   "%s: %d %lx-%lx %s failed %d\n", __func__,
 			   alloc->pid, vma->vm_start, vma->vm_end,
Signature · 1 line
2.38.1.431.g37b22c650d-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Carlos Llamas

Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference") binder caches a pointer to the current->mm during open(). This fixes a null-ptr dereference reported by syzkaller. Unfortunately, it also opens the door for a process to update its mm after the open(), (e.g. via execve) making the cached alloc->mm pointer invalid.

Things get worse when the process continues to mmap() a vma. From this point forward, binder will attempt to find this vma using an obsolete alloc->mm reference. Such as in binder_update_page_range(), where the wrong vma is obtained via vma_lookup(), yet binder proceeds to happily insert new pages into it.

To avoid this issue fail the ->mmap() callback if we detect a mismatch between the vma->vm_mm and the original alloc->mm pointer. This prevents alloc->vm_addr from getting set, so that any subsequent vma_lookup() calls fail as expected.

Quoted 4 lines
Fixes 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference")
Cc <[email protected]> # 5.15+
Quoted 32 lines
---
 drivers/android/binder_alloc.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/android/binder_alloc.c +7 -1
index 1c39cfce32fa..4ad42b0f75cd 100644
@@ -739,6 +739,12 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
        const char *failure_string;
        struct binder_buffer *buffer;
 
+       if (unlikely(vma->vm_mm != alloc->mm)) {
+               ret = -EINVAL;
+               failure_string = "invalid vma->vm_mm";
+               goto err_invalid_mm;
+       }
+
        mutex_lock(&binder_alloc_mmap_lock);
        if (alloc->buffer_size) {
                ret = -EBUSY;
@@ -785,6 +791,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
        alloc->buffer_size = 0;
 err_already_mapped:
        mutex_unlock(&binder_alloc_mmap_lock);
+err_invalid_mm:
        binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
                           "%s: %d %lx-%lx %s failed %d\n", __func__,
                           alloc->pid, vma->vm_start, vma->vm_end,
--

2.38.1.431.g37b22c650d-goog