1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
// SPDX-License-Identifier: GPL-2.0
//! Formatting utilities.
//!
//! This module is intended to be used in place of `core::fmt` in kernel code.
use kernel::prelude::*;
pub use core::fmt::{
Arguments,
Debug,
Error,
Formatter,
Result,
Write, //
};
/// Internal adapter used to route and allow implementations of formatting traits for foreign types.
///
/// It is inserted automatically by the [`fmt!`] macro and is not meant to be used directly.
///
/// [`fmt!`]: crate::prelude::fmt!
#[doc(hidden)]
pub struct Adapter<T>(pub T);
macro_rules! impl_fmt_adapter_forward {
($($trait:ident),* $(,)?) => {
$(
impl<T: $trait> $trait for Adapter<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let Self(t) = self;
$trait::fmt(t, f)
}
}
)*
};
}
use core::fmt::{
Binary,
LowerExp,
LowerHex,
Octal,
UpperExp,
UpperHex, //
};
use core::ptr::NonNull;
impl_fmt_adapter_forward!(Debug, LowerHex, UpperHex, Octal, Binary, LowerExp, UpperExp);
/// A copy of [`core::fmt::Pointer`] that allows implementing pointer formatting for foreign types.
///
/// Together with the [`Adapter`] type and [`fmt!`] macro, it enables raw pointer formatting to be
/// intercepted and routed to [`HashedPtr`] (kernel's `%p` hashed format), preventing kernel address
/// leaks.
///
/// [`fmt!`]: crate::prelude::fmt!
pub trait Pointer {
/// Same as [`core::fmt::Pointer::fmt`].
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
/// A wrapper for pointers that formats them using kernel's `%p` format specifier.
///
/// By default, `%p` prints a hashed representation of the pointer address to prevent kernel address
/// leaks. When the `no_hash_pointers` kernel command-line parameter is enabled, the real address is
/// printed instead (for debugging purposes).
pub struct HashedPtr<T: ?Sized>(pub *const T);
impl<T: ?Sized> Pointer for HashedPtr<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
use crate::str::CStrExt as _;
let mut buf = [0u8; 32];
// Use `%#0*p` for the `0x` prefix and zero-padding; `+2` compensates for
// the prefix counting toward the field width.
let default_width = (2 * size_of::<usize>() + 2) as c_int;
let width = match (f.sign_aware_zero_pad(), f.width()) {
(true, Some(w)) if w > 0 => w.min(buf.len() - 1) as c_int,
_ => default_width,
};
// SAFETY: `buf` is a valid, writable 32-byte buffer, sufficient for
// all architectures (max 19 bytes for 64-bit under the default width).
// The format string is null-terminated; `width` (c_int) and pointer
// match the `%*` and `%p` specifiers.
let len = unsafe {
crate::bindings::scnprintf(
buf.as_mut_ptr().cast(),
buf.len(),
c"%#0*p".as_char_ptr(),
width,
self.0.cast::<c_void>(),
)
};
// SAFETY: `%#0*p` produces only ASCII, which is valid UTF-8.
let s = unsafe { core::str::from_utf8_unchecked(&buf[..len as usize]) };
if f.sign_aware_zero_pad() {
// `scnprintf` already applied the width and zero-padding via `%#0*p`.
f.write_str(s)
} else {
f.pad(s)
}
}
}
// Raw pointers are formatted via `HashedPtr` (kernel `%p`: hashed by default, plain with
// `no_hash_pointers`).
impl<T: ?Sized> Pointer for *const T {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(&HashedPtr(*self), f)
}
}
impl<T: ?Sized> Pointer for *mut T {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(&HashedPtr(*self), f)
}
}
impl<T: ?Sized> Pointer for &T {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(&HashedPtr(*self), f)
}
}
impl<T: ?Sized> Pointer for &mut T {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(&HashedPtr(core::ptr::from_ref(*self)), f)
}
}
impl<T: ?Sized> Pointer for NonNull<T> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(&HashedPtr(self.as_ptr()), f)
}
}
// `Adapter<&T>` bridges our `Pointer` trait to `core::fmt::Pointer`
impl<T: Pointer> core::fmt::Pointer for Adapter<&T> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Pointer::fmt(self.0, f)
}
}
/// A copy of [`core::fmt::Display`] that allows us to implement it for foreign types.
///
/// Types should implement this trait rather than [`core::fmt::Display`]. Together with the
/// [`Adapter`] type and [`fmt!`] macro, it allows for formatting foreign types (e.g. types from
/// core) which do not implement [`core::fmt::Display`] directly.
///
/// [`fmt!`]: crate::prelude::fmt!
pub trait Display {
/// Same as [`core::fmt::Display::fmt`].
fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
impl<T: ?Sized + Display> Display for &T {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Display::fmt(*self, f)
}
}
impl<T: ?Sized + Display> core::fmt::Display for Adapter<&T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let Self(t) = self;
Display::fmt(t, f)
}
}
macro_rules! impl_display_forward {
($(
$( { $($generics:tt)* } )? $ty:ty $( { where $($where:tt)* } )?
),* $(,)?) => {
$(
impl$($($generics)*)? Display for $ty $(where $($where)*)? {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
core::fmt::Display::fmt(self, f)
}
}
)*
};
}
impl_display_forward!(
bool,
char,
core::panic::PanicInfo<'_>,
Arguments<'_>,
i128,
i16,
i32,
i64,
i8,
isize,
str,
u128,
u16,
u32,
u64,
u8,
usize,
{<T: ?Sized>} crate::sync::Arc<T> {where crate::sync::Arc<T>: core::fmt::Display},
{<T: ?Sized>} crate::sync::UniqueArc<T> {where crate::sync::UniqueArc<T>: core::fmt::Display},
);
#[macros::kunit_tests(rust_kernel_fmt)]
mod tests {
use crate::{
bindings,
prelude::fmt,
str::CString, //
};
#[cfg(CONFIG_64BIT)]
mod expected {
pub(super) const PTR_VALUE: usize = 0xffffffffdeadbeef;
pub(super) const PTR_VAL_NO_CRNG: &str = "(____ptrval____)";
pub(super) const HASHED_PREFIX: &str = "0x00000000";
pub(super) const RAW_POINTER: &str = "0xffffffffdeadbeef";
pub(super) const PADDED_RIGHT: &str = " 0xffffffffdeadbeef";
pub(super) const ZERO_PADDED: &str = "0x000000ffffffffdeadbeef";
pub(super) const HASHED_PADDED_RIGHT_PREFIX: &str = " ";
pub(super) const HASHED_ZERO_PADDED_PREFIX: &str = "0x00000000000000";
pub(super) const CLAMPED: &str = "0x0000000000000ffffffffdeadbeef";
}
#[cfg(not(CONFIG_64BIT))]
mod expected {
pub(super) const PTR_VALUE: usize = 0xdeadbeef;
pub(super) const PTR_VAL_NO_CRNG: &str = "(ptrval)";
pub(super) const HASHED_PREFIX: &str = "0x";
pub(super) const RAW_POINTER: &str = "0xdeadbeef";
pub(super) const PADDED_RIGHT: &str = " 0xdeadbeef";
pub(super) const ZERO_PADDED: &str = "0x00000000000000deadbeef";
pub(super) const HASHED_PADDED_RIGHT_PREFIX: &str = " ";
pub(super) const HASHED_ZERO_PADDED_PREFIX: &str = "0x00000000000000";
pub(super) const CLAMPED: &str = "0x0000000000000000000000deadbeef";
}
#[test]
fn test_ptr_formatting() -> core::result::Result<(), crate::error::Error> {
let ptr: *const u8 = core::ptr::without_provenance(expected::PTR_VALUE);
// SAFETY: `no_hash_pointers` is a global variable that is never concurrently modified —
// KUnit tests may run at boot (before `mark_readonly()`) or manually afterwards (when the
// variable is read-only). Reading is always safe.
let no_hash = unsafe { bindings::no_hash_pointers };
if no_hash {
let cstr = CString::try_from_fmt(fmt!("{:p}", ptr))?;
assert_eq!(cstr.to_str()?, expected::RAW_POINTER);
let cstr = CString::try_from_fmt(fmt!("{:>24p}", ptr))?;
assert_eq!(cstr.to_str()?, expected::PADDED_RIGHT);
let cstr = CString::try_from_fmt(fmt!("{:024p}", ptr))?;
assert_eq!(cstr.to_str()?, expected::ZERO_PADDED);
let cstr = CString::try_from_fmt(fmt!("{:0100p}", ptr))?;
assert_eq!(cstr.to_str()?, expected::CLAMPED);
} else {
let cstr = CString::try_from_fmt(fmt!("{:p}", ptr))?;
let formatted = cstr.to_str()?;
// If the RNG is not yet ready, `%p` falls back to a placeholder.
if formatted == expected::PTR_VAL_NO_CRNG {
return Ok(());
}
assert!(formatted.starts_with(expected::HASHED_PREFIX));
assert_ne!(formatted, expected::RAW_POINTER);
let cstr = CString::try_from_fmt(fmt!("{:>24p}", ptr))?;
assert!(cstr
.to_str()?
.starts_with(expected::HASHED_PADDED_RIGHT_PREFIX));
let cstr = CString::try_from_fmt(fmt!("{:024p}", ptr))?;
assert!(cstr
.to_str()?
.starts_with(expected::HASHED_ZERO_PADDED_PREFIX));
let cstr = CString::try_from_fmt(fmt!("{:0100p}", ptr))?;
let output = cstr.to_str()?;
assert!(output.starts_with("0x"));
assert!(!output[2..].chars().all(|c| c == '0'));
}
Ok(())
}
}
|