Skip to contentSkip to conversation

printk: hash addresses printed with %p

PATCH Superseded Tobin C. Harding 7 participants 12 messages Oct 17, 2017 – Oct 18, 2017

Subsystems (3)

v2 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 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.

For what it's worth, usage of unadorned %p can be broken down as follows

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

arch: 2512
block: 20
crypto: 12
fs: 1221
include: 147
kernel: 109
lib: 77
mm: 120
net: 1516
security: 11
sound: 168
virt: 2
drivers: 8420

Add helper function siphash_1ulong(). Add function ptr_to_id() to map an address to a 32 bit unique identifier.

---

V2:
- Use SipHash to do the hashing

The discussion related to this patch has been fragmented. There are three other 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

 include/linux/siphash.h |  2 ++
 lib/siphash.c           | 13 +++++++++++++
 lib/vsprintf.c          | 32 +++++++++++++++++++++++++++++---
 3 files changed, 44 insertions(+), 3 deletions(-)
include/linux/siphash.h +2 -0
index fa7a6b9cedbf..a9392568c8b8 100644
@@ -26,6 +26,8 @@ u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key);
 u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key);
 #endif
 
+unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t *key);
+
 u64 siphash_1u64(const u64 a, const siphash_key_t *key);
 u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
 u64 siphash_3u64(const u64 a, const u64 b, const u64 c,
lib/siphash.c +13 -0
index 3ae58b4edad6..63f4ff57c9ce 100644
@@ -116,6 +116,19 @@ EXPORT_SYMBOL(__siphash_unaligned);
 #endif
 
 /**
+ * siphash_1ulong - computes siphash PRF value
+ * @first: value to hash
+ * @key: the siphash key
+ */
+unsigned long siphash_1ulong(const unsigned long first, const siphash_key_t *key)
+{
+#ifdef CONFIG_64BIT
+	return (unsigned long)siphash_1u64((u64)first, key);
+#endif
+	return (unsigned long)siphash_1u32((u32)first, key);
+}
+
+/**
  * siphash_1u64 - compute 64-bit siphash PRF value of a u64
  * @first: first u64
  * @key: the siphash key
lib/vsprintf.c +29 -3
index 86c3385b9eb3..afd1c835b0f6 100644
@@ -33,6 +33,7 @@
 #include <linux/uuid.h>
 #include <linux/of.h>
 #include <net/addrconf.h>
+#include <linux/siphash.h>
 #ifdef CONFIG_BLOCK
 #include <linux/blkdev.h>
 #endif
@@ -503,6 +504,7 @@ char *number(char *buf, char *end, unsigned long long num,
 			*buf = '0';
 		++buf;
 	}
+
 	/* actual digits of result */
 	while (--i >= 0) {
 		if (buf < end)
@@ -1591,6 +1593,28 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
 	return widen_string(buf, buf - buf_start, end, spec);
 }
 
+/* Maps a pointer to a 32 bit unique identifier. */
+static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
+{
+	static siphash_key_t ptr_secret __read_mostly;
+	static bool have_key = false;
+	unsigned long hashval;
+
+	/* Kernel doesn't boot if we use get_random_once() */
+	if (!have_key) {
+		get_random_bytes(&ptr_secret, sizeof(ptr_secret));
+		have_key = true;
+	}
+
+	hashval = siphash_1ulong((unsigned long)ptr, &ptr_secret);
+
+	spec.field_width = 2 + 2 * sizeof(unsigned int); /* 0x + hex */
+	spec.flags = SPECIAL | SMALL | ZEROPAD;
+	spec.base = 16;
+
+	return number(buf, end, (u32)hashval, spec);
+}
+
 int kptr_restrict __read_mostly;
 
 /*
@@ -1703,6 +1727,9 @@ int kptr_restrict __read_mostly;
  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  * function pointers are really function descriptors, which contain a
  * pointer to the real address.
+ *
+ * Default behaviour (unadorned %p) is to hash the address, rendering it useful
+ * as a unique identifier.
  */
 static noinline_for_stack
 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
@@ -1858,14 +1885,13 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			return device_node_string(buf, end, ptr, spec, fmt + 1);
 		}
 	}
