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 1/2] binder: defer fd_install() until after copy_to_user() in binder_thread_read()
Date: Thu, 24 Sep 2026 10:00:18 +0000	[thread overview]
Message-ID: <20260924100019.3389555-2-benquike@gmail.com> (raw)
In-Reply-To: <20260919213650.3316812-1-benquike@gmail.com>

In binder_thread_read(), binder_apply_fd_fixups() installs translated file
descriptors into current->files via fd_install() BEFORE put_user() and
copy_to_user() copy the BR_TRANSACTION / BR_REPLY command to userspace.

If put_user() or copy_to_user() returns -EFAULT, any file descriptors in
t->fd_fixups have already been installed into the recipient process's file
descriptor table and removed from t->fd_fixups, even though the transaction
failed and the sender receives BR_FAILED_REPLY.

Split fd_install() out of binder_apply_fd_fixups() into
binder_fd_fixups_install(t) and invoke it only after put_user() and
copy_to_user() succeed.

Tested in QEMU against Linux 7.3.0-rc3 using a multi-process C reproducer:
a sender thread transmits a pipe write-end FD via BINDER_TYPE_FD in a
one-way transaction to a recipient thread whose read buffer spans an
unmapped virtual memory page, triggering -EFAULT in copy_to_user(): on the
unfixed kernel, the pipe write-end FD was prematurely installed into
current->files (polling the read-end did NOT produce POLLHUP); whereas
with this fix applied, fd_install() is deferred past copy_to_user(), so
the FD is not installed on -EFAULT (polling the read-end produces POLLHUP).

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 1/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 | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/drivers/android/binder.c b/drivers/android/binder.c
index fb9ff072bd6c..ee01e6a06631 100644
--- a/drivers/android/binder.c
+++ b/drivers/android/binder.c
@@ -4705,7 +4705,7 @@ static int binder_wait_for_work(struct binder_thread *thread,
 static int binder_apply_fd_fixups(struct binder_proc *proc,
 				  struct binder_transaction *t)
 {
-	struct binder_txn_fd_fixup *fixup, *tmp;
+	struct binder_txn_fd_fixup *fixup;
 	int ret = 0;
 
 	list_for_each_entry(fixup, &t->fd_fixups, fixup_entry) {
@@ -4730,19 +4730,25 @@ static int binder_apply_fd_fixups(struct binder_proc *proc,
 			goto err;
 		}
 	}
-	list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
-		fd_install(fixup->target_fd, fixup->file);
-		list_del(&fixup->fixup_entry);
-		kfree(fixup);
-	}
 
-	return ret;
+	return 0;
 
 err:
 	binder_free_txn_fixups(t);
 	return ret;
 }
 
+static void binder_fd_fixups_install(struct binder_transaction *t)
+{
+	struct binder_txn_fd_fixup *fixup, *tmp;
+
+	list_for_each_entry_safe(fixup, tmp, &t->fd_fixups, fixup_entry) {
+		fd_install(fixup->target_fd, fixup->file);
+		list_del(&fixup->fixup_entry);
+		kfree(fixup);
+	}
+}
+
 static int binder_thread_read(struct binder_proc *proc,
 			      struct binder_thread *thread,
 			      binder_uintptr_t binder_buffer, size_t size,
@@ -5141,6 +5147,8 @@ static int binder_thread_read(struct binder_proc *proc,
 		}
 		ptr += trsize;
 
+		binder_fd_fixups_install(t);
+
 		trace_binder_transaction_received(t);
 		binder_stat_br(proc, thread, cmd);
 		binder_debug(BINDER_DEBUG_TRANSACTION,

  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 on read -EFAULT 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 ` Hui Peng [this message]
2026-09-24 10:00 ` [PATCH v2 2/2] binder: free transaction buffer via binder_free_buf() " Hui Peng

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-2-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®