mm, kasan: switch SLUB to stackdepot, enable memory quarantine for SLUB
Subsystems (4)
Alexander Potapenko
reviewerPATCH v3 1/1 mm, kasan: switch SLUB to stackdepot, enable memory quarantine for SLUB
For KASAN builds:
- switch SLUB allocator to using stackdepot instead of storing the allocation/deallocation stacks in the objects;
- define SLAB_RED_ZONE, SLAB_POISON, SLAB_STORE_USER to zero, effectively disabling these debug features, as they're redundant in the presence of KASAN;
- change the freelist hook so that parts of the freelist can be put into the quarantine.
---
v3: - addressed comments by Andrey Ryabinin:
- replaced KMALLOC_MAX_CACHE_SIZE with KMALLOC_MAX_SIZE in kasan_cache_create();
- for caches with SLAB_KASAN flag set, their alloc_meta_offset and free_meta_offset are always valid.
v2: - incorporated kbuild fixes by Andrew Morton
--- include/linux/slab.h | 9 +++++++ include/linux/slub_def.h | 4 ++++ lib/Kconfig.kasan | 4 ++-- mm/kasan/Makefile | 3 +-- mm/kasan/kasan.c | 61 ++++++++++++++++++++++++++---------------------- mm/kasan/kasan.h | 2 +- mm/kasan/quarantine.c | 5 ++++ mm/kasan/report.c | 8 +++---- mm/slab.h | 8 +++++++ mm/slub.c | 58 +++++++++++++++++++++++++++++++++------------ 10 files changed, 109 insertions(+), 53 deletions(-)
include/linux/slab.h +9 -0
@@ -21,11 +21,20 @@* The ones marked DEBUG are only valid if CONFIG_DEBUG_SLAB is set. */ #define SLAB_CONSISTENCY_CHECKS 0x00000100UL/* DEBUG: Perform (expensive) checks on alloc/free */ +#ifndef CONFIG_KASAN #define SLAB_RED_ZONE 0x00000400UL/* DEBUG: Red zone objs in a cache */ #define SLAB_POISON 0x00000800UL/* DEBUG: Poison objects */ +#else +#define SLAB_RED_ZONE 0x00000000UL/* KASAN has its own redzones */ +#define SLAB_POISON 0x00000000UL/* No extra poisoning */ +#endif #define SLAB_HWCACHE_ALIGN 0x00002000UL/* Align objs on cache lines */ #define SLAB_CACHE_DMA 0x00004000UL/* Use GFP_DMA memory */ +#ifndef CONFIG_KASAN #define SLAB_STORE_USER 0x00010000UL/* DEBUG: Store the last owner for bug hunting */ +#else +#define SLAB_STORE_USER 0x00000000UL/* KASAN uses stack depot */ +#endif #define SLAB_PANIC 0x00040000UL/* Panic if kmem_cache_create() fails */ /* * SLAB_DESTROY_BY_RCU - **WARNING** READ THIS!
include/linux/slub_def.h +4 -0
@@ -99,6 +99,10 @@ struct kmem_cache {*/ int remote_node_defrag_ratio;#endif +#ifdef CONFIG_KASAN +struct kasan_cache kasan_info; +#endif +struct kmem_cache_node *node[MAX_NUMNODES ]; };
lib/Kconfig.kasan +2 -2
@@ -5,9 +5,9 @@ if HAVE_ARCH_KASAN config KASAN bool "KASan: runtime memory debugger" - depends on SLUB_DEBUG || (SLAB && !DEBUG_SLAB) + depends on SLUB || (SLAB && !DEBUG_SLAB) select CONSTRUCTORS - select STACKDEPOT if SLAB + select STACKDEPOT help Enables kernel address sanitizer - runtime memory debugger, designed to find out-of-bounds accesses and use-after-free bugs.
mm/kasan/Makefile +1 -2
@@ -7,5 +7,4 @@ CFLAGS_REMOVE_kasan.o = -pg # see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63533 CFLAGS_kasan.o := $(call cc-option, -fno-conserve-stack -fno-stack-protector) -obj-y := kasan.o report.o kasan_init.o -obj-$(CONFIG_SLAB) += quarantine.o +obj-y := kasan.o report.o kasan_init.o quarantine.o
mm/kasan/kasan.c +33 -28
144 lines not on this page · raw message
mm/kasan/kasan.h +1 -1
@@ -110,7 +110,7 @@ static inline bool kasan_report_enabled(void)void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); -#ifdef CONFIG_SLAB +#if defined(CONFIG_SLAB ) || defined(CONFIG_SLUB )void quarantine_put (struct kasan_free_meta *info,struct kmem_cache *cache);void quarantine_reduce (void );void quarantine_remove_cache (struct kmem_cache *cache);
mm/kasan/quarantine.c +5 -0
@@ -149,7 +149,12 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)local_irq_save (flags); alloc_info->state =KASAN_STATE_FREE ; +#ifdef CONFIG_SLAB ___cache_free (cache, object, _THIS_IP_); +#elif defined(CONFIG_SLUB ) +do_slab_free (cache,virt_to_head_page (object), object,NULL ,1 , + _RET_IP_); +#endif local_irq_restore (flags); }
mm/kasan/report.c +3 -5
@@ -116,7 +116,6 @@ static inline bool init_task_stack_addr(const void *addr) sizeof(init_thread_union.stack)); } -#ifdef CONFIG_SLAB static void print_track (struct kasan_track *track) {pr_err ("PID = %u\n" , track->pid);
@@ -130,8 +129,8 @@ static void print_track(struct kasan_track *track) } } -static void object_err (struct kmem_cache *cache,struct page *page, -void *object,char *unused_reason) +static void kasan_object_err (struct kmem_cache *cache,struct page *page, +void *object,char *unused_reason) {struct kasan_alloc_meta *alloc_info =get_alloc_info (cache, object);struct kasan_free_meta *free_info;
@@ -162,7 +161,6 @@ static void object_err(struct kmem_cache *cache, struct page *page,break ; } } -#endif static void print_address_description (struct kasan_access_info *info) {
@@ -177,7 +175,7 @@ static void print_address_description(struct kasan_access_info *info)struct kmem_cache *cache = page->slab_cache; object =nearest_obj (cache, page, (void *)info->access_addr); -object_err (cache, page, object, +kasan_object_err (cache, page, object,"kasan: bad access detected" );return ; }
mm/slab.h +8 -0
@@ -366,6 +366,8 @@ static inline size_t slab_ksize(const struct kmem_cache *s)if (s->flags & (SLAB_RED_ZONE |SLAB_POISON ))return s->object_size;# endif +if (s->flags &SLAB_KASAN ) +return s->object_size;/* * If we have the need to store the freelist pointer * back there or track user information then we can
@@ -462,6 +464,12 @@ void *slab_next(struct seq_file *m, void *p, loff_t *pos);void slab_stop (struct seq_file *m,void *p);int memcg_slab_show (struct seq_file *m,void *p); +#if defined(CONFIG_SLAB )void ___cache_free (struct kmem_cache *cache,void *x,unsigned long addr); +#elif defined(CONFIG_SLUB ) +void do_slab_free (struct kmem_cache *s, +struct page *page,void *head,void *tail, +int cnt,unsigned long addr); +#endif #endif /* MM_SLAB_H */
mm/slub.c +43 -15
116 lines not on this page · raw message
Signature · 7 lines
2.8.0.rc3.226.g39d4020 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Alexander Potapenko
For KASAN builds:
- switch SLUB allocator to using stackdepot instead of storing the allocation/deallocation stacks in the objects;
- defineSLAB_RED_ZONE,SLAB_POISON,SLAB_STORE_USERto zero, effectively disabling these debug features, as they're redundant in the presence of KASAN;
So, why we forbid these? If user wants to set these, why not? If you don't want it, just don't turn them on, that's it.
And sometimes POISON/REDZONE might be actually useful. KASAN doesn't catch everything, e.g. corruption may happen in assembly code, or DMA by some device.
Quoted 5 lines
- change the freelist hook so that parts of the freelist can be put into the quarantine.
Signed-off-by Alexander Potapenko <[email protected]>---
...
Quoted 18 lines
mm/kasan/kasan.h +1
-1@@ -110,7 +110,7 @@ static inline bool kasan_report_enabled(void)void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); -#ifdef CONFIG_SLAB +#if defined(CONFIG_SLAB ) || defined(CONFIG_SLUB )void quarantine_put (struct kasan_free_meta *info,struct kmem_cache *cache);void quarantine_reduce (void );void quarantine_remove_cache (struct kmem_cache *cache);mm/kasan/quarantine.c +0
-0
mm/kasan/quarantine.c static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) · line 157
Quoted 9 lines
mm/kasan/quarantine.c +5
-0@@ -149,7 +149,12 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)local_irq_save (flags); alloc_info->state =KASAN_STATE_FREE ; +#ifdef CONFIG_SLAB ___cache_free (cache, object, _THIS_IP_); +#elif defined(CONFIG_SLUB ) +do_slab_free (cache,virt_to_head_page (object), object,NULL ,1 , + _RET_IP_); +#endif
Please, add some simple wrapper instead of this.
Quoted 3 lines
mm/kasan/quarantine.c +0
-0local_irq_restore (flags); }
...
Quoted 17 lines
mm/slub.c +4
-0@@ -191,7 +191,11 @@ static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)#define MAX_OBJS_PER_PAGE 32767/* since page.objects is u15 */ /* Internal SLUB flags */ +#ifndef CONFIG_KASAN #define __OBJECT_POISON 0x80000000UL/* Poison object */ +#else +#define __OBJECT_POISON 0x00000000UL/* Disable object poisoning */ +#endif #define __CMPXCHG_DOUBLE 0x40000000UL/* Use cmpxchg_double */ #ifdef CONFIG_SMP
mm/slub.c static inline void *restore_red_left(struct kmem_cache *s, void *p) · line 462
Quoted 8 lines
mm/slub.c +1
-3@@ -454,10 +458,8 @@ static inline void *restore_red_left(struct kmem_cache *s, void *p)*/ #if defined(CONFIG_SLUB_DEBUG_ON )static int slub_debug =DEBUG_DEFAULT_FLAGS ; -#elif defined(CONFIG_KASAN ) -static int slub_debug =SLAB_STORE_USER ;#else -static int slub_debug; +static int slub_debug =SLAB_STORE_USER ;
Huh! So now it is on!? By default, and for everyone!
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Alexander Potapenko
reviewerQuoted 11 lines · Andrey Ryabinin
Quoted 6 lines · Alexander Potapenko
For KASAN builds:
- switch SLUB allocator to using stackdepot instead of storing the allocation/deallocation stacks in the objects;
- defineSLAB_RED_ZONE,SLAB_POISON,SLAB_STORE_USERto zero, effectively disabling these debug features, as they're redundant in the presence of KASAN;So, why we forbid these? If user wants to set these, why not? If you don't want it, just don't turn them on, that's it.
SLAB_RED_ZONE simply doesn't work with KASAN.
With additional efforts it may work, but I don't think we really need that. Extra red zones will just bloat the heap, and won't give any interesting signal except "someone corrupted this object from non-instrumented code".SLAB_POISON doesn't crash on simple tests, but I am not sure there are no corner cases which I haven't checked, so I thought it's safer to disable it.
As I said before, we can make SLAB_STORE_USER use stackdepot in a later CL, thus we disable it now.
Quoted 31 lines
And sometimes POISON/REDZONE might be actually useful. KASAN doesn't catch everything, e.g. corruption may happen in assembly code, or DMA by some device.
Quoted 5 lines
- change the freelist hook so that parts of the freelist can be put into the quarantine.
Signed-off-by Alexander Potapenko <[email protected]>---
...
Quoted 19 lines
mm/kasan/kasan.h +1
-1@@ -110,7 +110,7 @@ static inline bool kasan_report_enabled(void)void kasan_report (unsigned long addr,size_t size,bool is_write,unsigned long ip); -#ifdef CONFIG_SLAB +#if defined(CONFIG_SLAB ) || defined(CONFIG_SLUB )void quarantine_put (struct kasan_free_meta *info,struct kmem_cache *cache);void quarantine_reduce (void );void quarantine_remove_cache (struct kmem_cache *cache);mm/kasan/quarantine.c +0
-0@@ -149,7 +149,12 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache)
Quoted 14 lines
mm/kasan/quarantine.c static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) · line 157
Quoted 8 lines
mm/kasan/quarantine.c +5
-0local_irq_save (flags); alloc_info->state =KASAN_STATE_FREE ; +#ifdef CONFIG_SLAB ___cache_free (cache, object, _THIS_IP_); +#elif defined(CONFIG_SLUB ) +do_slab_free (cache,virt_to_head_page (object), object,NULL ,1 , + _RET_IP_); +#endif Please, add some simple wrapper instead of this.
Quoted 3 lines
mm/kasan/quarantine.c +0
-0local_irq_restore (flags); }
Done. I've reused ___cache_free().
Quoted 18 lines
...
Quoted 15 lines
mm/slub.c +4
-0@@ -191,7 +191,11 @@ static inline bool kmem_cache_has_cpu_partial(struct kmem_cache *s)#define MAX_OBJS_PER_PAGE 32767/* since page.objects is u15 */ /* Internal SLUB flags */ +#ifndef CONFIG_KASAN #define __OBJECT_POISON 0x80000000UL/* Poison object */ +#else +#define __OBJECT_POISON 0x00000000UL/* Disable object poisoning */ +#endif #define __CMPXCHG_DOUBLE 0x40000000UL/* Use cmpxchg_double */
Quoted 13 lines
mm/slub.c static inline void *restore_red_left(struct kmem_cache *s, void *p) · line 462
Quoted 10 lines
mm/slub.c +1
-3#ifdef CONFIG_SMP @@ -454,10 +458,8 @@ static inline void *restore_red_left(struct kmem_cache *s, void *p)*/ #if defined(CONFIG_SLUB_DEBUG_ON )static int slub_debug =DEBUG_DEFAULT_FLAGS ; -#elif defined(CONFIG_KASAN ) -static int slub_debug =SLAB_STORE_USER ;#else -static int slub_debug; +static int slub_debug =SLAB_STORE_USER ;Huh! So now it is on!? By default, and for everyone!
Good catch, thanks!
Signature · 16 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 12 lines · Alexander Potapenko
Quoted 10 lines · Andrey Ryabinin
Quoted 6 lines · Alexander Potapenko
For KASAN builds:
- switch SLUB allocator to using stackdepot instead of storing the allocation/deallocation stacks in the objects;
- defineSLAB_RED_ZONE,SLAB_POISON,SLAB_STORE_USERto zero, effectively disabling these debug features, as they're redundant in the presence of KASAN;
Quoted 2 lines
Quoted 1 line
So, why we forbid these? If user wants to set these, why not? If you don't want it, just don't turn them on, that's it.
SLAB_RED_ZONEsimply doesn't work with KASAN.
Why? This sounds like a bug.
Quoted 10 lines
With additional efforts it may work, but I don't think we really need that. Extra red zones will just bloat the heap, and won't give any interesting signal except "someone corrupted this object from non-instrumented code".
SLAB_POISONdoesn't crash on simple tests, but I am not sure there are no corner cases which I haven't checked, so I thought it's safer to disable it.
As I said before, we can makeSLAB_STORE_USERuse stackdepot in a later CL, thus we disable it now.
This doesn't explain why we need this. What's the problem you are trying to solve by this? And why it is ok to silently ignore user requests? You think that these options are redundant, I get it. Well, then just don't turn them on.
But, when a user requests for something, he expects that such request will be fulfilled, not just ignored.
Quoted 4 lines
Quoted 4 lines
And sometimes POISON/REDZONE might be actually useful. KASAN doesn't catch everything, e.g. corruption may happen in assembly code, or DMA by some device.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Alexander Potapenko
reviewerQuoted 15 lines · Andrey Ryabinin
Quoted 12 lines · Alexander Potapenko
Quoted 10 lines · Andrey Ryabinin
Quoted 6 lines · Alexander Potapenko
For KASAN builds:
- switch SLUB allocator to using stackdepot instead of storing the allocation/deallocation stacks in the objects;
- defineSLAB_RED_ZONE,SLAB_POISON,SLAB_STORE_USERto zero, effectively disabling these debug features, as they're redundant in the presence of KASAN;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
So, why we forbid these? If user wants to set these, why not? If you don't want it, just don't turn them on, that's it.
SLAB_RED_ZONEsimply doesn't work with KASAN.Why? This sounds like a bug.
I'm looking now. There are some issues with the left redzone being added, which messes up the offsets.
I'd say it's no surprise that different debugging tools do not work together, like e.g. KASAN and kmemcheck are not expected to.
Quoted 11 lines
Quoted 10 lines
With additional efforts it may work, but I don't think we really need that. Extra red zones will just bloat the heap, and won't give any interesting signal except "someone corrupted this object from non-instrumented code".
SLAB_POISONdoesn't crash on simple tests, but I am not sure there are no corner cases which I haven't checked, so I thought it's safer to disable it.
As I said before, we can makeSLAB_STORE_USERuse stackdepot in a later CL, thus we disable it now.
Quoted 1 line
This doesn't explain why we need this. What's the problem you are trying to solve by this? And why it is ok to silently ignore user requests?
Agreed, there's no point in redefining the flag constants.
Quoted 2 lines
You think that these options are redundant, I get it. Well, then just don't turn them on. But, when a user requests for something, he expects that such request will be fulfilled, not just ignored.
Yes, I'd better just document the incompatibility between the different operation modes (if I don't solve the problem).
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
And sometimes POISON/REDZONE might be actually useful. KASAN doesn't catch everything, e.g. corruption may happen in assembly code, or DMA by some device.
Signature · 16 lines
Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>