From: "Du, Changbin" <changbin.du@intel.com>
To: Jiri Olsa <jolsa@redhat.com>
Cc: "Du, Changbin" <changbin.du@intel.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
peterz@infradead.org, mingo@redhat.com,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field
Date: Tue, 11 Apr 2017 11:06:14 +0800 [thread overview]
Message-ID: <20170411030614.GA9155@intel.com> (raw)
In-Reply-To: <20170410113325.GE25354@krava>
[-- Attachment #1: Type: text/plain, Size: 3520 bytes --]
On Mon, Apr 10, 2017 at 01:33:25PM +0200, Jiri Olsa wrote:
> On Mon, Apr 10, 2017 at 06:21:12PM +0800, Du, Changbin wrote:
> > On Mon, Apr 10, 2017 at 10:39:50AM +0200, Jiri Olsa wrote:
> > > On Tue, Apr 04, 2017 at 12:19:40PM -0300, Arnaldo Carvalho de Melo wrote:
> > >
> > > SNIP
> > >
> > > > > ---
> > > > > tools/perf/ui/hist.c | 25 +++++++++++++++----------
> > > > > 1 file changed, 15 insertions(+), 10 deletions(-)
> > > > >
> > > > > diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c
> > > > > index 5d632dc..f94b301 100644
> > > > > --- a/tools/perf/ui/hist.c
> > > > > +++ b/tools/perf/ui/hist.c
> > > > > @@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
> > > > >
> > > > > void perf_hpp__reset_output_field(struct perf_hpp_list *list)
> > > > > {
> > > > > - struct perf_hpp_fmt *fmt, *tmp;
> > > > > + struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
> > > > >
> > > > > /* reset output fields */
> > > > > - perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
> > > > > - list_del_init(&fmt->list);
> > > > > - list_del_init(&fmt->sort_list);
> > > > > - fmt_free(fmt);
> > > > > + perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
> > > > > + list_del_init(&field_fmt->list);
> > > > > + /* reset sort keys */
> > > > > + perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
> > > > > + if (field_fmt == sort_fmt) {
> > > > > + list_del_init(&field_fmt->sort_list);
> > > > > + break;
> > > > > + }
> > > > > + }
> > >
> > > I agree with Namhyung in here.. seems like the only thing you
> > > added is to check if the field_fmt was also linked in as a sort
> > > entry before you call list_del_init on it
> > >
> > This is correct.
> >
> > > which I think should be also done with list_empty function, but
> > > more importantly I dont see a reason for that.. list_del_init
> > > call should be fine on empty list
> > >
> > You didn't catch the problem here. The problem is double free a fmt.
> > For exampe, fmt A is linked to both list. Then it will be first free
> > by the first iteration over list, then it will be freed again at the
> > second iteration over sort_list. This must cause application crash.
>
> the original code takes it out of both lists,
> so the next itaration won't go over that entry
>
oh, my bad, my desc is wrong. I replayed the crash. The problem is
list_del_init a unlinked entry.
perf: Segmentation fault
-------- backtrace --------
./perf[0x57394b]
/lib/x86_64-linux-gnu/libc.so.6(+0x354b0)[0x7fb8da3034b0]
./perf(perf_hpp__reset_output_field+0xb7)[0x55dfe7]
./perf(hists__sort_by_fields+0x3d7)[0x509777]
./perf[0x5704c1]
./perf(perf_evlist__tui_browse_hists+0x2e5)[0x5723e5]
./perf(cmd_report+0x1a9b)[0x43b4fb]
./perf[0x494731]
./perf(main+0x704)[0x426304]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fb8da2ee830]
./perf(_start+0x29)[0x4263f9]
[0x0]
(gdb) print fmt.list
$4 = {next = 0x100, prev = 0x200} // LIST_POISON
(gdb) print fmt.sort_list
$5 = {next = 0x9727d0 <perf_hpp_list+16>, prev = 0x9727d0 <perf_hpp_list+16>}
In this case, the fmt is linked in sort_list, but not in list. So crash
at the list_del_init(&fmt->list) of second loop.
Another potential case is the fmt is linked in list, but not in sort_list.
Oh, my brain was broken. correct patch but wrong commit message. :(
Will drop this one and submit a new one.
> jirka
--
Thanks,
Changbin Du
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 473 bytes --]
next prev parent reply other threads:[~2017-04-11 3:09 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-15 2:16 [PATCH] perf: double free at function perf_hpp__reset_output_field changbin.du
2017-03-27 6:22 ` [PATCH v2] perf: fix " changbin.du
2017-04-04 15:19 ` Arnaldo Carvalho de Melo
2017-04-04 15:34 ` Namhyung Kim
2017-04-04 15:51 ` Arnaldo Carvalho de Melo
2017-04-05 2:44 ` Du, Changbin
2017-04-09 17:05 ` Jiri Olsa
2017-04-10 2:13 ` Du, Changbin
2017-04-10 8:39 ` Jiri Olsa
2017-04-10 10:21 ` Du, Changbin
2017-04-10 11:33 ` Jiri Olsa
2017-04-11 3:06 ` Du, Changbin [this message]
2017-04-11 7:35 ` Jiri Olsa
2017-04-11 8:25 ` Du, Changbin
2017-04-11 10:05 ` Jiri Olsa
2017-04-11 10:13 ` Du, Changbin
2017-04-11 10:32 ` Jiri Olsa
2017-04-12 1:48 ` Du, Changbin
2017-05-31 7:05 ` Du, Changbin
2017-05-31 7:19 ` Jiri Olsa
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=20170411030614.GA9155@intel.com \
--to=changbin.du@intel.com \
--cc=acme@kernel.org \
--cc=jolsa@kernel.org \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/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