mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust_binder: fix process leak on proc file creation failure
@ 2026-11-27 10:08 Adhyansh Verma
  2026-11-27 11:26 ` Alice Ryhl
  0 siblings, 1 reply; 2+ messages in thread
From: Adhyansh Verma @ 2026-11-27 10:08 UTC (permalink / raw)
  To: Alice Ryhl, Greg Kroah-Hartman, Carlos Llamas
  Cc: Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	linux-kernel, Adhyansh Verma

In rust_binder_open(), Process::open() allocates and initializes a new
Process struct, registering it into the binder context's process list
(Context::manager.all_procs) via Context::register_process(). Because
Context holds an Arc<Process> in all_procs, and Process holds an
Arc<Context>, a cyclic reference is established.

Immediately following Process::open(), rust_binder_open() attempts to
create the proc file via BinderfsProcFile::new(). If this call fails
(e.g. under memory pressure when allocating the proc file dentry or
inode), rust_binder_open() returns err.to_errno() immediately.

Because open() returned an error, VFS drops the struct file and will
never invoke the ->release() file operation (rust_binder_release()).
Consequently, Process::deferred_release() is never scheduled, and
Context::deregister_process() is never called to remove the Process
from all_procs.

As a result, the cyclic reference prevents the Process from being
dropped, permanently leaking the Process struct, credentials, and task
references in kernel memory, while leaving a stale entry in all_procs.

Fix this by explicitly calling process.ctx.deregister_process(&process)
on the BinderfsProcFile::new() failure path to break the reference
cycle and allow the Process to be freed when exiting rust_binder_open().

Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Adhyansh Verma <adhyanshverma2010@gmail.com>
---
 drivers/android/binder/rust_binder_main.rs | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/android/binder/rust_binder_main.rs b/drivers/android/binder/rust_binder_main.rs
index 56c78647523..7ca70dae659 100644
--- a/drivers/android/binder/rust_binder_main.rs
+++ b/drivers/android/binder/rust_binder_main.rs
@@ -401,7 +401,10 @@ unsafe impl<T> Sync for AssertSync<T> {}
     match unsafe { BinderfsProcFile::new(inode, process.task.pid()) } {
         Ok(Some(file)) => process.inner.lock().binderfs_file = Some(file),
         Ok(None) => { /* pid already exists */ }
-        Err(err) => return err.to_errno(),
+        Err(err) => {
+            process.ctx.deregister_process(&process);
+            return err.to_errno();
+        }
     }
 
     // SAFETY: This file is associated with Rust binder, so we own the `private_data` field.
-- 
2.56.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust_binder: fix process leak on proc file creation failure
  2026-11-27 10:08 [PATCH] rust_binder: fix process leak on proc file creation failure Adhyansh Verma
@ 2026-11-27 11:26 ` Alice Ryhl
  0 siblings, 0 replies; 2+ messages in thread
From: Alice Ryhl @ 2026-11-27 11:26 UTC (permalink / raw)
  To: Adhyansh Verma
  Cc: Alice Ryhl, Greg Kroah-Hartman, Carlos Llamas,
	Arve Hjønnevåg, Todd Kjos, Christian Brauner,
	linux-kernel

On Fri, Nov 27, 2026 at 03:38:46PM +0530, Adhyansh Verma wrote:
> In rust_binder_open(), Process::open() allocates and initializes a new
> Process struct, registering it into the binder context's process list
> (Context::manager.all_procs) via Context::register_process(). Because
> Context holds an Arc<Process> in all_procs, and Process holds an
> Arc<Context>, a cyclic reference is established.
>
> Immediately following Process::open(), rust_binder_open() attempts to
> create the proc file via BinderfsProcFile::new(). If this call fails
> (e.g. under memory pressure when allocating the proc file dentry or
> inode), rust_binder_open() returns err.to_errno() immediately.
>
> Because open() returned an error, VFS drops the struct file and will
> never invoke the ->release() file operation (rust_binder_release()).
> Consequently, Process::deferred_release() is never scheduled, and
> Context::deregister_process() is never called to remove the Process
> from all_procs.
>
> As a result, the cyclic reference prevents the Process from being
> dropped, permanently leaking the Process struct, credentials, and task
> references in kernel memory, while leaving a stale entry in all_procs.
>
> Fix this by explicitly calling process.ctx.deregister_process(&process)
> on the BinderfsProcFile::new() failure path to break the reference
> cycle and allow the Process to be freed when exiting rust_binder_open().
>
> Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
> Signed-off-by: Adhyansh Verma <adhyanshverma2010@gmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>

Thanks!

Alice

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27 11:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 10:08 [PATCH] rust_binder: fix process leak on proc file creation failure Adhyansh Verma
2026-11-27 11:26 ` Alice Ryhl

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®