linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path
@ 2016-05-31 13:06 Wang Nan
  2016-05-31 13:27 ` Masami Hiramatsu
  2016-06-08  8:37 ` [tip:perf/core] perf evlist: Fix alloc_mmap() " tip-bot for Wang Nan
  0 siblings, 2 replies; 4+ messages in thread
From: Wang Nan @ 2016-05-31 13:06 UTC (permalink / raw)
  To: acme
  Cc: pi3orama, linux-kernel, Wang Nan, Arnaldo Carvalho de Melo,
	He Kuang, Jiri Olsa, Masami Hiramatsu, Namhyung Kim, Zefan Li

If zalloc fail, setting evlist->mmap[i].fd is unsafe.
perf_evlist__alloc_mmap() should fail right after zalloc() fail.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index e0f3094..1b918aa 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {
-- 
1.8.3.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path
  2016-05-31 13:06 [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path Wang Nan
@ 2016-05-31 13:27 ` Masami Hiramatsu
  2016-06-01 12:43   ` Arnaldo Carvalho de Melo
  2016-06-08  8:37 ` [tip:perf/core] perf evlist: Fix alloc_mmap() " tip-bot for Wang Nan
  1 sibling, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2016-05-31 13:27 UTC (permalink / raw)
  To: Wang Nan
  Cc: acme, pi3orama, linux-kernel, Arnaldo Carvalho de Melo, He Kuang,
	Jiri Olsa, Masami Hiramatsu, Namhyung Kim, Zefan Li

On Tue, 31 May 2016 13:06:15 +0000
Wang Nan <wangnan0@huawei.com> wrote:

> If zalloc fail, setting evlist->mmap[i].fd is unsafe.
> perf_evlist__alloc_mmap() should fail right after zalloc() fail.

Good catch!!

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Thanks!

> 
> Signed-off-by: Wang Nan <wangnan0@huawei.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> Cc: He Kuang <hekuang@huawei.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Zefan Li <lizefan@huawei.com>
> Cc: pi3orama@163.com
> ---
>  tools/perf/util/evlist.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index e0f3094..1b918aa 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
> @@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
>  	if (cpu_map__empty(evlist->cpus))
>  		evlist->nr_mmaps = thread_map__nr(evlist->threads);
>  	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
> +	if (!evlist->mmap)
> +		return -ENOMEM;
> +
>  	for (i = 0; i < evlist->nr_mmaps; i++)
>  		evlist->mmap[i].fd = -1;
> -	return evlist->mmap != NULL ? 0 : -ENOMEM;
> +	return 0;
>  }
>  
>  struct mmap_params {
> -- 
> 1.8.3.4
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path
  2016-05-31 13:27 ` Masami Hiramatsu
@ 2016-06-01 12:43   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2016-06-01 12:43 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Wang Nan, pi3orama, linux-kernel, Arnaldo Carvalho de Melo,
	He Kuang, Jiri Olsa, Namhyung Kim, Zefan Li

Em Tue, May 31, 2016 at 10:27:43PM +0900, Masami Hiramatsu escreveu:
> On Tue, 31 May 2016 13:06:15 +0000
> Wang Nan <wangnan0@huawei.com> wrote:
> 
> > If zalloc fail, setting evlist->mmap[i].fd is unsafe.
> > perf_evlist__alloc_mmap() should fail right after zalloc() fail.
> 
> Good catch!!
> 
> Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>

Applied, after adding a:

Fixes: d4c6fb36ac2c ("perf evsel: Record fd into perf_mmap")
 
> Thanks!
> 
> > 
> > Signed-off-by: Wang Nan <wangnan0@huawei.com>
> > Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
> > Cc: He Kuang <hekuang@huawei.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Cc: Masami Hiramatsu <mhiramat@kernel.org>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Cc: Zefan Li <lizefan@huawei.com>
> > Cc: pi3orama@163.com
> > ---
> >  tools/perf/util/evlist.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> > index e0f3094..1b918aa 100644
> > --- a/tools/perf/util/evlist.c
> > +++ b/tools/perf/util/evlist.c
> > @@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
> >  	if (cpu_map__empty(evlist->cpus))
> >  		evlist->nr_mmaps = thread_map__nr(evlist->threads);
> >  	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
> > +	if (!evlist->mmap)
> > +		return -ENOMEM;
> > +
> >  	for (i = 0; i < evlist->nr_mmaps; i++)
> >  		evlist->mmap[i].fd = -1;
> > -	return evlist->mmap != NULL ? 0 : -ENOMEM;
> > +	return 0;
> >  }
> >  
> >  struct mmap_params {
> > -- 
> > 1.8.3.4
> > 
> 
> 
> -- 
> Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip:perf/core] perf evlist: Fix alloc_mmap() failure path
  2016-05-31 13:06 [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path Wang Nan
  2016-05-31 13:27 ` Masami Hiramatsu
@ 2016-06-08  8:37 ` tip-bot for Wang Nan
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot for Wang Nan @ 2016-06-08  8:37 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: mhiramat, hpa, acme, hekuang, linux-kernel, namhyung, wangnan0,
	tglx, mingo, lizefan, jolsa

Commit-ID:  946ae1d41d4b0c77b9f63b4a0393d8a1283a7f9d
Gitweb:     http://git.kernel.org/tip/946ae1d41d4b0c77b9f63b4a0393d8a1283a7f9d
Author:     Wang Nan <wangnan0@huawei.com>
AuthorDate: Tue, 31 May 2016 13:06:15 +0000
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Fri, 3 Jun 2016 14:53:46 -0300

perf evlist: Fix alloc_mmap() failure path

If zalloc fail, setting evlist->mmap[i].fd is unsafe and
perf_evlist__alloc_mmap() should bail out right after that.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Fixes: d4c6fb36ac2c ("perf evsel: Record fd into perf_mmap")
Link: http://lkml.kernel.org/r/1464699975-230440-1-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/evlist.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index e0f3094..1b918aa 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -946,9 +946,12 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	if (cpu_map__empty(evlist->cpus))
 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
+	if (!evlist->mmap)
+		return -ENOMEM;
+
 	for (i = 0; i < evlist->nr_mmaps; i++)
 		evlist->mmap[i].fd = -1;
-	return evlist->mmap != NULL ? 0 : -ENOMEM;
+	return 0;
 }
 
 struct mmap_params {

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-06-08  8:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-31 13:06 [PATCH] perf tools: Fix perf_evlist__alloc_mmap() failure path Wang Nan
2016-05-31 13:27 ` Masami Hiramatsu
2016-06-01 12:43   ` Arnaldo Carvalho de Melo
2016-06-08  8:37 ` [tip:perf/core] perf evlist: Fix alloc_mmap() " tip-bot for Wang Nan

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).