-	spec.flags |= SMALL;
+
 	if (spec.field_width == -1) {
 		spec.field_width = default_width;
 		spec.flags |= ZEROPAD;
 	}
-	spec.base = 16;
 
-	return number(buf, end, (unsigned long) ptr, spec);
+	return ptr_to_id(buf, end, ptr, spec);
 }
 
 /*
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · 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.

Quoted 7 lines

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

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

Not really.
There are many asm uses included there

I think a better grep is:

$ 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
Quoted 1 line

arch: 2512

arch is especially overestimated.

maintainer
#2 lore raw
In reply to Tobin C. Harding
Quoted 5 lines · On Tue, 17 Oct 2017 15:52:51 +1100 "Tobin C. Harding" <[email protected]> wrote:

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.

Quoted 7 lines

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

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

function+0x<offset>

-- Steve

Quoted 21 lines

arch: 2512
block: 20
crypto: 12
fs: 1221
include: 147
kernel: 109
lib: 77
mm: 120
net: 1516
security: 11
sound: 168
virt: 2
drivers: 8420

Add helper function siphash_1ulong(). Add function ptr_to_id() to map an address to a 32 bit unique identifier.

---

#3 lore raw
Quoted 12 lines · Steven Rostedt
Quoted 9 lines · On Tue, 17 Oct 2017 15:52:51 +1100 "Tobin C. Harding" <[email protected]> wrote:

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.

Quoted 8 lines
Quoted 3 lines

For what it's worth, usage of unadorned %p can be broken down as follows

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

function+0x<offset>

You are correct %pF and %pS print an offset. Does this provide an attack vector, I didn't think so but I'm no security expert. If they do then we need to amend those calls also.

We still have %pa[pd] to see to as well obviously.

thanks for the review,
Tobin.

#4 lore raw
Quoted 17 lines · Tobin C. Harding
Quoted 16 lines · Steven Rostedt
Quoted 12 lines · On Tue, 17 Oct 2017 15:52:51 +1100 "Tobin C. Harding" <[email protected]> wrote:

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

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

Quoted 8 lines
Quoted 4 lines

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

function+0x<offset>

You are correct %pF and %pS print an offset. Does this provide an attack vector, I didn't think so but I'm no security expert. If they do then we need to amend those calls also.

They haven't traditionally been a big deal. If they turn out to be problematic, we can deal with it then, IMO.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw
Quoted 21 lines · Kees Cook
Quoted 20 lines · Tobin C. Harding
Quoted 19 lines · Steven Rostedt
Quoted 5 lines · On Tue, 17 Oct 2017 15:52:51 +1100 "Tobin C. Harding" <[email protected]> wrote:

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.

Quoted 7 lines

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

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

function+0x<offset>

You are correct %pF and %pS print an offset. Does this provide an attack vector, I didn't think so but I'm no security expert. If they do then we need to amend those calls also.

They haven't traditionally been a big deal. If they turn out to be problematic, we can deal with it then, IMO.

Thanks Kees,
Tobin.

#6 lore raw
Quoted 8 lines · Tobin C. Harding
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

function+0x<offset>

They haven't traditionally been a big deal. If they turn out to be problematic, we can deal with it then, IMO.

If it's not in kallsyms, the raw address is probably not going to be terribly useful --- so even if it's not traditionally been a big deal, why not just hash them if it's not going to be printed as "function+0x<offset>"?

If nothing else, it will help correlate the random address with other places where it was printed via %p.

- Ted

maintainer
#7 lore raw
In reply to Tobin C. Harding
Quoted 8 lines · On Wed, 18 Oct 2017 10:15:59 +1100 "Tobin C. Harding" <[email protected]> wrote:
Quoted 4 lines

Does %p[FfSs] leak addresses? Well, I guess it does if they are not found in kallsyms, but otherwise you have:

function+0x<offset>

You are correct %pF and %pS print an offset. Does this provide an attack vector, I didn't think so but I'm no security expert. If they do then we need to amend those calls also.

Hopefully not. We changed stack dumps to use them only instead of showing addresses because of the location leak.

-- Steve

#8 lore raw
In reply to Tobin C. Harding
Quoted 115 lines

-----Original Message-----
From: Tobin C. Harding [mailto:[email protected]]
Sent: Monday, October 16, 2017 9:53 PM
To: [email protected]
Cc: Tobin C. Harding <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Kees Cook <[email protected]>; Paolo Bonzini <[email protected]>; Tycho Andersen <[email protected]>; Roberts, William C <[email protected]>; Tejun Heo <[email protected]>; Jordan Glover <[email protected]>; Greg KH <[email protected]>; Petr Mladek <[email protected]>; Joe Perches <[email protected]>; Ian Campbell <[email protected]>; Sergey Senozhatsky <[email protected]>; Catalin Marinas <[email protected]>; Will Deacon <[email protected]>; Steven Rostedt <[email protected]>; Chris Fries <[email protected]>; Dave Weinstein <[email protected]>; Daniel Micay <[email protected]>; Djalal Harouni <[email protected]>; [email protected]
Subject: [PATCH v2] 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.

For what it's worth, usage of unadorned %p can be broken down as follows

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

arch: 2512
block: 20
crypto: 12
fs: 1221
include: 147
kernel: 109
lib: 77
mm: 120
net: 1516
security: 11
sound: 168
virt: 2
drivers: 8420

Add helper function siphash_1ulong(). Add function ptr_to_id() to map an address to a 32 bit unique identifier.

---

V2:
- Use SipHash to do the hashing

The discussion related to this patch has been fragmented. There are three other 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

 include/linux/siphash.h |  2 ++
 lib/siphash.c           | 13 +++++++++++++
 lib/vsprintf.c          | 32 +++++++++++++++++++++++++++++---
 3 files changed, 44 insertions(+), 3 deletions(-)
include/linux/siphash.h index renamed from include/linux/siphash.h +0 -0

fa7a6b9cedbf..a9392568c8b8 100644

include/linux/siphash.h +3 -0
@@ -26,6 +26,8 @@ u64 __siphash_aligned(const void *data, size_t len, const
 siphash_key_t *key);
 u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t
 *key);  #endif
 
+unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t
+*key);
+
 u64 siphash_1u64(const u64 a, const siphash_key_t *key);
 u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
 u64 siphash_3u64(const u64 a, const u64 b, const u64 c, diff --git a/lib/siphash.c

b/lib/siphash.c index 3ae58b4edad6..63f4ff57c9ce 100644

lib/siphash.c +10 -0
@@ -116,6 +116,19 @@ EXPORT_SYMBOL(__siphash_unaligned);
 #endif
 
 /**
+ * siphash_1ulong - computes siphash PRF value
+ * @first: value to hash
+ * @key: the siphash key
+ */
+unsigned long siphash_1ulong(const unsigned long first, const
+siphash_key_t *key) { #ifdef CONFIG_64BIT
+	return (unsigned long)siphash_1u64((u64)first, key); #endif
+	return (unsigned long)siphash_1u32((u32)first, key); }
+
+/**
  * siphash_1u64 - compute 64-bit siphash PRF value of a u64
  * @first: first u64
  * @key: the siphash key
lib/vsprintf.c +1 -0
@@ -33,6 +33,7 @@
 #include <linux/uuid.h>
 #include <linux/of.h>
 #include <net/addrconf.h>
+#include <linux/siphash.h>
 #ifdef CONFIG_BLOCK
 #include <linux/blkdev.h>
 #endif

lib/vsprintf.c char *number(char *buf, char *end, unsigned long long · line 508

Quoted 5 lines
lib/vsprintf.c +1 -0
@@ -503,6 +504,7 @@ char *number(char *buf, char *end, unsigned long long
 num,
 			*buf = '0';
 		++buf;
 	}
+

Unneeded whitespace change?

Quoted 7 lines
lib/vsprintf.c +0 -0
 	/* actual digits of result */
 	while (--i >= 0) {
lib/vsprintf.c +0 -0
 		if (buf < end)
@@ -1591,6 +1593,28 @@ char *device_node_string(char *buf, char *end, struct
 device_node *dn,
 	return widen_string(buf, buf - buf_start, end, spec);  }
 

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct · line 1605

Quoted 10 lines
lib/vsprintf.c +10 -0
+/* Maps a pointer to a 32 bit unique identifier. */ static char
+*ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) {
+	static siphash_key_t ptr_secret __read_mostly;
+	static bool have_key = false;
+	unsigned long hashval;
+
+	/* Kernel doesn't boot if we use get_random_once() */
+	if (!have_key) {
+		get_random_bytes(&ptr_secret, sizeof(ptr_secret));
+		have_key = true;

Wouldn't one want to use an atomic test and swap for this block?

Quoted 43 lines
lib/vsprintf.c +17 -4
+	}
+
+	hashval = siphash_1ulong((unsigned long)ptr, &ptr_secret);
+
+	spec.field_width = 2 + 2 * sizeof(unsigned int); /* 0x + hex */
+	spec.flags = SPECIAL | SMALL | ZEROPAD;
+	spec.base = 16;
+
+	return number(buf, end, (u32)hashval, spec); }
+
 int kptr_restrict __read_mostly;
 
 /*
@@ -1703,6 +1727,9 @@ int kptr_restrict __read_mostly;
  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  * function pointers are really function descriptors, which contain a
  * pointer to the real address.
+ *
+ * Default behaviour (unadorned %p) is to hash the address, rendering
+ it useful
+ * as a unique identifier.
  */
 static noinline_for_stack
 char *pointer(const char *fmt, char *buf, char *end, void *ptr, @@ -1858,14
+1885,13 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
 			return device_node_string(buf, end, ptr, spec, fmt + 1);
 		}
 	}
