Re: [PATCH v2 4/4] slub: Force on no_hash_pointers when slub_debug is enabled

From: Vlastimil Babka
Date: Wed May 26 2021 - 06:48:54 EST


On 5/26/21 4:56 AM, Stephen Boyd wrote:
> Obscuring the pointers that slub shows when debugging makes for some
> confusing slub debug messages:
>
> Padding overwritten. 0x0000000079f0674a-0x000000000d4dce17
>
> Those addresses are hashed for kernel security reasons. If we're trying
> to be secure with slub_debug on the commandline we have some big
> problems given that we dump whole chunks of kernel memory to the kernel
> logs. Let's force on the no_hash_pointers commandline flag when
> slub_debug is on the commandline. This makes slub debug messages more
> meaningful and if by chance a kernel address is in some slub debug
> object dump we will have a better chance of figuring out what went
> wrong.
>
> Note that we don't use %px in the slub code because we want to reduce
> the number of places that %px is used in the kernel. This also nicely
> prints a big fat warning at kernel boot if slub_debug is on the
> commandline so that we know that this kernel shouldn't be used on
> production systems.
>
> Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx>
> ---
>
> I opted for extern because I guess we don't want to advertise
> no_hash_pointers_enable() in some sort of header file? It can be put in
> a header file

Hm looks like the bots disagree. I suppose a declaration right above definition
in lib/vsprintf.c would silence them, but I'll leave it to printk maintainers if
they would prefer that way or traditionally
include/linux/kernel.h

> but I see that the no_hash_pointers variable is also not
> in a header file but exported as symbol.

Yeah it's only used by tests, and a variable doesn't need separate declaration.

> lib/vsprintf.c | 2 +-
> mm/slub.c | 6 ++++++
> 2 files changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index f0c35d9b65bf..cc281f5895f9 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -2186,7 +2186,7 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
> bool no_hash_pointers __ro_after_init;
> EXPORT_SYMBOL_GPL(no_hash_pointers);
>
> -static int __init no_hash_pointers_enable(char *str)
> +int __init no_hash_pointers_enable(char *str)
> {
> if (no_hash_pointers)
> return 0;
> diff --git a/mm/slub.c b/mm/slub.c
> index bf4949115412..1c30436d3e6c 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -4451,6 +4451,8 @@ static struct kmem_cache * __init bootstrap(struct kmem_cache *static_cache)
> return s;
> }
>
> +extern int no_hash_pointers_enable(char *str);
> +
> void __init kmem_cache_init(void)
> {
> static __initdata struct kmem_cache boot_kmem_cache,
> @@ -4470,6 +4472,10 @@ void __init kmem_cache_init(void)
> for_each_node_state(node, N_NORMAL_MEMORY)
> node_set(node, slab_nodes);
>
> + /* Print slub debugging pointers without hashing */
> + if (static_branch_unlikely(&slub_debug_enabled))
> + no_hash_pointers_enable(NULL);
> +
> create_boot_cache(kmem_cache_node, "kmem_cache_node",
> sizeof(struct kmem_cache_node), SLAB_HWCACHE_ALIGN, 0, 0);
>
>