Skip to contentSkip to conversation

mm, kasan: switch SLUB to stackdepot, enable memory quarantine for SLUB

PATCH Superseded Alexander Potapenko 2 participants 7 messages Jun 17, 2016 – Jun 21, 2016 1 message not in the archive

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alexander Potapenko

author reviewer
#0 lore raw

PATCH v4 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.

---
v4: - addressed comments by Andrey Ryabinin:
- don't set slub_debug by default for everyone;
- introduce the ___cache_free() helper function.
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/report.c        |  8 +++----
 mm/slab.h                |  7 ++++++
 mm/slub.c                | 62 +++++++++++++++++++++++++++++++++++++-----------
 9 files changed, 108 insertions(+), 52 deletions(-)
include/linux/slab.h +9 -0
index aeb3e6d..fe91eef 100644
@@ -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
index d1faa01..5585598 100644
@@ -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
index 67d8c68..bd38aab 100644
@@ -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
index 1548749..2976a9e 100644
@@ -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
index fb87923..8c75953 100644
@@ -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/report.c +3 -5
index b3c122d..861b977 100644
@@ -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 +7 -0
index dedb1a9..3381a74 100644
@@ -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
@@ -463,5 +465,10 @@ void slab_stop(struct seq_file *m, void *p);
 int memcg_slab_show(struct seq_file *m, void *p);
 
 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
+#if 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 +48 -14

125 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>
#1 lore raw
Quoted 10 lines · Alexander Potapenko

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.

Hi Alexander,

I was seeing a bunch of use-after-frees detected by kasan, such as:

BUG: KASAN: use-after-free in rb_next+0x117/0x1b0 at addr ffff8800b01d4f30
Read of size 8 by task syz-executor/31594
CPU: 2 PID: 31594 Comm: syz-executor Tainted: G        W       4.7.0-rc2-sasha-00205-g2d8a14b #3117
 1ffff10015450f0f 000000007b9351fc ffff8800aa287900 ffffffffa002778b
 ffffffff00000002 fffffbfff5630d30 0000000041b58ab3 ffffffffaaad5648
 ffffffffa002761c ffffffff9e006ab6 ffffffffa8439f65 ffffffffffffffff
Call Trace:
 [<ffffffffa002778b>] dump_stack+0x16f/0x1d4
 [<ffffffff9e79e8cf>] kasan_report_error+0x59f/0x8c0
 [<ffffffff9e79ee06>] __asan_report_load8_noabort+0x66/0x90
 [<ffffffffa003ccf7>] rb_next+0x117/0x1b0
 [<ffffffff9e71627c>] validate_mm_rb+0xac/0xd0
 [<ffffffff9e718594>] __vma_link_rb+0x2e4/0x310
 [<ffffffff9e718650>] vma_link+0x90/0x1b0
 [<ffffffff9e722870>] mmap_region+0x13a0/0x13c0
 [<ffffffff9e7232b2>] do_mmap+0xa22/0xaf0
 [<ffffffff9e6c86bf>] vm_mmap_pgoff+0x14f/0x1c0
 [<ffffffff9e71ba8b>] SyS_mmap_pgoff+0x81b/0x910
 [<ffffffff9e1bf966>] SyS_mmap+0x16/0x20
 [<ffffffff9e006ab6>] do_syscall_64+0x2a6/0x490
 [<ffffffffa8439f65>] entry_SYSCALL64_slow_path+0x25/0x25

Object at ffff8800b01d4f00, in cache vm_area_struct Object allocated with size 192 bytes. Allocation:
PID = 8855
(stack is not available)

Memory state around the buggy address:
 ffff8800b01d4e00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8800b01d4e80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>ffff8800b01d4f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                     ^
 ffff8800b01d4f80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff8800b01d5000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Or:

BUG: KASAN: use-after-free in validate_mm_rb+0x73/0xd0 at addr ffff8800b01d4f38
Read of size 8 by task syz-executor/31594
CPU: 2 PID: 31594 Comm: syz-executor Tainted: G    B   W       4.7.0-rc2-sasha-00205-g2d8a14b #3117
 1ffff10015450f16 000000007b9351fc ffff8800aa287938 ffffffffa002778b
 ffffffff00000002 fffffbfff5630d30 0000000041b58ab3 ffffffffaaad5648
 ffffffffa002761c ffffffffa84399e8 0000000000000010 ffff8800b61e8000
