linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf tests: Fix out of bounds memory access
@ 2019-11-07  2:02 Leo Yan
  2019-11-07  9:42 ` Jiri Olsa
  2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Leo Yan
  0 siblings, 2 replies; 6+ messages in thread
From: Leo Yan @ 2019-11-07  2:02 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	linux-kernel, Naresh Kamboju
  Cc: Leo Yan

The test case 'Read backward ring buffer' failed on 32-bit architectures
which were found by LKFT perf testing.  The test failed on arm32 x15
device, qemu_arm32, qemu_i386, and found intermittent failure on i386;
the failure log is as below:

  50: Read backward ring buffer                  :
  --- start ---
  test child forked, pid 510
  Using CPUID GenuineIntel-6-9E-9
  mmap size 1052672B
  mmap size 8192B
  Finished reading overwrite ring buffer: rewind
  free(): invalid next size (fast)
  test child interrupted
  ---- end ----
  Read backward ring buffer: FAILED!

The log hints there have issue for memory usage, thus free() reports
error 'invalid next size' and directly exit for the case.  Finally, this
issue is root caused as out of bounds memory access for the data array
'evsel->id'.

The backward ring buffer test invokes do_test() twice.  'evsel->id' is
allocated at the first call with the flow:

  test__backward_ring_buffer()
    `-> do_test()
	  `-> evlist__mmap()
	        `-> evlist__mmap_ex()
	              `-> perf_evsel__alloc_id()

So 'evsel->id' is allocated with one item, and it will be used in
function perf_evlist__id_add():

   evsel->id[0] = id
   evsel->ids   = 1

At the second call for do_test(), it skips to initialize 'evsel->id'
and reuses the array which is allocated in the first call.  But
'evsel->ids' contains the stale value.  Thus:

   evsel->id[1] = id    -> out of bound access
   evsel->ids   = 2

To fix this issue, we will use evlist__open() and evlist__close() pair
functions to prepare and cleanup context for evlist; so 'evsel->id' and
'evsel->ids' can be initialized properly when invoke do_test() and avoid
the out of bounds memory access.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
---
 tools/perf/tests/backward-ring-buffer.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/backward-ring-buffer.c
index 338cd9faa835..5128f727c0ef 100644
--- a/tools/perf/tests/backward-ring-buffer.c
+++ b/tools/perf/tests/backward-ring-buffer.c
@@ -147,6 +147,15 @@ int test__backward_ring_buffer(struct test *test __maybe_unused, int subtest __m
 		goto out_delete_evlist;
 	}
 
+	evlist__close(evlist);
+
+	err = evlist__open(evlist);
+	if (err < 0) {
+		pr_debug("perf_evlist__open: %s\n",
+			 str_error_r(errno, sbuf, sizeof(sbuf)));
+		goto out_delete_evlist;
+	}
+
 	err = do_test(evlist, 1, &sample_count, &comm_count);
 	if (err != TEST_OK)
 		goto out_delete_evlist;
-- 
2.17.1


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

* Re: [PATCH v2] perf tests: Fix out of bounds memory access
  2019-11-07  2:02 [PATCH v2] perf tests: Fix out of bounds memory access Leo Yan
@ 2019-11-07  9:42 ` Jiri Olsa
  2019-11-07 10:20   ` Leo Yan
  2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Leo Yan
  1 sibling, 1 reply; 6+ messages in thread
From: Jiri Olsa @ 2019-11-07  9:42 UTC (permalink / raw)
  To: Leo Yan
  Cc: Arnaldo Carvalho de Melo, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Namhyung Kim, linux-kernel,
	Naresh Kamboju

On Thu, Nov 07, 2019 at 10:02:44AM +0800, Leo Yan wrote:
> The test case 'Read backward ring buffer' failed on 32-bit architectures
> which were found by LKFT perf testing.  The test failed on arm32 x15
> device, qemu_arm32, qemu_i386, and found intermittent failure on i386;
> the failure log is as below:
> 
>   50: Read backward ring buffer                  :
>   --- start ---
>   test child forked, pid 510
>   Using CPUID GenuineIntel-6-9E-9
>   mmap size 1052672B
>   mmap size 8192B
>   Finished reading overwrite ring buffer: rewind
>   free(): invalid next size (fast)
>   test child interrupted
>   ---- end ----
>   Read backward ring buffer: FAILED!
> 
> The log hints there have issue for memory usage, thus free() reports
> error 'invalid next size' and directly exit for the case.  Finally, this
> issue is root caused as out of bounds memory access for the data array
> 'evsel->id'.
> 
> The backward ring buffer test invokes do_test() twice.  'evsel->id' is
> allocated at the first call with the flow:
> 
>   test__backward_ring_buffer()
>     `-> do_test()
> 	  `-> evlist__mmap()
> 	        `-> evlist__mmap_ex()
> 	              `-> perf_evsel__alloc_id()
> 
> So 'evsel->id' is allocated with one item, and it will be used in
> function perf_evlist__id_add():
> 
>    evsel->id[0] = id
>    evsel->ids   = 1
> 
> At the second call for do_test(), it skips to initialize 'evsel->id'
> and reuses the array which is allocated in the first call.  But
> 'evsel->ids' contains the stale value.  Thus:
> 
>    evsel->id[1] = id    -> out of bound access
>    evsel->ids   = 2
> 
> To fix this issue, we will use evlist__open() and evlist__close() pair
> functions to prepare and cleanup context for evlist; so 'evsel->id' and
> 'evsel->ids' can be initialized properly when invoke do_test() and avoid
> the out of bounds memory access.

right, we need to solve this on libperf level, so it's possible
to call mmap/munmap multiple time without close/open.. I'll try
to send something, but meanwhile this is good workaround

Reviewed-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> 
> Signed-off-by: Leo Yan <leo.yan@linaro.org>
> ---
>  tools/perf/tests/backward-ring-buffer.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/backward-ring-buffer.c
> index 338cd9faa835..5128f727c0ef 100644
> --- a/tools/perf/tests/backward-ring-buffer.c
> +++ b/tools/perf/tests/backward-ring-buffer.c
> @@ -147,6 +147,15 @@ int test__backward_ring_buffer(struct test *test __maybe_unused, int subtest __m
>  		goto out_delete_evlist;
>  	}
>  
> +	evlist__close(evlist);
> +
> +	err = evlist__open(evlist);
> +	if (err < 0) {
> +		pr_debug("perf_evlist__open: %s\n",
> +			 str_error_r(errno, sbuf, sizeof(sbuf)));
> +		goto out_delete_evlist;
> +	}
> +
>  	err = do_test(evlist, 1, &sample_count, &comm_count);
>  	if (err != TEST_OK)
>  		goto out_delete_evlist;
> -- 
> 2.17.1
> 


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

* Re: [PATCH v2] perf tests: Fix out of bounds memory access
  2019-11-07  9:42 ` Jiri Olsa
