Re: [kernel-hardening] [PATCH v3] printk: hash addresses printed with %p

From: Jason A. Donenfeld
Date: Tue Oct 17 2017 - 20:59:31 EST


Hi Tobin,

You submitted v3 without replying to my v2 comments. I'll give a
condensed version of those here for convenience.

> diff --git a/include/linux/siphash.h b/include/linux/siphash.h
> +unsigned long siphash_1ulong(const unsigned long a, const siphash_key_t *key);

Don't add this function here. It's not the right signature anyway.
> +unsigned long siphash_1ulong(const unsigned long first, const siphash_key_t *key)
> +{

Likewise, remove this.

> +/* 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 atomic_t have_key = ATOMIC_INIT(0);
> + unsigned long hashval;
> +
> + if (atomic_xchg(&have_key, 1) == 0)
> + get_random_bytes(&ptr_secret, sizeof(ptr_secret));

This isn't safe. Initialize ptr_secret in the callback function
provided to add_random_ready_callback. Before ptr_secret is
initialized, you should simply return a stub literal string from that
function of something like "(pointer value)".

> + hashval = siphash_1ulong((unsigned long)ptr, &ptr_secret);

Replace this with:

#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