-	spec.flags |= SMALL;
+
 	if (spec.field_width == -1) {
 		spec.field_width = default_width;
 		spec.flags |= ZEROPAD;
 	}
-	spec.base = 16;
 
-	return number(buf, end, (unsigned long) ptr, spec);
+	return ptr_to_id(buf, end, ptr, spec);
 }
 
 /*
--

2.7.4

#9 lore raw
Quoted 114 lines · William C
Quoted 114 lines

-----Original Message-----
From: Tobin C. Harding [mailto:[email protected]]
Sent: Monday, October 16, 2017 9:53 PM
To: [email protected]
Cc: Tobin C. Harding <[email protected]>; Linus Torvalds <torvalds@linux- foundation.org>; Kees Cook <[email protected]>; Paolo Bonzini <[email protected]>; Tycho Andersen <[email protected]>; Roberts, William C <[email protected]>; Tejun Heo <[email protected]>; Jordan Glover <[email protected]>; Greg KH <[email protected]>; Petr Mladek <[email protected]>; Joe Perches <[email protected]>; Ian Campbell <[email protected]>; Sergey Senozhatsky <[email protected]>; Catalin Marinas <[email protected]>; Will Deacon <[email protected]>; Steven Rostedt <[email protected]>; Chris Fries <[email protected]>; Dave Weinstein <[email protected]>; Daniel Micay <[email protected]>; Djalal Harouni <[email protected]>; [email protected]
Subject: [PATCH v2] 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.

For what it's worth, usage of unadorned %p can be broken down as follows

git grep '%p[^KFfSsBRrbMmIiEUVKNhdDgCGO]' | wc -l

arch: 2512
block: 20
crypto: 12
fs: 1221
include: 147
kernel: 109
lib: 77
mm: 120
net: 1516
security: 11
sound: 168
virt: 2
drivers: 8420

Add helper function siphash_1ulong(). Add function ptr_to_id() to map an address to a 32 bit unique identifier.

---

V2:
- Use SipHash to do the hashing

The discussion related to this patch has been fragmented. There are three other 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

 include/linux/siphash.h |  2 ++
 lib/siphash.c           | 13 +++++++++++++
 lib/vsprintf.c          | 32 +++++++++++++++++++++++++++++---
 3 files changed, 44 insertions(+), 3 deletions(-)
include/linux/siphash.h index renamed from include/linux/siphash.h +0 -0

fa7a6b9cedbf..a9392568c8b8 100644

include/linux/siphash.h +3 -0
@@ -26,6 +26,8 @@ u64 __siphash_aligned(const void *data, size_t len, const
 siphash_key_t *key);
 u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t
 *key);  #endif
 
+unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t
+*key);
+
 u64 siphash_1u64(const u64 a, const siphash_key_t *key);
 u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
 u64 siphash_3u64(const u64 a, const u64 b, const u64 c, diff --git a/lib/siphash.c

b/lib/siphash.c index 3ae58b4edad6..63f4ff57c9ce 100644

lib/siphash.c +10 -0
@@ -116,6 +116,19 @@ EXPORT_SYMBOL(__siphash_unaligned);
 #endif
 
 /**
+ * siphash_1ulong - computes siphash PRF value
+ * @first: value to hash
+ * @key: the siphash key
+ */
+unsigned long siphash_1ulong(const unsigned long first, const
+siphash_key_t *key) { #ifdef CONFIG_64BIT
+	return (unsigned long)siphash_1u64((u64)first, key); #endif
+	return (unsigned long)siphash_1u32((u32)first, key); }
+
+/**
  * siphash_1u64 - compute 64-bit siphash PRF value of a u64
  * @first: first u64
  * @key: the siphash key
lib/vsprintf.c +1 -0
@@ -33,6 +33,7 @@
 #include <linux/uuid.h>
 #include <linux/of.h>
 #include <net/addrconf.h>
+#include <linux/siphash.h>
 #ifdef CONFIG_BLOCK
 #include <linux/blkdev.h>
 #endif
Quoted 8 lines

lib/vsprintf.c char *number(char *buf, char *end, unsigned long long · line 508

Quoted 6 lines
lib/vsprintf.c +1 -0
@@ -503,6 +504,7 @@ char *number(char *buf, char *end, unsigned long long
 num,
 			*buf = '0';
 		++buf;
 	}
+

Unneeded whitespace change?

:) thanks

Quoted 8 lines
Quoted 8 lines
lib/vsprintf.c +0 -0
 	/* actual digits of result */
 	while (--i >= 0) {
lib/vsprintf.c +0 -0
 		if (buf < end)
@@ -1591,6 +1593,28 @@ char *device_node_string(char *buf, char *end, struct
 device_node *dn,
 	return widen_string(buf, buf - buf_start, end, spec);  }
 
Quoted 13 lines

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 1606

Quoted 10 lines
lib/vsprintf.c +10 -0
+/* Maps a pointer to a 32 bit unique identifier. */ static char
+*ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec) {
+	static siphash_key_t ptr_secret __read_mostly;
+	static bool have_key = false;
+	unsigned long hashval;
+
+	/* Kernel doesn't boot if we use get_random_once() */
+	if (!have_key) {
+		get_random_bytes(&ptr_secret, sizeof(ptr_secret));
+		have_key = true;

Wouldn't one want to use an atomic test and swap for this block?

Great, thanks for the pointer.

Thanks for the review William.

Tobin.

#10 lore raw
In reply to Tobin C. Harding

Hi Tobin,

Many comments in line below.

include/linux/siphash.h u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); · line 29

Quoted 10 lines · Tobin C. Harding
include/linux/siphash.h +1 -0
index fa7a6b9cedbf..a9392568c8b8 100644
@@ -26,6 +26,8 @@ u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key);
 u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key);
 #endif
 
+unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t *key);

This signature is incorrect, as siphash always returns a u64. The caller should do the casting, not the actual function itself. [However, see below. I don't think you should be touching this file.]

Quoted 8 lines
include/linux/siphash.h +0 -0
 u64 siphash_1u64(const u64 a, const siphash_key_t *key);
 u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key);
 u64 siphash_3u64(const u64 a, const u64 b, const u64 c,
lib/siphash.c +0 -0
index 3ae58b4edad6..63f4ff57c9ce 100644

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned); · line 122

Quoted 7 lines
lib/siphash.c +4 -0
@@ -116,6 +116,19 @@ EXPORT_SYMBOL(__siphash_unaligned);
 #endif
 
 /**
+ * siphash_1ulong - computes siphash PRF value
+ * @first: value to hash
+ * @key: the siphash key
+ */

Please match the template usage text of every single other function, like so:

* siphash_1ulong - compute 64-bit siphash PRF value of 1 unsigned long
* @first: first unsigned long
* @key: the siphash key

[However, see below. I don't think you should be touching this file.]

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned); · line 123

Quoted 1 line
lib/siphash.c +1 -0
+unsigned long siphash_1ulong(const unsigned long first, const siphash_key_t *key)

Return u64. [However, see below. I don't think you should be touching this file.]

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned); · line 126