@ 2019-11-07 10:20   ` Leo Yan
  2019-11-07 12:06     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Leo Yan @ 2019-11-07 10:20 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Arnaldo Carvalho de Melo, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Namhyung Kim, linux-kernel,
	Naresh Kamboju

On Thu, Nov 07, 2019 at 10:42:26AM +0100, Jiri Olsa wrote:

[...]

> > To fix this issue, we will use evlist__open() and evlist__close() pair
> > functions to prepare and cleanup context for evlist; so 'evsel->id' and
> > 'evsel->ids' can be initialized properly when invoke do_test() and avoid
> > the out of bounds memory access.
> 
> right, we need to solve this on libperf level, so it's possible
> to call mmap/munmap multiple time without close/open.. I'll try
> to send something, but meanwhile this is good workaround
> 
> Reviewed-by: Jiri Olsa <jolsa@kernel.org>

Thanks for reviewing, Jiri.

You are welcome to send us the fixing patches, I am glad to test it on
qemu_arm.

Thanks,
Leo Yan

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

* Re: [PATCH v2] perf tests: Fix out of bounds memory access
  2019-11-07 10:20   ` Leo Yan
@ 2019-11-07 12:06     ` Arnaldo Carvalho de Melo
  2019-11-07 13:35       ` Leo Yan
  0 siblings, 1 reply; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2019-11-07 12:06 UTC (permalink / raw)
  To: Leo Yan
  Cc: Jiri Olsa, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Namhyung Kim, linux-kernel, Naresh Kamboju

