Skip to content

Commit 8484291

Browse files
chleroypmladek
authored andcommitted
vsprintf: Fix %pK with kptr_restrict == 0
Although kptr_restrict is set to 0 and the kernel is booted with no_hash_pointers parameter, the content of /proc/vmallocinfo is lacking the real addresses. / # cat /proc/vmallocinfo 0x(ptrval)-0x(ptrval) 8192 load_module+0xc0c/0x2c0c pages=1 vmalloc 0x(ptrval)-0x(ptrval) 12288 start_kernel+0x4e0/0x690 pages=2 vmalloc 0x(ptrval)-0x(ptrval) 12288 start_kernel+0x4e0/0x690 pages=2 vmalloc 0x(ptrval)-0x(ptrval) 8192 _mpic_map_mmio.constprop.0+0x20/0x44 phys=0x80041000 ioremap 0x(ptrval)-0x(ptrval) 12288 _mpic_map_mmio.constprop.0+0x20/0x44 phys=0x80041000 ioremap ... According to the documentation for /proc/sys/kernel/, %pK is equivalent to %p when kptr_restrict is set to 0. Fixes: 5ead723 ("lib/vsprintf: no_hash_pointers prints all addresses as unhashed") Signed-off-by: Christophe Leroy <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/107476128e59bff11a309b5bf7579a1753a41aca.1645087605.git.christophe.leroy@csgroup.eu
1 parent a5a763b commit 8484291

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,8 +3491,7 @@
34913491
difficult since unequal pointers can no longer be
34923492
compared. However, if this command-line option is
34933493
specified, then all normal pointers will have their true
3494-
value printed. Pointers printed via %pK may still be
3495-
hashed. This option should only be specified when
3494+
value printed. This option should only be specified when
34963495
debugging the kernel. Please do not use on production
34973496
kernels.
34983497

lib/vsprintf.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
#include <linux/string_helpers.h>
5454
#include "kstrtox.h"
5555

56+
/* Disable pointer hashing if requested */
57+
bool no_hash_pointers __ro_after_init;
58+
EXPORT_SYMBOL_GPL(no_hash_pointers);
59+
5660
static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base)
5761
{
5862
const char *cp;
@@ -848,6 +852,19 @@ static char *ptr_to_id(char *buf, char *end, const void *ptr,
848852
return pointer_string(buf, end, (const void *)hashval, spec);
849853
}
850854

855+
static char *default_pointer(char *buf, char *end, const void *ptr,
856+
struct printf_spec spec)
857+
{
858+
/*
859+
* default is to _not_ leak addresses, so hash before printing,
860+
* unless no_hash_pointers is specified on the command line.
861+
*/
862+
if (unlikely(no_hash_pointers))
863+
return pointer_string(buf, end, ptr, spec);
864+
865+
return ptr_to_id(buf, end, ptr, spec);
866+
}
867+
851868
int kptr_restrict __read_mostly;
852869

853870
static noinline_for_stack
@@ -857,7 +874,7 @@ char *restricted_pointer(char *buf, char *end, const void *ptr,
857874
switch (kptr_restrict) {
858875
case 0:
859876
/* Handle as %p, hash and do _not_ leak addresses. */
860-
return ptr_to_id(buf, end, ptr, spec);
877+
return default_pointer(buf, end, ptr, spec);
861878
case 1: {
862879
const struct cred *cred;
863880

@@ -2233,10 +2250,6 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
22332250
return widen_string(buf, buf - buf_start, end, spec);
22342251
}
22352252

2236-
/* Disable pointer hashing if requested */
2237-
bool no_hash_pointers __ro_after_init;
2238-
EXPORT_SYMBOL_GPL(no_hash_pointers);
2239-
22402253
int __init no_hash_pointers_enable(char *str)
22412254
{
22422255
if (no_hash_pointers)
@@ -2465,7 +2478,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
24652478
case 'e':
24662479
/* %pe with a non-ERR_PTR gets treated as plain %p */
24672480
if (!IS_ERR(ptr))
2468-
break;
2481+
return default_pointer(buf, end, ptr, spec);
24692482
return err_ptr(buf, end, ptr, spec);
24702483
case 'u':
24712484
case 'k':
@@ -2475,16 +2488,9 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
24752488
default:
24762489
return error_string(buf, end, "(einval)", spec);
24772490
}
2491+
default:
2492+
return default_pointer(buf, end, ptr, spec);
24782493
}
2479-
2480-
/*
2481-
* default is to _not_ leak addresses, so hash before printing,
2482-
* unless no_hash_pointers is specified on the command line.
2483-
*/
2484-
if (unlikely(no_hash_pointers))
2485-
return pointer_string(buf, end, ptr, spec);
2486-
else
2487-
return ptr_to_id(buf, end, ptr, spec);
24882494
}
24892495

24902496
/*

0 commit comments

Comments
 (0)