Skip to contentSkip to conversation

slub: Improve bit diffusion for freelist ptr obfuscation

PATCH Applied Kees Cook 2 participants 5 messages Mar 6, 2020 – Mar 9, 2020

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 slub: Improve bit diffusion for freelist ptr obfuscation

Under CONFIG_SLAB_FREELIST_HARDENED=y, the obfuscation was relatively weak in that the ptr and ptr address were usually so close that the first XOR would result in an almost entirely 0-byte value[1], leaving most of the "secret" number ultimately being stored after the third XOR. A single blind memory content exposure of the freelist was generally sufficient to learn the secret.

Add a swab() call to mix bits a little more. This is a cheap way (1 cycle) to make attacks need more than a single exposure to learn the secret (or to know _where_ the exposure is in memory).

kmalloc-32 freelist walk, before:

ptr ptr_addr stored value secret
ffff90c22e019020@ffff90c22e019000 is 86528eb656b3b5bd (86528eb656b3b59d) ffff90c22e019040@ffff90c22e019020 is 86528eb656b3b5fd (86528eb656b3b59d) ffff90c22e019060@ffff90c22e019040 is 86528eb656b3b5bd (86528eb656b3b59d) ffff90c22e019080@ffff90c22e019060 is 86528eb656b3b57d (86528eb656b3b59d) ffff90c22e0190a0@ffff90c22e019080 is 86528eb656b3b5bd (86528eb656b3b59d)
...

after:

ptr ptr_addr stored value secret
ffff9eed6e019020@ffff9eed6e019000 is 793d1135d52cda42 (86528eb656b3b59d) ffff9eed6e019040@ffff9eed6e019020 is 593d1135d52cda22 (86528eb656b3b59d) ffff9eed6e019060@ffff9eed6e019040 is 393d1135d52cda02 (86528eb656b3b59d) ffff9eed6e019080@ffff9eed6e019060 is 193d1135d52cdae2 (86528eb656b3b59d) ffff9eed6e0190a0@ffff9eed6e019080 is f93d1135d52cdac2 (86528eb656b3b59d)

[1] https://blog.infosectcbr.com.au/2020/03/weaknesses-in-linux-kernel-heap.html

Fixes 2482ddec670f ("mm: add SLUB free list pointer obfuscation")
---
 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index 17dc00e33115..107d9d89cf96 100644
@@ -259,7 +259,7 @@ static inline void *freelist_ptr(const struct kmem_cache *s, void *ptr,
 	 * freepointer to be restored incorrectly.
 	 */
 	return (void *)((unsigned long)ptr ^ s->random ^
-			(unsigned long)kasan_reset_tag((void *)ptr_addr));
+			swab((unsigned long)kasan_reset_tag((void *)ptr_addr)));
 #else
 	return ptr;
 #endif
Signature · 5 lines
2.20.1


-- 
Kees Cook
maintainer
#1 lore raw

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: 2482ddec670f ("mm: add SLUB free list pointer obfuscation").

The bot has tested the following trees: v5.5.8, v5.4.24, v4.19.108, v4.14.172.

v5.5.8: Build failed! Errors:
mm/slub.c:262:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

v5.4.24: Build failed! Errors:
mm/slub.c:264:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

v4.19.108: Failed to apply! Possible dependencies:
d36a63a943e3 ("kasan, slub: fix more conflicts with CONFIG_SLAB_FREELIST_HARDENED")

v4.14.172: Failed to apply! Possible dependencies:
d36a63a943e3 ("kasan, slub: fix more conflicts with CONFIG_SLAB_FREELIST_HARDENED")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
author
#2 lore raw
Quoted 4 lines · Sasha Levin

Hi

[This is an automated email]

Quoted 8 lines

This commit has been processed because it contains a "Fixes:" tag fixing commit: 2482ddec670f ("mm: add SLUB free list pointer obfuscation").

The bot has tested the following trees: v5.5.8, v5.4.24, v4.19.108, v4.14.172.

v5.5.8: Build failed! Errors:
mm/slub.c:262:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

Eek; this must be missing an include that is implicit from something recent? I will investigate...

-Kees

Quoted 17 lines

v5.4.24: Build failed! Errors:
mm/slub.c:264:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

v4.19.108: Failed to apply! Possible dependencies:
d36a63a943e3 ("kasan, slub: fix more conflicts with CONFIG_SLAB_FREELIST_HARDENED")

v4.14.172: Failed to apply! Possible dependencies:
d36a63a943e3 ("kasan, slub: fix more conflicts with CONFIG_SLAB_FREELIST_HARDENED")

NOTE: The patch will not be queued to stable trees until it is upstream.

How should we proceed with this patch?

Signature · 2 lines
Thanks
Sasha
Signature · 1 line
Kees Cook
maintainer
#3 lore raw
Quoted 8 lines · Kees Cook
Quoted 7 lines · Sasha Levin

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: 2482ddec670f ("mm: add SLUB free list pointer obfuscation").

Quoted 8 lines
Quoted 5 lines

The bot has tested the following trees: v5.5.8, v5.4.24, v4.19.108, v4.14.172.

v5.5.8: Build failed! Errors:
mm/slub.c:262:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

Eek; this must be missing an include that is implicit from something recent? I will investigate...

swab() didn't exist "back then" :) It was introduced by d5767057c9a7 ("uapi: rename ext2_swab() to swab() and share globally in swab.h"). I can just take that patch to stable kernels.

Signature · 2 lines
Thanks,
Sasha
author
#4 lore raw
Quoted 15 lines · Sasha Levin
Quoted 14 lines · Kees Cook
Quoted 12 lines · Sasha Levin

Hi

[This is an automated email]

This commit has been processed because it contains a "Fixes:" tag fixing commit: 2482ddec670f ("mm: add SLUB free list pointer obfuscation").

The bot has tested the following trees: v5.5.8, v5.4.24, v4.19.108, v4.14.172.

v5.5.8: Build failed! Errors:
mm/slub.c:262:4: error: implicit declaration of function ‘swab’; did you mean ‘swap’? [-Werror=implicit-function-declaration]

Quoted 6 lines
Quoted 2 lines

Eek; this must be missing an include that is implicit from something recent? I will investigate...

swab() didn't exist "back then" :) It was introduced by d5767057c9a7 ("uapi: rename ext2_swab() to swab() and share globally in swab.h"). I can just take that patch to stable kernels.

Ah, perfect. Yes, that would be very nice.

Signature · 1 line
Kees Cook