Skip to content

Commit 7927029

Browse files
bebarinotorvalds
authored andcommitted
slub: force on no_hash_pointers when slub_debug is enabled
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. [akpm@linux-foundation.org: fix build with CONFIG_SLUB_DEBUG=n] Link: https://lkml.kernel.org/r/20210601182202.3011020-5-swboyd@chromium.org Signed-off-by: Stephen Boyd <swboyd@chromium.org> Acked-by: Vlastimil Babka <vbabka@suse.cz> Acked-by: Petr Mladek <pmladek@suse.com> Cc: Joe Perches <joe@perches.com> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 582d121 commit 7927029

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

include/linux/kernel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ int sscanf(const char *, const char *, ...);
357357
extern __scanf(2, 0)
358358
int vsscanf(const char *, const char *, va_list);
359359

360+
extern int no_hash_pointers_enable(char *str);
361+
360362
extern int get_option(char **str, int *pint);
361363
extern char *get_options(const char *str, int nints, int *ints);
362364
extern unsigned long long memparse(const char *ptr, char **retptr);

lib/vsprintf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
21862186
bool no_hash_pointers __ro_after_init;
21872187
EXPORT_SYMBOL_GPL(no_hash_pointers);
21882188

2189-
static int __init no_hash_pointers_enable(char *str)
2189+
int __init no_hash_pointers_enable(char *str)
21902190
{
21912191
if (no_hash_pointers)
21922192
return 0;

mm/slub.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,26 @@
118118
*/
119119

120120
#ifdef CONFIG_SLUB_DEBUG
121+
121122
#ifdef CONFIG_SLUB_DEBUG_ON
122123
DEFINE_STATIC_KEY_TRUE(slub_debug_enabled);
123124
#else
124125
DEFINE_STATIC_KEY_FALSE(slub_debug_enabled);
125126
#endif
126-
#endif
127+
128+
static inline bool __slub_debug_enabled(void)
129+
{
130+
return static_branch_unlikely(&slub_debug_enabled);
131+
}
132+
133+
#else /* CONFIG_SLUB_DEBUG */
134+
135+
static inline bool __slub_debug_enabled(void)
136+
{
137+
return false;
138+
}
139+
140+
#endif /* CONFIG_SLUB_DEBUG */
127141

128142
static inline bool kmem_cache_debug(struct kmem_cache *s)
129143
{
@@ -4487,6 +4501,10 @@ void __init kmem_cache_init(void)
44874501
if (debug_guardpage_minorder())
44884502
slub_max_order = 0;
44894503

4504+
/* Print slub debugging pointers without hashing */
4505+
if (__slub_debug_enabled())
4506+
no_hash_pointers_enable(NULL);
4507+
44904508
kmem_cache_node = &boot_kmem_cache_node;
44914509
kmem_cache = &boot_kmem_cache;
44924510

0 commit comments

Comments
 (0)