mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hui Peng <benquike@gmail.com>
To: gregkh@linuxfoundation.org, arve@android.com, tkjos@android.com,
	maco@android.com, brauner@kernel.org, cmllamas@google.com
Cc: maco@google.com, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, Hui Peng <benquike@gmail.com>
Subject: [PATCH v2 2/2] binder: free transaction buffer via binder_free_buf() on read -EFAULT
Date: Thu, 24 Sep 2026 10:00:19 +0000	[thread overview]
Message-ID: <20260924100019.3389555-3-benquike@gmail.com> (raw)
In-Reply-To: <20260919213650.3316812-1-benquike@gmail.com>

In binder_thread_read(), if put_user() or copy_to_user() returns -EFAULT,
the failure paths call binder_cleanup_transaction() without setting
t->buffer->transaction = NULL or calling binder_free_buf(proc, thread,
buffer, true).

Because allow_user_free remains 0, the binder_buffer and all translated
binder_node / binder_ref references inside it are permanently leaked. For
oneway transactions (TF_ONE_WAY), node->has_async_transaction remains true
forever, wedging all subsequent async transactions to that node.

Fix this by setting buffer->transaction = NULL and calling
binder_free_buf(proc, thread, buffer, true) on put_user() / copy_to_user()
-EFAULT errors in binder_thread_read().

Tested in QEMU against Linux 7.3.0-rc3 using a multi-process C reproducer:
triggering copy_to_user() failure on a one-way transaction: on the unfixed
kernel, binder_cleanup_transaction() leaked the transaction buffer and
left node->has_async_transaction set to true (wedging subsequent async
transactions); whereas with this fix applied, binder_free_buf() is called
on -EFAULT, clearing buffer->transaction and freeing the allocation cleanly.

Fixes: 44d8047f1d87 ("binder: use standard functions to allocate fds")
Cc: stable@vger.kernel.org
Reviewed-by: Carlos Llamas <cmllamas@google.com>
Assisted-by: LLM
Signed-off-by: Hui Peng <benquike@gmail.com>
---
Changes in v2:
- Split out as patch 2/2 as requested by Greg Kroah-Hartman and Carlos
  Llamas.
- Added QEMU test procedure details in commit message body.
- Added Reviewed-by tag from Carlos Llamas.

 drivers/android/binder.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/drivers/android/binder.c b/drivers/android/binder.c
index ee01e6a06631..efff59853752 100644
--- a/drivers/android/binder.c
+++ b/drivers/android/binder.c
@@ -5127,24 +5128,32 @@ static int binder_thread_read(struct binder_proc *proc,
 			trsize = sizeof(tr);
 		}
 		if (put_user(cmd, (uint32_t __user *)ptr)) {
+			struct binder_buffer *buffer = t->buffer;
+
 			if (t_from)
 				binder_thread_dec_tmpref(t_from);
 
+			buffer->transaction = NULL;
 			binder_cleanup_transaction(t, "put_user failed",
 						   BR_FAILED_REPLY);
+			binder_free_buf(proc, thread, buffer, true);
 
 			return -EFAULT;
 		}
 		ptr += sizeof(uint32_t);
 		if (copy_to_user(ptr, &tr, trsize)) {
+			struct binder_buffer *buffer = t->buffer;
+
 			if (t_from)
 				binder_thread_dec_tmpref(t_from);
 
+			buffer->transaction = NULL;
 			binder_cleanup_transaction(t, "copy_to_user failed",
 						   BR_FAILED_REPLY);
+			binder_free_buf(proc, thread, buffer, true);
 
 			return -EFAULT;
 		}
 		ptr += trsize;

      parent reply	other threads:[~2026-09-24 10:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-19 21:36 [PATCH] binder: fix buffer/node leak and premature fd_install " Hui Peng
2026-09-23 22:54 ` Carlos Llamas
2026-09-24 10:00 ` [PATCH v2 0/2] binder: fix premature fd_install() and buffer leak " Hui Peng
2026-09-24 10:00 ` [PATCH v2 1/2] binder: defer fd_install() until after copy_to_user() in binder_thread_read() Hui Peng
2026-09-24 10:00 ` Hui Peng [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260924100019.3389555-3-benquike@gmail.com \
    --to=benquike@gmail.com \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=maco@google.com \
    --cc=stable@vger.kernel.org \
    --cc=tkjos@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®