[PATCH v3 4/4] rust_binder: check current before closing fds
From: Alice Ryhl
Date: Tue Mar 24 2026 - 16:06:23 EST
This list gets populated once the transaction is delivered to the target
process, at which point it's not touched again except in BC_FREE_BUFFER
and process exit, so if the list has been populated then this code
should not run in the context of the wrong userspace process.
However, why tempt fate? The function itself can run in the context of
both the sender and receiver, and if someone can engineer a scenario
where it runs in the sender and this list is non-empty (or future Rust
Binder changes make such a scenario possible), then that'd be a problem
because we'd be closing random unrelated fds in the wrong process.
Note that on process exit, the == comparison may actually fail because
it's called from a kthread. The fd closing code is a no-op on kthreads,
so there is no actual behavior different though.
Suggested-by: Jann Horn <jannh@xxxxxxxxxx>
Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
drivers/android/binder/allocation.rs | 29 ++++++++++++++++-------------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/drivers/android/binder/allocation.rs b/drivers/android/binder/allocation.rs
index 7f65a9c3a0e5..691e4af5c1d0 100644
--- a/drivers/android/binder/allocation.rs
+++ b/drivers/android/binder/allocation.rs
@@ -260,19 +260,22 @@ fn drop(&mut self) {
}
}
- for &fd in &info.file_list.close_on_free {
- let closer = match DeferredFdCloser::new(GFP_KERNEL) {
- Ok(closer) => closer,
- Err(kernel::alloc::AllocError) => {
- // Ignore allocation failures.
- break;
- }
- };
-
- // Here, we ignore errors. The operation can fail if the fd is not valid, or if the
- // method is called from a kthread. However, this is always called from a syscall,
- // so the latter case cannot happen, and we don't care about the first case.
- let _ = closer.close_fd(fd);
+ if self.process.task == kernel::current!().group_leader() {
+ for &fd in &info.file_list.close_on_free {
+ let closer = match DeferredFdCloser::new(GFP_KERNEL) {
+ Ok(closer) => closer,
+ Err(kernel::alloc::AllocError) => {
+ // Ignore allocation failures.
+ break;
+ }
+ };
+
+ // Here, we ignore errors. The operation can fail if the fd is not valid, or if
+ // the method is called from a kthread. However, this is always called from a
+ // syscall, so the latter case cannot happen, and we don't care about the first
+ // case.
+ let _ = closer.close_fd(fd);
+ }
}
if info.clear_on_free {
--
2.53.0.1018.g2bb0e51243-goog