rust_binder: add state_hashed and transactions_hashed files
Subsystems (2)
Carlos Llamas
maintainerPATCH 1/1 rust_binder: add state_hashed and transactions_hashed files
The 'state' and 'transactions' binderfs files print the userspace ptr and cookie of each node as raw values, which leaks the userspace memory layout and forces access to these files to be heavily restricted. Add hashed versions of these files, as done for the C driver in commit 57483a362741 ("binder: Create safe versions of binder log files"), so that access to them can be granted more broadly.
Pass a 'hashed' flag down the debug_print() chain and print the ptr and cookie through a new MaybeHashed helper, which either prints the raw value or hashes it like {:p} does. Everything else in the files remains unchanged:
$ grep "node 2:" /dev/binderfs/binder_logs/state* state: node 2: u000000003cf2c740 c0000000000000000 hs true ... state_hashed: node 2: u00000000abab9b6d c0000000000000000 hs true ...
---
Notes:
The output of the hashed values depends on this {:p} fix:
https://lore.kernel.org/all/[email protected]/
Without it, the patch still builds and works, but an odd "0x" prefix is added to the hashed values, e.g. "u0x00000000abab9b6d".
drivers/android/binder/freeze.rs | 2 +- drivers/android/binder/node.rs | 39 ++++++++++---- drivers/android/binder/node/wrapper.rs | 14 +++-- drivers/android/binder/process.rs | 14 +++-- drivers/android/binder/rust_binder_internal.h | 2 + drivers/android/binder/rust_binder_main.rs | 52 +++++++++++++++---- drivers/android/binder/rust_binderfs.c | 18 +++++++ drivers/android/binder/thread.rs | 11 ++-- drivers/android/binder/transaction.rs | 2 +- 9 files changed, 122 insertions(+), 32 deletions(-)
drivers/android/binder/freeze.rs +1 -1
@@ -153,7 +153,7 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , _hashed:bool ) ->Result {seq_print! (m,"{}has frozen binder\n" , prefix);Ok (()) }
drivers/android/binder/node.rs +30 -9
@@ -3,6 +3,7 @@// Copyright (C) 2025 Google LLC. use kernel::{ + fmt, list::{AtomicTracker ,List ,ListArc ,ListLinks ,TryNewListArc }, prelude::*, seq_file::SeqFile ,
@@ -213,6 +214,21 @@ impl ListItem<0> for DTRWrap<Transaction> { } } +/// Formats the userspace `ptr` or `cookie` of a [`Node`] in full, or hashed like `%p` if the +/// flag is set, as done for the `*_hashed` binder_logs files. +struct MaybeHashed (u64 ,bool ); + +impl fmt::Display for MaybeHashed { +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +let Self (value, hashed) = *self; +if hashed { + fmt::Pointer ::fmt (&ptr::without_provenance ::<()>(valueas usize ), f) + }else { +write! (f,"{value:016x}" ) + } + } +} +impl Node {pub (crate )fn new ( ptr: u64,
@@ -255,14 +271,19 @@ pub(crate) fn has_oneway_transaction(&self, owner_inner: &mut ProcessInner) -> b }#[inline(never)] -pub (crate )fn full_debug_print (&self, m: &SeqFile , owner_inner: &mut ProcessInner ) ->Result { +pub (crate )fn full_debug_print ( + &self, + m: &SeqFile , + owner_inner: &mut ProcessInner , + hashed:bool , + ) ->Result {let inner = self.inner.access_mut (owner_inner);seq_print! ( m, -" node {}: u{:016x} c{:016x} hs {} hw {} cs {} cw {}" , +" node {}: u{} c{} hs {} hw {} cs {} cw {}" , self.debug_id, - self.ptr, - self.cookie, +MaybeHashed (self.ptr, hashed), +MaybeHashed (self.cookie, hashed), inner.strong.has_count, inner.weak.has_count, inner.strong.count,
@@ -755,14 +776,14 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , hashed:bool ) ->Result {seq_print! ( m, -"{}node work {}: u{:016x} c{:016x}\n" , +"{}node work {}: u{} c{}\n" , prefix, self.debug_id, - self.ptr, - self.cookie, +MaybeHashed (self.ptr, hashed), +MaybeHashed (self.cookie, hashed), );Ok (()) }
@@ -1150,7 +1171,7 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , _hashed:bool ) ->Result {let inner = self.inner.lock ();let dead_binder = inner.dead && !inner.notification_done;
drivers/android/binder/node/wrapper.rs +9 -5
@@ -4,7 +4,11 @@use kernel::{list::ListArc , prelude::*, seq_file::SeqFile , seq_print, sync::UniqueArc }; -use crate ::{node::Node , thread::Thread ,BinderReturnWriter ,DArc ,DLArc ,DTRWrap ,DeliverToRead }; +use crate ::{ + node::{MaybeHashed ,Node }, + thread::Thread , +BinderReturnWriter ,DArc ,DLArc ,DTRWrap ,DeliverToRead , +};use core::mem::MaybeUninit ;
@@ -94,14 +98,14 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , hashed:bool ) ->Result {seq_print! ( m, -"{}node work {}: u{:016x} c{:016x}\n" , +"{}node work {}: u{} c{}\n" , prefix, self.node.debug_id, - self.node.ptr, - self.node.cookie, +MaybeHashed (self.node.ptr, hashed), +MaybeHashed (self.node.cookie, hashed), );Ok (()) }
drivers/android/binder/process.rs +10 -4
@@ -596,7 +596,13 @@ pub(crate) fn debug_print_stats(&self, m: &SeqFile, ctx: &Context) -> Result { }#[inline(never)] -pub (crate )fn debug_print (&self, m: &SeqFile , ctx: &Context , print_all:bool ) ->Result { +pub (crate )fn debug_print ( + &self, + m: &SeqFile , + ctx: &Context , + print_all:bool , + hashed:bool , + ) ->Result {seq_print! (m,"proc {}\n" , self.pid_in_current_ns ());seq_print! (m,"context {}\n" , &*ctx.name);
@@ -628,13 +634,13 @@ pub(crate) fn debug_print(&self, m: &SeqFile, ctx: &Context, print_all: bool) -> }for threadin all_threads { - thread.debug_print (m, print_all)?; + thread.debug_print (m, print_all, hashed)?; }let mut inner = self.inner.lock ();for nodein all_nodes {if print_all || node.has_oneway_transaction (&mut inner) { - node.full_debug_print (m, &mut inner)?; + node.full_debug_print (m, &mut inner, hashed)?; } }drop (inner);
@@ -659,7 +665,7 @@ pub(crate) fn debug_print(&self, m: &SeqFile, ctx: &Context, print_all: bool) ->let inner = self.inner.lock ();for workin &inner.work { - work.debug_print (m," " ," pending transaction " )?; + work.debug_print (m," " ," pending transaction " , hashed)?; }for _deathin &inner.delivered_deaths {seq_print! (m," has delivered dead binder\n" );
drivers/android/binder/rust_binder_internal.h +2 -0
@@ -43,7 +43,9 @@ struct binder_device {int rust_binder_stats_show (struct seq_file *m,void *unused);int rust_binder_state_show (struct seq_file *m,void *unused); +int rust_binder_state_hashed_show (struct seq_file *m,void *unused);int rust_binder_transactions_show (struct seq_file *m,void *unused); +int rust_binder_transactions_hashed_show (struct seq_file *m,void *unused);int rust_binder_transaction_log_show (struct seq_file *m,void *unused);int rust_binder_proc_show (struct seq_file *m,void *pid);
drivers/android/binder/rust_binder_main.rs +43 -9
104 lines not on this page · raw message
drivers/android/binder/rust_binderfs.c +18 -0
@@ -45,7 +45,9 @@DEFINE_SHOW_ATTRIBUTE (rust_binder_stats);DEFINE_SHOW_ATTRIBUTE (rust_binder_state); +DEFINE_SHOW_ATTRIBUTE (rust_binder_state_hashed);DEFINE_SHOW_ATTRIBUTE (rust_binder_transactions); +DEFINE_SHOW_ATTRIBUTE (rust_binder_transactions_hashed);DEFINE_SHOW_ATTRIBUTE (rust_binder_transaction_log);DEFINE_SHOW_ATTRIBUTE (rust_binder_proc);
@@ -605,6 +607,13 @@ static int init_binder_logs(struct super_block *sb) goto out; } + dentry =rust_binderfs_create_file (binder_logs_root_dir,"state_hashed" , + &rust_binder_state_hashed_fops,NULL ); +if (IS_ERR (dentry)) { + ret =PTR_ERR (dentry); + goto out; + } + dentry =rust_binderfs_create_file (binder_logs_root_dir,"transactions" , &rust_binder_transactions_fops,NULL );if (IS_ERR (dentry)) {
@@ -612,6 +621,15 @@ static int init_binder_logs(struct super_block *sb) goto out; } + dentry =rust_binderfs_create_file (binder_logs_root_dir, +"transactions_hashed" , + &rust_binder_transactions_hashed_fops, +NULL ); +if (IS_ERR (dentry)) { + ret =PTR_ERR (dentry); + goto out; + } + dentry = rust_binderfs_create_file(binder_logs_root_dir, "transaction_log", &rust_binder_transaction_log_fops,
drivers/android/binder/thread.rs +8 -3
@@ -482,7 +482,12 @@ pub(crate) fn new(id: i32, process: Arc<Process>) -> Result<Arc<Self>> { }#[inline(never)] -pub (crate )fn debug_print (self: &Arc <Self >, m: &SeqFile , print_all:bool ) ->Result { +pub (crate )fn debug_print ( + self: &Arc <Self >, + m: &SeqFile , + print_all:bool , + hashed:bool , + ) ->Result {let inner = self.inner.lock ();if print_all || inner.current_transaction.is_some () || !inner.work_list.is_empty () {
@@ -510,7 +515,7 @@ pub(crate) fn debug_print(self: &Arc<Self>, m: &SeqFile, print_all: bool) -> Res }for workin &inner.work_list { - work.debug_print (m," " ," pending transaction " )?; + work.debug_print (m," " ," pending transaction " , hashed)?; }Ok (()) }
@@ -1788,7 +1793,7 @@ fn should_sync_wakeup(&self) -> bool {false } -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , _hashed:bool ) ->Result {seq_print! ( m,"{}transaction error: {}\n" ,
drivers/android/binder/transaction.rs +1 -1
@@ -772,7 +772,7 @@ fn should_sync_wakeup(&self) -> bool { !self.flags.is_oneway () } -fn debug_print (&self, m: &SeqFile , _prefix: &str , tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , _prefix: &str , tprefix: &str , _hashed:bool ) ->Result { self.debug_print_inner (m, tprefix);Ok (()) }
base-commit: c26cc979e28f5193310e4379e14aa6417375c565
prerequisite-patch-id: 4aaa9c816eb12666d45b37e79fd4eb54d68399f8
Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
Greg Kroah-Hartman
maintainerQuoted 18 lines · Carlos Llamas
The 'state' and 'transactions' binderfs files print the userspace ptr and cookie of each node as raw values, which leaks the userspace memory layout and forces access to these files to be heavily restricted. Add hashed versions of these files, as done for the C driver in commit
57483a362741("binder: Create safe versions of binder log files"), so that access to them can be granted more broadly.Pass a 'hashed' flag down the
debug_print()chain and print the ptr and cookie through a new MaybeHashed helper, which either prints the raw value or hashes it like {:p} does. Everything else in the files remains unchanged:$ grep "node 2:" /dev/binderfs/binder_logs/state* state: node 2: u000000003cf2c740 c0000000000000000 hs true ... state_hashed: node 2: u00000000abab9b6d c0000000000000000 hs true ...Assisted-by LLMSigned-off-by Carlos Llamas <[email protected]>
Quoted 7 lines
---
Notes:
The output of the hashed values depends on this {:p} fix:
https://lore.kernel.org/all/[email protected]/
Without it, the patch still builds and works, but an odd "0x" prefix is added to the hashed values, e.g. "u0x00000000abab9b6d".
As that's not merged yet, I'll drop this for now and wait for you to resubmit this when that is merged properly?
thanks,
greg k-h
Carlos Llamas
maintainerPATCH v2 1/1 rust_binder: add state_hashed and transactions_hashed files
The 'state' and 'transactions' binderfs files print the userspace ptr and cookie of each node as raw values, which leaks the userspace memory layout and forces access to these files to be heavily restricted. Add hashed versions of these files, as done for the C driver in commit 57483a362741 ("binder: Create safe versions of binder log files"), so that access to them can be granted more broadly.
Pass a 'hashed' flag down the debug_print() chain and print the ptr and cookie through a new MaybeHashed helper, which either prints the raw value or hashes it like %p does. Everything else in the files remains unchanged:
$ grep "node 2:" /dev/binderfs/binder_logs/state* state: node 2: u000000003cf2c740 c0000000000000000 hs true ... state_hashed: node 2: u00000000abab9b6d c0000000000000000 hs true ...
---
v2: open-code the %p behavior in MaybeHashed: No "0x" prefix.
v1:
https://lore.kernel.org/all/[email protected]/
drivers/android/binder/freeze.rs | 2 +- drivers/android/binder/node.rs | 46 ++++++++++++---- drivers/android/binder/node/wrapper.rs | 14 +++-- drivers/android/binder/process.rs | 14 +++-- drivers/android/binder/rust_binder_internal.h | 2 + drivers/android/binder/rust_binder_main.rs | 52 +++++++++++++++---- drivers/android/binder/rust_binderfs.c | 18 +++++++ drivers/android/binder/thread.rs | 11 ++-- drivers/android/binder/transaction.rs | 2 +- 9 files changed, 129 insertions(+), 32 deletions(-)
drivers/android/binder/freeze.rs +1 -1
@@ -153,7 +153,7 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , _hashed:bool ) ->Result {seq_print! (m,"{}has frozen binder\n" , prefix);Ok (()) }
drivers/android/binder/node.rs +37 -9
84 lines not on this page · raw message
drivers/android/binder/node/wrapper.rs +9 -5
@@ -4,7 +4,11 @@use kernel::{list::ListArc , prelude::*, seq_file::SeqFile , seq_print, sync::UniqueArc }; -use crate ::{node::Node , thread::Thread ,BinderReturnWriter ,DArc ,DLArc ,DTRWrap ,DeliverToRead }; +use crate ::{ + node::{MaybeHashed ,Node }, + thread::Thread , +BinderReturnWriter ,DArc ,DLArc ,DTRWrap ,DeliverToRead , +};use core::mem::MaybeUninit ;
@@ -94,14 +98,14 @@ fn should_sync_wakeup(&self) -> bool { }#[inline(never)] -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , hashed:bool ) ->Result {seq_print! ( m, -"{}node work {}: u{:016x} c{:016x}\n" , +"{}node work {}: u{} c{}\n" , prefix, self.node.debug_id, - self.node.ptr, - self.node.cookie, +MaybeHashed (self.node.ptr, hashed), +MaybeHashed (self.node.cookie, hashed), );Ok (()) }
drivers/android/binder/process.rs +10 -4
@@ -596,7 +596,13 @@ pub(crate) fn debug_print_stats(&self, m: &SeqFile, ctx: &Context) -> Result { }#[inline(never)] -pub (crate )fn debug_print (&self, m: &SeqFile , ctx: &Context , print_all:bool ) ->Result { +pub (crate )fn debug_print ( + &self, + m: &SeqFile , + ctx: &Context , + print_all:bool , + hashed:bool , + ) ->Result {seq_print! (m,"proc {}\n" , self.pid_in_current_ns ());seq_print! (m,"context {}\n" , &*ctx.name);
@@ -628,13 +634,13 @@ pub(crate) fn debug_print(&self, m: &SeqFile, ctx: &Context, print_all: bool) -> }for threadin all_threads { - thread.debug_print (m, print_all)?; + thread.debug_print (m, print_all, hashed)?; }let mut inner = self.inner.lock ();for nodein all_nodes {if print_all || node.has_oneway_transaction (&mut inner) { - node.full_debug_print (m, &mut inner)?; + node.full_debug_print (m, &mut inner, hashed)?; } }drop (inner);
@@ -659,7 +665,7 @@ pub(crate) fn debug_print(&self, m: &SeqFile, ctx: &Context, print_all: bool) ->let inner = self.inner.lock ();for workin &inner.work { - work.debug_print (m," " ," pending transaction " )?; + work.debug_print (m," " ," pending transaction " , hashed)?; }for _deathin &inner.delivered_deaths {seq_print! (m," has delivered dead binder\n" );
drivers/android/binder/rust_binder_internal.h +2 -0
@@ -43,7 +43,9 @@ struct binder_device {int rust_binder_stats_show (struct seq_file *m,void *unused);int rust_binder_state_show (struct seq_file *m,void *unused); +int rust_binder_state_hashed_show (struct seq_file *m,void *unused);int rust_binder_transactions_show (struct seq_file *m,void *unused); +int rust_binder_transactions_hashed_show (struct seq_file *m,void *unused);int rust_binder_transaction_log_show (struct seq_file *m,void *unused);int rust_binder_proc_show (struct seq_file *m,void *pid);
drivers/android/binder/rust_binder_main.rs +43 -9
104 lines not on this page · raw message
drivers/android/binder/rust_binderfs.c +18 -0
@@ -45,7 +45,9 @@DEFINE_SHOW_ATTRIBUTE (rust_binder_stats);DEFINE_SHOW_ATTRIBUTE (rust_binder_state); +DEFINE_SHOW_ATTRIBUTE (rust_binder_state_hashed);DEFINE_SHOW_ATTRIBUTE (rust_binder_transactions); +DEFINE_SHOW_ATTRIBUTE (rust_binder_transactions_hashed);DEFINE_SHOW_ATTRIBUTE (rust_binder_transaction_log);DEFINE_SHOW_ATTRIBUTE (rust_binder_proc);
@@ -605,6 +607,13 @@ static int init_binder_logs(struct super_block *sb) goto out; } + dentry =rust_binderfs_create_file (binder_logs_root_dir,"state_hashed" , + &rust_binder_state_hashed_fops,NULL ); +if (IS_ERR (dentry)) { + ret =PTR_ERR (dentry); + goto out; + } + dentry =rust_binderfs_create_file (binder_logs_root_dir,"transactions" , &rust_binder_transactions_fops,NULL );if (IS_ERR (dentry)) {
@@ -612,6 +621,15 @@ static int init_binder_logs(struct super_block *sb) goto out; } + dentry =rust_binderfs_create_file (binder_logs_root_dir, +"transactions_hashed" , + &rust_binder_transactions_hashed_fops, +NULL ); +if (IS_ERR (dentry)) { + ret =PTR_ERR (dentry); + goto out; + } + dentry = rust_binderfs_create_file(binder_logs_root_dir, "transaction_log", &rust_binder_transaction_log_fops,
drivers/android/binder/thread.rs +8 -3
@@ -482,7 +482,12 @@ pub(crate) fn new(id: i32, process: Arc<Process>) -> Result<Arc<Self>> { }#[inline(never)] -pub (crate )fn debug_print (self: &Arc <Self >, m: &SeqFile , print_all:bool ) ->Result { +pub (crate )fn debug_print ( + self: &Arc <Self >, + m: &SeqFile , + print_all:bool , + hashed:bool , + ) ->Result {let inner = self.inner.lock ();if print_all || inner.current_transaction.is_some () || !inner.work_list.is_empty () {
@@ -510,7 +515,7 @@ pub(crate) fn debug_print(self: &Arc<Self>, m: &SeqFile, print_all: bool) -> Res }for workin &inner.work_list { - work.debug_print (m," " ," pending transaction " )?; + work.debug_print (m," " ," pending transaction " , hashed)?; }Ok (()) }
@@ -1792,7 +1797,7 @@ fn should_sync_wakeup(&self) -> bool {false } -fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , prefix: &str , _tprefix: &str , _hashed:bool ) ->Result {seq_print! ( m,"{}transaction error: {}\n" ,
drivers/android/binder/transaction.rs +1 -1
@@ -762,7 +762,7 @@ fn should_sync_wakeup(&self) -> bool { !self.flags.is_oneway () } -fn debug_print (&self, m: &SeqFile , _prefix: &str , tprefix: &str ) ->Result { +fn debug_print (&self, m: &SeqFile , _prefix: &str , tprefix: &str , _hashed:bool ) ->Result { self.debug_print_inner (m, tprefix);Ok (()) }
base-commit: 985955d8e6b2b15cfd65b7ac6893e3a5a90c5870
Signature · 1 line
2.56.0.212.g129ede20d9-goog
Quoted 12 lines · Carlos Llamas
The 'state' and 'transactions' binderfs files print the userspace ptr and cookie of each node as raw values, which leaks the userspace memory layout and forces access to these files to be heavily restricted. Add hashed versions of these files, as done for the C driver in commit
57483a362741("binder: Create safe versions of binder log files"), so that access to them can be granted more broadly.Pass a 'hashed' flag down the
debug_print()chain and print the ptr and cookie through a new MaybeHashed helper, which either prints the raw value or hashes it like %p does. Everything else in the files remains unchanged:
Quoted 6 lines
$ grep "node 2:" /dev/binderfs/binder_logs/state* state: node 2: u000000003cf2c740 c0000000000000000 hs true ... state_hashed: node 2: u00000000abab9b6d c0000000000000000 hs true ...Assisted-by LLMSigned-off-by Carlos Llamas <[email protected]>
Quoted 2 lines · Carlos Llamas
v2: open-code the %p behavior in MaybeHashed: No "0x" prefix.
Thanks! I think we may want to have an easier way to do this non-0x formatting later, centrally, even if not as the default (which I am ambivalent about -- I didn't hear more opinions on your other patch).
Cheers,
Miguel
Alice Ryhl
maintainerQuoted 7 lines · Miguel Ojeda
Quoted 2 lines · Carlos Llamas
v2: open-code the %p behavior in MaybeHashed: No "0x" prefix.
Thanks! I think we may want to have an easier way to do this non-
0xformatting later, centrally, even if not as the default (which I am ambivalent about -- I didn't hear more opinions on your other patch).
The version that open codes it is probably what we will cherry-pick to Android's kernel branches for now, but we can definitely look into how to best support non-0x prefixes in a better way.
Alice
Quoted 4 lines · Alice Ryhl
The version that open codes it is probably what we will cherry-pick to Android's kernel branches for now, but we can definitely look into how to best support non-0x prefixes in a better way.
I meant later to clean this up, not here, i.e. no need to delay this of course.
Cheers,
Miguel
Alice Ryhl
maintainerQuoted 8 lines · Miguel Ojeda
Quoted 4 lines · Alice Ryhl
The version that open codes it is probably what we will cherry-pick to Android's kernel branches for now, but we can definitely look into how to best support non-0x prefixes in a better way.
I meant later to clean this up, not here, i.e. no need to delay this of course.
If you're ok with landing this as-is and fixing it in a follow-up, then let's do that.