Call Trace:
 [<ffffffffa002778b>] dump_stack+0x16f/0x1d4
 [<ffffffff9e79e8cf>] kasan_report_error+0x59f/0x8c0
 [<ffffffff9e79ee06>] __asan_report_load8_noabort+0x66/0x90
 [<ffffffff9e716243>] validate_mm_rb+0x73/0xd0
 [<ffffffff9e718594>] __vma_link_rb+0x2e4/0x310
 [<ffffffff9e718650>] vma_link+0x90/0x1b0
 [<ffffffff9e722870>] mmap_region+0x13a0/0x13c0
 [<ffffffff9e7232b2>] do_mmap+0xa22/0xaf0
 [<ffffffff9e6c86bf>] vm_mmap_pgoff+0x14f/0x1c0
 [<ffffffff9e71ba8b>] SyS_mmap_pgoff+0x81b/0x910
 [<ffffffff9e1bf966>] SyS_mmap+0x16/0x20
 [<ffffffff9e006ab6>] do_syscall_64+0x2a6/0x490
 [<ffffffffa8439f65>] entry_SYSCALL64_slow_path+0x25/0x25

Object at ffff8800b01d4f00, in cache vm_area_struct Object allocated with size 192 bytes. Allocation:
PID = 8855
(stack is not available)

Memory state around the buggy address:
 ffff8800b01d4e00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8800b01d4e80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>ffff8800b01d4f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                        ^
 ffff8800b01d4f80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff8800b01d5000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

And bisection pointed me to this commit. Now, I'm not sure how to tell if this is memory quarantine catching something, or is just a bug with the patch?

Thanks,
Sasha

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

author reviewer
#2 lore raw
Quoted 85 lines · Sasha Levin
Quoted 10 lines · Alexander Potapenko

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.

Hi Alexander,

I was seeing a bunch of use-after-frees detected by kasan, such as:

BUG: KASAN: use-after-free in rb_next+0x117/0x1b0 at addr ffff8800b01d4f30
Read of size 8 by task syz-executor/31594
CPU: 2 PID: 31594 Comm: syz-executor Tainted: G        W       4.7.0-rc2-sasha-00205-g2d8a14b #3117
 1ffff10015450f0f 000000007b9351fc ffff8800aa287900 ffffffffa002778b
 ffffffff00000002 fffffbfff5630d30 0000000041b58ab3 ffffffffaaad5648
 ffffffffa002761c ffffffff9e006ab6 ffffffffa8439f65 ffffffffffffffff
Call Trace:
 [<ffffffffa002778b>] dump_stack+0x16f/0x1d4
 [<ffffffff9e79e8cf>] kasan_report_error+0x59f/0x8c0
 [<ffffffff9e79ee06>] __asan_report_load8_noabort+0x66/0x90
 [<ffffffffa003ccf7>] rb_next+0x117/0x1b0
 [<ffffffff9e71627c>] validate_mm_rb+0xac/0xd0
 [<ffffffff9e718594>] __vma_link_rb+0x2e4/0x310
 [<ffffffff9e718650>] vma_link+0x90/0x1b0
 [<ffffffff9e722870>] mmap_region+0x13a0/0x13c0
 [<ffffffff9e7232b2>] do_mmap+0xa22/0xaf0
 [<ffffffff9e6c86bf>] vm_mmap_pgoff+0x14f/0x1c0
 [<ffffffff9e71ba8b>] SyS_mmap_pgoff+0x81b/0x910
 [<ffffffff9e1bf966>] SyS_mmap+0x16/0x20
 [<ffffffff9e006ab6>] do_syscall_64+0x2a6/0x490
 [<ffffffffa8439f65>] entry_SYSCALL64_slow_path+0x25/0x25

Object at ffff8800b01d4f00, in cache vm_area_struct Object allocated with size 192 bytes. Allocation:
PID = 8855
(stack is not available)

Memory state around the buggy address:
 ffff8800b01d4e00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8800b01d4e80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>ffff8800b01d4f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                     ^
 ffff8800b01d4f80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff8800b01d5000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Or:

