[PATCH 4/5] rust_binder: align scatter-gather buffers to u64
From: Alice Ryhl
Date: Thu Nov 26 2026 - 04:54:11 EST
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@xxxxxxxxxxxxxxx
Fixes: eafedbc7c050 ("rust_binder: add Rust Binder driver")
Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
---
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