Em Thu, Nov 07, 2019 at 06:20:29PM +0800, Leo Yan escreveu:
> On Thu, Nov 07, 2019 at 10:42:26AM +0100, Jiri Olsa wrote:
 
> [...]
 
> > > To fix this issue, we will use evlist__open() and evlist__close() pair
> > > functions to prepare and cleanup context for evlist; so 'evsel->id' and
> > > 'evsel->ids' can be initialized properly when invoke do_test() and avoid
> > > the out of bounds memory access.
 
> > right, we need to solve this on libperf level, so it's possible
> > to call mmap/munmap multiple time without close/open.. I'll try
> > to send something, but meanwhile this is good workaround
> > 
> > Reviewed-by: Jiri Olsa <jolsa@kernel.org>
 
> Thanks for reviewing, Jiri.
 
> You are welcome to send us the fixing patches, I am glad to test it on
> qemu_arm.

Thanks, applied after adding:

Fixes: ee74701ed8ad ("perf tests: Add test to check backward ring buffer")
Cc: stable@vger.kernel.org # v4.10+

Please consider doing it next time,

- Arnaldo

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

* Re: [PATCH v2] perf tests: Fix out of bounds memory access
  2019-11-07 12:06     ` Arnaldo Carvalho de Melo
@ 2019-11-07 13:35       ` Leo Yan
  0 siblings, 0 replies; 6+ messages in thread
From: Leo Yan @ 2019-11-07 13:35 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Jiri Olsa, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Namhyung Kim, linux-kernel, Naresh Kamboju

On Thu, Nov 07, 2019 at 09:06:43AM -0300, Arnaldo Carvalho de Melo wrote:
> Em Thu, Nov 07, 2019 at 06:20:29PM +0800, Leo Yan escreveu:
> > On Thu, Nov 07, 2019 at 10:42:26AM +0100, Jiri Olsa wrote:
>  
> > [...]
>  
> > > > To fix this issue, we will use evlist__open() and evlist__close() pair
> > > > functions to prepare and cleanup context for evlist; so 'evsel->id' and
> > > > 'evsel->ids' can be initialized properly when invoke do_test() and avoid
> > > > the out of bounds memory access.
>  
> > > right, we need to solve this on libperf level, so it's possible
> > > to call mmap/munmap multiple time without close/open.. I'll try
> > > to send something, but meanwhile this is good workaround
> > > 
> > > Reviewed-by: Jiri Olsa <jolsa@kernel.org>
>  
> > Thanks for reviewing, Jiri.
>  
> > You are welcome to send us the fixing patches, I am glad to test it on
> > qemu_arm.
> 
> Thanks, applied after adding:
> 
> Fixes: ee74701ed8ad ("perf tests: Add test to check backward ring buffer")
> Cc: stable@vger.kernel.org # v4.10+
> 
> Please consider doing it next time,

Thanks a lot for helping to add 'Fixes' tag, Arnaldo.

Will note this for later patches.

Thanks,
Leo Yan

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

* [tip: perf/core] perf tests: Fix out of bounds memory access
  2019-11-07  2:02 [PATCH v2] perf tests: Fix out of bounds memory access Leo Yan
  2019-11-07  9:42 ` Jiri Olsa
@ 2019-11-12 11:17 ` tip-bot2 for Leo Yan
  1 sibling, 0 replies; 6+ messages in thread
