* [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs
@ 2026-11-26 9:53 Alice Ryhl
2026-11-26 9:53 ` [PATCH 1/5] rust_binder: read u64 pointer in BC_FREE_BUFFER Alice Ryhl
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
This series fixes several bugs that only affect 32-bit platforms.
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Alice Ryhl (5):
rust_binder: read u64 pointer in BC_FREE_BUFFER
rust_binder: clamp write_read buffer sizes to usize::MAX
rust_binder: reject transaction sizes exceeding usize::MAX
rust_binder: align scatter-gather buffers to u64
rust_binder: use u64 for node and handle reference counts
drivers/android/binder/node.rs | 22 +++++++++++-----------
drivers/android/binder/process.rs | 7 ++++---
drivers/android/binder/rust_binder_main.rs | 5 +++++
drivers/android/binder/thread.rs | 26 +++++++++++++++-----------
4 files changed, 35 insertions(+), 25 deletions(-)
---
base-commit: 917fcbf033154a6c8d83cfa6c88f012b71eb37b2
change-id: 20261125-binder-32bit-fixes-8545e4a3066a
Best regards,
--
Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/5] rust_binder: read u64 pointer in BC_FREE_BUFFER
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
@ 2026-11-26 9:53 ` Alice Ryhl
2026-11-26 9:53 ` [PATCH 2/5] rust_binder: clamp write_read buffer sizes to usize::MAX Alice Ryhl
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
On 32-bit kernels, BC_FREE_BUFFER reads a usize instead of a 64-bit
binder_uintptr_t from userspace. This desynchronizes the command stream
and misinterprets the remaining bytes, causing subsequent commands to
fail or leaking the buffer. Thus, read a u64 pointer on all platforms.
To match legacy C Binder behavior, out of range pointer values are
ignored, rather than reported as an error.
Cc: stable@vger.kernel.org
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
drivers/android/binder/process.rs | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/android/binder/process.rs b/drivers/android/binder/process.rs
index e97213250beb..f23dbe98d688 100644
--- a/drivers/android/binder/process.rs
+++ b/drivers/android/binder/process.rs
@@ -1125,7 +1125,8 @@ pub(crate) fn buffer_alloc(
Ok(NewAllocation(res))
}
- pub(crate) fn buffer_get(self: &Arc<Self>, ptr: usize) -> Option<Allocation> {
+ pub(crate) fn buffer_get(self: &Arc<Self>, ptr: u64) -> Option<Allocation> {
+ let ptr = usize::try_from(ptr).ok()?;
let mut inner = self.inner.lock();
let mapping = inner.mapping.as_mut()?;
let offset = ptr.checked_sub(mapping.address)?;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/5] rust_binder: clamp write_read buffer sizes to usize::MAX
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
2026-11-26 9:53 ` [PATCH 1/5] rust_binder: read u64 pointer in BC_FREE_BUFFER Alice Ryhl
@ 2026-11-26 9:53 ` Alice Ryhl
2026-11-26 9:53 ` [PATCH 3/5] rust_binder: reject transaction sizes exceeding usize::MAX Alice Ryhl
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
On 32-bit kernels, casting `BinderWriteRead` buffer sizes to `usize`
silently truncates lengths of 4 GiB or more modulo 2^32. Moreover,
`Thread::read()` subtracts the remaining writer length from the
untruncated 64-bit length, inflating `read_consumed` by multiples of
4 GiB. Thus, clamp these buffer lengths to `usize::MAX` and compute
`read_consumed` from the clamped length.
Cc: stable@vger.kernel.org
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
drivers/android/binder/thread.rs | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs
index 54a208caebe1..8357313875d5 100644
--- a/drivers/android/binder/thread.rs
+++ b/drivers/android/binder/thread.rs
@@ -1520,9 +1520,10 @@ fn oneway_transaction_inner(self: &Arc<Self>, info: &mut TransactionInfo) -> Bin
fn write(self: &Arc<Self>, req: &mut BinderWriteRead) -> Result {
let write_start = req.write_buffer.wrapping_add(req.write_consumed);
- let write_len = req.write_size.saturating_sub(req.write_consumed);
+ let write_len = usize::try_from(req.write_size.saturating_sub(req.write_consumed))
+ .unwrap_or(usize::MAX);
let mut reader =
- UserSlice::new(UserPtr::from_addr(write_start as usize), write_len as usize).reader();
+ UserSlice::new(UserPtr::from_addr(write_start as usize), write_len).reader();
while reader.len() >= size_of::<u32>() && self.inner.lock().return_work.is_unused() {
let before = reader.len();
@@ -1592,9 +1593,10 @@ fn write(self: &Arc<Self>, req: &mut BinderWriteRead) -> Result {
fn read(self: &Arc<Self>, req: &mut BinderWriteRead, wait: bool) -> Result {
let read_start = req.read_buffer.wrapping_add(req.read_consumed);
- let read_len = req.read_size.saturating_sub(req.read_consumed);
+ let read_len =
+ usize::try_from(req.read_size.saturating_sub(req.read_consumed)).unwrap_or(usize::MAX);
let mut writer = BinderReturnWriter::new(
- UserSlice::new(UserPtr::from_addr(read_start as usize), read_len as usize).writer(),
+ UserSlice::new(UserPtr::from_addr(read_start as usize), read_len).writer(),
self,
);
let (in_pool, has_transaction, thread_todo, use_proc_queue) = {
@@ -1654,13 +1656,13 @@ fn read(self: &Arc<Self>, req: &mut BinderWriteRead, wait: bool) -> Result {
}
}
- req.read_consumed += read_len - writer.len() as u64;
+ req.read_consumed += (read_len - writer.len()) as u64;
// Write BR_SPAWN_LOOPER if the process needs more threads for its pool.
if has_noop_placeholder && in_pool && self.process.needs_thread() {
let mut writer = UserSlice::new(
UserPtr::from_addr(req.read_buffer as usize),
- req.read_size as usize,
+ usize::try_from(req.read_size).unwrap_or(usize::MAX),
)
.writer();
writer.write(&BR_SPAWN_LOOPER)?;
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 3/5] rust_binder: reject transaction sizes exceeding usize::MAX
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
2026-11-26 9:53 ` [PATCH 1/5] rust_binder: read u64 pointer in BC_FREE_BUFFER Alice Ryhl
2026-11-26 9:53 ` [PATCH 2/5] rust_binder: clamp write_read buffer sizes to usize::MAX Alice Ryhl
@ 2026-11-26 9:53 ` Alice Ryhl
2026-11-26 9:53 ` [PATCH 4/5] rust_binder: align scatter-gather buffers to u64 Alice Ryhl
2026-11-26 9:53 ` [PATCH 5/5] rust_binder: use u64 for node and handle reference counts Alice Ryhl
4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
On 32-bit kernels, casting `data_size`, `offsets_size`, and
`buffers_size` from u64 to usize when reading transaction metadata
silently truncates values exceeding `usize::MAX` instead of failing as
on 64-bit kernels. Note that C Binder also silently truncated these
u64 sizes to size_t on 32-bit kernels.
Thus, saturate values exceeding `usize::MAX` to `usize::MAX`, which
`copy_transaction_data()` rejects with `EINVAL`.
Cc: stable@vger.kernel.org
Fixes: 5326a18e3e64 ("rust_binder: introduce TransactionInfo")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
drivers/android/binder/thread.rs | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs
index 8357313875d5..23367dfbf393 100644
--- a/drivers/android/binder/thread.rs
+++ b/drivers/android/binder/thread.rs
@@ -1310,10 +1310,12 @@ fn read_transaction_info(
info.code = td.transaction_data.code;
info.flags = TransactionFlags::from_bits(td.transaction_data.flags);
info.data_ptr = UserPtr::from_addr(trd_data_ptr.buffer as usize);
- info.data_size = td.transaction_data.data_size as usize;
+ // Saturate sizes exceeding `usize::MAX` to `usize::MAX`, which is
+ // guaranteed to be rejected as `EINVAL` in `copy_transaction_data`.
+ info.data_size = usize::try_from(td.transaction_data.data_size).unwrap_or(usize::MAX);
info.offsets_ptr = UserPtr::from_addr(trd_data_ptr.offsets as usize);
- info.offsets_size = td.transaction_data.offsets_size as usize;
- info.buffers_size = td.buffers_size as usize;
+ info.offsets_size = usize::try_from(td.transaction_data.offsets_size).unwrap_or(usize::MAX);
+ info.buffers_size = usize::try_from(td.buffers_size).unwrap_or(usize::MAX);
// SAFETY: Above `read` call initializes all bytes, so this union read is ok.
info.target_handle = unsafe { td.transaction_data.target.handle };
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/5] rust_binder: align scatter-gather buffers to u64
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
` (2 preceding siblings ...)
2026-11-26 9:53 ` [PATCH 3/5] rust_binder: reject transaction sizes exceeding usize::MAX Alice Ryhl
@ 2026-11-26 9:53 ` Alice Ryhl
2026-11-26 9:53 ` [PATCH 5/5] rust_binder: use u64 for node and handle reference counts Alice Ryhl
4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
Currently, claim_next() aligns scatter-gather buffer lengths using
ptr_align(), which is 4 bytes on 32-bit systems. However, when userspace
computes the buffers_size, it assumes the kernel will always use 8-byte
alignment. Thus, to align with userspace and C Binder on 32-bit
platforms, update this code to use 64-bit alignment always.
Cc: stable@vger.kernel.org
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
drivers/android/binder/rust_binder_main.rs | 5 +++++
drivers/android/binder/thread.rs | 4 ++--
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/drivers/android/binder/rust_binder_main.rs b/drivers/android/binder/rust_binder_main.rs
index 9070c1c46a5f..c143ca34b813 100644
--- a/drivers/android/binder/rust_binder_main.rs
+++ b/drivers/android/binder/rust_binder_main.rs
@@ -300,6 +300,11 @@ fn ptr_align(value: usize) -> Option<usize> {
Some(value.checked_add(size)? & !size)
}
+fn u64_align(value: usize) -> Option<usize> {
+ let size = size_of::<u64>() - 1;
+ Some(value.checked_add(size)? & !size)
+}
+
// SAFETY: We call register in `init`.
static BINDER_SHRINKER: Shrinker = unsafe { Shrinker::new() };
diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs
index 23367dfbf393..a36514e0760c 100644
--- a/drivers/android/binder/thread.rs
+++ b/drivers/android/binder/thread.rs
@@ -32,7 +32,7 @@
stats::GLOBAL_STATS,
trace::{trace_transaction_alloc_buf, trace_transaction_buffer_release},
transaction::{Transaction, TransactionFlag, TransactionFlags, TransactionInfo},
- BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverCode, DeliverToRead,
+ u64_align, BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverCode, DeliverToRead,
};
use core::mem::size_of;
@@ -213,7 +213,7 @@ impl UnusedBufferSpace {
/// into the buffer is returned.
fn claim_next(&mut self, size: usize) -> Result<usize> {
// We require every chunk to be aligned.
- let size = ptr_align(size).ok_or(EINVAL)?;
+ let size = u64_align(size).ok_or(EINVAL)?;
let new_offset = self.offset.checked_add(size).ok_or(EINVAL)?;
if new_offset <= self.limit {
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 5/5] rust_binder: use u64 for node and handle reference counts
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
` (3 preceding siblings ...)
2026-11-26 9:53 ` [PATCH 4/5] rust_binder: align scatter-gather buffers to u64 Alice Ryhl
@ 2026-11-26 9:53 ` Alice Ryhl
4 siblings, 0 replies; 6+ messages in thread
From: Alice Ryhl @ 2026-11-26 9:53 UTC (permalink / raw)
To: Greg Kroah-Hartman, Carlos Llamas
Cc: Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel,
stable
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@vger.kernel.org
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-26 9:53 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 9:53 [PATCH 0/5] rust_binder: fix 32-bit truncation and alignment bugs Alice Ryhl
2026-11-26 9:53 ` [PATCH 1/5] rust_binder: read u64 pointer in BC_FREE_BUFFER Alice Ryhl
2026-11-26 9:53 ` [PATCH 2/5] rust_binder: clamp write_read buffer sizes to usize::MAX Alice Ryhl
2026-11-26 9:53 ` [PATCH 3/5] rust_binder: reject transaction sizes exceeding usize::MAX Alice Ryhl
2026-11-26 9:53 ` [PATCH 4/5] rust_binder: align scatter-gather buffers to u64 Alice Ryhl
2026-11-26 9:53 ` [PATCH 5/5] rust_binder: use u64 for node and handle reference counts 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®