BUG: KASAN: use-after-free in validate_mm_rb+0x73/0xd0 at addr ffff8800b01d4f38
Read of size 8 by task syz-executor/31594
CPU: 2 PID: 31594 Comm: syz-executor Tainted: G    B   W       4.7.0-rc2-sasha-00205-g2d8a14b #3117
 1ffff10015450f16 000000007b9351fc ffff8800aa287938 ffffffffa002778b
 ffffffff00000002 fffffbfff5630d30 0000000041b58ab3 ffffffffaaad5648
 ffffffffa002761c ffffffffa84399e8 0000000000000010 ffff8800b61e8000
Call Trace:
 [<ffffffffa002778b>] dump_stack+0x16f/0x1d4
 [<ffffffff9e79e8cf>] kasan_report_error+0x59f/0x8c0
 [<ffffffff9e79ee06>] __asan_report_load8_noabort+0x66/0x90
 [<ffffffff9e716243>] validate_mm_rb+0x73/0xd0
 [<ffffffff9e718594>] __vma_link_rb+0x2e4/0x310
 [<ffffffff9e718650>] vma_link+0x90/0x1b0
 [<ffffffff9e722870>] mmap_region+0x13a0/0x13c0
 [<ffffffff9e7232b2>] do_mmap+0xa22/0xaf0
 [<ffffffff9e6c86bf>] vm_mmap_pgoff+0x14f/0x1c0
 [<ffffffff9e71ba8b>] SyS_mmap_pgoff+0x81b/0x910
 [<ffffffff9e1bf966>] SyS_mmap+0x16/0x20
 [<ffffffff9e006ab6>] do_syscall_64+0x2a6/0x490
 [<ffffffffa8439f65>] entry_SYSCALL64_slow_path+0x25/0x25

Object at ffff8800b01d4f00, in cache vm_area_struct Object allocated with size 192 bytes. Allocation:
PID = 8855
(stack is not available)

Memory state around the buggy address:
 ffff8800b01d4e00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
 ffff8800b01d4e80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
>ffff8800b01d4f00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                        ^
 ffff8800b01d4f80: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
 ffff8800b01d5000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Quoted 7 lines

And bisection pointed me to this commit. Now, I'm not sure how to tell if this is memory quarantine catching something, or is just a bug with the patch?

Thanks,
Sasha

Hi Sasha,

This commit delays the reuse of memory after it has been freed, so it's intended to help people find more use-after-free errors. But I'm puzzled why the stacks are missing.
Can you please share the reproduction steps for this bug? I also wonder whether it's reproducible when you:
- revert this commit?
- build with SLAB instead of SLUB?

HTH,
Alex

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>
#3 lore raw
Quoted 4 lines · Alexander Potapenko

Hi Sasha,

This commit delays the reuse of memory after it has been freed, so it's intended to help people find more use-after-free errors.

Is there a way to tell if the use-after-free access was to a memory that is quarantined?

Quoted 1 line

But I'm puzzled why the stacks are missing.

I looked at the logs, it looks like stackdepot ran out of room pretty early during boot. I've increased the max count and that solved the problem. Here's a trace with all the stacks:

[ 1157.040216] BUG: KASAN: use-after-free in print_bad_pte+0x5c7/0x6e0 at addr ffff8801b82286a0
[ 1157.040222] Read of size 8 by task syz-executor/20583
[ 1157.040236] CPU: 0 PID: 20583 Comm: syz-executor Tainted: G    B           4.7.0-rc2-next-20160609-sasha-00032-g779e0df-dirty #3123
[ 1157.040249]  1ffff10016b26e97 000000001af4d42c ffff8800b5937540 ffffffffa103380b
[ 1157.040262]  ffffffff00000000 fffffbfff5830bf4 0000000041b58ab3 ffffffffabaf1240
[ 1157.040274]  ffffffffa103369c 0000000000000006 0000000000000000 ffff8800b5937550
[ 1157.040276] Call Trace:
[ 1157.040290]  [<ffffffffa103380b>] dump_stack+0x16f/0x1d4
[ 1157.040319]  [<ffffffff9f7a148f>] kasan_report_error+0x59f/0x8c0
[ 1157.040382]  [<ffffffff9f7a19c6>] __asan_report_load8_noabort+0x66/0x90
[ 1157.040409]  [<ffffffff9f6fa5e7>] print_bad_pte+0x5c7/0x6e0
[ 1157.040418]  [<ffffffff9f702e02>] unmap_page_range+0x12f2/0x1e20
[ 1157.040445]  [<ffffffff9f703b69>] unmap_single_vma+0x239/0x250
[ 1157.040452]  [<ffffffff9f7045e9>] unmap_vmas+0x119/0x1d0
[ 1157.040461]  [<ffffffff9f720a73>] exit_mmap+0x2a3/0x410
[ 1157.040485]  [<ffffffff9f3769e2>] mmput+0x192/0x350
[ 1157.040524]  [<ffffffff9f38d745>] do_exit+0xea5/0x19e0
[ 1157.040566]  [<ffffffff9f38e5d3>] do_group_exit+0x2e3/0x2f0
[ 1157.040580]  [<ffffffff9f3b1928>] get_signal+0x1128/0x1370
[ 1157.040593]  [<ffffffff9f1afca6>] do_signal+0x86/0x1da0
[ 1157.040700]  [<ffffffff9f00539c>] exit_to_usermode_loop+0xac/0x200
[ 1157.040712]  [<ffffffff9f006c20>] do_syscall_64+0x410/0x490
[ 1157.040725]  [<ffffffffa94d0ca5>] entry_SYSCALL64_slow_path+0x25/0x25
[ 1157.040733] Object at ffff8801b8228600, in cache vm_area_struct
[ 1157.040737] Object allocated with size 192 bytes.
[ 1157.040738] Allocation:
[ 1157.040741] PID = 20521
[ 1157.040757]  [<ffffffff9f1dfae6>] save_stack_trace+0x26/0x70
[ 1157.040770]  [<ffffffff9f7a01e6>] save_stack+0x46/0xd0
[ 1157.040784]  [<ffffffff9f7a0470>] kasan_kmalloc+0x110/0x130
[ 1157.040797]  [<ffffffff9f7a09a2>] kasan_slab_alloc+0x12/0x20
[ 1157.040811]  [<ffffffff9f79a546>] kmem_cache_alloc+0x1e6/0x230
[ 1157.040826]  [<ffffffff9f7245ad>] mmap_region+0x56d/0x13c0
[ 1157.040840]  [<ffffffff9f725e22>] do_mmap+0xa22/0xaf0
[ 1157.040853]  [<ffffffff9f6cb1af>] vm_mmap_pgoff+0x14f/0x1c0
[ 1157.040889]  [<ffffffff9f71e5fb>] SyS_mmap_pgoff+0x81b/0x910
[ 1157.040901]  [<ffffffff9f1bf966>] SyS_mmap+0x16/0x20
[ 1157.040910]  [<ffffffff9f006ab6>] do_syscall_64+0x2a6/0x490
[ 1157.040919]  [<ffffffffa94d0ca5>] return_from_SYSCALL_64+0x0/0x6a
[ 1157.040920] Memory state around the buggy address:
[ 1157.040927]  ffff8801b8228580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040933]  ffff8801b8228600: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040938] >ffff8801b8228680: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040940]                                ^
[ 1157.040946]  ffff8801b8228700: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040951]  ffff8801b8228780: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
Quoted 1 line

Can you please share the reproduction steps for this bug?

Just running syzkaller inside a kvmtool guest.

Quoted 2 lines

I also wonder whether it's reproducible when you:
- revert this commit?

Not reproducible.

Quoted 1 line

- build with SLAB instead of SLUB?

Not reproducible.

Thanks,
Sasha

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

author reviewer
#4 lore raw
Quoted 6 lines · Sasha Levin
Quoted 4 lines · Alexander Potapenko

Hi Sasha,

This commit delays the reuse of memory after it has been freed, so it's intended to help people find more use-after-free errors.

Quoted 7 lines

Is there a way to tell if the use-after-free access was to a memory that is quarantined?

Quoted 1 line

But I'm puzzled why the stacks are missing.

I looked at the logs, it looks like stackdepot ran out of room pretty early during boot.

This is quite strange, as there's room for ~80k stacks in the depot, and usually the number of unique stacks is lower than 30k. I wonder if this is specific to your config, can you please share it (assuming you're using ToT kernel)?
Attached is the patch that you can try out to dump the new stacks after 30k - it's really interesting where do they come from (note the patch is not for submission).

Quoted 41 lines

I've increased the max count and that solved the
problem. Here's a trace with all the stacks:

