linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf buildid: Avoid copy of uninitialized memory
@ 2023-01-13 18:57 Ian Rogers
  2023-01-19 16:08 ` Ian Rogers
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Rogers @ 2023-01-13 18:57 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, Ian Rogers,
	Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel, llvm
  Cc: Stephane Eranian

build_id__init only copies the buildid data up to size leaving the
rest of the data array uninitialized. Copying the full array during
synthesis means the written event contains uninitialized memory.  This
was detected by the Clang/LLVM memory sanitizer.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/synthetic-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
index 3ab6a92b1a6d..348d05e4ec03 100644
--- a/tools/perf/util/synthetic-events.c
+++ b/tools/perf/util/synthetic-events.c
@@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
 
 	len = pos->long_name_len + 1;
 	len = PERF_ALIGN(len, NAME_ALIGN);
-	memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
+	memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);
 	ev.build_id.size = pos->bid.size;
 	ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
 	ev.build_id.header.misc = misc | PERF_RECORD_MISC_BUILD_ID_SIZE;
-- 
2.39.0.314.g84b9a713c41-goog


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

* Re: [PATCH] perf buildid: Avoid copy of uninitialized memory
  2023-01-13 18:57 [PATCH] perf buildid: Avoid copy of uninitialized memory Ian Rogers
@ 2023-01-19 16:08 ` Ian Rogers
  2023-01-19 16:29   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Rogers @ 2023-01-19 16:08 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Nathan Chancellor, Nick Desaulniers, Tom Rix, Ian Rogers,
	Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel, llvm
  Cc: Stephane Eranian

On Fri, Jan 13, 2023 at 10:57 AM Ian Rogers <irogers@google.com> wrote:
>
> build_id__init only copies the buildid data up to size leaving the
> rest of the data array uninitialized. Copying the full array during
> synthesis means the written event contains uninitialized memory.  This
> was detected by the Clang/LLVM memory sanitizer.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/util/synthetic-events.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> index 3ab6a92b1a6d..348d05e4ec03 100644
> --- a/tools/perf/util/synthetic-events.c
> +++ b/tools/perf/util/synthetic-events.c
> @@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
>
>         len = pos->long_name_len + 1;
>         len = PERF_ALIGN(len, NAME_ALIGN);
> -       memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
> +       memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);

Ping. Should be an uncontroversial change to fix a copy of
uninitialized memory into the perf.data file during synthesis.

Thanks,
Ian

>         ev.build_id.size = pos->bid.size;
>         ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
>         ev.build_id.header.misc = misc | PERF_RECORD_MISC_BUILD_ID_SIZE;
> --
> 2.39.0.314.g84b9a713c41-goog
>

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

* Re: [PATCH] perf buildid: Avoid copy of uninitialized memory
  2023-01-19 16:08 ` Ian Rogers
@ 2023-01-19 16:29   ` Arnaldo Carvalho de Melo
  2023-01-19 16:39     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2023-01-19 16:29 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel,
	llvm, Stephane Eranian

Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
> On Fri, Jan 13, 2023 at 10:57 AM Ian Rogers <irogers@google.com> wrote:
> >
> > build_id__init only copies the buildid data up to size leaving the
> > rest of the data array uninitialized. Copying the full array during
> > synthesis means the written event contains uninitialized memory.  This
> > was detected by the Clang/LLVM memory sanitizer.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/util/synthetic-events.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> > index 3ab6a92b1a6d..348d05e4ec03 100644
> > --- a/tools/perf/util/synthetic-events.c
> > +++ b/tools/perf/util/synthetic-events.c
> > @@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
> >
> >         len = pos->long_name_len + 1;
> >         len = PERF_ALIGN(len, NAME_ALIGN);
> > -       memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
> > +       memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);
> 
> Ping. Should be an uncontroversial change to fix a copy of
> uninitialized memory into the perf.data file during synthesis.

Indeed, applied.

- Arnaldo

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

* Re: [PATCH] perf buildid: Avoid copy of uninitialized memory
  2023-01-19 16:29   ` Arnaldo Carvalho de Melo
@ 2023-01-19 16:39     ` Arnaldo Carvalho de Melo
  2023-01-19 16:57       ` Ian Rogers
  0 siblings, 1 reply; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2023-01-19 16:39 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel,
	llvm, Stephane Eranian

Em Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
> > On Fri, Jan 13, 2023 at 10:57 AM Ian Rogers <irogers@google.com> wrote:
> > >
> > > build_id__init only copies the buildid data up to size leaving the
> > > rest of the data array uninitialized. Copying the full array during
> > > synthesis means the written event contains uninitialized memory.  This
> > > was detected by the Clang/LLVM memory sanitizer.
> > >
> > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/util/synthetic-events.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > >
> > > diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> > > index 3ab6a92b1a6d..348d05e4ec03 100644
> > > --- a/tools/perf/util/synthetic-events.c
> > > +++ b/tools/perf/util/synthetic-events.c
> > > @@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
> > >
> > >         len = pos->long_name_len + 1;
> > >         len = PERF_ALIGN(len, NAME_ALIGN);
> > > -       memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
> > > +       memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);
> > 
> > Ping. Should be an uncontroversial change to fix a copy of
> > uninitialized memory into the perf.data file during synthesis.
> 
> Indeed, applied.

Humm, don't we better do it as:

+       memcpy(&ev.build_id.build_id, pos->bid.data, min(pos->bid.size, sizeof(pos->bid.data));

Lemme check what is setting that pos->bid.size....

Things like sysfs__read_build_id() that does such checks, but perhaps we
should be defensive and do it in this function as well?

- Arnaldo


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

* Re: [PATCH] perf buildid: Avoid copy of uninitialized memory
  2023-01-19 16:39     ` Arnaldo Carvalho de Melo
