From: Steven Rostedt <rostedt@goodmis.org>
To: Axel Rasmussen <axelrasmussen@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Chinwen Chang <chinwen.chang@mediatek.com>,
Daniel Jordan <daniel.m.jordan@oracle.com>,
David Rientjes <rientjes@google.com>,
Davidlohr Bueso <dbueso@suse.de>, Ingo Molnar <mingo@redhat.com>,
Jann Horn <jannh@google.com>,
Laurent Dufour <ldufour@linux.ibm.com>,
Michel Lespinasse <walken@google.com>,
Stephen Rothwell <sfr@canb.auug.org.au>,
Vlastimil Babka <vbabka@suse.cz>,
Yafang Shao <laoar.shao@gmail.com>,
davem@davemloft.net, dsahern@kernel.org,
gregkh@linuxfoundation.org, kuba@kernel.org,
liuhangbin@gmail.com, tj@kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH] mm: mmap_lock: fix use-after-free race and css ref leak in tracepoints
Date: Mon, 30 Nov 2020 22:57:54 -0500 [thread overview]
Message-ID: <20201130225754.38a8d717@oasis.local.home> (raw)
In-Reply-To: <20201130233504.3725241-1-axelrasmussen@google.com>
On Mon, 30 Nov 2020 15:35:04 -0800
Axel Rasmussen <axelrasmussen@google.com> wrote:
> syzbot reported[1] a use-after-free introduced in 0f818c4bc1f3. The bug
> is that an ongoing trace event might race with the tracepoint being
> disabled (and therefore the _unreg() callback being called). Consider
> this ordering:
>
> T1: trace event fires, get_mm_memcg_path() is called
> T1: get_memcg_path_buf() returns a buffer pointer
> T2: trace_mmap_lock_unreg() is called, buffers are freed
> T1: cgroup_path() is called with the now-freed buffer
>
> The solution in this commit is to modify trace_mmap_lock_unreg() to
> first stop new buffers from being handed out, and then to wait (spin)
> until any existing buffer references are dropped (i.e., those trace
> events complete).
>
> I have a simple reproducer program which spins up two pools of threads,
> doing the following in a tight loop:
>
> Pool 1:
> mmap(NULL, 4096, PROT_READ | PROT_WRITE,
> MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
> munmap()
>
> Pool 2:
> echo 1 > /sys/kernel/debug/tracing/events/mmap_lock/enable
> echo 0 > /sys/kernel/debug/tracing/events/mmap_lock/enable
>
> This triggers the use-after-free very quickly. With this patch, I let it
> run for an hour without any BUGs.
>
> While fixing this, I also noticed and fixed a css ref leak. Previously
> we called get_mem_cgroup_from_mm(), but we never called css_put() to
> release that reference. get_mm_memcg_path() now does this properly.
>
> [1]: https://syzkaller.appspot.com/bug?extid=19e6dd9943972fa1c58a
>
> Fixes: 0f818c4bc1f3 ("mm: mmap_lock: add tracepoints around lock acquisition")
> Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
>
Looking at the original patch that this fixes, I'm thinking, why use a
spinlock in the reg/unreg callers? Registering and unregistering a
tracepoint can sleep (it calls mutex locks), so the reg/unreg can sleep
too. As the use of the get_mm_memcg_path() is done under
preempt_disable, which is a rcu grace period, you could simply change
the unregister to:
void trace_mmap_lock_unreg(void)
{
int cpu;
mutex_lock(®_lock);
if (--reg_refcount)
goto out;
/* Make sure all users of memcg_path_buf are done */
synchronize_rcu();
for_each_possible_cpu(cpu) {
kfree(per_cpu(memcg_path_buf, cpu));
}
out:
mutex_unlock(®_lock);
}
Obviously, you would need to change reg_lock to mutex in the _reg()
function.
-- Steve
prev parent reply other threads:[~2020-12-01 3:58 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-30 23:35 [PATCH] mm: mmap_lock: fix use-after-free race and css ref leak in tracepoints Axel Rasmussen
2020-12-01 1:33 ` Shakeel Butt
2020-12-01 17:36 ` Axel Rasmussen
2020-12-01 17:56 ` Greg Thelen
2020-12-01 18:42 ` Shakeel Butt
2020-12-01 19:13 ` Axel Rasmussen
2020-12-01 20:53 ` Shakeel Butt
2020-12-02 0:15 ` Axel Rasmussen
2020-12-02 0:36 ` Shakeel Butt
2020-12-02 1:07 ` Steven Rostedt
2020-12-02 1:11 ` Shakeel Butt
2020-12-04 16:36 ` Vlastimil Babka
2020-12-04 17:46 ` Axel Rasmussen
2020-12-02 19:00 ` Tejun Heo
2020-12-02 23:23 ` Shakeel Butt
2020-12-02 23:30 ` Tejun Heo
2020-12-01 3:57 ` Steven Rostedt [this message]
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=20201130225754.38a8d717@oasis.local.home \
--to=rostedt@goodmis.org \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=chinwen.chang@mediatek.com \
--cc=daniel.m.jordan@oracle.com \
--cc=davem@davemloft.net \
--cc=dbueso@suse.de \
--cc=dsahern@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jannh@google.com \
--cc=kuba@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=ldufour@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=liuhangbin@gmail.com \
--cc=mingo@redhat.com \
--cc=rientjes@google.com \
--cc=sfr@canb.auug.org.au \
--cc=tj@kernel.org \
--cc=vbabka@suse.cz \
--cc=walken@google.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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).