[ 1157.040216] BUG: KASAN: use-after-free in print_bad_pte+0x5c7/0x6e0 at addr ffff8801b82286a0
[ 1157.040222] Read of size 8 by task syz-executor/20583
[ 1157.040236] CPU: 0 PID: 20583 Comm: syz-executor Tainted: G    B           4.7.0-rc2-next-20160609-sasha-00032-g779e0df-dirty #3123
[ 1157.040249]  1ffff10016b26e97 000000001af4d42c ffff8800b5937540 ffffffffa103380b
[ 1157.040262]  ffffffff00000000 fffffbfff5830bf4 0000000041b58ab3 ffffffffabaf1240
[ 1157.040274]  ffffffffa103369c 0000000000000006 0000000000000000 ffff8800b5937550
[ 1157.040276] Call Trace:
[ 1157.040290]  [<ffffffffa103380b>] dump_stack+0x16f/0x1d4
[ 1157.040319]  [<ffffffff9f7a148f>] kasan_report_error+0x59f/0x8c0
[ 1157.040382]  [<ffffffff9f7a19c6>] __asan_report_load8_noabort+0x66/0x90
[ 1157.040409]  [<ffffffff9f6fa5e7>] print_bad_pte+0x5c7/0x6e0
[ 1157.040418]  [<ffffffff9f702e02>] unmap_page_range+0x12f2/0x1e20
[ 1157.040445]  [<ffffffff9f703b69>] unmap_single_vma+0x239/0x250
[ 1157.040452]  [<ffffffff9f7045e9>] unmap_vmas+0x119/0x1d0
[ 1157.040461]  [<ffffffff9f720a73>] exit_mmap+0x2a3/0x410
[ 1157.040485]  [<ffffffff9f3769e2>] mmput+0x192/0x350
[ 1157.040524]  [<ffffffff9f38d745>] do_exit+0xea5/0x19e0
[ 1157.040566]  [<ffffffff9f38e5d3>] do_group_exit+0x2e3/0x2f0
[ 1157.040580]  [<ffffffff9f3b1928>] get_signal+0x1128/0x1370
[ 1157.040593]  [<ffffffff9f1afca6>] do_signal+0x86/0x1da0
[ 1157.040700]  [<ffffffff9f00539c>] exit_to_usermode_loop+0xac/0x200
[ 1157.040712]  [<ffffffff9f006c20>] do_syscall_64+0x410/0x490
[ 1157.040725]  [<ffffffffa94d0ca5>] entry_SYSCALL64_slow_path+0x25/0x25
[ 1157.040733] Object at ffff8801b8228600, in cache vm_area_struct
[ 1157.040737] Object allocated with size 192 bytes.
[ 1157.040738] Allocation:
[ 1157.040741] PID = 20521
[ 1157.040757]  [<ffffffff9f1dfae6>] save_stack_trace+0x26/0x70
[ 1157.040770]  [<ffffffff9f7a01e6>] save_stack+0x46/0xd0
[ 1157.040784]  [<ffffffff9f7a0470>] kasan_kmalloc+0x110/0x130
[ 1157.040797]  [<ffffffff9f7a09a2>] kasan_slab_alloc+0x12/0x20
[ 1157.040811]  [<ffffffff9f79a546>] kmem_cache_alloc+0x1e6/0x230
[ 1157.040826]  [<ffffffff9f7245ad>] mmap_region+0x56d/0x13c0
[ 1157.040840]  [<ffffffff9f725e22>] do_mmap+0xa22/0xaf0
[ 1157.040853]  [<ffffffff9f6cb1af>] vm_mmap_pgoff+0x14f/0x1c0
[ 1157.040889]  [<ffffffff9f71e5fb>] SyS_mmap_pgoff+0x81b/0x910
[ 1157.040901]  [<ffffffff9f1bf966>] SyS_mmap+0x16/0x20
[ 1157.040910]  [<ffffffff9f006ab6>] do_syscall_64+0x2a6/0x490
Quoted 8 lines
[ 1157.040919]  [<ffffffffa94d0ca5>] return_from_SYSCALL_64+0x0/0x6a
[ 1157.040920] Memory state around the buggy address:
[ 1157.040927]  ffff8801b8228580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040933]  ffff8801b8228600: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040938] >ffff8801b8228680: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040940]                                ^
[ 1157.040946]  ffff8801b8228700: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040951]  ffff8801b8228780: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc

Again, it's interesting why is the deallocation stack missing in a use-after-free report.

Quoted 3 lines
Quoted 1 line

Can you please share the reproduction steps for this bug?

Just running syzkaller inside a kvmtool guest.

Any syzkaller programs in the logs?

