Skip to content

Commit 9f36e2c

Browse files
Kees Cooktorvalds
authored andcommitted
printk: use %pK for /proc/kallsyms and /proc/modules
In an effort to reduce kernel address leaks that might be used to help target kernel privilege escalation exploits, this patch uses %pK when displaying addresses in /proc/kallsyms, /proc/modules, and /sys/module/*/sections/*. Note that this changes %x to %p, so some legitimately 0 values in /proc/kallsyms would have changed from 00000000 to "(null)". To avoid this, "(null)" is not used when using the "K" format. Anything that was already successfully parsing "(null)" in addition to full hex digits should have no problem with this change. (Thanks to Joe Perches for the suggestion.) Due to the %x to %p, "void *" casts are needed since these addresses are already "unsigned long" everywhere internally, due to their starting life as ELF section offsets. Signed-off-by: Kees Cook <kees.cook@canonical.com> Cc: Eugene Teo <eugene@redhat.com> Cc: Dan Rosenberg <drosenberg@vsecurity.com> Cc: Rusty Russell <rusty@rustcorp.com.au> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fe3d8ad commit 9f36e2c

3 files changed

Lines changed: 7 additions & 9 deletions

File tree

‎kernel/kallsyms.c‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,11 @@ static int s_show(struct seq_file *m, void *p)
477477
*/
478478
type = iter->exported ? toupper(iter->type) :
479479
tolower(iter->type);
480-
seq_printf(m, "%0*lx %c %s\t[%s]\n",
481-
(int)(2 * sizeof(void *)),
482-
iter->value, type, iter->name, iter->module_name);
480+
seq_printf(m, "%pK %c %s\t[%s]\n", (void *)iter->value,
481+
type, iter->name, iter->module_name);
483482
} else
484-
seq_printf(m, "%0*lx %c %s\n",
485-
(int)(2 * sizeof(void *)),
486-
iter->value, iter->type, iter->name);
483+
seq_printf(m, "%pK %c %s\n", (void *)iter->value,
484+
iter->type, iter->name);
487485
return 0;
488486
}
489487

‎kernel/module.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ static ssize_t module_sect_show(struct module_attribute *mattr,
11681168
{
11691169
struct module_sect_attr *sattr =
11701170
container_of(mattr, struct module_sect_attr, mattr);
1171-
return sprintf(buf, "0x%lx\n", sattr->address);
1171+
return sprintf(buf, "0x%pK\n", (void *)sattr->address);
11721172
}
11731173

11741174
static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
@@ -3224,7 +3224,7 @@ static int m_show(struct seq_file *m, void *p)
32243224
mod->state == MODULE_STATE_COMING ? "Loading":
32253225
"Live");
32263226
/* Used by oprofile and other similar tools. */
3227-
seq_printf(m, " 0x%p", mod->module_core);
3227+
seq_printf(m, " 0x%pK", mod->module_core);
32283228

32293229
/* Taints info */
32303230
if (mod->taints)

‎lib/vsprintf.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ static noinline_for_stack
991991
char *pointer(const char *fmt, char *buf, char *end, void *ptr,
992992
struct printf_spec spec)
993993
{
994-
if (!ptr) {
994+
if (!ptr && *fmt != 'K') {
995995
/*
996996
* Print (null) with the same width as a pointer so it makes
997997
* tabular output look nice.

0 commit comments

Comments
 (0)