Skip to contentSkip to conversation

binder: free transaction buffer in binder_release_work() on BINDER_THREAD_EXIT

PATCH In review Hui Peng 2 participants 2 messages Sep 19 – Sep 23 Android drivers

lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 binder: free transaction buffer in binder_release_work() on BINDER_THREAD_EXIT

When a binder thread exits via BINDER_THREAD_EXIT (binder_ioctl() -> binder_thread_release()) while the process itself is still alive (!proc->is_dead), binder_release_work(proc, &thread->todo) drains pending BINDER_WORK_TRANSACTION items queued specifically on thread->todo.

For each BINDER_WORK_TRANSACTION, binder_release_work() calls binder_cleanup_transaction(t, "process died.", BR_DEAD_REPLY) without clearing t->buffer->transaction or calling binder_free_buf(proc, NULL, buffer, true). Because allow_user_free is still 0 (the transaction was never read by userspace) and proc is still alive (binder_deferred_release() will not run until the process exits), t->buffer and all translated binder_node/binder_ref references inside t->buffer are leaked in the active binder mmap pool, and t->buffer->transaction is left as a dangling pointer to the freed t!

Clear buffer->transaction = NULL before binder_cleanup_transaction(), and when !proc->is_dead && buffer, free buffer via binder_free_buf(proc, NULL, buffer, true).

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Assisted-by LLM
---
 drivers/android/binder.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/android/binder.c +6 -0
index 8f2ef1bd539f..1c7218e599a1 100644
@@ -5206,11 +5224,17 @@ static void binder_release_work(struct binder_proc *proc,
 		switch (wtype) {
 		case BINDER_WORK_TRANSACTION: {
 			struct binder_transaction *t;
+			struct binder_buffer *buffer;
 
 			t = container_of(w, struct binder_transaction, work);
+			buffer = t->buffer;
+			if (buffer)
+				buffer->transaction = NULL;
 
 			binder_cleanup_transaction(t, "process died.",
 						   BR_DEAD_REPLY);
+			if (!proc->is_dead && buffer)
+				binder_free_buf(proc, NULL, buffer, true);
 		} break;
 		case BINDER_WORK_RETURN_ERROR: {
 			struct binder_error *e = container_of(
maintainer
#1 lore raw
Quoted 6 lines · Hui Peng

When a binder thread exits via BINDER_THREAD_EXIT (binder_ioctl() -> binder_thread_release()) while the process itself is still alive (!proc->is_dead), binder_release_work(proc, &thread->todo) drains pending BINDER_WORK_TRANSACTION items queued specifically on thread->todo.

Quoted 9 lines

For each BINDER_WORK_TRANSACTION, binder_release_work() calls binder_cleanup_transaction(t, "process died.", BR_DEAD_REPLY) without clearing t->buffer->transaction or calling binder_free_buf(proc, NULL, buffer, true). Because allow_user_free is still 0 (the transaction was never read by userspace) and proc is still alive (binder_deferred_release() will not run until the process exits), t->buffer and all translated binder_node/binder_ref references inside t->buffer are leaked in the active binder mmap pool, and t->buffer->transaction is left as a dangling pointer to the freed t!

I agree this is a leak. However, the buffer->transaction will also be cleared so there is no risk of a dangling pointer here.

Quoted 6 lines

Clear buffer->transaction = NULL before binder_cleanup_transaction(), and when !proc->is_dead && buffer, free buffer via binder_free_buf(proc, NULL, buffer, true).

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

This commit is again incorrect.

Quoted 19 lines
Assisted-by LLM
---
 drivers/android/binder.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/android/binder.c +2 -0
index 8f2ef1bd539f..1c7218e599a1 100644
@@ -5206,11 +5224,17 @@ static void binder_release_work(struct binder_proc *proc,
 		switch (wtype) {
 		case BINDER_WORK_TRANSACTION: {
 			struct binder_transaction *t;
+			struct binder_buffer *buffer;
 
 			t = container_of(w, struct binder_transaction, work);
+			buffer = t->buffer;

drivers/android/binder.c static void binder_release_work(struct binder_proc *proc, · line 5240

Quoted 10 lines
drivers/android/binder.c +4 -0
+			if (buffer)
+				buffer->transaction = NULL;
 
 			binder_cleanup_transaction(t, "process died.",
 						   BR_DEAD_REPLY);
+			if (!proc->is_dead && buffer)
+				binder_free_buf(proc, NULL, buffer, true);
 		} break;
 		case BINDER_WORK_RETURN_ERROR: {
 			struct binder_error *e = container_of(

The fix itself looks good to me, but could you please fix up the commit message?

Thanks
Carlos Llamas