Skip to content

Commit a48849e

Browse files
tehcasterpmladek
authored andcommitted
printk: clarify the documentation for plain pointer printing
We have several modifiers for plain pointers (%p, %px and %pK) and now also the no_hash_pointers boot parameter. The documentation should help to choose which variant to use. Importantly, we should discourage %px in favor of %p (with the new boot parameter when debugging), and stress that %pK should be only used for procfs and similar files, not dmesg buffer. This patch clarifies the documentation in that regard. Signed-off-by: Vlastimil Babka <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ea35d86 commit a48849e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

Documentation/core-api/printk-formats.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,19 @@ Pointers printed without a specifier extension (i.e unadorned %p) are
7979
hashed to prevent leaking information about the kernel memory layout. This
8080
has the added benefit of providing a unique identifier. On 64-bit machines
8181
the first 32 bits are zeroed. The kernel will print ``(ptrval)`` until it
82-
gathers enough entropy. If you *really* want the address see %px below.
82+
gathers enough entropy.
83+
84+
When possible, use specialised modifiers such as %pS or %pB (described below)
85+
to avoid the need of providing an unhashed address that has to be interpreted
86+
post-hoc. If not possible, and the aim of printing the address is to provide
87+
more information for debugging, use %p and boot the kernel with the
88+
``no_hash_pointers`` parameter during debugging, which will print all %p
89+
addresses unmodified. If you *really* always want the unmodified address, see
90+
%px below.
91+
92+
If (and only if) you are printing addresses as a content of a virtual file in
93+
e.g. procfs or sysfs (using e.g. seq_printf(), not printk()) read by a
94+
userspace process, use the %pK modifier described below instead of %p or %px.
8395

8496
Error Pointers
8597
--------------
@@ -139,6 +151,11 @@ For printing kernel pointers which should be hidden from unprivileged
139151
users. The behaviour of %pK depends on the kptr_restrict sysctl - see
140152
Documentation/admin-guide/sysctl/kernel.rst for more details.
141153

154+
This modifier is *only* intended when producing content of a file read by
155+
userspace from e.g. procfs or sysfs, not for dmesg. Please refer to the
156+
section about %p above for discussion about how to manage hashing pointers
157+
in printk().
158+
142159
Unmodified Addresses
143160
--------------------
144161

@@ -153,6 +170,13 @@ equivalent to %lx (or %lu). %px is preferred because it is more uniquely
153170
grep'able. If in the future we need to modify the way the kernel handles
154171
printing pointers we will be better equipped to find the call sites.
155172

173+
Before using %px, consider if using %p is sufficient together with enabling the
174+
``no_hash_pointers`` kernel parameter during debugging sessions (see the %p
175+
description above). One valid scenario for %px might be printing information
176+
immediately before a panic, which prevents any sensitive information to be
177+
exploited anyway, and with %px there would be no need to reproduce the panic
178+
with no_hash_pointers.
179+
156180
Pointer Differences
157181
-------------------
158182

lib/vsprintf.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,9 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
21892189
* Implements a "recursive vsnprintf".
21902190
* Do not use this feature without some mechanism to verify the
21912191
* correctness of the format string and va_list arguments.
2192-
* - 'K' For a kernel pointer that should be hidden from unprivileged users
2192+
* - 'K' For a kernel pointer that should be hidden from unprivileged users.
2193+
* Use only for procfs, sysfs and similar files, not printk(); please
2194+
* read the documentation (path below) first.
21932195
* - 'NF' For a netdev_features_t
21942196
* - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
21952197
* a certain separator (' ' by default):
@@ -2228,7 +2230,8 @@ early_param("no_hash_pointers", no_hash_pointers_enable);
22282230
* Without an option prints the full name of the node
22292231
* f full name
22302232
* P node name, including a possible unit address
2231-
* - 'x' For printing the address. Equivalent to "%lx".
2233+
* - 'x' For printing the address unmodified. Equivalent to "%lx".
2234+
* Please read the documentation (path below) before using!
22322235
* - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
22332236
* bpf_trace_printk() where [ku] prefix specifies either kernel (k)
22342237
* or user (u) memory to probe, and:

0 commit comments

Comments
 (0)