Quoted 3 lines
lib/siphash.c +3 -0
+{
+#ifdef CONFIG_64BIT
+       return (unsigned long)siphash_1u64((u64)first, key);

Don't cast it here. [However, see below. I don't think you should be touching this file.]

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned); · line 127

Quoted 1 line
lib/siphash.c +1 -0
+#endif

There's no point in making gcc's life harder. Use an #else for the 32-bit section. [However, see below. I don't think you should be touching this file.]

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned); · line 128

Quoted 1 line
lib/siphash.c +1 -0
+       return (unsigned long)siphash_1u32((u32)first, key);

Also don't cast. [However, see below. I don't think you should be touching this file.]

lib/siphash.c EXPORT_SYMBOL(__siphash_unaligned);

Quoted 12 lines
lib/siphash.c +12 -0
+/* Maps a pointer to a 32 bit unique identifier. */
+static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
+{
+       static siphash_key_t ptr_secret __read_mostly;
+       static bool have_key = false;
+       unsigned long hashval;
+
+       /* Kernel doesn't boot if we use get_random_once() */
+       if (!have_key) {
+               get_random_bytes(&ptr_secret, sizeof(ptr_secret));
+               have_key = true;
+       }

This is wrong. You need to either use get_random_bytes_wait, which you can't actually do safely here. So, better, use add_random_ready_callback to get a notification of when this is safe to use. Before it's safe to use, simply return "(ptr value)" or some similar stub.

lib/siphash.c

Quoted 2 lines
lib/siphash.c +2 -0
+
+       hashval = siphash_1ulong((unsigned long)ptr, &ptr_secret);

As mentioned above with the [brackets], don't pollute siphash.h/siphash.c with the helper, and just put the #ifdef stuff here. That should make it much more clear what's going on and also make it easier in the future to swap out the 32-bit function when we're ready.

So, this looks like instead:

#ifdef CONFIG_64BIT
hashval = (unsigned long)siphash_1u64((u64)ptr, key); #else
hashval = (unsigned long)siphash_1u32((u32)ptr, key); #endif

However, in another thread, Linus mentioned that he'd prefer all the obfuscated values actually be 32-bit. So, this then looks like:

unsigned int hashval;
...
#ifdef CONFIG_64BIT
hashval = (unsigned int)siphash_1u64((u64)ptr, key); #else
hashval = (unsigned int)siphash_1u32((u32)ptr, key); #endif

Looking forward to v3!

Thanks,
Jason

#11 lore raw

On Wed, Oct 18, 2017 at 02:27:43AM +0200, Jason A. Donenfeld wrote:
[snip]

Thank you for your extensive comments Jason. I had v3 in flight before I received your email, please don't think I ignored your suggestions.

v4 to come!

thanks,
Tobin.