Skip to contentSkip to conversation

rust_binder: disallow binder fd transfers

PATCH Superseded Carlos Llamas 2 participants 3 messages Nov 17 – Nov 21

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Carlos Llamas

author maintainer
#0 lore raw

PATCH 1/1 rust_binder: disallow binder fd transfers

Transferring a binder device fd via BINDER_TYPE_FD or BINDER_TYPE_FDA has no valid use case and can create direct reference cycles that prevent Process::release() from running when a queued transaction is left unread. Moreover, a binder file is bound to the opening process's task, credentials, and mmap state, so transferring it to another process breaks binder's per-process ownership model.

Disallow binder device fd transfers by returning EINVAL in Allocation::info_add_fd().

---
 drivers/android/binder/allocation.rs | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/android/binder/allocation.rs +7 -0
index 927bf47d5d20..8cd4c2a4402a 100644
@@ -182,6 +182,13 @@ pub(crate) fn info_add_fd(
         buffer_offset: usize,
         close_on_free: bool,
     ) -> Result {
+        // SAFETY: `file` is valid; read `f_op` with `READ_ONCE` semantics.
+        let f_op = unsafe { (&raw const (*file.as_ptr()).f_op).read_volatile() };
+        if core::ptr::eq(f_op, &crate::rust_binder_fops.0) {
+            // Binder FD transfers are not allowed.
+            return Err(EINVAL);
+        }
+
         self.get_or_init_info().file_list.files_to_translate.push(
             FileEntry {
                 file,
Signature · 1 line
2.56.1.703.gf848ed807c-goog
maintainer
#1 lore raw

drivers/android/binder/allocation.rs pub(crate) fn info_add_fd( · line 190

Quoted 10 lines · Carlos Llamas
drivers/android/binder/allocation.rs +6 -0
@@ -182,6 +182,13 @@ pub(crate) fn info_add_fd(
         buffer_offset: usize,
         close_on_free: bool,
     ) -> Result {
+        // SAFETY: `file` is valid; read `f_op` with `READ_ONCE` semantics.
+        let f_op = unsafe { (&raw const (*file.as_ptr()).f_op).read_volatile() };
+        if core::ptr::eq(f_op, &crate::rust_binder_fops.0) {
+            // Binder FD transfers are not allowed.
+            return Err(EINVAL);
+        }

We have moved to using atomic_load instead of a volatile read when the C side uses READ_ONCE. For example:

	// SAFETY: `file` is valid, so `f_op` is valid and aligned, and any
	// concurrent writes are atomic under LKMM.
	let f_op = unsafe { atomic_load(&raw mut (*file.as_ptr()).f_op, Relaxed) };

however the f_op field isn't read/modified with READ/WRITE_ONCE on the C side, so I think we can use a bare access.

	let f_op = unsafe { (*file.as_ptr()).f_op };

Alice

author maintainer
#2 lore raw
Quoted 15 lines · Alice Ryhl

drivers/android/binder/allocation.rs pub(crate) fn info_add_fd( · line 190

Quoted 10 lines · Carlos Llamas
drivers/android/binder/allocation.rs +6 -0
@@ -182,6 +182,13 @@ pub(crate) fn info_add_fd(
         buffer_offset: usize,
         close_on_free: bool,
     ) -> Result {
+        // SAFETY: `file` is valid; read `f_op` with `READ_ONCE` semantics.
+        let f_op = unsafe { (&raw const (*file.as_ptr()).f_op).read_volatile() };
+        if core::ptr::eq(f_op, &crate::rust_binder_fops.0) {
+            // Binder FD transfers are not allowed.
+            return Err(EINVAL);
+        }

We have moved to using atomic_load instead of a volatile read when the C side uses READ_ONCE. For example:

Quoted 3 lines
	// SAFETY: `file` is valid, so `f_op` is valid and aligned, and any
	// concurrent writes are atomic under LKMM.
	let f_op = unsafe { atomic_load(&raw mut (*file.as_ptr()).f_op, Relaxed) };

I see, ok I'll switch to this for v2.

Quoted 5 lines

however the f_op field isn't read/modified with READ/WRITE_ONCE on the C side, so I think we can use a bare access.

	let f_op = unsafe { (*file.as_ptr()).f_op };

We don't but there are other fd's in other drivers that do. This was flagged by my local LLM review. It was in some tty driver and somewhere else that override the f_ops. It seems pretty far-fetched that a false rust_binder_fops would trigger from this, but at least it would protect against KCSAN reports and similar. So probably best to keep it.

--
Carlos Llamas