In aslr_tool__process_sample(), PERF_SAMPLE_READ only copied the id word when evsel->core.attr.read_format had PERF_FORMAT_ID set, whereas the kernel and __evsel__parse_sample() unconditionally include the id word in both single-counter and PERF_FORMAT_GROUP modes. When PERF_FORMAT_ID was not explicitly set in attr.read_format, in_array[i] became desynchronized with subsequent sample fields. In addition, CHECK_BOUNDS() used addition (i + required_i > max_i) which could overflow, and the dropped PERF_SAMPLE_RAW, PERF_SAMPLE_STACK_USER, and PERF_SAMPLE_AUX handlers copied word-rounded lengths into out_array before discarding the sample. Unconditionally copy the id word in PERF_SAMPLE_READ using sample_read_value_size() with division-based bounds checks, make CHECK_BOUNDS() subtraction-based, and drop RAW/STACK_USER/AUX samples directly without dead copies. Fixes: 575c6d2bc470 ("perf inject/aslr: Implement sample address remapping") Signed-off-by: Ian Rogers --- tools/perf/util/aslr.c | 95 +++++++++--------------------------------- 1 file changed, 20 insertions(+), 75 deletions(-) diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c index dee077cca5fe..e236d5287043 100644 --- a/tools/perf/util/aslr.c +++ b/tools/perf/util/aslr.c @@ -8,6 +8,7 @@ #include "evlist.h" #include "machine.h" #include "map.h" +#include "sample.h" #include "thread.h" #include "tool.h" #include "session.h" @@ -780,7 +781,7 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, out_array = &new_event->sample.array[0]; #define CHECK_BOUNDS(required_i, required_j) \ - (i + (required_i) > max_i || j + (required_j) > max_j) + ((required_i) > max_i - i || (required_j) > max_j - j) #define COPY_U64() \ do { \ @@ -835,18 +836,20 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, if (orig_sample_type & PERF_SAMPLE_PERIOD) COPY_U64(); /* period */ if (orig_sample_type & PERF_SAMPLE_READ) { - if ((evsel->core.attr.read_format & PERF_FORMAT_GROUP) == 0) { + u64 read_format = evsel->core.attr.read_format; + + if ((read_format & PERF_FORMAT_GROUP) == 0) { COPY_U64(); /* value */ - if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) COPY_U64(); /* time_enabled */ - if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) COPY_U64(); /* time_running */ - if (evsel->core.attr.read_format & PERF_FORMAT_ID) - COPY_U64(); /* id */ - if (evsel->core.attr.read_format & PERF_FORMAT_LOST) + /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */ + COPY_U64(); /* id */ + if (read_format & PERF_FORMAT_LOST) COPY_U64(); /* lost */ } else { - u64 nr; + u64 nr, entry_words; if (CHECK_BOUNDS(1, 1)) { ret = -EFAULT; @@ -854,17 +857,18 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } nr = in_array[i]; COPY_U64(); - if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) + if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) COPY_U64(); /* time_enabled */ - if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) + if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) COPY_U64(); /* time_running */ - for (u64 cntr = 0; cntr < nr; cntr++) { - COPY_U64(); /* value */ - if (evsel->core.attr.read_format & PERF_FORMAT_ID) - COPY_U64(); /* id */ - if (evsel->core.attr.read_format & PERF_FORMAT_LOST) - COPY_U64(); /* lost */ + entry_words = sample_read_value_size(read_format) / sizeof(u64); + if (nr > (max_i - i) / entry_words || + nr > (max_j - j) / entry_words) { + ret = -EFAULT; + goto out_put; } + for (u64 cntr = 0; cntr < nr * entry_words; cntr++) + out_array[j++] = in_array[i++]; } } if (orig_sample_type & PERF_SAMPLE_CALLCHAIN) { @@ -934,16 +938,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } } if (orig_sample_type & PERF_SAMPLE_RAW) { - size_t bytes = sizeof(u32) + sample->raw_size; - size_t u64_words = (bytes + 7) / 8; - - if (i + u64_words > max_i || j + u64_words > max_j) { - ret = -EFAULT; - goto out_put; - } - memcpy(&out_array[j], &in_array[i], bytes); - i += u64_words; - j += u64_words; /* * TODO: certain raw samples can be remapped, such as * tracepoints by examining their fields. @@ -1041,30 +1035,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, } } if (orig_sample_type & PERF_SAMPLE_STACK_USER) { - u64 size; - - if (CHECK_BOUNDS(1, 1)) { - ret = -EFAULT; - goto out_put; - } - size = in_array[i]; - COPY_U64(); - if (size > 0) { - size_t u64_words = size / 8 + (size % 8 ? 1 : 0); - - if (u64_words > max_i - i || u64_words > max_j - j) { - ret = -EFAULT; - goto out_put; - } - memcpy(&out_array[j], &in_array[i], size); - if (size % 8) { - size_t pad = 8 - (size % 8); - - memset(((char *)&out_array[j]) + size, 0, pad); - } - i += u64_words; - j += u64_words; - } /* TODO: can this be less conservative? */ pr_debug("Dropping stack user sample as possible ASLR leak\n"); ret = 0; @@ -1110,31 +1080,6 @@ static int aslr_tool__process_sample(const struct perf_tool *tool, COPY_U64(); /* code_page_size */ if (orig_sample_type & PERF_SAMPLE_AUX) { - u64 size; - - if (CHECK_BOUNDS(1, 1)) { - ret = -EFAULT; - goto out_put; - } - out_array[j] = in_array[i]; - size = out_array[j++]; - i++; - if (size > 0) { - size_t u64_words = size / 8 + (size % 8 ? 1 : 0); - - if (u64_words > max_i - i || u64_words > max_j - j) { - ret = -EFAULT; - goto out_put; - } - memcpy(&out_array[j], &in_array[i], size); - if (size % 8) { - size_t pad = 8 - (size % 8); - - memset(((char *)&out_array[j]) + size, 0, pad); - } - i += u64_words; - j += u64_words; - } /* TODO: can this be less conservative? */ pr_debug("Dropping aux sample as possible ASLR leak\n"); ret = 0; -- 2.56.1.703.gf848ed807c-goog