@ 2023-01-19 16:57       ` Ian Rogers
  2023-01-20  1:15         ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Rogers @ 2023-01-19 16:57 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel,
	llvm, Stephane Eranian

On Thu, Jan 19, 2023 at 8:39 AM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> Em Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
> > > On Fri, Jan 13, 2023 at 10:57 AM Ian Rogers <irogers@google.com> wrote:
> > > >
> > > > build_id__init only copies the buildid data up to size leaving the
> > > > rest of the data array uninitialized. Copying the full array during
> > > > synthesis means the written event contains uninitialized memory.  This
> > > > was detected by the Clang/LLVM memory sanitizer.
> > > >
> > > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > > ---
> > > >  tools/perf/util/synthetic-events.c | 2 +-
> > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > >
> > > > diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> > > > index 3ab6a92b1a6d..348d05e4ec03 100644
> > > > --- a/tools/perf/util/synthetic-events.c
> > > > +++ b/tools/perf/util/synthetic-events.c
> > > > @@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
> > > >
> > > >         len = pos->long_name_len + 1;
> > > >         len = PERF_ALIGN(len, NAME_ALIGN);
> > > > -       memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
> > > > +       memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);
> > >
> > > Ping. Should be an uncontroversial change to fix a copy of
> > > uninitialized memory into the perf.data file during synthesis.
> >
> > Indeed, applied.
>
> Humm, don't we better do it as:
>
> +       memcpy(&ev.build_id.build_id, pos->bid.data, min(pos->bid.size, sizeof(pos->bid.data));
>
> Lemme check what is setting that pos->bid.size....
>
> Things like sysfs__read_build_id() that does such checks, but perhaps we
> should be defensive and do it in this function as well?

Defensive is good, another option would be an assert but they can be
compiled out. Do you want me to repost?

Thanks,
Ian

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

* Re: [PATCH] perf buildid: Avoid copy of uninitialized memory
  2023-01-19 16:57       ` Ian Rogers
@ 2023-01-20  1:15         ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2023-01-20  1:15 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Nathan Chancellor, Nick Desaulniers,
	Tom Rix, Adrian Hunter, Leo Yan, linux-perf-users, linux-kernel,
	llvm, Stephane Eranian

Em Thu, Jan 19, 2023 at 08:57:21AM -0800, Ian Rogers escreveu:
> On Thu, Jan 19, 2023 at 8:39 AM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > Em Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
> > > > On Fri, Jan 13, 2023 at 10:57 AM Ian Rogers <irogers@google.com> wrote:
> > > > >
> > > > > build_id__init only copies the buildid data up to size leaving the
> > > > > rest of the data array uninitialized. Copying the full array during
> > > > > synthesis means the written event contains uninitialized memory.  This
> > > > > was detected by the Clang/LLVM memory sanitizer.
> > > > >
> > > > > Signed-off-by: Ian Rogers <irogers@google.com>
> > > > > ---
> > > > >  tools/perf/util/synthetic-events.c | 2 +-
> > > > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > > >
> > > > > diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> > > > > index 3ab6a92b1a6d..348d05e4ec03 100644
> > > > > --- a/tools/perf/util/synthetic-events.c
> > > > > +++ b/tools/perf/util/synthetic-events.c
> > > > > @@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
> > > > >
> > > > >         len = pos->long_name_len + 1;
> > > > >         len = PERF_ALIGN(len, NAME_ALIGN);
> > > > > -       memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
> > > > > +       memcpy(&ev.build_id.build_id, pos->bid.data, pos->bid.size);
> > > >
> > > > Ping. Should be an uncontroversial change to fix a copy of
> > > > uninitialized memory into the perf.data file during synthesis.
> > >
> > > Indeed, applied.
> >
> > Humm, don't we better do it as:
> >
> > +       memcpy(&ev.build_id.build_id, pos->bid.data, min(pos->bid.size, sizeof(pos->bid.data));
> >
> > Lemme check what is setting that pos->bid.size....
> >
> > Things like sysfs__read_build_id() that does such checks, but perhaps we
> > should be defensive and do it in this function as well?
> 
> Defensive is good, another option would be an assert but they can be
> compiled out. Do you want me to repost?

Please.

- Arnaldo

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

end of thread, other threads:[~2023-01-20  5:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-13 18:57 [PATCH] perf buildid: Avoid copy of uninitialized memory Ian Rogers
2023-01-19 16:08 ` Ian Rogers
2023-01-19 16:29   ` Arnaldo Carvalho de Melo
2023-01-19 16:39     ` Arnaldo Carvalho de Melo
2023-01-19 16:57       ` Ian Rogers
2023-01-20  1:15         ` Arnaldo Carvalho de Melo

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