Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v3 0/4] rust: Add safe pointer formatting support
@ 2025-12-24  8:13 Ke Sun
  2025-12-24  8:13 ` [PATCH v3 1/4] lib/vsprintf: Export ptr_to_hashval for Rust use Ke Sun
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Ke Sun @ 2025-12-24  8:13 UTC (permalink / raw)
  To: Miguel Ojeda, Petr Mladek, Steven Rostedt, Timur Tabi,
	Danilo Krummrich, Benno Lossin
  Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Tamir Duberstein, rust-for-linux,
	Ke Sun

This patch series adds safe pointer formatting support for Rust kernel code,
providing three pointer wrapper types (HashedPtr, RestrictedPtr, RawPtr) that
correspond to C kernel's printk format specifiers %p, %pK, and %px.

The implementation ensures that raw pointers are automatically hashed when
formatted with {:p}, providing safe default behavior that prevents information
leaks about kernel memory layout. Users can also explicitly use wrapper types
when they need specific formatting behavior.

Changes in this series:

1. Export ptr_to_hashval() from lib/vsprintf.c to allow Rust code to hash
   pointers for safe printing.

2. Add three pointer wrapper types in rust/kernel/ptr.rs:
   - HashedPtr: Hashes pointers before printing (corresponds to %p)
   - RestrictedPtr: Respects kptr_restrict sysctl (corresponds to %pK)
   - RawPtr: Prints raw address for debugging (corresponds to %px)

3. Make raw pointers automatically use HashedPtr when formatted with {:p},
   providing safe default behavior while still allowing explicit use of
   wrapper types when needed.

4. Add documentation explaining the pointer wrapper types and their usage.

---
v2: https://lore.kernel.org/rust-for-linux/20251223033018.2814732-1-sunke@kylinos.cn/
- Disabled {:p} raw pointer printing by default to prevent accidental
  information leaks

v1: https://lore.kernel.org/rust-for-linux/20251218032709.2184890-1-sunke@kylinos.cn/
- Initial implementation that directly printed raw pointers
---

Ke Sun (4):
  lib/vsprintf: Export ptr_to_hashval for Rust use
  rust: kernel: Add pointer wrapper types for safe pointer formatting
  rust: fmt: Default raw pointer formatting to HashedPtr
  docs: rust: Add pointer formatting documentation

 Documentation/rust/index.rst              |   1 +
 Documentation/rust/pointer-formatting.rst |  67 ++++++++
 lib/vsprintf.c                            |   1 +
 rust/helpers/fmt.c                        |  61 +++++++
 rust/helpers/helpers.c                    |   3 +-
 rust/kernel/fmt.rs                        |  60 ++++++-
 rust/kernel/ptr.rs                        | 186 ++++++++++++++++++++++
 7 files changed, 377 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/rust/pointer-formatting.rst
 create mode 100644 rust/helpers/fmt.c

base-commit: 9448598b22c50c8a5bb77a9103e2d49f134c9578
-- 
2.43.0

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-12-25 15:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-24  8:13 [PATCH v3 0/4] rust: Add safe pointer formatting support Ke Sun
2025-12-24  8:13 ` [PATCH v3 1/4] lib/vsprintf: Export ptr_to_hashval for Rust use Ke Sun
2025-12-24  8:13 ` [PATCH v3 2/4] rust: kernel: Add pointer wrapper types for safe pointer formatting Ke Sun
2025-12-25  9:19   ` Dirk Behme
2025-12-25 15:22   ` kernel test robot
2025-12-24  8:13 ` [PATCH v3 3/4] rust: fmt: Default raw pointer formatting to HashedPtr Ke Sun
2025-12-24 20:58   ` kernel test robot
2025-12-25  2:50     ` Ke Sun
2025-12-24  8:13 ` [PATCH v3 4/4] docs: rust: Add pointer formatting documentation Ke Sun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox