LKML Archive on lore.kernel.org
 help / color / Atom feed
From: Qian Cai <cai@lca.pw>
To: akpm@linux-foundation.org
Cc: guro@fb.com, vdavydov.dev@gmail.com, hannes@cmpxchg.org,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	Qian Cai <cai@lca.pw>
Subject: [PATCH -next] mm/slab: fix an use-after-free in kmemcg_workfn()
Date: Wed, 19 Jun 2019 16:52:53 -0400
Message-ID: <1560977573-10715-1-git-send-email-cai@lca.pw> (raw)

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@lca.pw>
---
 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);
 
 	put_online_mems();
@@ -753,7 +750,6 @@ static void kmemcg_cache_shutdown(struct percpu_ref *percpu_ref)
 	if (s->memcg_params.root_cache->memcg_params.dying)
 		goto unlock;
 
-	WARN_ON(s->memcg_params.work_fn);
 	s->memcg_params.work_fn = kmemcg_cache_shutdown_fn;
 	INIT_WORK(&s->memcg_params.work, kmemcg_workfn);
 	queue_work(memcg_kmem_cache_wq, &s->memcg_params.work);
@@ -784,7 +780,6 @@ static void kmemcg_cache_deactivate(struct kmem_cache *s)
 	if (s->memcg_params.root_cache->memcg_params.dying)
 		goto unlock;
 
-	WARN_ON_ONCE(s->memcg_params.work_fn);
 	s->memcg_params.work_fn = kmemcg_cache_deactivate_after_rcu;
 	call_rcu(&s->memcg_params.rcu_head, kmemcg_rcufn);
 unlock:
-- 
1.8.3.1


             reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-19 20:52 Qian Cai [this message]
2019-06-19 21:07 ` Roman Gushchin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1560977573-10715-1-git-send-email-cai@lca.pw \
    --to=cai@lca.pw \
    --cc=akpm@linux-foundation.org \
    --cc=guro@fb.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=vdavydov.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git
	git clone --mirror https://lore.kernel.org/lkml/9 lkml/git/9.git
	git clone --mirror https://lore.kernel.org/lkml/10 lkml/git/10.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git