Skip to contentSkip to conversation

printk: hash addresses printed with %p

PATCH Superseded Tobin C. Harding 1 participant 3 messages Nov 1, 2017

Subsystems (5)

v10 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v10 cover printk: hash addresses printed with %p

Currently there are many places in the kernel where addresses are being printed using an unadorned %p. Kernel pointers should be printed using %pK allowing some control via the kptr_restrict sysctl. Exposing addresses gives attackers sensitive information about the kernel layout in memory.

We can reduce the attack surface by hashing all addresses printed with %p. This will of course break some users, forcing code printing needed addresses to be updated.

This version adds testing, this is my first effort at kernel unit testing. Modules in lib don't seem contained within a selftest target so in order to incrementally develop the tests I implemented the tests in lib/test_printf.c, built with make M=lib and then to insert the module, instead of running selftest, I spun up a VM and inserted the module manually. Comments or suggestions much appreciated.

Here is the behaviour that this series implements.

For kpt_restrict==0

Randomness not ready:
printed with %p: (ptrval) # NOTE: with padding
Valid pointer:

  printed with %pK:             deadbeefdeadbeef
  printed with %p:              00000000deadbeef
  malformed specifier (eg %i):  00000000deadbeef

NULL pointer:
printed with %pK: 0000000000000000
printed with %p: (null) # NOTE: with padding
malformed specifier (eg %i): (null)

For kpt_restrict==2

Valid pointer:
printed with %pK: 0000000000000000

All other output as for kptr_restrict==0

V10:
- Add patch so KASAN uses %pK instead of %p.
- Add documentation to Documentation/printk-formats.txt
- Add tests to lib/test_printf.c
- Change "(pointer value)" -> "(ptrval)" to fit within columns on 32 bit machines.

V9:
- Drop the initial patch from V8, leaving null pointer handling as is.
- Print the hashed ID _without_ a '0x' suffix.
- Mask the first 32 bits of the hashed ID to all zeros on 64 bit architectures.

V8:
- Add second patch cleaning up null pointer printing in pointer()
- Move %pK handling to separate function, further cleaning up pointer()
- Move ptr_to_id() call outside of switch statement making hashing the default behaviour (including malformed specifiers).
- Remove use of static_key, replace with simple boolean.

V7:
- Use tabs instead of spaces (ouch!).

V6:
- Use __early_initcall() to fill the SipHash key.
- Use static keys to guard hashing before the key is available.

V5:
- Remove spin lock.
- Add Jason A. Donenfeld to CC list by request.
- Add Theodore Ts'o to CC list due to comment on previous version.

V4:
- Remove changes to siphash.{ch}
- Do word size check, and return value cast, directly in ptr_to_id().
- Use add_ready_random_callback() to guard call to get_random_bytes()

V3:
- Use atomic_xchg() to guard setting [random] key.
- Remove erroneous white space change.

V2:
- Use SipHash to do the hashing.

The discussion related to this patch has been fragmented. There are three threads associated with this patch. Email threads by subject:

[PATCH] printk: hash addresses printed with %p
[PATCH 0/3] add %pX specifier
[kernel-hardening] [RFC V2 0/6] add more kernel pointer filter options

Tobin C. Harding (2):
kasan: use %pK to print addresses instead of %p
printk: hash addresses printed with %p

 Documentation/printk-formats.txt |  17 +++-
 lib/test_printf.c                | 108 +++++++++++++++---------
 lib/vsprintf.c                   | 176 ++++++++++++++++++++++++++++-----------
 mm/kasan/report.c                |   8 +-
 4 files changed, 217 insertions(+), 92 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v10 1/2 kasan: use %pK to print addresses instead of %p

In preparation for hashing addresses printed using %p. We need the actual address for error reporting in kasan.

Use %pK instead of %p to print addresses.

---
 mm/kasan/report.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/kasan/report.c +4 -4
index 6bcfb01ba038..ad042f025a1a 100644
@@ -134,7 +134,7 @@ static void print_error_description(struct kasan_access_info *info)
 
 	pr_err("BUG: KASAN: %s in %pS\n",
 		bug_type, (void *)info->ip);
-	pr_err("%s of size %zu at addr %p by task %s/%d\n",
+	pr_err("%s of size %zu at addr %pK by task %s/%d\n",
 		info->is_write ? "Write" : "Read", info->access_size,
 		info->access_addr, current->comm, task_pid_nr(current));
 }
