From: Link Mauve <linkmauve@linkmauve.fr>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Ke Sun" <sunke@kylinos.cn>, "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH RESEND v13 2/2] rust: fmt: route {:p} through HashedPtr to prevent address leaks
Date: Thu, 6 Aug 2026 16:24:35 +0200 [thread overview]
Message-ID: <anSZIy78YzTH_CAb@desktop> (raw)
In-Reply-To: <anLxCshIflMu-1zI@google.com>
On Wed, Aug 05, 2026 at 08:15:06AM +0000, Alice Ryhl wrote:
> On Mon, Jul 06, 2026 at 01:18:44PM +0800, Ke Sun wrote:
> > Define a custom `kernel::fmt::Pointer` trait and `HashedPtr` wrapper
> > so that `{:p}` formatting uses the kernel's `%p` hashed format instead
> > of printing raw pointer values, preventing kernel address space leaks.
> >
> > Signed-off-by: Ke Sun <sunke@kylinos.cn>
>
> Overall looks good to me, but one thing:
>
> > +impl<T: ?Sized> Pointer for HashedPtr<T> {
> > + fn fmt(&self, f: &mut Formatter<'_>) -> Result {
> > + use crate::str::CStrExt as _;
> > +
> > + let mut buf = [0u8; 32];
> > +
> > + // SAFETY: `buf` is a valid, writable buffer of 32 bytes, sufficient for all architectures
> > + // (max 19 bytes for 64-bit). The format string `c"0x%p"` is null-terminated and `%p`
> > + // matches the pointer argument.
> > + let len = unsafe {
> > + crate::bindings::scnprintf(
> > + buf.as_mut_ptr().cast(),
> > + buf.len(),
> > + // Rust's `{:p}` includes a "0x" prefix, the kernel's `%p` does not.
> > + c"0x%p".as_char_ptr(),
> > + self.0.cast::<core::ffi::c_void>(),
> > + )
> > + };
>
> When given a null pointer, this will print 0x(null), which seems a bit
> weird. It may also print 0x(ptrval) or 0x(____ptrval____) during early
> boot.
I’ve also seen a bunch of 0x(ptrval) during testing.
This series resolves a mystery where I thought I was crazy since
addresses of pointers and references were never what I thought they
were, and instead were always on the stack, thanks a lot for resolving
this!
Tested-by: Link Mauve <linkmauve@linkmauve.fr>
>
> It seems like it'd be nice to special-case these to provide better
> output in those cases.
>
> Alice
>
--
Link Mauve
next prev parent reply other threads:[~2026-08-06 14:24 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-06 5:18 [PATCH RESEND v13 0/2] rust: Add safe pointer formatting support Ke Sun
2026-07-06 5:18 ` [PATCH RESEND v13 1/2] rust: fmt: fix {:p} printing stack addresses Ke Sun
2026-08-05 8:15 ` Alice Ryhl
2026-08-06 14:14 ` Gary Guo
2026-07-06 5:18 ` [PATCH RESEND v13 2/2] rust: fmt: route {:p} through HashedPtr to prevent address leaks Ke Sun
2026-08-05 8:15 ` Alice Ryhl
2026-08-06 14:24 ` Link Mauve [this message]
2026-08-07 7:45 ` Alvin Sun
2026-08-07 8:01 ` Ke Sun
2026-08-06 14:41 ` Gary Guo
2026-08-07 7:32 ` Ke Sun
2026-08-05 8:16 ` [PATCH RESEND v13 0/2] rust: Add safe pointer formatting support Alice Ryhl
2026-08-05 12:30 ` Miguel Ojeda
2026-08-05 14:48 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=anSZIy78YzTH_CAb@desktop \
--to=linkmauve@linkmauve.fr \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sunke@kylinos.cn \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox