Skip to content

Commit 5ead723

Browse files
Timur Tabipmladek
authored andcommitted
lib/vsprintf: no_hash_pointers prints all addresses as unhashed
If the no_hash_pointers command line parameter is set, then printk("%p") will print pointers as unhashed, which is useful for debugging purposes. This change applies to any function that uses vsprintf, such as print_hex_dump() and seq_buf_printf(). A large warning message is displayed if this option is enabled. Unhashed pointers expose kernel addresses, which can be a security risk. Also update test_printf to skip the hashed pointer tests if the command-line option is set. Signed-off-by: Timur Tabi <[email protected]> Acked-by: Petr Mladek <[email protected]> Acked-by: Randy Dunlap <[email protected]> Acked-by: Sergey Senozhatsky <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Acked-by: Marco Elver <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent d9d4de2 commit 5ead723

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,21 @@
32813281
in certain environments such as networked servers or
32823282
real-time systems.
32833283

3284+
no_hash_pointers
3285+
Force pointers printed to the console or buffers to be
3286+
unhashed. By default, when a pointer is printed via %p
3287+
format string, that pointer is "hashed", i.e. obscured
3288+
by hashing the pointer value. This is a security feature
3289+
that hides actual kernel addresses from unprivileged
3290+
users, but it also makes debugging the kernel more
3291+
difficult since unequal pointers can no longer be
3292+
compared. However, if this command-line option is
3293+
specified, then all normal pointers will have their true
3294+
value printed. Pointers printed via %pK may still be
3295+
hashed. This option should only be specified when
3296+
debugging the kernel. Please do not use on production
3297+
kernels.
3298+
32843299
nohibernate [HIBERNATION] Disable hibernation and resume.
32853300

32863301
nohz= [KNL] Boottime enable/disable dynamic ticks

lib/test_printf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ KSTM_MODULE_GLOBALS();
3535
static char *test_buffer __initdata;
3636
static char *alloced_buffer __initdata;
3737

38+
extern bool no_hash_pointers;
39+
3840
static int __printf(4, 0) __init
3941
do_test(int bufsize, const char *expect, int elen,
4042
const char *fmt, va_list ap)
@@ -301,6 +303,12 @@ plain(void)
301303
{
302304
int err;
303305

306+
if (no_hash_pointers) {
307+
pr_warn("skipping plain 'p' tests");
308+
skipped_tests += 2;
309+
return;
310+
}
311+
304312
err = plain_hash();
305313
if (err) {
306314
pr_warn("plain 'p' does not appear to be hashed\n");

lib/vsprintf.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,32 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
20902090
return widen_string(buf, buf - buf_start, end, spec);
20912091
}
20922092

2093+
/* Disable pointer hashing if requested */
2094+
bool no_hash_pointers __ro_after_init;
2095+
EXPORT_SYMBOL_GPL(no_hash_pointers);
2096+
2097+
static int __init no_hash_pointers_enable(char *str)
2098+
{
2099+
no_hash_pointers = true;
2100+
2101+
pr_warn("**********************************************************\n");
2102+
pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2103+
pr_warn("** **\n");
2104+
pr_warn("** This system shows unhashed kernel memory addresses **\n");
2105+
pr_warn("** via the console, logs, and other interfaces. This **\n");
2106+
pr_warn("** might reduce the security of your system. **\n");
2107+
pr_warn("** **\n");
2108+
pr_warn("** If you see this message and you are not debugging **\n");
2109+
pr_warn("** the kernel, report this immediately to your system **\n");
2110+
pr_warn("** administrator! **\n");
2111+
pr_warn("** **\n");
2112+
pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
2113+
pr_warn("**********************************************************\n");
2114+
2115+
return 0;
2116+
}
2117+
early_param("no_hash_pointers", no_hash_pointers_enable);
2118+
20932119
/*
20942120
* Show a '%p' thing. A kernel extension is that the '%p' is followed
20952121
* by an extra set of alphanumeric characters that are extended format
@@ -2297,8 +2323,14 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
22972323
}
22982324
}
22992325

2300-
/* default is to _not_ leak addresses, hash before printing */
2301-
return ptr_to_id(buf, end, ptr, spec);
2326+
/*
2327+
* default is to _not_ leak addresses, so hash before printing,
2328+
* unless no_hash_pointers is specified on the command line.
2329+
*/
2330+
if (unlikely(no_hash_pointers))
2331+
return pointer_string(buf, end, ptr, spec);
2332+
else
2333+
return ptr_to_id(buf, end, ptr, spec);
23022334
}
23032335

23042336
/*

0 commit comments

Comments
 (0)