perf buildid: Avoid copy of uninitialized memory
Ian Rogers
reviewerPATCH 1/1 perf buildid: Avoid copy of uninitialized memory
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.
--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/synthetic-events.c +1 -1
@@ -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 ;
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Ian Rogers
reviewerQuoted 16 lines · Ian Rogers
build_id__initonly 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 <[email protected]>--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/synthetic-events.c +0
-0
tools/perf/util/synthetic-events.c int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 · line 2222
Quoted 5 lines
tools/perf/util/synthetic-events.c +1
-1@@ -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
Quoted 6 lines
tools/perf/util/synthetic-events.c +0
-1ev.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
Arnaldo Carvalho de Melo
maintainerEm Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
Quoted 18 lines · Ian Rogers
Quoted 17 lines
build_id__initonly 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 <[email protected]>--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/synthetic-events.c +0
-0@@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16
Quoted 7 lines
tools/perf/util/synthetic-events.c int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 · line 2222
Quoted 4 lines
tools/perf/util/synthetic-events.c +1
-1len = 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
Arnaldo Carvalho de Melo
maintainerEm Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 27 lines
Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
Quoted 18 lines · Ian Rogers
Quoted 17 lines
build_id__initonly 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 <[email protected]>--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/synthetic-events.c +0
-0@@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16Quoted 7 lines
tools/perf/util/synthetic-events.c int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 · line 2222
Quoted 4 lines
tools/perf/util/synthetic-events.c +1
-1len = 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.
Quoted 1 line
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
Ian Rogers
reviewerQuoted 31 lines · Arnaldo Carvalho de Melo
Em Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 27 lines
Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
Quoted 18 lines · Ian Rogers
Quoted 17 lines
build_id__initonly 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 <[email protected]>--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/synthetic-events.c +0
-0@@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16Quoted 7 lines
tools/perf/util/synthetic-events.c int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 · line 2222
Quoted 4 lines
tools/perf/util/synthetic-events.c +1
-1len = 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.
Quoted 1 line
Indeed, applied.
Quoted 8 lines
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
Arnaldo Carvalho de Melo
maintainerEm Thu, Jan 19, 2023 at 08:57:21AM -0800, Ian Rogers escreveu:
Quoted 37 lines
Quoted 35 lines · Arnaldo Carvalho de Melo
Em Thu, Jan 19, 2023 at 01:29:30PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 27 lines
Em Thu, Jan 19, 2023 at 08:08:13AM -0800, Ian Rogers escreveu:
Quoted 18 lines · Ian Rogers
Quoted 17 lines
build_id__initonly 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 <[email protected]>--- tools/perf/util/synthetic-events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/synthetic-events.c +0
-0@@ -2219,7 +2219,7 @@ int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16Quoted 7 lines
tools/perf/util/synthetic-events.c int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 · line 2222
Quoted 4 lines
tools/perf/util/synthetic-events.c +1
-1len = 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.
Quoted 1 line
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));
Quoted 7 lines
Quoted 4 lines
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