binder: free transaction buffer in binder_release_work() on BINDER_THREAD_EXIT
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).
--- drivers/android/binder.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/android/binder.c +6 -0
@@ -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(
Carlos Llamas
maintainerQuoted 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 pendingBINDER_WORK_TRANSACTIONitems queued specifically onthread->todo.
Quoted 9 lines
For each
BINDER_WORK_TRANSACTION,binder_release_work()callsbinder_cleanup_transaction(t, "process died.", BR_DEAD_REPLY)without clearingt->buffer->transactionor callingbinder_free_buf(proc, NULL, buffer, true). Becauseallow_user_freeis still0(the transaction was never read by userspace) andprocis still alive (binder_deferred_release()will not run until the process exits),t->bufferand all translatedbinder_node/binder_refreferences insidet->bufferare leaked in the active binder mmap pool, andt->buffer->transactionis left as a dangling pointer to the freedt!
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 = NULLbeforebinder_cleanup_transaction(), and when!proc->is_dead && buffer, freebufferviabinder_free_buf(proc, NULL, buffer, true).Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
This commit is again incorrect.
Quoted 19 lines
Assisted-by LLMSigned-off-by Hui Peng <[email protected]>--- drivers/android/binder.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)drivers/android/binder.c +2
-0@@ -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