[PATCH] rust_binder: only remove a node from the tree if it is still there
From: Nguyen Le Duy
Date: Sun Nov 22 2026 - 14:15:10 EST
Node::do_work_locked() removes a node without references from its
owner's tree with remove_node(self.ptr), i.e. by the userspace address
of the node. That is only correct if the tree entry at that address is
this node, which is not guaranteed.
A node can be queued twice: as the Node itself and through a NodeWrapper
(a strong zero-to-one increment made while the Node is already queued).
If all references are dropped before either work item is delivered and
the wrapper is delivered first, NodeWrapper::do_work() removes the node
from the tree while the Node work item is still queued. Userspace can
then create a new node at the same address. When the stale work item is
delivered, do_work_locked() sees a node without references again and
removes whatever is at that address: the new, live node.
The owner can then no longer look up its own node. BC_INCREFS_DONE and
BC_ACQUIRE_DONE for it are ignored, so BR_RELEASE and BR_DECREFS are
never sent, and the next BINDER_TYPE_BINDER object with that address
creates a second live node with the same (ptr, cookie). Freeing a buffer
that holds a binder object for the hidden node makes update_node(), a
lookup by address, decrement the other node. Its count drops below what
the handles to it hold, and the next NodeRef::absorb() on such a handle
hits assert_ne!(inner.strong.count, 0):
rust_kernel: panicked at drivers/android/binder/node.rs:822:17:
assertion `left != right` failed
left: 0
right: 0
kernel BUG at rust/helpers/bug.c:7!
Call Trace:
NodeRef::absorb
Process::insert_or_update_handle
AllocationView::transfer_binder_object
Thread::copy_transaction_data
Transaction::new
Thread::transaction
An unprivileged user can trigger this with ten ordinary binder commands
from one process; no race is involved. All that is needed is a binder
device, and with CONFIG_USER_NS=y any user gets one by mounting binderfs
in a user namespace. The calling thread dies in BUG() with binder locks
held (the task exits with preempt_count 2).
Pass the node to remove_node() and only remove the tree entry if it
still refers to that node. Node::global_id() is the address of the node
and both nodes are alive at that point, so a node that is still in the
tree is removed exactly as before, while the stale work item of a node
that has already left the tree no longer touches another node's entry.
The problem was found on v7.4-rc3 with the Kani model checker (0.68.0):
an invariant over the node state is not preserved by Node::do_work()
and NodeWrapper::do_work(). It was then reproduced on an unmodified
v7.4-rc3 kernel (x86_64, QEMU) with a C program that runs as uid 1000,
mounts binderfs in its own user namespace and issues the ten commands.
Build-tested on x86_64 (defconfig plus CONFIG_RUST=y,
CONFIG_ANDROID_BINDER_IPC_RUST=y and CONFIG_USER_NS=y, rustc 1.100.0):
no new warnings, also with CLIPPY=1, and rustfmtcheck is clean.
Run-time tested with two test programs only: with this patch the
reproducer runs to the end and the owner receives BR_RELEASE and
BR_DECREFS again.
The analysis, the reproducer and this patch were prepared with the help
of an AI assistant (Claude).
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Nguyen Le Duy <inguyen@xxxxxxxxxx>
---
The reproducer was sent to the maintainers separately; following
Documentation/process/security-bugs.rst it is not posted to the list.
The patch also applies, with line offsets, to linux-next (next-20261120)
and to v6.18, 6.18.64 and 7.3.5. On those three kernels the reproducer
hits the BUG without the patch and runs to the end with it.
drivers/android/binder/node.rs | 2 +-
drivers/android/binder/process.rs | 17 +++++++++++++++--
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/drivers/android/binder/node.rs b/drivers/android/binder/node.rs
index efdafc2..052b0e5 100644
--- a/drivers/android/binder/node.rs
+++ b/drivers/android/binder/node.rs
@@ -636,7 +636,7 @@ impl Node {
}
if no_active_inc_refs && !weak {
// Remove the node if there are no references to it.
- guard.remove_node(self.ptr);
+ guard.remove_node(self);
}
drop(guard);
diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs
index 1f4cb43..e5b3992 100644
--- a/drivers/android/binder/process.rs
+++ b/drivers/android/binder/process.rs
@@ -221,8 +221,21 @@ impl ProcessInner {
}
}
- pub(crate) fn remove_node(&mut self, ptr: u64) {
- self.nodes.remove(&ptr);
+ /// Removes `node` from the `nodes` tree.
+ ///
+ /// The tree is keyed by the userspace address of the node. A node that has lost all its
+ /// references can be removed while one of its work items is still queued; userspace may then
+ /// create a new node with the same address before that work item runs. Only remove the entry
+ /// if it still refers to `node`, so that a stale work item cannot remove the newer node.
+ pub(crate) fn remove_node(&mut self, node: &Node) {
+ let (ptr, _cookie) = node.get_id();
+ if self
+ .nodes
+ .get(&ptr)
+ .is_some_and(|cur| cur.global_id() == node.global_id())
+ {
+ self.nodes.remove(&ptr);
+ }
}
/// Updates the reference count on the given node.
base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd
--
2.56.1