@@ -206,7 +206,7 @@ static void describe_object_addr(struct kmem_cache *cache, void *object,
 	const char *rel_type;
 	int rel_bytes;
 
-	pr_err("The buggy address belongs to the object at %p\n"
+	pr_err("The buggy address belongs to the object at %pK\n"
 	       " which belongs to the cache %s of size %d\n",
 		object, cache->name, cache->object_size);
 
@@ -225,7 +225,7 @@ static void describe_object_addr(struct kmem_cache *cache, void *object,
 	}
 
 	pr_err("The buggy address is located %d bytes %s of\n"
-	       " %d-byte region [%p, %p)\n",
+	       " %d-byte region [%pK, %pK)\n",
 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
 		(void *)(object_addr + cache->object_size));
 }
@@ -302,7 +302,7 @@ static void print_shadow_for_address(const void *addr)
 		char shadow_buf[SHADOW_BYTES_PER_ROW];
 
 		snprintf(buffer, sizeof(buffer),
-			(i == 0) ? ">%p: " : " %p: ", kaddr);
+			(i == 0) ? ">%pK: " : " %pK: ", kaddr);
 		/*
 		 * We should not pass a shadow pointer to generic
 		 * function, because generic functions may try to
Signature · 1 line
2.7.4
#2 lore raw

PATCH v10 2/2 printk: hash addresses printed with %p

In reply to Tobin C. Harding

Currently there are many places in the kernel where addresses are being printed using an unadorned %p. Kernel pointers should be printed using %pK allowing some control via the kptr_restrict sysctl. Exposing addresses gives attackers sensitive information about the kernel layout in memory.

We can reduce the attack surface by hashing all addresses printed with %p. This will of course break some users, forcing code printing needed addresses to be updated.

For what it's worth, usage of unadorned %p can be broken down as follows (thanks to Joe Perches).

$ git grep -E '%p[^A-Za-z0-9]' | cut -f1 -d"/" | sort | uniq -c
   1084 arch
     20 block
     10 crypto
     32 Documentation
   8121 drivers
   1221 fs
    143 include
    101 kernel
     69 lib
    100 mm
   1510 net
     40 samples
      7 scripts
     11 security
    166 sound
    152 tools
      2 virt

Add function ptr_to_id() to map an address to a 32 bit unique identifier. Hash any unadorned usage of specifier %p and any malformed specifiers.

---
 Documentation/printk-formats.txt |  17 +++-
 lib/test_printf.c                | 108 +++++++++++++++---------
 lib/vsprintf.c                   | 176 ++++++++++++++++++++++++++++-----------
 3 files changed, 213 insertions(+), 88 deletions(-)
Documentation/printk-formats.txt +16 -1
index 361789df51ec..ec7deb80d035 100644
@@ -5,6 +5,9 @@ How to get printk format specifiers right
 :Author: Randy Dunlap <[email protected]>
 :Author: Andrew Murray <[email protected]>
 
+Please do not print kernel addresses using %x. Exposing kernel addresses to
+user space leaks sensitive information that increases the attack surface of the
+kernel. In order to print pointers, please see 'Pointer Types' below.
 
 Integer types
 =============
@@ -45,6 +48,18 @@ return from vsnprintf.
 Raw pointer value SHOULD be printed with %p. The kernel supports
 the following extended format specifiers for pointer types:
 
+Pointer Types
+=============
+
+Pointers printed without a specifier extension (i.e unadorned %p) are hashed
+to give a unique identifier without leaking kernel addresses to user space.
+If you _really_ want to see the address please use %pK (see 'Kernel Pointers'
+below). On 64 bit machines the first 32 bits are zeroed.
+
+::
+
+	%p	abcdef12 or 00000000abcdef12
+
 Symbols/Function Pointers
 =========================
 
@@ -91,7 +106,7 @@ Kernel Pointers
 
 ::
 
-	%pK	0x01234567 or 0x0123456789abcdef
+	%pK	01234567 or 0123456789abcdef
 
 For printing kernel pointers which should be hidden from unprivileged
 users. The behaviour of ``%pK`` depends on the ``kptr_restrict sysctl`` - see
lib/test_printf.c +70 -38

130 lines not on this page · raw message

lib/vsprintf.c +127 -49

213 lines not on this page · raw message

Signature · 1 line
2.7.4