linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Fengguang Wu <fengguang.wu@intel.com>,
	linux-kernel@vger.kernel.org, Wang Nan <wangnan0@huawei.com>,
	Ingo Molnar <mingo@redhat.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Will Deacon <will.deacon@arm.com>,
	lkp@01.org, Dmitry Vyukov <dvyukov@google.com>,
	kasan-dev@googlegroups.com, kernel-team@lge.com
Subject: Re: BUG: KASAN: slab-out-of-bounds in perf_callchain_user+0x494/0x530
Date: Thu, 7 Dec 2017 00:49:57 +0900	[thread overview]
Message-ID: <20171206154957.GB3367@danjae.aot.lge.com> (raw)
In-Reply-To: <20171206154544.oiavdgfrpryak23z@hirez.programming.kicks-ass.net>

On Wed, Dec 06, 2017 at 04:45:44PM +0100, Peter Zijlstra wrote:
> On Wed, Dec 06, 2017 at 11:31:30PM +0900, Namhyung Kim wrote:
> 
> > > There's also a race against put_callchain_buffers() there, consider:
> > > 
> > > 
> > > 	get_callchain_buffers()		put_callchain_buffers()
> > > 	  mutex_lock();
> > > 	  inc()
> > > 					  dec_and_test() // false
> > > 
> > > 	  dec() // 0
> > > 
> > > 
> > > And the buffers leak.
> > 
> > Hmm.. did you mean that get_callchain_buffers() returns an error?
> 
> Yes, get_callchain_buffers() fails, but while doing so it has a
> temporary increment on the count.
> 
> > AFAICS it cannot fail when it sees count > 1 (and callchain_cpus_
> > entries is allocated).  
> 
> It can with your patch. We only test event_max_stack against the sysctl
> after incrementing.

So, are you ok with this?

Thanks,
Namhyung


diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c
index 1b2be63c8528..ee0ba22d3993 100644
--- a/kernel/events/callchain.c
+++ b/kernel/events/callchain.c
@@ -137,8 +137,11 @@ int get_callchain_buffers(int event_max_stack)
 
        err = alloc_callchain_buffers();
 exit:
-       if (err)
-               atomic_dec(&nr_callchain_events);
+       if (err) {
+               /* might race with put_callchain_buffers() */
+               if (atomic_dec_and_test(&nr_callchain_events))
+                       release_callchain_buffers();
+       }
 
        mutex_unlock(&callchain_mutex);
 

  reply	other threads:[~2017-12-06 15:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-30  2:32 BUG: KASAN: slab-out-of-bounds in perf_callchain_user+0x494/0x530 Fengguang Wu
2017-11-30  8:20 ` Peter Zijlstra
2017-11-30 19:37   ` Arnaldo Carvalho de Melo
2017-12-05  8:11     ` Namhyung Kim
2017-12-05 13:37       ` Arnaldo Carvalho de Melo
2017-12-05 14:47         ` Namhyung Kim
2017-12-06 13:47           ` Peter Zijlstra
2017-12-06 14:12             ` Arnaldo Carvalho de Melo
2017-12-06 14:31             ` Namhyung Kim
2017-12-06 15:45               ` Peter Zijlstra
2017-12-06 15:49                 ` Namhyung Kim [this message]
2017-12-06 16:39                   ` Peter Zijlstra
2017-12-06 15:46               ` Namhyung Kim
2017-12-06 13:40       ` Peter Zijlstra

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=20171206154957.GB3367@danjae.aot.lge.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dvyukov@google.com \
    --cc=fengguang.wu@intel.com \
    --cc=jolsa@redhat.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kernel-team@lge.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lkp@01.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=wangnan0@huawei.com \
    --cc=will.deacon@arm.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).