[PATCH] android: binder: Fix lockdep check on clearing vma

From: Liam Howlett
Date: Mon Jun 27 2022 - 11:19:15 EST


When munmapping a vma, the mmap_lock can be degraded to a write before
calling close() on the file handle. The binder close() function calls
binder_alloc_set_vma() to clear the vma address, which now has a lock
dep check for writing on the mmap_lock. Change the lockdep check to
ensure the reading lock is held while clearing and keep the write check
while writing.

Reported-by: syzbot+da54fa8d793ca89c741f@xxxxxxxxxxxxxxxxxxxxxxxxx
Fixes: 472a68df605b ("android: binder: stop saving a pointer to the VMA")
Signed-off-by: Liam R. Howlett <Liam.Howlett@xxxxxxxxxx>
---
drivers/android/binder_alloc.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index f555eebceef6..1014beb12802 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -315,12 +315,19 @@ static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
{
unsigned long vm_start = 0;

+ /*
+ * Allow clearing the vma with holding just the read lock to allow
+ * munmapping downgrade of the write lock before freeing and closing the
+ * file using binder_alloc_vma_close().
+ */
if (vma) {
vm_start = vma->vm_start;
alloc->vma_vm_mm = vma->vm_mm;
+ mmap_assert_write_locked(alloc->vma_vm_mm);
+ } else {
+ mmap_assert_locked(alloc->vma_vm_mm);
}

- mmap_assert_write_locked(alloc->vma_vm_mm);
alloc->vma_addr = vm_start;
}

--
2.35.1