Quoted 12 lines
Quoted 2 lines

I also wonder whether it's reproducible when you:
- revert this commit?

Not reproducible.

Quoted 1 line

- build with SLAB instead of SLUB?

Not reproducible.

Thanks,
Sasha

Signature · 10 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
lib/stackdepot.c +6 -0
index 53ad6c0..f1d84e1 100644
@@ -104,6 +104,7 @@ static bool init_stack_slab(void **prealloc)
 	return true;
 }
 
+static int alloc_cnt = 0;  /* imprecise */
 /* Allocation of a new stack in raw storage */
 static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
 		u32 hash, void **prealloc, gfp_t alloc_flags)
@@ -143,6 +144,11 @@ static struct stack_record *depot_alloc_stack(unsigned long *entries, int size,
 	memcpy(stack->entries, entries, size * sizeof(unsigned long));
 	depot_offset += required_size;
 
+	alloc_cnt++;
+	if (alloc_cnt % 100 == 0)
+		pr_err("alloc_cnt: %d\n", alloc_cnt);
+	if ((alloc_cnt > 20000) && (alloc_cnt % 10 == 0))
+		dump_stack();
 	return stack;
 }
 

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#6 lore raw
Quoted 23 lines · Sasha Levin
Quoted 14 lines · Alexander Potapenko
Quoted 6 lines · Sasha Levin
Quoted 6 lines · Alexander Potapenko
Quoted 4 lines
Quoted 4 lines

Hi Sasha,

This commit delays the reuse of memory after it has been freed, so it's intended to help people find more use-after-free errors.

Quoted 7 lines
Quoted 7 lines

Is there a way to tell if the use-after-free access was to a memory that is quarantined?

Quoted 1 line
Quoted 1 line

But I'm puzzled why the stacks are missing.

I looked at the logs, it looks like stackdepot ran out of room pretty early during boot.

Quoted 7 lines

This is quite strange, as there's room for ~80k stacks in the depot, and usually the number of unique stacks is lower than 30k. I wonder if this is specific to your config, can you please share it (assuming you're using ToT kernel)?
Attached is the patch that you can try out to dump the new stacks after 30k - it's really interesting where do they come from (note the patch is not for submission).

Quoted 1 line

Attached a log file generated with that patch, and my kernel config.

I haven't looked close yet, but your log contains 1455 unique 'DRIVERNAME_driver_init' function names, for which it's quite likely that 80k allocation stacks are generated.
Can you remove the dump_stack() call from my patch, fuzz for a while and see to which number does |alloc_cnt| converge on your machine? Maybe our estimate was just too optimistic, and we need to increase the memory limit for the stack depot.

Quoted 4 lines

Thanks,
Sasha

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>
author reviewer
#7 lore raw
In reply to Sasha Levin
Quoted 8 lines · Sasha Levin
Quoted 4 lines · Alexander Potapenko

Hi Sasha,

This commit delays the reuse of memory after it has been freed, so it's intended to help people find more use-after-free errors.

Is there a way to tell if the use-after-free access was to a memory that is quarantined?

Sorry, missed this question.
In theory, these can be distinguished by looking at alloc_info->state (see kasan_object_err() in mm/kasan/report.c): the quarantined chunk should have the KASAN_STATE_QUARANTINE state.
But your report contains the line "Object allocated with size 192 bytes.", which means the chunk had the KASAN_STATE_ALLOC state. Therefore, we're dealing with an inconsistency between the shadow memory value (0xFB, i.e. freed memory) and the chunk header (KASAN_STATE_ALLOC) here.
Can you apply "mm, kasan: improve double-free detection" by Kuthonuzo Luruo and see if the report changes?

Quoted 69 lines
Quoted 1 line

But I'm puzzled why the stacks are missing.

I looked at the logs, it looks like stackdepot ran out of room pretty early during boot. I've increased the max count and that solved the problem. Here's a trace with all the stacks:

[ 1157.040216] BUG: KASAN: use-after-free in print_bad_pte+0x5c7/0x6e0 at addr ffff8801b82286a0
[ 1157.040222] Read of size 8 by task syz-executor/20583
[ 1157.040236] CPU: 0 PID: 20583 Comm: syz-executor Tainted: G    B           4.7.0-rc2-next-20160609-sasha-00032-g779e0df-dirty #3123
[ 1157.040249]  1ffff10016b26e97 000000001af4d42c ffff8800b5937540 ffffffffa103380b
[ 1157.040262]  ffffffff00000000 fffffbfff5830bf4 0000000041b58ab3 ffffffffabaf1240
[ 1157.040274]  ffffffffa103369c 0000000000000006 0000000000000000 ffff8800b5937550
[ 1157.040276] Call Trace:
[ 1157.040290]  [<ffffffffa103380b>] dump_stack+0x16f/0x1d4
[ 1157.040319]  [<ffffffff9f7a148f>] kasan_report_error+0x59f/0x8c0
[ 1157.040382]  [<ffffffff9f7a19c6>] __asan_report_load8_noabort+0x66/0x90
[ 1157.040409]  [<ffffffff9f6fa5e7>] print_bad_pte+0x5c7/0x6e0
[ 1157.040418]  [<ffffffff9f702e02>] unmap_page_range+0x12f2/0x1e20
[ 1157.040445]  [<ffffffff9f703b69>] unmap_single_vma+0x239/0x250
[ 1157.040452]  [<ffffffff9f7045e9>] unmap_vmas+0x119/0x1d0
[ 1157.040461]  [<ffffffff9f720a73>] exit_mmap+0x2a3/0x410
[ 1157.040485]  [<ffffffff9f3769e2>] mmput+0x192/0x350
[ 1157.040524]  [<ffffffff9f38d745>] do_exit+0xea5/0x19e0
[ 1157.040566]  [<ffffffff9f38e5d3>] do_group_exit+0x2e3/0x2f0
[ 1157.040580]  [<ffffffff9f3b1928>] get_signal+0x1128/0x1370
[ 1157.040593]  [<ffffffff9f1afca6>] do_signal+0x86/0x1da0
[ 1157.040700]  [<ffffffff9f00539c>] exit_to_usermode_loop+0xac/0x200
[ 1157.040712]  [<ffffffff9f006c20>] do_syscall_64+0x410/0x490
[ 1157.040725]  [<ffffffffa94d0ca5>] entry_SYSCALL64_slow_path+0x25/0x25
[ 1157.040733] Object at ffff8801b8228600, in cache vm_area_struct
[ 1157.040737] Object allocated with size 192 bytes.
[ 1157.040738] Allocation:
[ 1157.040741] PID = 20521
[ 1157.040757]  [<ffffffff9f1dfae6>] save_stack_trace+0x26/0x70
[ 1157.040770]  [<ffffffff9f7a01e6>] save_stack+0x46/0xd0
[ 1157.040784]  [<ffffffff9f7a0470>] kasan_kmalloc+0x110/0x130
[ 1157.040797]  [<ffffffff9f7a09a2>] kasan_slab_alloc+0x12/0x20
[ 1157.040811]  [<ffffffff9f79a546>] kmem_cache_alloc+0x1e6/0x230
[ 1157.040826]  [<ffffffff9f7245ad>] mmap_region+0x56d/0x13c0
[ 1157.040840]  [<ffffffff9f725e22>] do_mmap+0xa22/0xaf0
[ 1157.040853]  [<ffffffff9f6cb1af>] vm_mmap_pgoff+0x14f/0x1c0
[ 1157.040889]  [<ffffffff9f71e5fb>] SyS_mmap_pgoff+0x81b/0x910
[ 1157.040901]  [<ffffffff9f1bf966>] SyS_mmap+0x16/0x20
[ 1157.040910]  [<ffffffff9f006ab6>] do_syscall_64+0x2a6/0x490
[ 1157.040919]  [<ffffffffa94d0ca5>] return_from_SYSCALL_64+0x0/0x6a
[ 1157.040920] Memory state around the buggy address:
[ 1157.040927]  ffff8801b8228580: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040933]  ffff8801b8228600: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040938] >ffff8801b8228680: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
[ 1157.040940]                                ^
[ 1157.040946]  ffff8801b8228700: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
[ 1157.040951]  ffff8801b8228780: fb fb fb fb fb fb fb fb fc fc fc fc fc fc fc fc
Quoted 1 line

Can you please share the reproduction steps for this bug?

Just running syzkaller inside a kvmtool guest.

Quoted 2 lines

I also wonder whether it's reproducible when you:
- revert this commit?

Not reproducible.

Quoted 1 line

- build with SLAB instead of SLUB?

Not reproducible.

Thanks,
Sasha

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>