Re: [PATCH -next] mm/slab: fix an use-after-free in kmemcg_workfn()
From: Roman Gushchin
Date: Wed Jun 19 2019 - 17:13:12 EST
On Wed, Jun 19, 2019 at 04:52:53PM -0400, Qian Cai wrote:
> The linux-next commit "mm: rework non-root kmem_cache lifecycle
> management" [1] introduced an use-after-free below because
> kmemcg_workfn() may call slab_kmem_cache_release() which has already
> freed the whole kmem_cache. Fix it by removing the bogus NULL assignment
> and checkings that will not work with SLUB_DEBUG poisoning anyway.
>
> [1] https://lore.kernel.org/patchwork/patch/1087376/
>
> BUG kmem_cache (Tainted: G B W ): Poison overwritten
> INFO: 0x(____ptrval____)-0x(____ptrval____). First byte 0x0 instead of
> 0x6b
> INFO: Allocated in create_cache+0x6c/0x1bc age=2653 cpu=154 pid=1599
> kmem_cache_alloc+0x514/0x568
> create_cache+0x6c/0x1bc
> memcg_create_kmem_cache+0xfc/0x11c
> memcg_kmem_cache_create_func+0x40/0x170
> process_one_work+0x4e0/0xa54
> worker_thread+0x498/0x650
> kthread+0x1b8/0x1d4
> ret_from_fork+0x10/0x18
> INFO: Freed in slab_kmem_cache_release+0x3c/0x48 age=255 cpu=7 pid=1505
> slab_kmem_cache_release+0x3c/0x48
> kmem_cache_release+0x1c/0x28
> kobject_cleanup+0x134/0x288
> kobject_put+0x5c/0x68
> sysfs_slab_release+0x2c/0x38
> shutdown_cache+0x190/0x234
> kmemcg_cache_shutdown_fn+0x1c/0x34
> kmemcg_workfn+0x44/0x68
> process_one_work+0x4e0/0xa54
> worker_thread+0x498/0x650
> kthread+0x1b8/0x1d4
> ret_from_fork+0x10/0x18
> INFO: Slab 0x(____ptrval____) objects=64 used=64 fp=0x(____ptrval____)
> flags=0x17ffffffc000200
> INFO: Object 0x(____ptrval____) @offset=11601272640106456192
> fp=0x(____ptrval____)
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb
> bb ................
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 00 00 00 00 00 00 00 00
> kkkkkkkk........
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
> kkkkkkkkkkkkkkkk
> Object (____ptrval____): 6b 6b 6b 6b 6b 6b 6b a5
> kkkkkkk.
> Redzone (____ptrval____): bb bb bb bb bb bb bb bb
> ........
> Padding (____ptrval____): 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a
> 5a ZZZZZZZZZZZZZZZZ
> Padding (____ptrval____): 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a
> 5a ZZZZZZZZZZZZZZZZ
> Padding (____ptrval____): 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a
> 5a ZZZZZZZZZZZZZZZZ
> Padding (____ptrval____): 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a
> 5a ZZZZZZZZZZZZZZZZ
> Padding (____ptrval____): 5a 5a 5a 5a 5a 5a 5a 5a
> ZZZZZZZZ
> CPU: 193 PID: 1557 Comm: kworker/193:1 Tainted: G B W
> 5.2.0-rc5-next-20190619+ #8
> Hardware name: HPE Apollo 70 /C01_APACHE_MB , BIOS
> L50_5.13_1.0.9 03/01/2019
> Workqueue: memcg_kmem_cache memcg_kmem_cache_create_func
> Call trace:
> dump_backtrace+0x0/0x268
> show_stack+0x20/0x2c
> dump_stack+0xb4/0x108
> print_trailer+0x274/0x298
> check_bytes_and_report+0xc4/0x118
> check_object+0x2fc/0x36c
> alloc_debug_processing+0x154/0x240
> ___slab_alloc+0x710/0xa68
> kmem_cache_alloc+0x514/0x568
> create_cache+0x6c/0x1bc
> memcg_create_kmem_cache+0xfc/0x11c
> memcg_kmem_cache_create_func+0x40/0x170
> process_one_work+0x4e0/0xa54
> worker_thread+0x498/0x650
> kthread+0x1b8/0x1d4
> ret_from_fork+0x10/0x18
> FIX kmem_cache: Restoring 0x(____ptrval____)-0x(____ptrval____)=0x6b
>
> FIX kmem_cache: Marking all objects used
>
> Signed-off-by: Qian Cai <cai@xxxxxx>
> ---
> mm/slab_common.c | 5 -----
> 1 file changed, 5 deletions(-)
>
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 91e8c739dc97..bb8aec6d8744 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -714,10 +714,7 @@ static void kmemcg_workfn(struct work_struct *work)
> get_online_mems();
>
> mutex_lock(&slab_mutex);
> -
> s->memcg_params.work_fn(s);
> - s->memcg_params.work_fn = NULL;
> -
> mutex_unlock(&slab_mutex);
Ah, perfect catch! Thank you, Qian!
Acked-by: Roman Gushchin <guro@xxxxxx>