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

From: Stephen Boyd
Date: Tue May 25 2021 - 22:56:43 EST

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

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 but I see that the no_hash_pointers variable is also not
in a header file but exported as symbol.

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;

-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);