public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] binder: add mutex_lock for mmap and NULL when free
@ 2023-10-07  3:40 Kassey Li
  2023-10-07  6:44 ` Greg KH
  0 siblings, 1 reply; 7+ messages in thread
From: Kassey Li @ 2023-10-07  3:40 UTC (permalink / raw)
  To: gregkh, gregkh, cmllamas, surenb, arve, joel, brauner
  Cc: tkjos, maco, quic_yingangl, linux-kernel

Enforce alloc->mutex in binder_alloc_mmap_handler when add
the entry to list.

Assign the freed pages/page_ptr to NULL to catch possible
use after free with NULL pointer access.

Signed-off-by: Kassey Li <quic_yingangl@quicinc.com>
---
 drivers/android/binder_alloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index e3db8297095a..c7d126e04343 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -775,6 +775,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	}
 
 	buffer->user_data = alloc->buffer;
+	mutex_lock(&alloc->mutex);
 	list_add(&buffer->entry, &alloc->buffers);
 	buffer->free = 1;
 	binder_insert_free_buffer(alloc, buffer);
@@ -782,7 +783,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 
 	/* Signal binder_alloc is fully initialized */
 	binder_alloc_set_vma(alloc, vma);
-
+	mutex_unlock(&alloc->mutex);
 	return 0;
 
 err_alloc_buf_struct_failed:
@@ -856,9 +857,11 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc)
 				     __func__, alloc->pid, i, page_addr,
 				     on_lru ? "on lru" : "active");
 			__free_page(alloc->pages[i].page_ptr);
+			alloc->pages[i].page_ptr = NULL;
 			page_count++;
 		}
 		kfree(alloc->pages);
+		alloc->pages = NULL;
 	}
 	mutex_unlock(&alloc->mutex);
 	if (alloc->mm)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-10-07 11:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-07  3:40 [PATCH] binder: add mutex_lock for mmap and NULL when free Kassey Li
2023-10-07  6:44 ` Greg KH
2023-10-07 11:07   ` Kassey Li
2023-10-07 11:18     ` Greg KH
2023-10-07 11:34       ` Kassey Li
2023-10-07 11:37         ` Greg KH
2023-10-07 11:43           ` Kassey Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox