binder: fix TF_UPDATE_TXN supersede cleanup bugs
Subsystems (2)
cover binder: fix TF_UPDATE_TXN supersede cleanup bugs
Two bugs in the t_outdated cleanup path of binder_proc_transaction(), both introduced by commit 9864bb480133 ("binder: add TF_UPDATE_TXN to replace outdated txn"):
1. kfree(t_outdated) is called without binder_free_txn_fixups(), permanently leaking binder_txn_fd_fixup entries and their fget()'d struct file references. The refcount never reaches zero; the leak survives process exit and accumulates until file-max exhaustion.
2. binder_release_entire_buffer() is called with is_failure=false for a transaction that was never delivered. Since binder_apply_fd_fixups() was never called, the BINDER_TYPE_FDA handler reads stale buffer data as fd numbers and closes unrelated fds via binder_deferred_fd_close().
Confirmed on mainline Linux (6.8.0-124-generic, binder_linux module) and Android (Pixel 8, kernel 6.1.124, /dev/hwbinder).
Tomer Pomeranc (2):
binder: fix leaked fd fixups on TF_UPDATE_TXN supersede
binder: fix is_failure flag for superseded transaction cleanup
drivers/android/binder.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
PATCH 1/2 binder: fix leaked fd fixups on TF_UPDATE_TXN supersede
When a TF_UPDATE_TXN transaction supersedes a pending async transaction in a frozen process, the outdated transaction is freed with kfree() directly. This skips binder_free_txn_fixups(), leaking all binder_txn_fd_fixup entries and their fget()'d struct file references.
The leaked file refcounts never reach zero, so the struct file objects are permanently pinned in memory. They survive process exit and accumulate across invocations until file-max exhaustion.
Every other transaction cleanup path (binder_free_transaction(), binder_transaction() error paths, binder_release_work()) correctly calls binder_free_txn_fixups(). Add the missing call before kfree() in the t_outdated cleanup block.
--- drivers/android/binder.c | 1 + 1 file changed, 1 insertion(+)
drivers/android/binder.c +1 -0
@@ -2932,6 +2932,7 @@ static int binder_proc_transaction(struct binder_transaction *t,trace_binder_transaction_update_buffer_release (buffer);binder_release_entire_buffer (proc,NULL , buffer, false);binder_alloc_free_buf (&proc->alloc, buffer); +binder_free_txn_fixups (t_outdated);kfree (t_outdated);binder_stats_deleted (BINDER_STAT_TRANSACTION ); }
Signature · 1 line
2.34.1
Re: [PATCH 1/2] binder: fix leaked fd fixups on TF_UPDATE_TXN supersede
Quoted 36 lines · Tomer Pomeranc
When a
TF_UPDATE_TXNtransaction supersedes a pending async transaction in a frozen process, the outdated transaction is freed withkfree()directly. This skipsbinder_free_txn_fixups(), leaking allbinder_txn_fd_fixupentries and their fget()'d struct file references.The leaked file refcounts never reach zero, so the struct file objects are permanently pinned in memory. They survive process exit and accumulate across invocations until file-max exhaustion.
Every other transaction cleanup path (
binder_free_transaction(),binder_transaction()error paths,binder_release_work()) correctly callsbinder_free_txn_fixups(). Add the missing call beforekfree()in thet_outdatedcleanup block.Fixes 9864bb480133 ("binder: add TF_UPDATE_TXN to replace outdated txn")Signed-off-by Tomer Pomeranc <[email protected]>--- drivers/android/binder.c | 1 + 1 file changed, 1 insertion(+)drivers/android/binder.c +1
-0@@ -2932,6 +2932,7 @@ static int binder_proc_transaction(struct binder_transaction *t,trace_binder_transaction_update_buffer_release (buffer);binder_release_entire_buffer (proc,NULL , buffer, false);binder_alloc_free_buf (&proc->alloc, buffer); +binder_free_txn_fixups (t_outdated);kfree (t_outdated);binder_stats_deleted (BINDER_STAT_TRANSACTION ); }Signature · 1 line
2.34.1
Thanks,
PATCH 2/2 binder: fix is_failure flag for superseded transaction cleanup
In reply to Tomer PomerancWhen a TF_UPDATE_TXN transaction supersedes a pending async transaction, binder_release_entire_buffer() is called with is_failure=false. Since the superseded transaction was never delivered, binder_apply_fd_fixups() was never called and no fds were installed in the target process.
With is_failure=false, the BINDER_TYPE_FDA cleanup handler interprets stale buffer contents as installed fd numbers and passes them to binder_deferred_fd_close(), closing unrelated file descriptors.
Pass is_failure=true since the transaction was never delivered to the target, matching the semantics of all other undelivered-transaction cleanup paths.
--- drivers/android/binder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/android/binder.c +1 -1
@@ -2930,7 +2930,7 @@ static int binder_proc_transaction(struct binder_transaction *t, t_outdated->buffer =NULL ; buffer->transaction =NULL ;trace_binder_transaction_update_buffer_release (buffer); -binder_release_entire_buffer (proc,NULL , buffer, false); +binder_release_entire_buffer (proc,NULL , buffer, true);binder_alloc_free_buf (&proc->alloc, buffer);binder_free_txn_fixups (t_outdated);kfree (t_outdated);
Signature · 1 line
2.34.1
Re: [PATCH 2/2] binder: fix is_failure flag for superseded transaction cleanup
Quoted 36 lines · Tomer Pomeranc
When a
TF_UPDATE_TXNtransaction supersedes a pending async transaction,binder_release_entire_buffer()is called with is_failure=false. Since the superseded transaction was never delivered,binder_apply_fd_fixups()was never called and no fds were installed in the target process.With is_failure=false, the
BINDER_TYPE_FDAcleanup handler interprets stale buffer contents as installed fd numbers and passes them tobinder_deferred_fd_close(), closing unrelated file descriptors.Pass is_failure=true since the transaction was never delivered to the target, matching the semantics of all other undelivered-transaction cleanup paths.
Fixes 9864bb480133 ("binder: add TF_UPDATE_TXN to replace outdated txn")Signed-off-by Tomer Pomeranc <[email protected]>--- drivers/android/binder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/android/binder.c +1
-1@@ -2930,7 +2930,7 @@ static int binder_proc_transaction(struct binder_transaction *t, t_outdated->buffer =NULL ; buffer->transaction =NULL ;trace_binder_transaction_update_buffer_release (buffer); -binder_release_entire_buffer (proc,NULL , buffer, false); +binder_release_entire_buffer (proc,NULL , buffer, true);binder_alloc_free_buf (&proc->alloc, buffer);binder_free_txn_fixups (t_outdated);kfree (t_outdated);Signature · 1 line
2.34.1
Thanks,
Carlos Llamas
maintainerQuoted 3 lines · Tomer Pomeranc
Two bugs in the
t_outdatedcleanup path ofbinder_proc_transaction(), both introduced by commit9864bb480133("binder: addTF_UPDATE_TXNto replace outdated txn"):
I was never a fan of this TF_UPDATE_TXN flag. This is a kernel band-aid patch for a flow-control problem in userspace.
Quoted 5 lines
1.
kfree(t_outdated)is called withoutbinder_free_txn_fixups(), permanently leakingbinder_txn_fd_fixupentries and their fget()'d struct file references. The refcount never reaches zero; the leak survives process exit and accumulates until file-max exhaustion.
Yes.
Quoted 5 lines
2.
binder_release_entire_buffer()is called with is_failure=false for a transaction that was never delivered. Sincebinder_apply_fd_fixups()was never called, theBINDER_TYPE_FDAhandler reads stale buffer data as fd numbers and closes unrelated fds viabinder_deferred_fd_close().
Ha! good catch.
Quoted 4 lines
Confirmed on mainline Linux (6.8.0-124-generic,
binder_linuxmodule) and Android (Pixel 8, kernel 6.1.124,/dev/hwbinder).
Quoted 10 lines
Tomer Pomeranc (2):
binder: fix leaked fd fixups onTF_UPDATE_TXNsupersede
binder: fixis_failureflag for superseded transaction cleanupdrivers/android/binder.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Signature · 1 line
2.34.1
So both fixes LGTM, and we should take them.
However, I'm thinking we should drop this code. We now have frozen process notifications and that should prevent duplicate transactions to frozen processes from happening in the first place. So we remove the code and mark TF_UPDATE_TXN as obsolete.
... or maybe we just drop the entire C binder code base. I'm tired of all these memory issues.
--
Carlos Llamas