From: tip-bot2 for Leo Yan @ 2019-11-12 11:17 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Leo Yan, Jiri Olsa, Alexander Shishkin, Mark Rutland,
	Namhyung Kim, Naresh Kamboju, Peter Zijlstra, Wang Nan, stable, #,
	v4.10+,
	Arnaldo Carvalho de Melo, Ingo Molnar, Borislav Petkov,
	linux-kernel

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     af8490eb2b33684e26a0a927a9d93ae43cd08890
Gitweb:        https://git.kernel.org/tip/af8490eb2b33684e26a0a927a9d93ae43cd08890
Author:        Leo Yan <leo.yan@linaro.org>
AuthorDate:    Thu, 07 Nov 2019 10:02:44 +08:00
Committer:     Arnaldo Carvalho de Melo <acme@redhat.com>
CommitterDate: Thu, 07 Nov 2019 09:04:22 -03:00

perf tests: Fix out of bounds memory access

The test case 'Read backward ring buffer' failed on 32-bit architectures
which were found by LKFT perf testing.  The test failed on arm32 x15
device, qemu_arm32, qemu_i386, and found intermittent failure on i386;
the failure log is as below:

  50: Read backward ring buffer                  :
  --- start ---
  test child forked, pid 510
  Using CPUID GenuineIntel-6-9E-9
  mmap size 1052672B
  mmap size 8192B
  Finished reading overwrite ring buffer: rewind
  free(): invalid next size (fast)
  test child interrupted
  ---- end ----
  Read backward ring buffer: FAILED!

The log hints there have issue for memory usage, thus free() reports
error 'invalid next size' and directly exit for the case.  Finally, this
issue is root caused as out of bounds memory access for the data array
'evsel->id'.

The backward ring buffer test invokes do_test() twice.  'evsel->id' is
allocated at the first call with the flow:

  test__backward_ring_buffer()
    `-> do_test()
	  `-> evlist__mmap()
	        `-> evlist__mmap_ex()
	              `-> perf_evsel__alloc_id()

So 'evsel->id' is allocated with one item, and it will be used in
function perf_evlist__id_add():

   evsel->id[0] = id
   evsel->ids   = 1

At the second call for do_test(), it skips to initialize 'evsel->id'
and reuses the array which is allocated in the first call.  But
'evsel->ids' contains the stale value.  Thus:

   evsel->id[1] = id    -> out of bound access
   evsel->ids   = 2

To fix this issue, we will use evlist__open() and evlist__close() pair
functions to prepare and cleanup context for evlist; so 'evsel->id' and
'evsel->ids' can be initialized properly when invoke do_test() and avoid
the out of bounds memory access.

Fixes: ee74701ed8ad ("perf tests: Add test to check backward ring buffer")
Signed-off-by: Leo Yan <leo.yan@linaro.org>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naresh Kamboju <naresh.kamboju@linaro.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: stable@vger.kernel.org # v4.10+
Link: http://lore.kernel.org/lkml/20191107020244.2427-1-leo.yan@linaro.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/tests/backward-ring-buffer.c |  9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/backward-ring-buffer.c
index a4cd30c..15cea51 100644
--- a/tools/perf/tests/backward-ring-buffer.c
+++ b/tools/perf/tests/backward-ring-buffer.c
@@ -148,6 +148,15 @@ int test__backward_ring_buffer(struct test *test __maybe_unused, int subtest __m
 		goto out_delete_evlist;
 	}
 
+	evlist__close(evlist);
+
+	err = evlist__open(evlist);
+	if (err < 0) {
+		pr_debug("perf_evlist__open: %s\n",
+			 str_error_r(errno, sbuf, sizeof(sbuf)));
+		goto out_delete_evlist;
+	}
+
 	err = do_test(evlist, 1, &sample_count, &comm_count);
 	if (err != TEST_OK)
 		goto out_delete_evlist;

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

end of thread, other threads:[~2019-11-12 11:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-07  2:02 [PATCH v2] perf tests: Fix out of bounds memory access Leo Yan
2019-11-07  9:42 ` Jiri Olsa
2019-11-07 10:20   ` Leo Yan
2019-11-07 12:06     ` Arnaldo Carvalho de Melo
2019-11-07 13:35       ` Leo Yan
2019-11-12 11:17 ` [tip: perf/core] " tip-bot2 for Leo Yan

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