[PATCH 5/5] rust_binder: use u64 for node and handle reference counts

From: Alice Ryhl

Date: Thu Nov 26 2026 - 04:54:35 EST


On 32-bit platforms, node and handle reference counts currently use
usize, so an unprivileged process can overflow them using repeated
BC_ACQUIRE or BC_INCREFS commands. This triggers an overflow panic when
overflow checks are enabled, or wraps around and corrupts reference
counts when they are disabled. Thus, use u64 for node and handle
reference counts to prevent overflow, and saturate the UAPI counts
reported via BinderNodeInfoForRef at u32::MAX.

Cc: stable@xxxxxxxxxxxxxxx
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
drivers/android/binder/node.rs | 22 +++++++++++-----------
drivers/android/binder/process.rs | 4 ++--
2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/drivers/android/binder/node.rs b/drivers/android/binder/node.rs
index b27554aacc95..33547b8e71c5 100644
--- a/drivers/android/binder/node.rs
+++ b/drivers/android/binder/node.rs
@@ -129,7 +129,7 @@ fn did_push_strong_zero2one_wrapper(&mut self) {

struct CountState {
/// The reference count.
- count: usize,
+ count: u64,
/// Whether the process that owns this node thinks that we hold a refcount on it. (Note that
/// even if count is greater than one, we only increment it once in the owning process.)
has_count: bool,
@@ -409,7 +409,7 @@ pub(crate) fn update_refcount_locked(
self: &DArc<Node>,
inc: bool,
strong: bool,
- count: usize,
+ count: u64,
owner_inner: &mut ProcessInner,
) -> Option<DLArc<Node>> {
let is_dead = owner_inner.is_dead;
@@ -506,7 +506,7 @@ pub(crate) fn incr_refcount_allow_zero2one_with_wrapper(
}
}

- pub(crate) fn update_refcount(self: &DArc<Self>, inc: bool, count: usize, strong: bool) {
+ pub(crate) fn update_refcount(self: &DArc<Self>, inc: bool, count: u64, strong: bool) {
self.owner
.inner
.lock()
@@ -519,8 +519,8 @@ pub(crate) fn populate_counts(
guard: &Guard<'_, ProcessInner, SpinLockBackend>,
) {
let inner = self.inner.access(guard);
- out.strong_count = inner.strong.count as u32;
- out.weak_count = inner.weak.count as u32;
+ out.strong_count = u32::try_from(inner.strong.count).unwrap_or(u32::MAX);
+ out.weak_count = u32::try_from(inner.weak.count).unwrap_or(u32::MAX);
}

pub(crate) fn populate_debug_info(
@@ -808,15 +808,15 @@ fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str, hashed: bool) -
pub(crate) struct NodeRef {
pub(crate) node: DArc<Node>,
/// How many times does this NodeRef hold a refcount on the Node?
- strong_node_count: usize,
- weak_node_count: usize,
+ strong_node_count: u64,
+ weak_node_count: u64,
/// How many times does userspace hold a refcount on this NodeRef?
- strong_count: usize,
- weak_count: usize,
+ strong_count: u64,
+ weak_count: u64,
}

impl NodeRef {
- pub(crate) fn new(node: DArc<Node>, strong_count: usize, weak_count: usize) -> Self {
+ pub(crate) fn new(node: DArc<Node>, strong_count: u64, weak_count: u64) -> Self {
Self {
node,
strong_node_count: strong_count,
@@ -857,7 +857,7 @@ pub(crate) fn absorb(&mut self, mut other: Self) {
}
}

- pub(crate) fn get_count(&self) -> (usize, usize) {
+ pub(crate) fn get_count(&self) -> (u64, u64) {
(self.strong_count, self.weak_count)
}

diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs
index f23dbe98d688..719e794757a2 100644
--- a/drivers/android/binder/process.rs
+++ b/drivers/android/binder/process.rs
@@ -231,7 +231,7 @@ pub(crate) fn update_node_refcount(
node: &DArc<Node>,
inc: bool,
strong: bool,
- count: usize,
+ count: u64,
othread: Option<&Thread>,
) {
let push = node.update_refcount_locked(inc, strong, count, self);
@@ -579,7 +579,7 @@ pub(crate) fn debug_print_stats(&self, m: &SeqFile, ctx: &Context) -> Result {

{
let mut refs = self.node_refs.lock();
- let (mut count, mut weak, mut strong) = (0, 0, 0);
+ let (mut count, mut weak, mut strong) = (0usize, 0u64, 0u64);
for r in refs.by_handle.values_mut() {
let node_ref = r.node_ref();
let (nstrong, nweak) = node_ref.get_count();

--
2.56.1.839.g22a8290db9-goog