perf inject/aslr: Add aslr tool to remap/obfuscate virtual addresses
Ian Rogers
reviewerPATCH v20 3/5 perf inject/aslr: Implement sample address remapping
In reply to Ian RogersAdd the sample address remapping logic to the ASLR tool. This patch implements aslr_tool__process_sample, which parses sample events, remaps IPs, ADDRs, callchains, and branch stacks using the mappings collected from metadata events, and drops potentially leaking raw, register, stack, physical address, and aux samples.
Also adds the aslr_tool__remap_address helper function.
Note on cross-endian compatibility:
'perf inject' functions as an endianness converter. Input files are read, and their events are byte-swapped to host endianness in memory. When the tool emits its output, it writes a host-endian PERF_MAGIC in the file header, thereby marking the output file as host-endian natively.
Because the output file is always written in host endianness, events and payloads must be constructed entirely using host-endian layouts. For this reason, this patch explicitly un-packs and repacks PERF_SAMPLE_TID (and PERF_SAMPLE_CPU) using unions to ensure that the sequential 32-bit layout is correctly aligned in host endianness. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event. When re-parsing the newly synthesized event, 'needs_swap=false' is explicitly used to prevent double swapping the already host-endian fields.
--- tools/perf/util/aslr.c | 465 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- 3 files changed, 472 insertions(+), 9 deletions(-)
tools/perf/util/aslr.c +461 -4
@@ -20,6 +20,7 @@#include <linux/zalloc.h> #include <inttypes.h> #include <unistd.h> +#include <byteswap.h> /** * struct remap_addresses_key - Key for mapping original addresses to remapped ones.
@@ -112,6 +113,60 @@ static u64 round_up_to_page_size(u64 addr)return (addr + page_size -1 ) & ~((u64 )page_size -1 ); } +static u64 aslr_tool__remap_address (struct aslr_tool *aslr, +struct thread *aslr_thread, +u8 cpumode, +u64 addr) +{ +struct addr_location al; +struct remap_addresses_key key; +u64 *remapped_invariant_ptr =NULL ; +u64 remap_addr =0 ; +u8 effective_cpumode = cpumode; + +if (!aslr_thread) +return 0 ;/* No thread. */ + +addr_location__init (&al); +if (!thread__find_map (aslr_thread, cpumode, addr, &al)) { +/* +* If lookup fails with specified cpumode, try fallback to the other space +* to be robust against bad cpumode in samples. +*/ +if (cpumode ==PERF_RECORD_MISC_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_USER ; +else if (cpumode ==PERF_RECORD_MISC_USER ) + effective_cpumode =PERF_RECORD_MISC_KERNEL ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_GUEST_USER ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_USER ) + effective_cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; + +if (!thread__find_map (aslr_thread, effective_cpumode, addr, &al)) { +addr_location__exit (&al); +return 0 ;/* No mmap. */ + } + } + + key.machine =maps__machine (thread__maps (aslr_thread)); + key.dso =map__dso (al.map); + key.invariant =map__start (al.map) -map__pgoff (al.map); + key.pid = (effective_cpumode ==PERF_RECORD_MISC_KERNEL || + effective_cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) ? + kernel_pid :thread__pid (aslr_thread); + +if (hashmap__find (&aslr->remap_addresses, &key, &remapped_invariant_ptr)) { + remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + + (addr -map__start (al.map)); + }else { +pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , + addr,map__start (al.map),map__size (al.map), key.pid); + } + +addr_location__exit (&al); +return remap_addr; +} +struct aslr_machine_priv {bool kernel_maps_loaded; };
@@ -616,13 +671,415 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,struct perf_sample *sample, struct machine *machine) { - struct delegate_tool *del_tool = container_of(tool, struct delegate_tool, tool); - struct aslr_tool *aslr = container_of(del_tool, struct aslr_tool, tool); -struct perf_tool *delegate = aslr->tool.delegate; + struct evsel *evsel = sample->evsel; +struct delegate_tool *del_tool; +struct aslr_tool *aslr; +struct perf_tool *delegate; +int ret; +u64 sample_type; +struct thread *thread; +struct machine *aslr_machine; +__u64 max_i; +__u64 max_j; +union perf_event *new_event; +struct perf_sample new_sample; +__u64 *in_array, *out_array; +u8 cpumode; +u64 addr; +size_t i; +size_t j; +struct aslr_evsel_priv *priv =NULL ; +u64 orig_sample_type; +u64 orig_regs_user; +u64 orig_regs_intr; + + del_tool = container_of(tool, struct delegate_tool, tool); + aslr = container_of(del_tool, struct aslr_tool, tool); + delegate = aslr->tool.delegate; + + + +if (evsel__is_dummy_event (evsel)) +return delegate->sample (delegate, event, sample, machine); + + ret = -EFAULT ; + sample_type = evsel->core.attr.sample_type; + max_i = (event->header.size -sizeof (struct perf_event_header )) /sizeof (__u64); + max_j = (PERF_SAMPLE_MAX_SIZE -sizeof (struct perf_event_header )) /sizeof (__u64); + new_event = (union perf_event *)aslr->event_copy; + cpumode = sample->cpumode; + i =0 ; + j =0 ; + + aslr_machine =machines__findnew (&aslr->machines, machine->pid); +if (!aslr_machine) +return -ENOMEM ; +if (aslr_tool__preload_kernel_maps (aslr_machine) <0 ) +return -ENOMEM ; + + thread =machine__findnew_thread (aslr_machine, sample->pid, sample->tid); + +if (!thread) +return -ENOMEM ; + +if (max_i >PERF_SAMPLE_MAX_SIZE /sizeof (u64)) + goto out_put; + + new_event->sample.header = event->sample.header; + + in_array = &event->sample.array[0 ]; + out_array = &new_event->sample.array[0 ]; + +#define CHECK_BOUNDS (required_i, required_j) \ + (i + (required_i) > max_i || j + (required_j) > max_j) + +#define COPY_U64 () \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] = in_array[i++]; \ + } while (0) + +#define REMAP_U64 (addr_field) \ + do { \ + u64 remapped; \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + remapped = aslr_tool__remap_address(aslr, thread, cpumode, addr_field); \ + out_array[j++] = remapped; \ + i++; \ + } while (0) + +if (sample_type &PERF_SAMPLE_IDENTIFIER ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_IP ) +REMAP_U64 (sample->ip); +if (sample_type &PERF_SAMPLE_TID ) { +union { +u64 val64; +u32 val32[2 ]; + } u; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + u.val32[0 ] = sample->pid; + u.val32[1 ] = sample->tid; + out_array[j++] = u.val64; + i++; + } +if (sample_type &PERF_SAMPLE_TIME ) +COPY_U64 ();/* time */ +if (sample_type &PERF_SAMPLE_ADDR ) +REMAP_U64 (sample->addr); +if (sample_type &PERF_SAMPLE_ID ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_STREAM_ID ) +COPY_U64 ();/* stream_id */ +if (sample_type &PERF_SAMPLE_CPU ) +COPY_U64 ();/* cpu, res */ +if (sample_type &PERF_SAMPLE_PERIOD ) +COPY_U64 ();/* period */ +if (sample_type &PERF_SAMPLE_READ ) { +if ((evsel->core.attr.read_format &PERF_FORMAT_GROUP ) ==0 ) { +COPY_U64 ();/* value */ +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 ) +COPY_U64 ();/* lost */ + }else { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 */ + } + } + } +if (sample_type &PERF_SAMPLE_CALLCHAIN ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +for (u64 cntr =0 ; cntr < nr; cntr++) { +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + addr = in_array[i++]; +if (addr >=PERF_CONTEXT_MAX ) { + out_array[j++] = addr; +switch (addr) { +case PERF_CONTEXT_HV : + cpumode =PERF_RECORD_MISC_HYPERVISOR ; +break ; +case PERF_CONTEXT_KERNEL : + cpumode =PERF_RECORD_MISC_KERNEL ; +break ; +case PERF_CONTEXT_USER : + cpumode =PERF_RECORD_MISC_USER ; +break ; +case PERF_CONTEXT_GUEST : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_KERNEL : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_USER : + cpumode =PERF_RECORD_MISC_GUEST_USER ; +break ; +case PERF_CONTEXT_USER_DEFERRED : +if (cntr +1 >= nr) { +pr_debug ("Truncated callchain deferred cookie context\n" ); + ret =0 ; + goto out_put; + } +/* +* Immediately followed by a 64-bit +* stitching cookie. Skip/Copy it! +*/ +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + out_array[j++] = in_array[i++]; + cntr++; + cpumode =PERF_RECORD_MISC_USER ; +break ; +default : +pr_debug ("invalid callchain context: %" PRIx64"\n" , addr); + ret =0 ; + goto out_put; + } +continue ; + } + addr =aslr_tool__remap_address (aslr, thread, cpumode, addr); + out_array[j++] = addr; + } + } +if (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. +*/ +pr_debug ("Dropping raw samples as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_HW_INDEX ) +COPY_U64 ();/* hw_idx */ + +if (nr > (ULLONG_MAX /3 )) { + ret = -EFAULT ; + goto out_put; + } +if (nr *3 > max_i - i || nr *3 > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) { +u64 from = in_array[i++]; +u64 to = in_array[i++]; + + from =aslr_tool__remap_address (aslr, thread, sample->cpumode, from); + to =aslr_tool__remap_address (aslr, thread, sample->cpumode, to); + + out_array[j++] = from; + out_array[j++] = to; + out_array[j++] = in_array[i++];/* flags */ + } +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_COUNTERS ) { +if (nr > max_i - i || nr > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) +COPY_U64 (); + } + } +if (sample_type &PERF_SAMPLE_REGS_USER ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping regs user sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_STACK_USER ) { +u64 size; -return delegate->sample (delegate, event, sample, machine); +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 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_WEIGHT_TYPE ) +COPY_U64 ();/* perf_sample_weight */ +if (sample_type &PERF_SAMPLE_DATA_SRC ) +COPY_U64 ();/* data_src */ +if (sample_type &PERF_SAMPLE_TRANSACTION ) +COPY_U64 ();/* transaction */ +if (sample_type &PERF_SAMPLE_REGS_INTR ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping interrupt register sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_PHYS_ADDR ) { +COPY_U64 ();/* phys_addr */ +/* TODO: can this be less conservative? */ +pr_debug ("Dropping physical address sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_CGROUP ) +COPY_U64 ();/* cgroup */ +if (sample_type &PERF_SAMPLE_DATA_PAGE_SIZE ) +COPY_U64 ();/* data_page_size */ +if (sample_type &PERF_SAMPLE_CODE_PAGE_SIZE ) +COPY_U64 ();/* code_page_size */ + +if (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 ; + goto out_put; + } + +if (evsel__is_offcpu_event (evsel)) { +/* TODO: can this be less conservative? */ +pr_debug ("Dropping off-CPU sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } + + new_event->sample.header.size =sizeof (struct perf_event_header ) + j *sizeof (u64); + +perf_sample__init (&new_sample,/*all=*/ true); + ret =__evsel__parse_sample (evsel, new_event, &new_sample,/*needs_swap=*/ false); + +if (ret) { +perf_sample__exit (&new_sample); + goto out_put; + } + + new_sample.evsel = evsel; + ret = delegate->sample (delegate, new_event, &new_sample, machine); +perf_sample__exit (&new_sample); + +out_put: +thread__put (thread); +return ret; } +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 +static int skipn (int fd,off_t n) {char buf[4096 ];
tools/perf/util/evsel.c +3 -3
@@ -3337,11 +3337,11 @@ static int __set_offcpu_sample(struct perf_sample *data)return -EFAULT ; } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *data) +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap) {u64 type = evsel->core.attr.sample_type; -bool swapped = evsel->needs_swap; +bool swapped = needs_swap;const __u64 *array;u16 max_size = event->header.size;const void *endp = (void *)event + max_size;
tools/perf/util/evsel.h +8 -2
@@ -432,8 +432,14 @@ static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu_map_idxreturn __evsel__read_on_cpu (evsel, cpu_map_idx, thread, true); } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *sample); +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap); + +static inline int evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data) +{ +return __evsel__parse_sample (evsel, event, data, evsel->needs_swap); +}int evsel__parse_sample_timestamp (struct evsel *evsel,union perf_event *event,u64 *timestamp);
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
[email protected]
AI reviewRe: [PATCH v20 3/5] perf inject/aslr: Implement sample address remapping
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Addresses that cannot be remapped are destructively overwritten with 0. - [Medium] Missing…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Addresses that cannot be remapped are destructively overwritten with 0.
- [Medium] Missing union-based repacking for PERF_SAMPLE_CPU causes cross-endian event synthesis failure. --
commit 4363deb515e79cb7e3488a5d19b247858fb88edd
Author: Gabriel Marin <[email protected]>
Add the sample address remapping logic to the ASLR tool.
This patch implements sample event processing for the ASLR tool, parsing sample events and remapping addresses like IPs, callchains, and branch stacks. It also adds the aslr_tool__remap_address helper function.
Quoted 16 lines
tools/perf/util/aslr.c +9
-0@@ -112,6 +113,60 @@ static u64 round_up_to_page_size(u64 addr)return (addr + page_size -1 ) & ~((u64 )page_size -1 ); } +static u64 aslr_tool__remap_address (struct aslr_tool *aslr, +struct thread *aslr_thread, +u8 cpumode, +u64 addr) +{ +struct addr_location al; +struct remap_addresses_key key; +u64 *remapped_invariant_ptr =NULL ; +u64 remap_addr =0 ;
[ ... ]
tools/perf/util/aslr.c static u64 round_up_to_page_size(u64 addr) · line 147
Quoted 3 lines
tools/perf/util/aslr.c +3
-0+if (!thread__find_map (aslr_thread, effective_cpumode, addr, &al)) { +addr_location__exit (&al); +return 0 ;/* No mmap. */
[Severity: High]
Does returning 0 here destructively overwrite valid non-randomized addresses?
If an address cannot be mapped (such as JIT code, vsyscall, or missing map regions), returning 0 unconditionally overwrites the sample's IP, ADDR, callchains, and branch stacks. Should this return the original addr instead to avoid clustering profiler trace data at NULL?
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+ } + } + + key.machine =maps__machine (thread__maps (aslr_thread)); + key.dso =map__dso (al.map); + key.invariant =map__start (al.map) -map__pgoff (al.map); + key.pid = (effective_cpumode ==PERF_RECORD_MISC_KERNEL || + effective_cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) ? + kernel_pid :thread__pid (aslr_thread); +
tools/perf/util/aslr.c static u64 round_up_to_page_size(u64 addr) · line 167
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+if (hashmap__find (&aslr->remap_addresses, &key, &remapped_invariant_ptr)) { + remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + + (addr -map__start (al.map)); + }else { +pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , + addr,map__start (al.map),map__size (al.map), key.pid); + } + +addr_location__exit (&al); +return remap_addr;
[Severity: High]
Similarly, when the mapping is not in the remap_addresses hashmap, this will return remap_addr which was initialized to 0. Would it be safer to default remap_addr to the original addr instead of 0?
Quoted 1 line
+}
[ ... ]
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 785
Quoted 6 lines
tools/perf/util/aslr.c +6
-0+if (sample_type &PERF_SAMPLE_ID ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_STREAM_ID ) +COPY_U64 ();/* stream_id */ +if (sample_type &PERF_SAMPLE_CPU ) +COPY_U64 ();/* cpu, res */
[Severity: Medium]
The commit message states that both PERF_SAMPLE_TID and PERF_SAMPLE_CPU must be unpacked and repacked using unions to ensure correct 32-bit layout alignment for cross-endian hosts. However, it looks like PERF_SAMPLE_CPU blindly uses COPY_U64() here instead of a union.
Since the generic perf event parsing layer byte-swaps the 64-bit payload array, this reverses the internal order of the two 32-bit fields (cpu and res). Does copying this improperly swapped 64-bit word verbatim write the reversed fields directly to the output, corrupting them when reparsed with needs_swap=false?
Quoted 2 lines
tools/perf/util/aslr.c +2
-0+if (sample_type &PERF_SAMPLE_PERIOD ) +COPY_U64 ();/* period */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v20 3/5] perf inject/aslr: Implement sample address remapping
In reply to Ian RogersQuoted 121 lines · Ian Rogers
Add the sample address remapping logic to the ASLR tool. This patch implements
aslr_tool__process_sample, which parses sample events, remaps IPs, ADDRs, callchains, and branch stacks using the mappings collected from metadata events, and drops potentially leaking raw, register, stack, physical address, and aux samples.Also adds the
aslr_tool__remap_addresshelper function.Note on cross-endian compatibility:
'perf inject' functions as an endianness converter. Input files are read, and their events are byte-swapped to host endianness in memory. When the tool emits its output, it writes a host-endianPERF_MAGICin the file header, thereby marking the output file as host-endian natively.Because the output file is always written in host endianness, events and payloads must be constructed entirely using host-endian layouts. For this reason, this patch explicitly un-packs and repacks
PERF_SAMPLE_TID(andPERF_SAMPLE_CPU) using unions to ensure that the sequential 32-bit layout is correctly aligned in host endianness. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event. When re-parsing the newly synthesized event, 'needs_swap=false' is explicitly used to prevent double swapping the already host-endian fields.Co-developed-by Gabriel Marin <[email protected]>Signed-off-by Gabriel Marin <[email protected]>Signed-off-by Ian Rogers <[email protected]>Assisted-by Antigravity:gemini-3.1-pro--- tools/perf/util/aslr.c | 465 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- 3 files changed, 472 insertions(+), 9 deletions(-)tools/perf/util/aslr.c +62
-3@@ -20,6 +20,7 @@#include <linux/zalloc.h> #include <inttypes.h> #include <unistd.h> +#include <byteswap.h> /** * struct remap_addresses_key - Key for mapping original addresses to remapped ones. @@ -112,6 +113,60 @@ static u64 round_up_to_page_size(u64 addr)return (addr + page_size -1 ) & ~((u64 )page_size -1 ); } +static u64 aslr_tool__remap_address (struct aslr_tool *aslr, +struct thread *aslr_thread, +u8 cpumode, +u64 addr) +{ +struct addr_location al; +struct remap_addresses_key key; +u64 *remapped_invariant_ptr =NULL ; +u64 remap_addr =0 ; +u8 effective_cpumode = cpumode; + +if (!aslr_thread) +return 0 ;/* No thread. */ + +addr_location__init (&al); +if (!thread__find_map (aslr_thread, cpumode, addr, &al)) { +/* +* If lookup fails with specified cpumode, try fallback to the other space +* to be robust against bad cpumode in samples. +*/ +if (cpumode ==PERF_RECORD_MISC_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_USER ; +else if (cpumode ==PERF_RECORD_MISC_USER ) + effective_cpumode =PERF_RECORD_MISC_KERNEL ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_GUEST_USER ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_USER ) + effective_cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; + +if (!thread__find_map (aslr_thread, effective_cpumode, addr, &al)) { +addr_location__exit (&al); +return 0 ;/* No mmap. */ + } + } + + key.machine =maps__machine (thread__maps (aslr_thread)); + key.dso =map__dso (al.map); + key.invariant =map__start (al.map) -map__pgoff (al.map); + key.pid = (effective_cpumode ==PERF_RECORD_MISC_KERNEL || + effective_cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) ? + kernel_pid :thread__pid (aslr_thread); + +if (hashmap__find (&aslr->remap_addresses, &key, &remapped_invariant_ptr)) { + remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + + (addr -map__start (al.map)); + }else { +pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , + addr,map__start (al.map),map__size (al.map), key.pid); + } + +addr_location__exit (&al); +return remap_addr; +} +struct aslr_machine_priv {bool kernel_maps_loaded; };@@ -616,13 +671,415 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,struct perf_sample *sample, struct machine *machine) { - struct delegate_tool *del_tool = container_of(tool, struct delegate_tool, tool); - struct aslr_tool *aslr = container_of(del_tool, struct aslr_tool, tool); -struct perf_tool *delegate = aslr->tool.delegate; + struct evsel *evsel = sample->evsel; +struct delegate_tool *del_tool; +struct aslr_tool *aslr; +struct perf_tool *delegate; +int ret; +u64 sample_type; +struct thread *thread;
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 690
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+struct machine *aslr_machine; +__u64 max_i; +__u64 max_j; +union perf_event *new_event; +struct perf_sample new_sample; +__u64 *in_array, *out_array; +u8 cpumode; +u64 addr; +size_t i; +size_t j;
The following 4 variables are not used in this patch, so the build at this point fails:
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 694
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+struct aslr_evsel_priv *priv =NULL ; +u64 orig_sample_type; +u64 orig_regs_user; +u64 orig_regs_intr;
With:
CC /tmp/build/perf-tools-next/util/bpf_lock_contention.outil/aslr.c: In function ‘aslr_tool__process_sample’:util/aslr.c:694:13: error: unused variable ‘orig_regs_intr’ [-Werror=unused-variable]
694 | u64 orig_regs_intr;
| ^~~~~~~~~~~~~~
util/aslr.c:693:13: error: unused variable ‘orig_regs_user’ [-Werror=unused-variable]
693 | u64 orig_regs_user;
| ^~~~~~~~~~~~~~
util/aslr.c:692:13: error: unused variable ‘orig_sample_type’ [-Werror=unused-variable]
692 | u64 orig_sample_type;
| ^~~~~~~~~~~~~~~~util/aslr.c:691:33: error: unused variable ‘priv’ [-Werror=unused-variable]
691 | struct aslr_evsel_priv *priv = NULL;
| ^~~~
cc1: all warnings being treated as errors
make[4]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:95: /tmp/build/perf-tools-next/util/aslr.o] Error 1
So I moved it to the next patch, where they start to be used.
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 699
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+ del_tool = container_of(tool, struct delegate_tool, tool); + aslr = container_of(del_tool, struct aslr_tool, tool); + delegate = aslr->tool.delegate; +
I also removed these extra blank lines and collected James Tested-by.
Thanks, applied to perf-tools-next, for v7.2.
- Arnaldo
Quoted 429 lines
tools/perf/util/aslr.c +380
-1+ + +if (evsel__is_dummy_event (evsel)) +return delegate->sample (delegate, event, sample, machine); + + ret = -EFAULT ; + sample_type = evsel->core.attr.sample_type; + max_i = (event->header.size -sizeof (struct perf_event_header )) /sizeof (__u64); + max_j = (PERF_SAMPLE_MAX_SIZE -sizeof (struct perf_event_header )) /sizeof (__u64); + new_event = (union perf_event *)aslr->event_copy; + cpumode = sample->cpumode; + i =0 ; + j =0 ; + + aslr_machine =machines__findnew (&aslr->machines, machine->pid); +if (!aslr_machine) +return -ENOMEM ; +if (aslr_tool__preload_kernel_maps (aslr_machine) <0 ) +return -ENOMEM ; + + thread =machine__findnew_thread (aslr_machine, sample->pid, sample->tid); + +if (!thread) +return -ENOMEM ; + +if (max_i >PERF_SAMPLE_MAX_SIZE /sizeof (u64)) + goto out_put; + + new_event->sample.header = event->sample.header; + + in_array = &event->sample.array[0 ]; + out_array = &new_event->sample.array[0 ]; + +#define CHECK_BOUNDS (required_i, required_j) \ + (i + (required_i) > max_i || j + (required_j) > max_j) + +#define COPY_U64 () \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] = in_array[i++]; \ + } while (0) + +#define REMAP_U64 (addr_field) \ + do { \ + u64 remapped; \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + remapped = aslr_tool__remap_address(aslr, thread, cpumode, addr_field); \ + out_array[j++] = remapped; \ + i++; \ + } while (0) + +if (sample_type &PERF_SAMPLE_IDENTIFIER ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_IP ) +REMAP_U64 (sample->ip); +if (sample_type &PERF_SAMPLE_TID ) { +union { +u64 val64; +u32 val32[2 ]; + } u; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + u.val32[0 ] = sample->pid; + u.val32[1 ] = sample->tid; + out_array[j++] = u.val64; + i++; + } +if (sample_type &PERF_SAMPLE_TIME ) +COPY_U64 ();/* time */ +if (sample_type &PERF_SAMPLE_ADDR ) +REMAP_U64 (sample->addr); +if (sample_type &PERF_SAMPLE_ID ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_STREAM_ID ) +COPY_U64 ();/* stream_id */ +if (sample_type &PERF_SAMPLE_CPU ) +COPY_U64 ();/* cpu, res */ +if (sample_type &PERF_SAMPLE_PERIOD ) +COPY_U64 ();/* period */ +if (sample_type &PERF_SAMPLE_READ ) { +if ((evsel->core.attr.read_format &PERF_FORMAT_GROUP ) ==0 ) { +COPY_U64 ();/* value */ +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 ) +COPY_U64 ();/* lost */ + }else { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 */ + } + } + } +if (sample_type &PERF_SAMPLE_CALLCHAIN ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +for (u64 cntr =0 ; cntr < nr; cntr++) { +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + addr = in_array[i++]; +if (addr >=PERF_CONTEXT_MAX ) { + out_array[j++] = addr; +switch (addr) { +case PERF_CONTEXT_HV : + cpumode =PERF_RECORD_MISC_HYPERVISOR ; +break ; +case PERF_CONTEXT_KERNEL : + cpumode =PERF_RECORD_MISC_KERNEL ; +break ; +case PERF_CONTEXT_USER : + cpumode =PERF_RECORD_MISC_USER ; +break ; +case PERF_CONTEXT_GUEST : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_KERNEL : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_USER : + cpumode =PERF_RECORD_MISC_GUEST_USER ; +break ; +case PERF_CONTEXT_USER_DEFERRED : +if (cntr +1 >= nr) { +pr_debug ("Truncated callchain deferred cookie context\n" ); + ret =0 ; + goto out_put; + } +/* +* Immediately followed by a 64-bit +* stitching cookie. Skip/Copy it! +*/ +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + out_array[j++] = in_array[i++]; + cntr++; + cpumode =PERF_RECORD_MISC_USER ; +break ; +default : +pr_debug ("invalid callchain context: %" PRIx64"\n" , addr); + ret =0 ; + goto out_put; + } +continue ; + } + addr =aslr_tool__remap_address (aslr, thread, cpumode, addr); + out_array[j++] = addr; + } + } +if (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. +*/ +pr_debug ("Dropping raw samples as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_HW_INDEX ) +COPY_U64 ();/* hw_idx */ + +if (nr > (ULLONG_MAX /3 )) { + ret = -EFAULT ; + goto out_put; + } +if (nr *3 > max_i - i || nr *3 > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) { +u64 from = in_array[i++]; +u64 to = in_array[i++]; + + from =aslr_tool__remap_address (aslr, thread, sample->cpumode, from); + to =aslr_tool__remap_address (aslr, thread, sample->cpumode, to); + + out_array[j++] = from; + out_array[j++] = to; + out_array[j++] = in_array[i++];/* flags */ + } +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_COUNTERS ) { +if (nr > max_i - i || nr > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) +COPY_U64 (); + } + } +if (sample_type &PERF_SAMPLE_REGS_USER ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping regs user sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_STACK_USER ) { +u64 size; -return delegate->sample (delegate, event, sample, machine); +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 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_WEIGHT_TYPE ) +COPY_U64 ();/* perf_sample_weight */ +if (sample_type &PERF_SAMPLE_DATA_SRC ) +COPY_U64 ();/* data_src */ +if (sample_type &PERF_SAMPLE_TRANSACTION ) +COPY_U64 ();/* transaction */ +if (sample_type &PERF_SAMPLE_REGS_INTR ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping interrupt register sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_PHYS_ADDR ) { +COPY_U64 ();/* phys_addr */ +/* TODO: can this be less conservative? */ +pr_debug ("Dropping physical address sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_CGROUP ) +COPY_U64 ();/* cgroup */ +if (sample_type &PERF_SAMPLE_DATA_PAGE_SIZE ) +COPY_U64 ();/* data_page_size */ +if (sample_type &PERF_SAMPLE_CODE_PAGE_SIZE ) +COPY_U64 ();/* code_page_size */ + +if (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 ; + goto out_put; + } + +if (evsel__is_offcpu_event (evsel)) { +/* TODO: can this be less conservative? */ +pr_debug ("Dropping off-CPU sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } + + new_event->sample.header.size =sizeof (struct perf_event_header ) + j *sizeof (u64); + +perf_sample__init (&new_sample,/*all=*/ true); + ret =__evsel__parse_sample (evsel, new_event, &new_sample,/*needs_swap=*/ false); + +if (ret) { +perf_sample__exit (&new_sample); + goto out_put; + } + + new_sample.evsel = evsel; + ret = delegate->sample (delegate, new_event, &new_sample, machine); +perf_sample__exit (&new_sample); + +out_put: +thread__put (thread); +return ret; } +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 +static int skipn (int fd,off_t n) {char buf[4096 ];tools/perf/util/evsel.c +3
-3@@ -3337,11 +3337,11 @@ static int __set_offcpu_sample(struct perf_sample *data)return -EFAULT ; } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *data) +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap) {u64 type = evsel->core.attr.sample_type; -bool swapped = evsel->needs_swap; +bool swapped = needs_swap;const __u64 *array;u16 max_size = event->header.size;const void *endp = (void *)event + max_size;tools/perf/util/evsel.h +8
-2@@ -432,8 +432,14 @@ static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu_map_idxreturn __evsel__read_on_cpu (evsel, cpu_map_idx, thread, true); } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *sample); +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap); + +static inline int evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data) +{ +return __evsel__parse_sample (evsel, event, data, evsel->needs_swap); +}int evsel__parse_sample_timestamp (struct evsel *evsel,union perf_event *event,u64 *timestamp);Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Ian Rogers
reviewerRe: [PATCH v20 3/5] perf inject/aslr: Implement sample address remapping
Quoted 164 lines · Arnaldo Carvalho de Melo
Quoted 121 lines · Ian Rogers
Add the sample address remapping logic to the ASLR tool. This patch implements
aslr_tool__process_sample, which parses sample events, remaps IPs, ADDRs, callchains, and branch stacks using the mappings collected from metadata events, and drops potentially leaking raw, register, stack, physical address, and aux samples.Also adds the
aslr_tool__remap_addresshelper function.Note on cross-endian compatibility:
'perf inject' functions as an endianness converter. Input files are read, and their events are byte-swapped to host endianness in memory. When the tool emits its output, it writes a host-endianPERF_MAGICin the file header, thereby marking the output file as host-endian natively.Because the output file is always written in host endianness, events and payloads must be constructed entirely using host-endian layouts. For this reason, this patch explicitly un-packs and repacks
PERF_SAMPLE_TID(andPERF_SAMPLE_CPU) using unions to ensure that the sequential 32-bit layout is correctly aligned in host endianness. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event. When re-parsing the newly synthesized event, 'needs_swap=false' is explicitly used to prevent double swapping the already host-endian fields.Co-developed-by Gabriel Marin <[email protected]>Signed-off-by Gabriel Marin <[email protected]>Signed-off-by Ian Rogers <[email protected]>Assisted-by Antigravity:gemini-3.1-pro--- tools/perf/util/aslr.c | 465 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- 3 files changed, 472 insertions(+), 9 deletions(-)tools/perf/util/aslr.c +62
-3@@ -20,6 +20,7 @@#include <linux/zalloc.h> #include <inttypes.h> #include <unistd.h> +#include <byteswap.h> /** * struct remap_addresses_key - Key for mapping original addresses to remapped ones. @@ -112,6 +113,60 @@ static u64 round_up_to_page_size(u64 addr)return (addr + page_size -1 ) & ~((u64 )page_size -1 ); } +static u64 aslr_tool__remap_address (struct aslr_tool *aslr, +struct thread *aslr_thread, +u8 cpumode, +u64 addr) +{ +struct addr_location al; +struct remap_addresses_key key; +u64 *remapped_invariant_ptr =NULL ; +u64 remap_addr =0 ; +u8 effective_cpumode = cpumode; + +if (!aslr_thread) +return 0 ;/* No thread. */ + +addr_location__init (&al); +if (!thread__find_map (aslr_thread, cpumode, addr, &al)) { +/* +* If lookup fails with specified cpumode, try fallback to the other space +* to be robust against bad cpumode in samples. +*/ +if (cpumode ==PERF_RECORD_MISC_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_USER ; +else if (cpumode ==PERF_RECORD_MISC_USER ) + effective_cpumode =PERF_RECORD_MISC_KERNEL ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) + effective_cpumode =PERF_RECORD_MISC_GUEST_USER ; +else if (cpumode ==PERF_RECORD_MISC_GUEST_USER ) + effective_cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; + +if (!thread__find_map (aslr_thread, effective_cpumode, addr, &al)) { +addr_location__exit (&al); +return 0 ;/* No mmap. */ + } + } + + key.machine =maps__machine (thread__maps (aslr_thread)); + key.dso =map__dso (al.map); + key.invariant =map__start (al.map) -map__pgoff (al.map); + key.pid = (effective_cpumode ==PERF_RECORD_MISC_KERNEL || + effective_cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) ? + kernel_pid :thread__pid (aslr_thread); + +if (hashmap__find (&aslr->remap_addresses, &key, &remapped_invariant_ptr)) { + remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + + (addr -map__start (al.map)); + }else { +pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , + addr,map__start (al.map),map__size (al.map), key.pid); + } + +addr_location__exit (&al); +return remap_addr; +} +struct aslr_machine_priv {bool kernel_maps_loaded; };@@ -616,13 +671,415 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,struct perf_sample *sample, struct machine *machine) { - struct delegate_tool *del_tool = container_of(tool, struct delegate_tool, tool); - struct aslr_tool *aslr = container_of(del_tool, struct aslr_tool, tool); -struct perf_tool *delegate = aslr->tool.delegate; + struct evsel *evsel = sample->evsel; +struct delegate_tool *del_tool; +struct aslr_tool *aslr; +struct perf_tool *delegate; +int ret; +u64 sample_type; +struct thread *thread;tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 690
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+struct machine *aslr_machine; +__u64 max_i; +__u64 max_j; +union perf_event *new_event; +struct perf_sample new_sample; +__u64 *in_array, *out_array; +u8 cpumode; +u64 addr; +size_t i; +size_t j;The following 4 variables are not used in this patch, so the build at this point fails:
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 694
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+struct aslr_evsel_priv *priv =NULL ; +u64 orig_sample_type; +u64 orig_regs_user; +u64 orig_regs_intr;With:
CC
/tmp/build/perf-tools-next/util/bpf_lock_contention.outil/aslr.c: In function ‘aslr_tool__process_sample’:util/aslr.c:694:13: error: unused variable ‘orig_regs_intr’ [-Werror=unused-variable]694 | u64 orig_regs_intr; | ^~~~~~~~~~~~~~ util/aslr.c:693:13: error: unused variable ‘orig_regs_user’ [-Werror=unused-variable] 693 | u64 orig_regs_user; | ^~~~~~~~~~~~~~ util/aslr.c:692:13: error: unused variable ‘orig_sample_type’ [-Werror=unused-variable] 692 | u64 orig_sample_type; | ^~~~~~~~~~~~~~~~
util/aslr.c:691:33: error: unused variable ‘priv’ [-Werror=unused-variable]
691 | structaslr_evsel_priv*priv = NULL;
| ^~~~
cc1: all warnings being treated as errors
make[4]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:95:/tmp/build/perf-tools-next/util/aslr.o] Error 1So I moved it to the next patch, where they start to be used.
Quoted 8 lines
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 699
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+ del_tool = container_of(tool, struct delegate_tool, tool); + aslr = container_of(del_tool, struct aslr_tool, tool); + delegate = aslr->tool.delegate; +I also removed these extra blank lines and collected James Tested-by.
Thanks, applied to perf-tools-next, for v7.2.
Thanks for merging and for the fixes!
Ian
Quoted 431 lines
- Arnaldo
Quoted 429 lines
tools/perf/util/aslr.c +380
-1+ + +if (evsel__is_dummy_event (evsel)) +return delegate->sample (delegate, event, sample, machine); + + ret = -EFAULT ; + sample_type = evsel->core.attr.sample_type; + max_i = (event->header.size -sizeof (struct perf_event_header )) /sizeof (__u64); + max_j = (PERF_SAMPLE_MAX_SIZE -sizeof (struct perf_event_header )) /sizeof (__u64); + new_event = (union perf_event *)aslr->event_copy; + cpumode = sample->cpumode; + i =0 ; + j =0 ; + + aslr_machine =machines__findnew (&aslr->machines, machine->pid); +if (!aslr_machine) +return -ENOMEM ; +if (aslr_tool__preload_kernel_maps (aslr_machine) <0 ) +return -ENOMEM ; + + thread =machine__findnew_thread (aslr_machine, sample->pid, sample->tid); + +if (!thread) +return -ENOMEM ; + +if (max_i >PERF_SAMPLE_MAX_SIZE /sizeof (u64)) + goto out_put; + + new_event->sample.header = event->sample.header; + + in_array = &event->sample.array[0 ]; + out_array = &new_event->sample.array[0 ]; + +#define CHECK_BOUNDS (required_i, required_j) \ + (i + (required_i) > max_i || j + (required_j) > max_j) + +#define COPY_U64 () \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] = in_array[i++]; \ + } while (0) + +#define REMAP_U64 (addr_field) \ + do { \ + u64 remapped; \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + remapped = aslr_tool__remap_address(aslr, thread, cpumode, addr_field); \ + out_array[j++] = remapped; \ + i++; \ + } while (0) + +if (sample_type &PERF_SAMPLE_IDENTIFIER ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_IP ) +REMAP_U64 (sample->ip); +if (sample_type &PERF_SAMPLE_TID ) { +union { +u64 val64; +u32 val32[2 ]; + } u; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + u.val32[0 ] = sample->pid; + u.val32[1 ] = sample->tid; + out_array[j++] = u.val64; + i++; + } +if (sample_type &PERF_SAMPLE_TIME ) +COPY_U64 ();/* time */ +if (sample_type &PERF_SAMPLE_ADDR ) +REMAP_U64 (sample->addr); +if (sample_type &PERF_SAMPLE_ID ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_STREAM_ID ) +COPY_U64 ();/* stream_id */ +if (sample_type &PERF_SAMPLE_CPU ) +COPY_U64 ();/* cpu, res */ +if (sample_type &PERF_SAMPLE_PERIOD ) +COPY_U64 ();/* period */ +if (sample_type &PERF_SAMPLE_READ ) { +if ((evsel->core.attr.read_format &PERF_FORMAT_GROUP ) ==0 ) { +COPY_U64 ();/* value */ +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 ) +COPY_U64 ();/* lost */ + }else { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 */ + } + } + } +if (sample_type &PERF_SAMPLE_CALLCHAIN ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +for (u64 cntr =0 ; cntr < nr; cntr++) { +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + addr = in_array[i++]; +if (addr >=PERF_CONTEXT_MAX ) { + out_array[j++] = addr; +switch (addr) { +case PERF_CONTEXT_HV : + cpumode =PERF_RECORD_MISC_HYPERVISOR ; +break ; +case PERF_CONTEXT_KERNEL : + cpumode =PERF_RECORD_MISC_KERNEL ; +break ; +case PERF_CONTEXT_USER : + cpumode =PERF_RECORD_MISC_USER ; +break ; +case PERF_CONTEXT_GUEST : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_KERNEL : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_USER : + cpumode =PERF_RECORD_MISC_GUEST_USER ; +break ; +case PERF_CONTEXT_USER_DEFERRED : +if (cntr +1 >= nr) { +pr_debug ("Truncated callchain deferred cookie context\n" ); + ret =0 ; + goto out_put; + } +/* +* Immediately followed by a 64-bit +* stitching cookie. Skip/Copy it! +*/ +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + out_array[j++] = in_array[i++]; + cntr++; + cpumode =PERF_RECORD_MISC_USER ; +break ; +default : +pr_debug ("invalid callchain context: %" PRIx64"\n" , addr); + ret =0 ; + goto out_put; + } +continue ; + } + addr =aslr_tool__remap_address (aslr, thread, cpumode, addr); + out_array[j++] = addr; + } + } +if (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. +*/ +pr_debug ("Dropping raw samples as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_HW_INDEX ) +COPY_U64 ();/* hw_idx */ + +if (nr > (ULLONG_MAX /3 )) { + ret = -EFAULT ; + goto out_put; + } +if (nr *3 > max_i - i || nr *3 > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) { +u64 from = in_array[i++]; +u64 to = in_array[i++]; + + from =aslr_tool__remap_address (aslr, thread, sample->cpumode, from); + to =aslr_tool__remap_address (aslr, thread, sample->cpumode, to); + + out_array[j++] = from; + out_array[j++] = to; + out_array[j++] = in_array[i++];/* flags */ + } +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_COUNTERS ) { +if (nr > max_i - i || nr > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) +COPY_U64 (); + } + } +if (sample_type &PERF_SAMPLE_REGS_USER ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping regs user sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_STACK_USER ) { +u64 size; -return delegate->sample (delegate, event, sample, machine); +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 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_WEIGHT_TYPE ) +COPY_U64 ();/* perf_sample_weight */ +if (sample_type &PERF_SAMPLE_DATA_SRC ) +COPY_U64 ();/* data_src */ +if (sample_type &PERF_SAMPLE_TRANSACTION ) +COPY_U64 ();/* transaction */ +if (sample_type &PERF_SAMPLE_REGS_INTR ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping interrupt register sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_PHYS_ADDR ) { +COPY_U64 ();/* phys_addr */ +/* TODO: can this be less conservative? */ +pr_debug ("Dropping physical address sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_CGROUP ) +COPY_U64 ();/* cgroup */ +if (sample_type &PERF_SAMPLE_DATA_PAGE_SIZE ) +COPY_U64 ();/* data_page_size */ +if (sample_type &PERF_SAMPLE_CODE_PAGE_SIZE ) +COPY_U64 ();/* code_page_size */ + +if (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 ; + goto out_put; + } + +if (evsel__is_offcpu_event (evsel)) { +/* TODO: can this be less conservative? */ +pr_debug ("Dropping off-CPU sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } + + new_event->sample.header.size =sizeof (struct perf_event_header ) + j *sizeof (u64); + +perf_sample__init (&new_sample,/*all=*/ true); + ret =__evsel__parse_sample (evsel, new_event, &new_sample,/*needs_swap=*/ false); + +if (ret) { +perf_sample__exit (&new_sample); + goto out_put; + } + + new_sample.evsel = evsel; + ret = delegate->sample (delegate, new_event, &new_sample, machine); +perf_sample__exit (&new_sample); + +out_put: +thread__put (thread); +return ret; } +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 +static int skipn (int fd,off_t n) {char buf[4096 ];tools/perf/util/evsel.c +3
-3@@ -3337,11 +3337,11 @@ static int __set_offcpu_sample(struct perf_sample *data)return -EFAULT ; } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *data) +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap) {u64 type = evsel->core.attr.sample_type; -bool swapped = evsel->needs_swap; +bool swapped = needs_swap;const __u64 *array;u16 max_size = event->header.size;const void *endp = (void *)event + max_size;tools/perf/util/evsel.h +8
-3@@ -432,8 +432,14 @@ static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu_map_idxreturn __evsel__read_on_cpu (evsel, cpu_map_idx, thread, true); } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *sample); +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap); + +static inline int evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data) +{ +return __evsel__parse_sample (evsel, event, data, evsel->needs_swap); +}int evsel__parse_sample_timestamp (struct evsel *evsel,union perf_event *event,u64 *timestamp); --2.54.0.1099.g489fc7bff1-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v20 3/5] perf inject/aslr: Implement sample address remapping
In reply to Arnaldo Carvalho de MeloQuoted 16 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers
tools/perf/util/aslr.c +3
-0+ key.machine =maps__machine (thread__maps (aslr_thread)); + key.dso =map__dso (al.map); + key.invariant =map__start (al.map) -map__pgoff (al.map);tools/perf/util/aslr.c
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+ key.pid = (effective_cpumode ==PERF_RECORD_MISC_KERNEL || + effective_cpumode ==PERF_RECORD_MISC_GUEST_KERNEL ) ? + kernel_pid :thread__pid (aslr_thread); + +if (hashmap__find (&aslr->remap_addresses, &key, &remapped_invariant_ptr)) { + remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + + (addr -map__start (al.map)); + }else { +pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , + addr,map__start (al.map),map__size (al.map), key.pid);
Had to apply this:
tools/perf/util/aslr.c +1 -1
@@ -159,7 +159,7 @@ static u64 aslr_tool__remap_address(struct aslr_tool *aslr, remap_addr = *remapped_invariant_ptr +map__pgoff (al.map) + (addr -map__start (al.map)); }else { -pr_debug ("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n" , +pr_debug ("Cannot find a remapped entry for address %" PRIx64" in mapping %" PRIx64"(%zu) for pid=%d\n" , addr,map__start (al.map),map__size (al.map), key.pid); }
To address this on 32-bit:
3 17.06 almalinux:9-i386 : FAIL gcc version 11.5.0 20240719 (Red Hat 11.5.0-14) (GCC)
util/aslr.c: In function ‘aslr_tool__remap_address’:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘u64’ {aka ‘long long unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
util/aslr.c:193:70: note: format string is defined here
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ~~^
| |
| long unsigned int
| %llx
In file included from util/aslr.c:5:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘u64’ {aka ‘long long unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
util/aslr.c:193:85: note: format string is defined here
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ~~^
| |
| long unsigned int
| %llx
In file included from util/aslr.c:5:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 6 has type ‘size_t’ {aka ‘unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
util/aslr.c:193:89: note: format string is defined here
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ~~^
| |
| long unsigned int
| %x
cc1: all warnings being treated as errors
LD /tmp/build/perf/util/dwarf-regs-arch/perf-util-in.o
CC /tmp/build/perf/util/block-range.o
CC /tmp/build/perf/builtin-kwork.o
CC /tmp/build/perf/util/build-id.o
In file included from util/aslr.c:5:
util/aslr.c: In function ‘aslr_tool__remap_address’:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 4 has type ‘u64’ {aka ‘long long unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
--
| ~~^
| |
| long unsigned int
| %llx
In file included from util/aslr.c:5:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘u64’ {aka ‘long long unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
util/aslr.c:193:85: note: format string is defined here
--
| ~~^
| |
| long unsigned int
| %llx
In file included from util/aslr.c:5:
util/aslr.c:193:26: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 6 has type ‘size_t’ {aka ‘unsigned int’} [-Werror=format=]
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/debug.h:20:21: note: in definition of macro ‘pr_fmt’
20 | #define pr_fmt(fmt) fmt
| ^~~
util/aslr.c:193:17: note: in expansion of macro ‘pr_debug’
193 | pr_debug("Cannot find a remapped entry for address %lx in mapping %lx(%lx) for pid=%d\n",
| ^~~~~~~~
util/aslr.c:193:89: note: format string is defined here
- Arnaldo
Quoted 504 lines
Quoted 23 lines
tools/perf/util/aslr.c +13
-3+ } + +addr_location__exit (&al); +return remap_addr; +} +struct aslr_machine_priv {bool kernel_maps_loaded; };@@ -616,13 +671,415 @@ static int aslr_tool__process_sample(const struct perf_tool *tool,struct perf_sample *sample, struct machine *machine) { - struct delegate_tool *del_tool = container_of(tool, struct delegate_tool, tool); - struct aslr_tool *aslr = container_of(del_tool, struct aslr_tool, tool); -struct perf_tool *delegate = aslr->tool.delegate; + struct evsel *evsel = sample->evsel; +struct delegate_tool *del_tool; +struct aslr_tool *aslr; +struct perf_tool *delegate; +int ret; +u64 sample_type; +struct thread *thread;tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 690
Quoted 10 lines
tools/perf/util/aslr.c +10
-0+struct machine *aslr_machine; +__u64 max_i; +__u64 max_j; +union perf_event *new_event; +struct perf_sample new_sample; +__u64 *in_array, *out_array; +u8 cpumode; +u64 addr; +size_t i; +size_t j;The following 4 variables are not used in this patch, so the build at this point fails:
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 694
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+struct aslr_evsel_priv *priv =NULL ; +u64 orig_sample_type; +u64 orig_regs_user; +u64 orig_regs_intr;With:
CC
/tmp/build/perf-tools-next/util/bpf_lock_contention.outil/aslr.c: In function ‘aslr_tool__process_sample’:util/aslr.c:694:13: error: unused variable ‘orig_regs_intr’ [-Werror=unused-variable]694 | u64 orig_regs_intr; | ^~~~~~~~~~~~~~ util/aslr.c:693:13: error: unused variable ‘orig_regs_user’ [-Werror=unused-variable] 693 | u64 orig_regs_user; | ^~~~~~~~~~~~~~ util/aslr.c:692:13: error: unused variable ‘orig_sample_type’ [-Werror=unused-variable] 692 | u64 orig_sample_type; | ^~~~~~~~~~~~~~~~
util/aslr.c:691:33: error: unused variable ‘priv’ [-Werror=unused-variable]
691 | structaslr_evsel_priv*priv = NULL;
| ^~~~
cc1: all warnings being treated as errors
make[4]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:95:/tmp/build/perf-tools-next/util/aslr.o] Error 1So I moved it to the next patch, where they start to be used.
tools/perf/util/aslr.c static int aslr_tool__process_sample(const struct perf_tool *tool, · line 698
Quoted 4 lines
tools/perf/util/aslr.c +4
-0+ del_tool = container_of(tool, struct delegate_tool, tool); + aslr = container_of(del_tool, struct aslr_tool, tool); + delegate = aslr->tool.delegate; +I also removed these extra blank lines and collected James Tested-by.
Thanks, applied to perf-tools-next, for v7.2.
- Arnaldo
Quoted 429 lines
tools/perf/util/aslr.c +380
-1+ + +if (evsel__is_dummy_event (evsel)) +return delegate->sample (delegate, event, sample, machine); + + ret = -EFAULT ; + sample_type = evsel->core.attr.sample_type; + max_i = (event->header.size -sizeof (struct perf_event_header )) /sizeof (__u64); + max_j = (PERF_SAMPLE_MAX_SIZE -sizeof (struct perf_event_header )) /sizeof (__u64); + new_event = (union perf_event *)aslr->event_copy; + cpumode = sample->cpumode; + i =0 ; + j =0 ; + + aslr_machine =machines__findnew (&aslr->machines, machine->pid); +if (!aslr_machine) +return -ENOMEM ; +if (aslr_tool__preload_kernel_maps (aslr_machine) <0 ) +return -ENOMEM ; + + thread =machine__findnew_thread (aslr_machine, sample->pid, sample->tid); + +if (!thread) +return -ENOMEM ; + +if (max_i >PERF_SAMPLE_MAX_SIZE /sizeof (u64)) + goto out_put; + + new_event->sample.header = event->sample.header; + + in_array = &event->sample.array[0 ]; + out_array = &new_event->sample.array[0 ]; + +#define CHECK_BOUNDS (required_i, required_j) \ + (i + (required_i) > max_i || j + (required_j) > max_j) + +#define COPY_U64 () \ + do { \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + out_array[j++] = in_array[i++]; \ + } while (0) + +#define REMAP_U64 (addr_field) \ + do { \ + u64 remapped; \ + if (CHECK_BOUNDS(1, 1)) { \ + ret = -EFAULT; \ + goto out_put; \ + } \ + remapped = aslr_tool__remap_address(aslr, thread, cpumode, addr_field); \ + out_array[j++] = remapped; \ + i++; \ + } while (0) + +if (sample_type &PERF_SAMPLE_IDENTIFIER ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_IP ) +REMAP_U64 (sample->ip); +if (sample_type &PERF_SAMPLE_TID ) { +union { +u64 val64; +u32 val32[2 ]; + } u; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + u.val32[0 ] = sample->pid; + u.val32[1 ] = sample->tid; + out_array[j++] = u.val64; + i++; + } +if (sample_type &PERF_SAMPLE_TIME ) +COPY_U64 ();/* time */ +if (sample_type &PERF_SAMPLE_ADDR ) +REMAP_U64 (sample->addr); +if (sample_type &PERF_SAMPLE_ID ) +COPY_U64 ();/* id */ +if (sample_type &PERF_SAMPLE_STREAM_ID ) +COPY_U64 ();/* stream_id */ +if (sample_type &PERF_SAMPLE_CPU ) +COPY_U64 ();/* cpu, res */ +if (sample_type &PERF_SAMPLE_PERIOD ) +COPY_U64 ();/* period */ +if (sample_type &PERF_SAMPLE_READ ) { +if ((evsel->core.attr.read_format &PERF_FORMAT_GROUP ) ==0 ) { +COPY_U64 ();/* value */ +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 ) +COPY_U64 ();/* lost */ + }else { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); +if (evsel->core.attr.read_format &PERF_FORMAT_TOTAL_TIME_ENABLED ) +COPY_U64 ();/* time_enabled */ +if (evsel->core.attr.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 */ + } + } + } +if (sample_type &PERF_SAMPLE_CALLCHAIN ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +for (u64 cntr =0 ; cntr < nr; cntr++) { +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + addr = in_array[i++]; +if (addr >=PERF_CONTEXT_MAX ) { + out_array[j++] = addr; +switch (addr) { +case PERF_CONTEXT_HV : + cpumode =PERF_RECORD_MISC_HYPERVISOR ; +break ; +case PERF_CONTEXT_KERNEL : + cpumode =PERF_RECORD_MISC_KERNEL ; +break ; +case PERF_CONTEXT_USER : + cpumode =PERF_RECORD_MISC_USER ; +break ; +case PERF_CONTEXT_GUEST : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_KERNEL : + cpumode =PERF_RECORD_MISC_GUEST_KERNEL ; +break ; +case PERF_CONTEXT_GUEST_USER : + cpumode =PERF_RECORD_MISC_GUEST_USER ; +break ; +case PERF_CONTEXT_USER_DEFERRED : +if (cntr +1 >= nr) { +pr_debug ("Truncated callchain deferred cookie context\n" ); + ret =0 ; + goto out_put; + } +/* +* Immediately followed by a 64-bit +* stitching cookie. Skip/Copy it! +*/ +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + out_array[j++] = in_array[i++]; + cntr++; + cpumode =PERF_RECORD_MISC_USER ; +break ; +default : +pr_debug ("invalid callchain context: %" PRIx64"\n" , addr); + ret =0 ; + goto out_put; + } +continue ; + } + addr =aslr_tool__remap_address (aslr, thread, cpumode, addr); + out_array[j++] = addr; + } + } +if (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. +*/ +pr_debug ("Dropping raw samples as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { +u64 nr; + +if (CHECK_BOUNDS (1 ,1 )) { + ret = -EFAULT ; + goto out_put; + } + nr = in_array[i]; +COPY_U64 (); + +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_HW_INDEX ) +COPY_U64 ();/* hw_idx */ + +if (nr > (ULLONG_MAX /3 )) { + ret = -EFAULT ; + goto out_put; + } +if (nr *3 > max_i - i || nr *3 > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) { +u64 from = in_array[i++]; +u64 to = in_array[i++]; + + from =aslr_tool__remap_address (aslr, thread, sample->cpumode, from); + to =aslr_tool__remap_address (aslr, thread, sample->cpumode, to); + + out_array[j++] = from; + out_array[j++] = to; + out_array[j++] = in_array[i++];/* flags */ + } +if (evsel->core.attr.branch_sample_type &PERF_SAMPLE_BRANCH_COUNTERS ) { +if (nr > max_i - i || nr > max_j - j) { + ret = -EFAULT ; + goto out_put; + } +for (u64 cntr =0 ; cntr < nr; cntr++) +COPY_U64 (); + } + } +if (sample_type &PERF_SAMPLE_REGS_USER ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping regs user sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_STACK_USER ) { +u64 size; -return delegate->sample (delegate, event, sample, machine); +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 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_WEIGHT_TYPE ) +COPY_U64 ();/* perf_sample_weight */ +if (sample_type &PERF_SAMPLE_DATA_SRC ) +COPY_U64 ();/* data_src */ +if (sample_type &PERF_SAMPLE_TRANSACTION ) +COPY_U64 ();/* transaction */ +if (sample_type &PERF_SAMPLE_REGS_INTR ) { +if (CHECK_BOUNDS (1 ,0 )) { + ret = -EFAULT ; + goto out_put; + } +/* abi */ +COPY_U64 (); +/* TODO: can this be less conservative? */ +pr_debug ("Dropping interrupt register sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_PHYS_ADDR ) { +COPY_U64 ();/* phys_addr */ +/* TODO: can this be less conservative? */ +pr_debug ("Dropping physical address sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } +if (sample_type &PERF_SAMPLE_CGROUP ) +COPY_U64 ();/* cgroup */ +if (sample_type &PERF_SAMPLE_DATA_PAGE_SIZE ) +COPY_U64 ();/* data_page_size */ +if (sample_type &PERF_SAMPLE_CODE_PAGE_SIZE ) +COPY_U64 ();/* code_page_size */ + +if (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 ; + goto out_put; + } + +if (evsel__is_offcpu_event (evsel)) { +/* TODO: can this be less conservative? */ +pr_debug ("Dropping off-CPU sample as possible ASLR leak\n" ); + ret =0 ; + goto out_put; + } + + new_event->sample.header.size =sizeof (struct perf_event_header ) + j *sizeof (u64); + +perf_sample__init (&new_sample,/*all=*/ true); + ret =__evsel__parse_sample (evsel, new_event, &new_sample,/*needs_swap=*/ false); + +if (ret) { +perf_sample__exit (&new_sample); + goto out_put; + } + + new_sample.evsel = evsel; + ret = delegate->sample (delegate, new_event, &new_sample, machine); +perf_sample__exit (&new_sample); + +out_put: +thread__put (thread); +return ret; } +#undef CHECK_BOUNDS +#undef COPY_U64 +#undef REMAP_U64 +static int skipn (int fd,off_t n) {char buf[4096 ];tools/perf/util/evsel.c +3
-3@@ -3337,11 +3337,11 @@ static int __set_offcpu_sample(struct perf_sample *data)return -EFAULT ; } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *data) +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap) {u64 type = evsel->core.attr.sample_type; -bool swapped = evsel->needs_swap; +bool swapped = needs_swap;const __u64 *array;u16 max_size = event->header.size;const void *endp = (void *)event + max_size;tools/perf/util/evsel.h +8
-2@@ -432,8 +432,14 @@ static inline int evsel__read_on_cpu_scaled(struct evsel *evsel, int cpu_map_idxreturn __evsel__read_on_cpu (evsel, cpu_map_idx, thread, true); } -int evsel__parse_sample (struct evsel *evsel,union perf_event *event, -struct perf_sample *sample); +int __evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data,bool needs_swap); + +static inline int evsel__parse_sample (struct evsel *evsel,union perf_event *event, +struct perf_sample *data) +{ +return __evsel__parse_sample (evsel, event, data, evsel->needs_swap); +}int evsel__parse_sample_timestamp (struct evsel *evsel,union perf_event *event,u64 *timestamp);Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Ian Rogers
reviewerPATCH v20 4/5 perf aslr: Strip sample registers
In reply to Ian RogersExtend the ASLR tool stripping helpers to drop register dump payloads by masking out the relevant perf_event_attr fields (sample_regs_user, sample_regs_intr) when the delegated tool is handling the data.
struct aslr_evsel_priv maintains the original perf_event_attr values and is looked up via the evsel_orig_attrs hashmap so that sample sizes can be properly parsed even when bits are stripped from the pipeline. This is critical for bounded array copying within aslr_tool__process_sample, which relies on orig_sample_type to determine exactly which fields were captured by the kernel before any stripping occurred.
This allows us to keep samples that would otherwise be dropped because they contain registers, while still obfuscating the registers.
--- tools/perf/builtin-inject.c | 28 +++- tools/perf/util/aslr.c | 263 +++++++++++++++++++++++++++--------- tools/perf/util/aslr.h | 9 +- 3 files changed, 230 insertions(+), 70 deletions(-)
tools/perf/builtin-inject.c +25 -3
@@ -248,7 +248,7 @@ static int perf_event__repipe_attr(const struct perf_tool *tool,if (!aslr_event)return -ENOMEM ;memcpy (aslr_event, event, event->header.size); -aslr_tool__strip_attr_event (aslr_event, pevlist); +aslr_tool__strip_attr_event (aslr_event, *pevlist); event = aslr_event; }
@@ -297,6 +297,7 @@ static int perf_event__repipe_attr(const struct perf_tool *tool, attr.size =sizeof (struct perf_event_attr ); attr.sample_type &= ~PERF_SAMPLE_AUX ; +if (inject->itrace_synth_opts.add_last_branch) { attr.sample_type |=PERF_SAMPLE_BRANCH_STACK ; attr.branch_sample_type |=PERF_SAMPLE_BRANCH_HW_INDEX ;
@@ -2617,6 +2618,10 @@ static int __cmd_inject(struct perf_inject *inject) evsel->core.attr.exclude_callchain_user =0 ; } } + +if (inject->aslr) +aslr_tool__strip_evlist (inject->session->tool, session->evlist); + session->header.data_offset = output_data_offset; session->header.data_size = inject->bytes_written; perf_session__inject_header(session, session->evlist, fd, &inj_fc.fc,
@@ -2875,6 +2880,18 @@ int cmd_inject(int argc, const char **argv)if (zstd_init (&(inject.session->zstd_data),0 ) <0 )pr_warning ("Decompression initialization failed.\n" ); +if (inject.aslr) { +struct evsel *evsel; + +evlist__for_each_entry (inject.session->evlist, evsel) { + ret =aslr_tool__cache_orig_attrs (tool, evsel); +if (ret) { +pr_err ("Failed to cache original attributes: %d\n" , ret); + goto out_delete; + } + } + } +/* Save original section info before feature bits change */ ret =save_section_info (&inject);if (ret)
@@ -2893,10 +2910,17 @@ int cmd_inject(int argc, const char **argv)* the input. */ if (!data.is_pipe) { +if (inject.aslr) +aslr_tool__strip_evlist (tool, inject.session->evlist); + ret =perf_event__synthesize_for_pipe (&inject.tool, inject.session, &inject.output, perf_event__repipe); + +if (inject.aslr) +aslr_tool__restore_evlist (tool, inject.session->evlist); +if (ret <0 ) goto out_delete; }
@@ -2961,8 +2985,6 @@ int cmd_inject(int argc, const char **argv) goto out_delete; ret =__cmd_inject (&inject); -if (inject.aslr) -aslr_tool__strip_evlist (tool, inject.session->evlist);guest_session__exit (&inject.guest_session);
tools/perf/util/aslr.c +199 -64
438 lines not on this page · raw message
tools/perf/util/aslr.h +6 -3
@@ -34,8 +34,11 @@ struct evlist;union perf_event ;struct perf_tool *aslr_tool__new (struct perf_tool *delegate); -void aslr_tool__delete (struct perf_tool *aslr); -void aslr_tool__strip_attr_event (union perf_event *event,struct evlist **pevlist); -void aslr_tool__strip_evlist (struct perf_tool *tool,struct evlist *evlist); +void aslr_tool__delete (struct perf_tool *tool); + +void aslr_tool__strip_attr_event (union perf_event *event,struct evlist *evlist); +int aslr_tool__cache_orig_attrs (struct perf_tool *tool,struct evsel *evsel); +void aslr_tool__strip_evlist (const struct perf_tool *tool,struct evlist *evlist); +void aslr_tool__restore_evlist (const struct perf_tool *tool,struct evlist *evlist);#endif /* __PERF_ASLR_H */
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Ian Rogers
reviewerPATCH v20 5/5 perf test: Add inject ASLR test
In reply to Ian RogersAdd a new shell test to verify the feature. The test covers:
- Basic address remapping for user space samples.
- Pipe mode coverage for piped into.
- Callchain address remapping.
- Consistency of output before and after injection.
- Pipe mode report consistency.
- Dropping of samples that leak ASLR info (physical addresses).
- Kernel address remapping (utilizing a dedicated kernel-intensive VFS dd workload to guarantee continuous timer interrupts sampling flow inside kernel privilege states).
- Kernel report consistency with address normalization.
The test suite is hardened with global 'set -o pipefail' assertions to catch pipeline failures, stream-consuming awk processors to handle SIGPIPE signals, and a dedicated pipe output scenario validating raw 'perf inject -o -' stdout streams.
Note on kernel DSO normalization in the test script:
The test script deliberately normalizes all kernel DSOs to a generic [kernel] tag before diffing, as obfuscating physical kernel addresses forces perf report to occasionally shift samples between individual modules and [kernel.kallsyms] due to the lack of valid host module boundary maps.
Note on ARM:
Kernel-based ASLR test cases (test_kernel_aslr and test_kernel_report_aslr) are skipped on ARM architectures (aarch64 and arm*) to bypass high latency constraints (such as check_invariants() spending excessive execution time in maps__split_kallsyms() on debug builds) and symbolization inconsistencies.
--- tools/perf/tests/shell/inject_aslr.sh | 533 ++++++++++++++++++++++++++ 1 file changed, 533 insertions(+) create mode 100755 tools/perf/tests/shell/inject_aslr.sh
tools/perf/tests/shell/inject_aslr.sh new file +533 -0
@@ -0,0 +1,533 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# perf inject --aslr test + +set -e +set -o pipefail + +shelldir=$(dirname "$0") +# shellcheck source=lib/perf_has_symbol.sh +. "${shelldir}"/lib/perf_has_symbol.sh + +sym="noploop" + +skip_test_missing_symbol ${sym} + +# Create global temp directory +temp_dir=$(mktemp -d /tmp/perf-test-aslr.XXXXXXXXXX) + +prog="perf test -w noploop" +[ "$(uname -m)" = "s390x" ] && prog="$prog 3" +err=0 +kprog="dd if=/dev/urandom of=/dev/null bs=1M count=50" + +cleanup() { + local exit_code=${1:-$?} + trap - EXIT TERM INT + if [ "${exit_code}" -ne 0 ] || [ "${err}" -ne 0 ]; then + echo "Test failed! Preserving temp directory: ${temp_dir}" + return + fi + # Check if temp_dir is set and looks sane before removing + if [[ "${temp_dir}" =~ ^/tmp/perf-test-aslr\. ]]; then + rm -rf "${temp_dir}" + fi +} + +trap_cleanup() { + local exit_code=$? + echo "Unexpected signal in ${FUNCNAME[1]}" + cleanup ${exit_code} + exit ${exit_code} +} +trap trap_cleanup EXIT TERM INT + +get_noploop_addr() { + local file=$1 + perf script -i "$file" | awk ' + BEGIN { found=0 } + { + for (i=1; i<=NF; i++) { + if ($i ~ /noploop\+/) { + if (!found) { + print $(i-1) + found=1 + } + } + } + }' +} + +test_basic_aslr() { + echo "Test basic ASLR remapping" + local data + data=$(mktemp "${temp_dir}/perf.data.basic.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.basic.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + perf inject -v --aslr -i "${data}" -o "${data2}" + + orig_addr=$(get_noploop_addr "${data}") + new_addr=$(get_noploop_addr "${data2}") + + echo "Basic ASLR: orig_addr=$orig_addr, new_addr=$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Basic ASLR test [Failed - no noploop samples in original file]" + err=1 + elif [ -z "$new_addr" ]; then + echo "Basic ASLR test [Failed - could not find remapped address]" + err=1 + elif [ "$orig_addr" = "$new_addr" ]; then + echo "Basic ASLR test [Failed - addresses are not remapped]" + err=1 + else + echo "Basic ASLR test [Success]" + fi +} + +test_pipe_aslr() { + echo "Test pipe mode ASLR remapping" + local data + data=$(mktemp "${temp_dir}/perf.data.pipe.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.pipe.XXXXXX") + + # Use tee to save the original pipe data for comparison + perf record -e task-clock:u -o - ${prog} | tee "${data}" | perf inject --aslr -o "${data2}" + + orig_addr=$(get_noploop_addr "${data}") + new_addr=$(get_noploop_addr "${data2}") + + echo "Pipe ASLR: orig_addr=$orig_addr, new_addr=$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Pipe ASLR test [Failed - no noploop samples in original file]" + err=1 + elif [ -z "$new_addr" ]; then + echo "Pipe ASLR test [Failed - could not find remapped address]" + err=1 + elif [ "$orig_addr" = "$new_addr" ]; then + echo "Pipe ASLR test [Failed - addresses are not remapped]" + err=1 + else + echo "Pipe ASLR test [Success]" + fi +} + +test_callchain_aslr() { + echo "Test Callchain ASLR remapping" + local data + data=$(mktemp "${temp_dir}/perf.data.callchain.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.callchain.XXXXXX") + + perf record -g -e task-clock:u -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + orig_addr=$(get_noploop_addr "${data}") + new_addr=$(get_noploop_addr "${data2}") + + echo "Callchain ASLR: orig_addr=$orig_addr, new_addr=$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Callchain ASLR test [Failed - no noploop samples in original file]" + err=1 + elif [ -z "$new_addr" ]; then + echo "Callchain ASLR test [Failed - could not find remapped address]" + err=1 + elif [ "$orig_addr" = "$new_addr" ]; then + echo "Callchain ASLR test [Failed - addresses are not remapped]" + err=1 + else + # Extract callchain addresses (indented lines starting with hex addresses) + orig_callchain=$(perf script -i "${data}" | awk '/^[[:space:]]+[0-9a-f]+/ {print $1}') + new_callchain=$(perf script -i "${data2}" | awk '/^[[:space:]]+[0-9a-f]+/ {print $1}') + + if [ -z "$orig_callchain" ]; then + echo "Callchain ASLR test [Failed - no callchain samples in original file]" + err=1 + elif [ -z "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain data was dropped]" + err=1 + elif [ "$orig_callchain" = "$new_callchain" ]; then + echo "Callchain ASLR test [Failed - callchain addresses were not remapped]" + err=1 + else + echo "Callchain ASLR test [Success]" + fi + fi +} + +test_report_aslr() { + echo "Test perf report consistency" + local data + data=$(mktemp "${temp_dir}/perf.data.report.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.report.XXXXXX") + local data_clean + data_clean=$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${data}" -o "${data_clean}" + perf inject -v -b --aslr -i "${data}" -o "${data2}" + + local report1="${temp_dir}/report1_basic" + local report2="${temp_dir}/report2_basic" + local report1_clean="${temp_dir}/report1_basic.clean" + local report2_clean="${temp_dir}/report2_basic.clean" + local diff_file="${temp_dir}/diff_basic" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Report ASLR test [Failed - no samples captured]" + err=1 + elif [ -s "${diff_file}" ]; then + echo "Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=1 + else + echo "Report ASLR test [Success]" + fi +} + +test_pipe_report_aslr() { + echo "Test pipe mode perf report consistency" + local data + data=$(mktemp "${temp_dir}/perf.data.pipe_report.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.pipe_report.XXXXXX") + local data_clean + data_clean=$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + # Use tee to save the original pipe data, then process it with inject -b + perf record -e task-clock:u -o - ${prog} | \ + tee "${data}" | \ + perf inject -b --aslr -o "${data2}" + perf inject -b -i "${data}" -o "${data_clean}" + + local report1="${temp_dir}/report1_pipe" + local report2="${temp_dir}/report2_pipe" + local report1_clean="${temp_dir}/report1_pipe.clean" + local report2_clean="${temp_dir}/report2_pipe.clean" + local diff_file="${temp_dir}/diff_pipe" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${data2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Pipe Report ASLR test [Failed - no samples captured]" + err=1 + elif [ -s "${diff_file}" ]; then + echo "Pipe Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=1 + else + echo "Pipe Report ASLR test [Success]" + fi +} + +test_pipe_out_report_aslr() { + echo "Test pipe output mode perf report consistency" + local data + data=$(mktemp "${temp_dir}/perf.data.pipe_out_report.XXXXXX") + local data_clean + data_clean=$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + + perf record -e task-clock:u -o "${data}" ${prog} + perf inject -b -i "${data}" -o "${data_clean}" + + local report1="${temp_dir}/report1_pipe_out" + local report2="${temp_dir}/report2_pipe_out" + local report1_clean="${temp_dir}/report1_pipe_out.clean" + local report2_clean="${temp_dir}/report2_pipe_out.clean" + local diff_file="${temp_dir}/diff_pipe_out" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf inject -b --aslr -i "${data}" -o - | perf report -i - --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "Pipe Output Report ASLR test [Failed - no samples captured]" + err=1 + elif [ -s "${diff_file}" ]; then + echo "Pipe Output Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=1 + else + echo "Pipe Output Report ASLR test [Success]" + fi +} + +test_dropped_samples() { + echo "Test dropped samples (phys-data)" + local data + data=$(mktemp "${temp_dir}/perf.data.dropped.XXXXXX") + local data2 + data2=$(mktemp "${temp_dir}/perf.data2.dropped.XXXXXX") + + # Check if --phys-data is supported by recording a short run + if ! perf record -e task-clock:u --phys-data -o "${data}" -- sleep 0.1 > /dev/null 2>&1; then + echo "Skipping dropped samples test as --phys-data is not supported" + return + fi + + perf record -e task-clock:u --phys-data -o "${data}" ${prog} + perf inject --aslr -i "${data}" -o "${data2}" + + # Verify that the original file actually contained samples! + orig_samples=$(perf script -i "${data}" | wc -l) + if [ "$orig_samples" -eq 0 ]; then + echo "Dropped samples test [Failed - no samples in original file]" + err=1 + else + # Verify that samples are dropped. + samples_count=$(perf script -i "${data2}" | wc -l) + + if [ "$samples_count" -gt 0 ]; then + echo "Dropped samples test [Failed - samples were not dropped]" + err=1 + else + echo "Dropped samples test [Success]" + fi + fi +} + +test_kernel_aslr() { + echo "Test kernel ASLR remapping" + local kdata + kdata=$(mktemp "${temp_dir}/perf.data.kernel.XXXXXX") + local kdata2 + kdata2=$(mktemp "${temp_dir}/perf.data2.kernel.XXXXXX") + local log_file + log_file=$(mktemp "${temp_dir}/kernel_record.log.XXXXXX") + + # Try to record kernel samples + if ! perf record -e task-clock:k -o "${kdata}" ${kprog} > "${log_file}" 2>&1; then + echo "Skipping kernel ASLR test as recording failed (maybe no permissions)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_file}"; then + echo "Skipping kernel ASLR test as kernel map could not be recorded (permissions restricted)" + return + fi + + perf inject -v --aslr -i "${kdata}" -o "${kdata2}" + + # Check if kernel addresses are remapped. + # Find the field that ends with :k: (the event name) and take the next field! + orig_addr=$(perf script -i "${kdata}" | awk ' + BEGIN { found=0 } + { + for (i=1; i<NF; i++) { + if ($i ~ /:[k]+:?$/) { + if (!found) { + print $(i+1) + found=1 + } + } + } + }') + new_addr=$(perf script -i "${kdata2}" | awk ' + BEGIN { found=0 } + { + for (i=1; i<NF; i++) { + if ($i ~ /:[k]+:?$/) { + if (!found) { + print $(i+1) + found=1 + } + } + } + }') + + echo "Kernel ASLR: orig_addr=$orig_addr, new_addr=$new_addr" + + if [ -z "$orig_addr" ]; then + echo "Kernel ASLR test [Failed - no kernel samples in original file]" + err=1 + elif [ -z "$new_addr" ]; then + echo "Kernel ASLR test [Failed - could not find remapped address]" + err=1 + elif [ "$orig_addr" = "$new_addr" ]; then + echo "Kernel ASLR test [Failed - addresses are not remapped]" + err=1 + else + echo "Kernel ASLR test [Success]" + fi +} + +test_kernel_report_aslr() { + echo "Test kernel perf report consistency" + local kdata + kdata=$(mktemp "${temp_dir}/perf.data.kernel_report.XXXXXX") + local kdata2 + kdata2=$(mktemp "${temp_dir}/perf.data2.kernel_report.XXXXXX") + local data_clean + data_clean=$(mktemp "${temp_dir}/perf.data.clean.XXXXXX") + local log_file + log_file=$(mktemp "${temp_dir}/kernel_report_record.log.XXXXXX") + + # Try to record kernel samples + if ! perf record -e task-clock:k -o "${kdata}" ${kprog} > "${log_file}" 2>&1; then + echo "Skipping kernel report test as recording failed (maybe no permissions)" + return + fi + + # Check for warning about kernel map restriction + if grep -q "Couldn't record kernel reference relocation symbol" "${log_file}"; then + echo "Skipping kernel report test as kernel map could not be recorded (permissions restricted)" + return + fi + + # Use -b to inject build-ids and force ordered events processing in both + perf inject -b -i "${kdata}" -o "${data_clean}" + perf inject -v -b --aslr -i "${kdata}" -o "${kdata2}" + + local report1="${temp_dir}/report_kernel1" + local report2="${temp_dir}/report_kernel2" + local report1_clean="${temp_dir}/report_kernel1.clean" + local report2_clean="${temp_dir}/report_kernel2.clean" + + perf report -i "${data_clean}" --stdio > "${report1}" + perf report -i "${kdata2}" --stdio > "${report2}" + + # Strip headers and compare lines with percentages + grep '%' "${report1}" | grep -v '^#' > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' > "${report2_clean}" || true + + # Normalize kernel DSOs and addresses in clean reports + # This allows kernel modules to be either a module or kernel.kallsyms + local report1_norm="${temp_dir}/report_kernel1.norm" + local report2_norm="${temp_dir}/report_kernel2.norm" + local diff_file="${temp_dir}/diff_kernel" + + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' "${report1_clean}" | \ + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0); print}' | \ + sort > "${report1_norm}" || true + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' "${report2_clean}" | \ + awk '{gsub(/\[[a-zA-Z0-9_.-]{2,}\](\.[a-zA-Z0-9_]+)?/, "[kernel]", $0); print}' | \ + sort > "${report2_norm}" || true + + diff -u -w "${report1_norm}" "${report2_norm}" > "${diff_file}" || true + + if [ ! -s "${report1_norm}" ]; then + echo "Kernel Report ASLR test [Failed - no samples captured]" + err=1 + elif [ -s "${diff_file}" ]; then + echo "Kernel Report ASLR test [Failed - reports differ]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=1 + else + echo "Kernel Report ASLR test [Success]" + fi +} + +test_regs_stripping() { + echo "Test user register stripping" + local rdata="${temp_dir}/perf.data.regs" + local rdata2="${temp_dir}/perf.data.regs.injected" + local rdata_clean="${temp_dir}/perf.data.regs.clean" + + if ! perf record -e cycles:u --user-regs -o "${rdata}" ${prog} > /dev/null 2>&1; then + echo "Skipping user registers test as recording failed (unsupported flag/platform)" + return + fi + + perf inject -b -i "${rdata}" -o "${rdata_clean}" + perf inject -v -b --aslr -i "${rdata}" -o "${rdata2}" + + local report1="${temp_dir}/report_regs1" + local report2="${temp_dir}/report_regs2" + local report1_clean="${temp_dir}/report_regs1.clean" + local report2_clean="${temp_dir}/report_regs2.clean" + local diff_file="${temp_dir}/diff_regs" + + perf report -i "${rdata_clean}" --stdio > "${report1}" 2>/dev/null || true + perf report -i "${rdata2}" --stdio > "${report2}" 2>/dev/null || true + + grep '%' "${report1}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | \ + sort > "${report1_clean}" || true + grep '%' "${report2}" | grep -v '^#' | \ + grep -v -E '0x[0-9a-f]{8,}|0000000000000000' | \ + sort > "${report2_clean}" || true + + diff -u -w "${report1_clean}" "${report2_clean}" > "${diff_file}" || true + + if [ ! -s "${report1_clean}" ]; then + echo "User registers stripping test [Failed - profile trace starved/empty]" + err=1 + return + elif [ -s "${diff_file}" ]; then + echo "User registers stripping test [Failed - report parsing differs]" + echo "Showing first 20 lines of diff:" + head -n 20 "${diff_file}" + err=1 + return + fi + + local script_dump="${temp_dir}/script_regs_dump" + perf script -D -i "${rdata2}" > "${script_dump}" 2>/dev/null || true + if grep -q "user regs:" "${script_dump}"; then + echo "User registers stripping test [Failed - register dumps still present]" + err=1 + else + echo "User registers stripping test [Success]" + fi +} + +test_basic_aslr +test_pipe_aslr +test_callchain_aslr +test_report_aslr +test_pipe_report_aslr +test_pipe_out_report_aslr +test_dropped_samples +case "$(uname -m)" in + aarch64*|arm*) + echo "Skipping kernel ASLR tests on ARM" + ;; + *) + test_kernel_aslr + test_kernel_report_aslr + ;; +esac + +test_regs_stripping + +cleanup ${err} +exit $err
Signature · 1 line
2.54.0.1099.g489fc7bff1-goog
Ian Rogers
reviewerRe: [PATCH v20 0/5] perf tools: Add inject --aslr feature
In reply to Ian RogersQuoted 225 lines · Ian Rogers
This patch series introduces the new 'perf inject --aslr' feature to remap virtual memory addresses or drop physical memory event leaks when profile record data is shared between machines. Bundled with this feature is a bug fix inside the core map tracking tool that hardens perf session analysis against concurrent lookup data races.
Detailed Mechanism of MMAP Mapping and ASLR virtual Address Allocation:
The ASLR tool virtualizes the address space of the recorded processes by intercepting MMAP and MMAP2 events to build a consistent translation database, which is subsequently used to rewrite sample addresses.
It maintains two primary lookup databases using hash maps:
1. 'remap_addresses': Maps an original mapping key to its new remapped base address. The key uses topological invariant coordinates:
(machine, dso, invariant). The invariant is computed as (start - pgoff) for DSO-backed mappings. This invariant remains constant even when perf's internal overlap-resolution splits a VMA into fragmented pieces, ensuring split maps resolve consistently back to the same remapped base.
2. 'top_addresses': Tracks the allocation state per process (machine, pid). It maintains 'remapped_max' (the highest allocated address in the virtualized space).For each MMAP/MMAP2 event:
- We look up the DSO and invariant key in 'remap_addresses'. If found, we reuse the translation, preserving the offset within the mapping.
- If not found, we allocate a new remapped address space:
- We usethread__find_mapto look up the mapping immediately preceding the new one in the original address space (at start - 1). If the preceding
mapping was also remapped, we place the new mapping contiguously after it in the remapped space. This preserves contiguity of split mappings (e.g., symbols split by HugeTLB, or anonymous .bss segments adjacent to initialized data).
- If no contiguous mapping is found, we insert a 1-page gap from the highest allocated address (remapped_max) to prevent accidental merging of unrelated VMAs.
- The event's start address (and pgoff for kernel maps) is rewritten, and the event is delegated to the output writer.To remain strictly conservative and guarantee security, the tool scrubs breakpoint addresses (
bp_addr) from all synthesized stream headers, completely dropsPERF_RECORD_TEXT_POKEevents to prevent absolute immediate pointer operands leaks, and drops unsupported complex payloads (such as user register stacks, raw tracepoints, and hardware AUX tracing frames).Verification is reinforced with shell test ('
inject_aslr.sh').Prerequisite Bug Fix (Patch 1). During development, a core map indexing issue was identified and resolved to prevent concurrent lookup data races during session analysis.
Changes since v19:
- Patch 1: Group lock and unlock operations insidemaps__mutate_mapping()into a single conditional block to resolve Clang 15 -Wthread-safety-analysis compilation errors.
- Patch 5: Skip kernel-based ASLR test cases (test_kernel_aslrandtest_kernel_report_aslr) on ARM architectures (aarch64 and arm*) to bypass high latency constraints and symbolization inconsistencies.Changes since v18:
- Patch 2 & 3: Squashed the bounds checking boundary fixes into the "Strip sample registers" patch. The array bounds checking now correctly uses 'orig_sample_type' to traverse the event payload, preventing heap corruption when dealing with events that have had their registers stripped by the ASLR tool pipeline.
- Patch 2 & 3: Rebased the commit series to properly isolate the sample address remapping logic from the register stripping logic.
- Patch 2 & 3: Expanded commit messages to extensively document the cross-endian behavior of 'perf inject'. Because 'perf inject' effectively acts as an endianness converter (writing a host-endianPERF_MAGICand flushing events exactly as they sit in memory after being byte-swapped byperf_event__all64_swap), all injected events must be perfectly constructed in the host's native endianness. Specifically,perf_event__all64_swapbyte-swaps the raw 64-bit payloads, which causes 32-bit sequential fields likePERF_SAMPLE_TID(containing pid and tid) to have their ordering reversed in memory (e.g., [BE_pid][BE_tid] becomes [LE_tid][LE_pid]). The ASLR tool's sample construction logic was expanded to explicitly unpack these fields and repack them sequentially via unions to guarantee a strictly host-endian layout that resolves these inversion anomalies. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event, and 'needs_swap=false' is explicitly used when re-parsing the synthesized event to prevent erroneous double swapping.
- Series: Verified cross-endian robustness via the sashiko analyzer.Changes since v17:
- Patch 2: Reordered ksymbol deletion logic to ensureperf_event__process_ksymboldeletes the map *after* theaslr_tool__findnew_mappingtranslates the unregister offsets.
- Patch 2: Changedaslr_tool__deleteto cleanly handle guest machine deletion memory leaks.
- Patch 2: Resolved read-only segfaults on memory-mapped perf.data headers during attribute stripping by using deep copies inperf_event__repipe_attr.
- Patch 2: Fixed user space remap invariant logic to include(start - map__start(al.map))preventing negative overflows on module offset boundaries.
- Patch 3: Removed duplicatebswap_64payload byte-swapping inside the array logic, allowing the host endianness macrosCOPY_U64()to handle it dynamically.
- Patch 3: Fixed LBR branch sample starvation by explicitly reading branch counters instead of dropping the entire sample.
- Patch 5: Fixed test flakiness by grepping out physical hex addresses0x[0-9a-f]{8,}instead of matching exact address strings.
- Patch 5: Parameterized temp reports and updated test to scale with/dev/urandomcontinuous random reads.
- Patch Series: Added Signed-off-by tags uniformly and Assisted-by tags to track assistance.Changes since v16:
- Patch 2: Refactored inline ASLR stripping logic out ofbuiltin-inject.cand into dedicated helpers (aslr_tool__strip_attr_eventandaslr_tool__strip_evlist) inaslr.cto better separate concerns.
- Patch 2: Fixed guest machine allocation memory leak inaslr_tool__delete()wheremachines__exit()explicitly skipped freeing the guest processes tree.
- Patch 3: Fixed bounds-check violations during cross-endian parsing insideaslr_tool__process_sample()by correctly applyingbswap_64()to raw offsets, iteration counts, sizes, and addresses prior to logical evaluation whenorig_needs_swapis active.
- Patch 4: Fixed pipe mode parser misalignment bug by safely fetchingneeds_swapfrom the initialized evsel rather than blindly interceptingHEADER_ATTRevents prior to session parsing.
- Patch 4: Resolved checkpatch.pl line length warnings in thebswap_64endianness swapping logic.
- Patch Series: Reordered the final two patches. "perf aslr: Strip sample registers" is now Patch 4, and "perf test: Add inject ASLR test" is now Patch 5. This ensures the register stripping logic is fully introduced before the comprehensive shell tests validate it, preventing bisectability test failures and easing merge conflicts.
- Patch 5: Fixed "User registers stripping test" starvation when run as root by explicitly using '-e cycles:u' during recording, preventing the ring buffer from overflowing with kernel samples.Changes since v15:
- Patch 2: Added bounds checking forevent->header.sizebefore writing to breakpoint fields to avoid heap buffer overflow on older ABI events.
- Patch 2: Fixed asymmetric calculation bug inaslr_tool__findnew_mapping()where pgoff for anonymous kernel memory was not properly subtracted upon insertion, causing the lookup addition to overflow.
- Patch 2: Added detailed comments documenting the symmetric lookup and insertion math for unmapped and mapped memory blocks.
- Patch 5: Add missing kprobe and uprobe scrubbing of config1 and config2 duringaslr_tool__strip_evlist()to strictly conform with repipe constraints.Changes since v14:
- Patch 2: Removed unnecessary vertical whitespace inbuiltin-inject.c.
- Patch 2: Added comments explaining why pgoff is assigned for anonymous memory maps to prevent ASLR leaks.
- Patch 2: Removedorig_last_endtracking and refactored contiguous mapping detection to usethread__find_map(..., start - 1, ...)based on Gabriel's feedback.
- Patch 2: Scrub kprobe/uprobe event config1 and config2 fields to prevent address leaks.
- Patch 2: Overwrite pgoff with the remapped start address for anonymous mappings (detected viais_anon_memoryandis_no_dso_memory).
- Patch 3: Fix C90 mixed declaration error fororig_needs_swap.
- Patch 3: Temporarily disableevsel->needs_swapduring the secondaryevsel__parse_sample()call to prevent branch stack double-swapping bugs.Changes since v13:
- Patch 2: Added a NULL check for env before callingperf_env__kernel_is_64_bit(env)to prevent potential segfaults if the recorded environment has no headers.
- Patch 5: Fixedsample_sizeandid_posgoing out of sync duringaslr_tool__strip_evlist()andaslr_tool__restore_evlist(). Instead of usingevsel__reset_sample_bit(), which was acting as a no-op due to early bit clearing and corruptedsample_size, the tool now directly updatessample_typeand recomputes sample_size/id_pos dynamically. Addedorig_sample_sizetoaslr_evsel_privto correctly restore the state.Changes since v12:
- Patch 2: Fixed potential NULL pointer dereference inremap_addresses__hash()when handling unmapped memory events (key->dsois NULL) underREFCNT_CHECKING.
- Patch 2: Dynamically detect machine architecture bitness viaperf_env__kernel_is_64_bit()to select appropriatekernel_space_startboundaries, avoiding 64-bit address injection on 32-bit platforms.Changes since v11:
- Patch 1: Fixed struct dso name accessor inmaps.cby usingdso__name()instead of ->name.
- Patch 2: Fixed hash function inaslr.cto hash the underlying dso pointer usingRC_CHK_ACCESSto support reference count checking.Changes since v10:
- Patch 1: Added explicit tracking array logic inmaps__load_maps()to correctly accumulate valid maps (skipping NULL entries after failures) and safely return the exact populated count, resolving out-of-bounds pointer iteration panics.
- Patch 3: Fixed endianness bug during cross-endian sample parsing by passingevsel->needs_swapinstead of false to__evsel__parse_sampleinaslr.c, ensuring correct 32-bit field byte unswapping for packed fields. Refactoredevsel__parse_sampleto take aneeds_swapargument via__evsel__parse_sample.
- Patch 4: Fixedinject_aslr.shexit code handling in trap functions to capture and propagate the correct pipeline failure status code instead of unconditionally returning success or failing the test.Changes since v9:
- Patch 1: Added-ENOMEMerror check insidemaps__find_symbol_by_name()and returnNULLearly. Added map sorting state invalidation on early return inmaps__load_maps().
- Patch 2: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__findnew_mapping(). Addedpr_warning_oncewarning when raw auxtrace data is dropped.
- Patch 3: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__remap_address(). Wrappedevsel__parse_sample()to temporarily disableneeds_swapto avoid branch stack endianness corruption on cross-endian files. Fixed ISO C90 warning for declaration-after-statement fororig_needs_swap.
- Patch 4: Fixed duplicate cleanup by explicitly removing trap handlers (trap - EXIT TERM INT) inside thecleanup()function.
- Patch 5: Fixed heap corruption by adding size bounds checking before writing tosample_regs_userandsample_regs_intrfields. Added missing register mask clearing logic for theitracesynthesis path ofperf_event__repipe_attr().
Quoted 6 lines
Ian Rogers (5):
perf maps: Addmaps__mutate_mapping
perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking perf inject/aslr: Implement sample address remapping perf aslr: Strip sample registers
perf test: Add inject ASLR test
The sashiko reviews are at:
https://sashiko.dev/#/patchset/20260611164122.3974068-1-irogers%40google.com
To summarize:
Patch 2:
* TOCTOU if underlying event buffer mmaps change. Not an issue as
rewriting a perf.data file while it is being read is out of scope.
Patch 3:
* Mapping addresses to 0 for unknown mappings is criticized but the
proposed alternative doesn't hide ASLR. This will cluster things on address 0 but the fix is simply to ensure no MMAPs are missing.
* Cross-endian issues, but as explained previously, these are out of scope.
The clang build issue reported by James and disabling the kernel testing for ARM are both in the v20 series. So I think the patches are ready for review/merging.
Thanks,
Ian
Quoted 20 lines
tools/perf/builtin-inject.c | 81 +- tools/perf/tests/shell/inject_aslr.sh | 533 ++++++++++ tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 1406 +++++++++++++++++++++++++ tools/perf/util/aslr.h | 44 + tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- tools/perf/util/machine.c | 32 +- tools/perf/util/maps.c | 148 ++- tools/perf/util/maps.h | 3 + tools/perf/util/symbol-elf.c | 41 +- tools/perf/util/symbol.c | 17 +- 12 files changed, 2251 insertions(+), 71 deletions(-) create mode 100755 tools/perf/tests/shell/inject_aslr.sh create mode 100644 tools/perf/util/aslr.c create mode 100644 tools/perf/util/aslr.h--
2.54.0.1099.g489fc7bff1-goog
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v20 0/5] perf tools: Add inject --aslr feature
Quoted 236 lines · Ian Rogers
Quoted 225 lines · Ian Rogers
This patch series introduces the new 'perf inject --aslr' feature to remap virtual memory addresses or drop physical memory event leaks when profile record data is shared between machines. Bundled with this feature is a bug fix inside the core map tracking tool that hardens perf session analysis against concurrent lookup data races.
Detailed Mechanism of MMAP Mapping and ASLR virtual Address Allocation:
The ASLR tool virtualizes the address space of the recorded processes by intercepting MMAP and MMAP2 events to build a consistent translation database, which is subsequently used to rewrite sample addresses.
It maintains two primary lookup databases using hash maps:
1. 'remap_addresses': Maps an original mapping key to its new remapped base address. The key uses topological invariant coordinates:
(machine, dso, invariant). The invariant is computed as (start - pgoff) for DSO-backed mappings. This invariant remains constant even when perf's internal overlap-resolution splits a VMA into fragmented pieces, ensuring split maps resolve consistently back to the same remapped base.
2. 'top_addresses': Tracks the allocation state per process (machine, pid). It maintains 'remapped_max' (the highest allocated address in the virtualized space).For each MMAP/MMAP2 event:
- We look up the DSO and invariant key in 'remap_addresses'. If found, we reuse the translation, preserving the offset within the mapping.
- If not found, we allocate a new remapped address space:
- We usethread__find_mapto look up the mapping immediately preceding the new one in the original address space (at start - 1). If the preceding
mapping was also remapped, we place the new mapping contiguously after it in the remapped space. This preserves contiguity of split mappings (e.g., symbols split by HugeTLB, or anonymous .bss segments adjacent to initialized data).
- If no contiguous mapping is found, we insert a 1-page gap from the highest allocated address (remapped_max) to prevent accidental merging of unrelated VMAs.
- The event's start address (and pgoff for kernel maps) is rewritten, and the event is delegated to the output writer.To remain strictly conservative and guarantee security, the tool scrubs breakpoint addresses (
bp_addr) from all synthesized stream headers, completely dropsPERF_RECORD_TEXT_POKEevents to prevent absolute immediate pointer operands leaks, and drops unsupported complex payloads (such as user register stacks, raw tracepoints, and hardware AUX tracing frames).Verification is reinforced with shell test ('
inject_aslr.sh').Prerequisite Bug Fix (Patch 1). During development, a core map indexing issue was identified and resolved to prevent concurrent lookup data races during session analysis.
Changes since v19:
- Patch 1: Group lock and unlock operations insidemaps__mutate_mapping()into a single conditional block to resolve Clang 15 -Wthread-safety-analysis compilation errors.
- Patch 5: Skip kernel-based ASLR test cases (test_kernel_aslrandtest_kernel_report_aslr) on ARM architectures (aarch64 and arm*) to bypass high latency constraints and symbolization inconsistencies.Changes since v18:
- Patch 2 & 3: Squashed the bounds checking boundary fixes into the "Strip sample registers" patch. The array bounds checking now correctly uses 'orig_sample_type' to traverse the event payload, preventing heap corruption when dealing with events that have had their registers stripped by the ASLR tool pipeline.
- Patch 2 & 3: Rebased the commit series to properly isolate the sample address remapping logic from the register stripping logic.
- Patch 2 & 3: Expanded commit messages to extensively document the cross-endian behavior of 'perf inject'. Because 'perf inject' effectively acts as an endianness converter (writing a host-endianPERF_MAGICand flushing events exactly as they sit in memory after being byte-swapped byperf_event__all64_swap), all injected events must be perfectly constructed in the host's native endianness. Specifically,perf_event__all64_swapbyte-swaps the raw 64-bit payloads, which causes 32-bit sequential fields likePERF_SAMPLE_TID(containing pid and tid) to have their ordering reversed in memory (e.g., [BE_pid][BE_tid] becomes [LE_tid][LE_pid]). The ASLR tool's sample construction logic was expanded to explicitly unpack these fields and repack them sequentially via unions to guarantee a strictly host-endian layout that resolves these inversion anomalies. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event, and 'needs_swap=false' is explicitly used when re-parsing the synthesized event to prevent erroneous double swapping.
- Series: Verified cross-endian robustness via the sashiko analyzer.Changes since v17:
- Patch 2: Reordered ksymbol deletion logic to ensureperf_event__process_ksymboldeletes the map *after* theaslr_tool__findnew_mappingtranslates the unregister offsets.
- Patch 2: Changedaslr_tool__deleteto cleanly handle guest machine deletion memory leaks.
- Patch 2: Resolved read-only segfaults on memory-mapped perf.data headers during attribute stripping by using deep copies inperf_event__repipe_attr.
- Patch 2: Fixed user space remap invariant logic to include(start - map__start(al.map))preventing negative overflows on module offset boundaries.
- Patch 3: Removed duplicatebswap_64payload byte-swapping inside the array logic, allowing the host endianness macrosCOPY_U64()to handle it dynamically.
- Patch 3: Fixed LBR branch sample starvation by explicitly reading branch counters instead of dropping the entire sample.
- Patch 5: Fixed test flakiness by grepping out physical hex addresses0x[0-9a-f]{8,}instead of matching exact address strings.
- Patch 5: Parameterized temp reports and updated test to scale with/dev/urandomcontinuous random reads.
- Patch Series: Added Signed-off-by tags uniformly and Assisted-by tags to track assistance.Changes since v16:
- Patch 2: Refactored inline ASLR stripping logic out ofbuiltin-inject.cand into dedicated helpers (aslr_tool__strip_attr_eventandaslr_tool__strip_evlist) inaslr.cto better separate concerns.
- Patch 2: Fixed guest machine allocation memory leak inaslr_tool__delete()wheremachines__exit()explicitly skipped freeing the guest processes tree.
- Patch 3: Fixed bounds-check violations during cross-endian parsing insideaslr_tool__process_sample()by correctly applyingbswap_64()to raw offsets, iteration counts, sizes, and addresses prior to logical evaluation whenorig_needs_swapis active.
- Patch 4: Fixed pipe mode parser misalignment bug by safely fetchingneeds_swapfrom the initialized evsel rather than blindly interceptingHEADER_ATTRevents prior to session parsing.
- Patch 4: Resolved checkpatch.pl line length warnings in thebswap_64endianness swapping logic.
- Patch Series: Reordered the final two patches. "perf aslr: Strip sample registers" is now Patch 4, and "perf test: Add inject ASLR test" is now Patch 5. This ensures the register stripping logic is fully introduced before the comprehensive shell tests validate it, preventing bisectability test failures and easing merge conflicts.
- Patch 5: Fixed "User registers stripping test" starvation when run as root by explicitly using '-e cycles:u' during recording, preventing the ring buffer from overflowing with kernel samples.Changes since v15:
- Patch 2: Added bounds checking forevent->header.sizebefore writing to breakpoint fields to avoid heap buffer overflow on older ABI events.
- Patch 2: Fixed asymmetric calculation bug inaslr_tool__findnew_mapping()where pgoff for anonymous kernel memory was not properly subtracted upon insertion, causing the lookup addition to overflow.
- Patch 2: Added detailed comments documenting the symmetric lookup and insertion math for unmapped and mapped memory blocks.
- Patch 5: Add missing kprobe and uprobe scrubbing of config1 and config2 duringaslr_tool__strip_evlist()to strictly conform with repipe constraints.Changes since v14:
- Patch 2: Removed unnecessary vertical whitespace inbuiltin-inject.c.
- Patch 2: Added comments explaining why pgoff is assigned for anonymous memory maps to prevent ASLR leaks.
- Patch 2: Removedorig_last_endtracking and refactored contiguous mapping detection to usethread__find_map(..., start - 1, ...)based on Gabriel's feedback.
- Patch 2: Scrub kprobe/uprobe event config1 and config2 fields to prevent address leaks.
- Patch 2: Overwrite pgoff with the remapped start address for anonymous mappings (detected viais_anon_memoryandis_no_dso_memory).
- Patch 3: Fix C90 mixed declaration error fororig_needs_swap.
- Patch 3: Temporarily disableevsel->needs_swapduring the secondaryevsel__parse_sample()call to prevent branch stack double-swapping bugs.Changes since v13:
- Patch 2: Added a NULL check for env before callingperf_env__kernel_is_64_bit(env)to prevent potential segfaults if the recorded environment has no headers.
- Patch 5: Fixedsample_sizeandid_posgoing out of sync duringaslr_tool__strip_evlist()andaslr_tool__restore_evlist(). Instead of usingevsel__reset_sample_bit(), which was acting as a no-op due to early bit clearing and corruptedsample_size, the tool now directly updatessample_typeand recomputes sample_size/id_pos dynamically. Addedorig_sample_sizetoaslr_evsel_privto correctly restore the state.Changes since v12:
- Patch 2: Fixed potential NULL pointer dereference inremap_addresses__hash()when handling unmapped memory events (key->dsois NULL) underREFCNT_CHECKING.
- Patch 2: Dynamically detect machine architecture bitness viaperf_env__kernel_is_64_bit()to select appropriatekernel_space_startboundaries, avoiding 64-bit address injection on 32-bit platforms.Changes since v11:
- Patch 1: Fixed struct dso name accessor inmaps.cby usingdso__name()instead of ->name.
- Patch 2: Fixed hash function inaslr.cto hash the underlying dso pointer usingRC_CHK_ACCESSto support reference count checking.Changes since v10:
- Patch 1: Added explicit tracking array logic inmaps__load_maps()to correctly accumulate valid maps (skipping NULL entries after failures) and safely return the exact populated count, resolving out-of-bounds pointer iteration panics.
- Patch 3: Fixed endianness bug during cross-endian sample parsing by passingevsel->needs_swapinstead of false to__evsel__parse_sampleinaslr.c, ensuring correct 32-bit field byte unswapping for packed fields. Refactoredevsel__parse_sampleto take aneeds_swapargument via__evsel__parse_sample.
- Patch 4: Fixedinject_aslr.shexit code handling in trap functions to capture and propagate the correct pipeline failure status code instead of unconditionally returning success or failing the test.Changes since v9:
- Patch 1: Added-ENOMEMerror check insidemaps__find_symbol_by_name()and returnNULLearly. Added map sorting state invalidation on early return inmaps__load_maps().
- Patch 2: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__findnew_mapping(). Addedpr_warning_oncewarning when raw auxtrace data is dropped.
- Patch 3: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__remap_address(). Wrappedevsel__parse_sample()to temporarily disableneeds_swapto avoid branch stack endianness corruption on cross-endian files. Fixed ISO C90 warning for declaration-after-statement fororig_needs_swap.
- Patch 4: Fixed duplicate cleanup by explicitly removing trap handlers (trap - EXIT TERM INT) inside thecleanup()function.
- Patch 5: Fixed heap corruption by adding size bounds checking before writing tosample_regs_userandsample_regs_intrfields. Added missing register mask clearing logic for theitracesynthesis path ofperf_event__repipe_attr().Quoted 6 lines
Ian Rogers (5):
perf maps: Addmaps__mutate_mapping
perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking perf inject/aslr: Implement sample address remapping perf aslr: Strip sample registers
perf test: Add inject ASLR testThe sashiko reviews are at:
https://sashiko.dev/#/patchset/20260611164122.3974068-1-irogers%40google.com
Quoted 8 lines
To summarize:
Patch 2:
* TOCTOU if underlying event buffer mmaps change. Not an issue as
rewriting a perf.data file while it is being read is out of scope.Patch 3:
* Mapping addresses to 0 for unknown mappings is criticized but the
Why not then have some unknown mappings hashmap that will assign a random, unique address on a address range that doesn't overlap with any of the other maps?
Zero has special meaning, mapping some non-zero address to it introduces confusion when what we want is to just make sure that we don't leak addresses?
Quoted 7 lines
proposed alternative doesn't hide ASLR. This will cluster things on address 0 but the fix is simply to ensure no MMAPs are missing.
* Cross-endian issues, but as explained previously, these are out of scope.The clang build issue reported by James and disabling the kernel testing for ARM are both in the v20 series. So I think the patches are ready for review/merging.
I reviewed one other patch in the series besides the above suggestion.
Thanks, its an useful feature!
- Arnaldo
Quoted 23 lines
Thanks,
IanQuoted 20 lines
tools/perf/builtin-inject.c | 81 +- tools/perf/tests/shell/inject_aslr.sh | 533 ++++++++++ tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 1406 +++++++++++++++++++++++++ tools/perf/util/aslr.h | 44 + tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- tools/perf/util/machine.c | 32 +- tools/perf/util/maps.c | 148 ++- tools/perf/util/maps.h | 3 + tools/perf/util/symbol-elf.c | 41 +- tools/perf/util/symbol.c | 17 +- 12 files changed, 2251 insertions(+), 71 deletions(-) create mode 100755 tools/perf/tests/shell/inject_aslr.sh create mode 100644 tools/perf/util/aslr.c create mode 100644 tools/perf/util/aslr.h--
2.54.0.1099.g489fc7bff1-goog
Ian Rogers
reviewerRe: [PATCH v20 0/5] perf tools: Add inject --aslr feature
Quoted 247 lines · Arnaldo Carvalho de Melo
Quoted 236 lines · Ian Rogers
Quoted 225 lines · Ian Rogers
This patch series introduces the new 'perf inject --aslr' feature to remap virtual memory addresses or drop physical memory event leaks when profile record data is shared between machines. Bundled with this feature is a bug fix inside the core map tracking tool that hardens perf session analysis against concurrent lookup data races.
Detailed Mechanism of MMAP Mapping and ASLR virtual Address Allocation:
The ASLR tool virtualizes the address space of the recorded processes by intercepting MMAP and MMAP2 events to build a consistent translation database, which is subsequently used to rewrite sample addresses.
It maintains two primary lookup databases using hash maps:
1. 'remap_addresses': Maps an original mapping key to its new remapped base address. The key uses topological invariant coordinates:
(machine, dso, invariant). The invariant is computed as (start - pgoff) for DSO-backed mappings. This invariant remains constant even when perf's internal overlap-resolution splits a VMA into fragmented pieces, ensuring split maps resolve consistently back to the same remapped base.
2. 'top_addresses': Tracks the allocation state per process (machine, pid). It maintains 'remapped_max' (the highest allocated address in the virtualized space).For each MMAP/MMAP2 event:
- We look up the DSO and invariant key in 'remap_addresses'. If found, we reuse the translation, preserving the offset within the mapping.
- If not found, we allocate a new remapped address space:
- We usethread__find_mapto look up the mapping immediately preceding the new one in the original address space (at start - 1). If the preceding
mapping was also remapped, we place the new mapping contiguously after it in the remapped space. This preserves contiguity of split mappings (e.g., symbols split by HugeTLB, or anonymous .bss segments adjacent to initialized data).
- If no contiguous mapping is found, we insert a 1-page gap from the highest allocated address (remapped_max) to prevent accidental merging of unrelated VMAs.
- The event's start address (and pgoff for kernel maps) is rewritten, and the event is delegated to the output writer.To remain strictly conservative and guarantee security, the tool scrubs breakpoint addresses (
bp_addr) from all synthesized stream headers, completely dropsPERF_RECORD_TEXT_POKEevents to prevent absolute immediate pointer operands leaks, and drops unsupported complex payloads (such as user register stacks, raw tracepoints, and hardware AUX tracing frames).Verification is reinforced with shell test ('
inject_aslr.sh').Prerequisite Bug Fix (Patch 1). During development, a core map indexing issue was identified and resolved to prevent concurrent lookup data races during session analysis.
Changes since v19:
- Patch 1: Group lock and unlock operations insidemaps__mutate_mapping()into a single conditional block to resolve Clang 15 -Wthread-safety-analysis compilation errors.
- Patch 5: Skip kernel-based ASLR test cases (test_kernel_aslrandtest_kernel_report_aslr) on ARM architectures (aarch64 and arm*) to bypass high latency constraints and symbolization inconsistencies.Changes since v18:
- Patch 2 & 3: Squashed the bounds checking boundary fixes into the "Strip sample registers" patch. The array bounds checking now correctly uses 'orig_sample_type' to traverse the event payload, preventing heap corruption when dealing with events that have had their registers stripped by the ASLR tool pipeline.
- Patch 2 & 3: Rebased the commit series to properly isolate the sample address remapping logic from the register stripping logic.
- Patch 2 & 3: Expanded commit messages to extensively document the cross-endian behavior of 'perf inject'. Because 'perf inject' effectively acts as an endianness converter (writing a host-endianPERF_MAGICand flushing events exactly as they sit in memory after being byte-swapped byperf_event__all64_swap), all injected events must be perfectly constructed in the host's native endianness. Specifically,perf_event__all64_swapbyte-swaps the raw 64-bit payloads, which causes 32-bit sequential fields likePERF_SAMPLE_TID(containing pid and tid) to have their ordering reversed in memory (e.g., [BE_pid][BE_tid] becomes [LE_tid][LE_pid]). The ASLR tool's sample construction logic was expanded to explicitly unpack these fields and repack them sequentially via unions to guarantee a strictly host-endian layout that resolves these inversion anomalies. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event, and 'needs_swap=false' is explicitly used when re-parsing the synthesized event to prevent erroneous double swapping.
- Series: Verified cross-endian robustness via the sashiko analyzer.Changes since v17:
- Patch 2: Reordered ksymbol deletion logic to ensureperf_event__process_ksymboldeletes the map *after* theaslr_tool__findnew_mappingtranslates the unregister offsets.
- Patch 2: Changedaslr_tool__deleteto cleanly handle guest machine deletion memory leaks.
- Patch 2: Resolved read-only segfaults on memory-mapped perf.data headers during attribute stripping by using deep copies inperf_event__repipe_attr.
- Patch 2: Fixed user space remap invariant logic to include(start - map__start(al.map))preventing negative overflows on module offset boundaries.
- Patch 3: Removed duplicatebswap_64payload byte-swapping inside the array logic, allowing the host endianness macrosCOPY_U64()to handle it dynamically.
- Patch 3: Fixed LBR branch sample starvation by explicitly reading branch counters instead of dropping the entire sample.
- Patch 5: Fixed test flakiness by grepping out physical hex addresses0x[0-9a-f]{8,}instead of matching exact address strings.
- Patch 5: Parameterized temp reports and updated test to scale with/dev/urandomcontinuous random reads.
- Patch Series: Added Signed-off-by tags uniformly and Assisted-by tags to track assistance.Changes since v16:
- Patch 2: Refactored inline ASLR stripping logic out ofbuiltin-inject.cand into dedicated helpers (aslr_tool__strip_attr_eventandaslr_tool__strip_evlist) inaslr.cto better separate concerns.
- Patch 2: Fixed guest machine allocation memory leak inaslr_tool__delete()wheremachines__exit()explicitly skipped freeing the guest processes tree.
- Patch 3: Fixed bounds-check violations during cross-endian parsing insideaslr_tool__process_sample()by correctly applyingbswap_64()to raw offsets, iteration counts, sizes, and addresses prior to logical evaluation whenorig_needs_swapis active.
- Patch 4: Fixed pipe mode parser misalignment bug by safely fetchingneeds_swapfrom the initialized evsel rather than blindly interceptingHEADER_ATTRevents prior to session parsing.
- Patch 4: Resolved checkpatch.pl line length warnings in thebswap_64endianness swapping logic.
- Patch Series: Reordered the final two patches. "perf aslr: Strip sample registers" is now Patch 4, and "perf test: Add inject ASLR test" is now Patch 5. This ensures the register stripping logic is fully introduced before the comprehensive shell tests validate it, preventing bisectability test failures and easing merge conflicts.
- Patch 5: Fixed "User registers stripping test" starvation when run as root by explicitly using '-e cycles:u' during recording, preventing the ring buffer from overflowing with kernel samples.Changes since v15:
- Patch 2: Added bounds checking forevent->header.sizebefore writing to breakpoint fields to avoid heap buffer overflow on older ABI events.
- Patch 2: Fixed asymmetric calculation bug inaslr_tool__findnew_mapping()where pgoff for anonymous kernel memory was not properly subtracted upon insertion, causing the lookup addition to overflow.
- Patch 2: Added detailed comments documenting the symmetric lookup and insertion math for unmapped and mapped memory blocks.
- Patch 5: Add missing kprobe and uprobe scrubbing of config1 and config2 duringaslr_tool__strip_evlist()to strictly conform with repipe constraints.Changes since v14:
- Patch 2: Removed unnecessary vertical whitespace inbuiltin-inject.c.
- Patch 2: Added comments explaining why pgoff is assigned for anonymous memory maps to prevent ASLR leaks.
- Patch 2: Removedorig_last_endtracking and refactored contiguous mapping detection to usethread__find_map(..., start - 1, ...)based on Gabriel's feedback.
- Patch 2: Scrub kprobe/uprobe event config1 and config2 fields to prevent address leaks.
- Patch 2: Overwrite pgoff with the remapped start address for anonymous mappings (detected viais_anon_memoryandis_no_dso_memory).
- Patch 3: Fix C90 mixed declaration error fororig_needs_swap.
- Patch 3: Temporarily disableevsel->needs_swapduring the secondaryevsel__parse_sample()call to prevent branch stack double-swapping bugs.Changes since v13:
- Patch 2: Added a NULL check for env before callingperf_env__kernel_is_64_bit(env)to prevent potential segfaults if the recorded environment has no headers.
- Patch 5: Fixedsample_sizeandid_posgoing out of sync duringaslr_tool__strip_evlist()andaslr_tool__restore_evlist(). Instead of usingevsel__reset_sample_bit(), which was acting as a no-op due to early bit clearing and corruptedsample_size, the tool now directly updatessample_typeand recomputes sample_size/id_pos dynamically. Addedorig_sample_sizetoaslr_evsel_privto correctly restore the state.Changes since v12:
- Patch 2: Fixed potential NULL pointer dereference inremap_addresses__hash()when handling unmapped memory events (key->dsois NULL) underREFCNT_CHECKING.
- Patch 2: Dynamically detect machine architecture bitness viaperf_env__kernel_is_64_bit()to select appropriatekernel_space_startboundaries, avoiding 64-bit address injection on 32-bit platforms.Changes since v11:
- Patch 1: Fixed struct dso name accessor inmaps.cby usingdso__name()instead of ->name.
- Patch 2: Fixed hash function inaslr.cto hash the underlying dso pointer usingRC_CHK_ACCESSto support reference count checking.Changes since v10:
- Patch 1: Added explicit tracking array logic inmaps__load_maps()to correctly accumulate valid maps (skipping NULL entries after failures) and safely return the exact populated count, resolving out-of-bounds pointer iteration panics.
- Patch 3: Fixed endianness bug during cross-endian sample parsing by passingevsel->needs_swapinstead of false to__evsel__parse_sampleinaslr.c, ensuring correct 32-bit field byte unswapping for packed fields. Refactoredevsel__parse_sampleto take aneeds_swapargument via__evsel__parse_sample.
- Patch 4: Fixedinject_aslr.shexit code handling in trap functions to capture and propagate the correct pipeline failure status code instead of unconditionally returning success or failing the test.Changes since v9:
- Patch 1: Added-ENOMEMerror check insidemaps__find_symbol_by_name()and returnNULLearly. Added map sorting state invalidation on early return inmaps__load_maps().
- Patch 2: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__findnew_mapping(). Addedpr_warning_oncewarning when raw auxtrace data is dropped.
- Patch 3: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__remap_address(). Wrappedevsel__parse_sample()to temporarily disableneeds_swapto avoid branch stack endianness corruption on cross-endian files. Fixed ISO C90 warning for declaration-after-statement fororig_needs_swap.
- Patch 4: Fixed duplicate cleanup by explicitly removing trap handlers (trap - EXIT TERM INT) inside thecleanup()function.
- Patch 5: Fixed heap corruption by adding size bounds checking before writing tosample_regs_userandsample_regs_intrfields. Added missing register mask clearing logic for theitracesynthesis path ofperf_event__repipe_attr().Quoted 6 lines
Ian Rogers (5):
perf maps: Addmaps__mutate_mapping
perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking perf inject/aslr: Implement sample address remapping perf aslr: Strip sample registers
perf test: Add inject ASLR testThe sashiko reviews are at:
https://sashiko.dev/#/patchset/20260611164122.3974068-1-irogers%40google.comQuoted 8 lines
To summarize:
Patch 2:
* TOCTOU if underlying event buffer mmaps change. Not an issue as
rewriting a perf.data file while it is being read is out of scope.Patch 3:
* Mapping addresses to 0 for unknown mappings is criticized but the
Quoted 7 lines
Why not then have some unknown mappings hashmap that will assign a random, unique address on a address range that doesn't overlap with any of the other maps?
Zero has special meaning, mapping some non-zero address to it introduces confusion when what we want is to just make sure that we don't leak addresses?
I don't think it is a bad idea. I think like with the ARM kernel symbolization issue we should do it as follow up work.
Quoted 11 lines
Quoted 7 lines
proposed alternative doesn't hide ASLR. This will cluster things on address 0 but the fix is simply to ensure no MMAPs are missing.
* Cross-endian issues, but as explained previously, these are out of scope.The clang build issue reported by James and disabling the kernel testing for ARM are both in the v20 series. So I think the patches are ready for review/merging.
I reviewed one other patch in the series besides the above suggestion.
Thanks, its an useful feature!
Thanks!
Ian
Quoted 25 lines
- Arnaldo
Quoted 23 lines
Thanks,
IanQuoted 20 lines
tools/perf/builtin-inject.c | 81 +- tools/perf/tests/shell/inject_aslr.sh | 533 ++++++++++ tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 1406 +++++++++++++++++++++++++ tools/perf/util/aslr.h | 44 + tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- tools/perf/util/machine.c | 32 +- tools/perf/util/maps.c | 148 ++- tools/perf/util/maps.h | 3 + tools/perf/util/symbol-elf.c | 41 +- tools/perf/util/symbol.c | 17 +- 12 files changed, 2251 insertions(+), 71 deletions(-) create mode 100755 tools/perf/tests/shell/inject_aslr.sh create mode 100644 tools/perf/util/aslr.c create mode 100644 tools/perf/util/aslr.h--
2.54.0.1099.g489fc7bff1-goog
Re: [PATCH v20 0/5] perf tools: Add inject --aslr feature
In reply to Ian RogersQuoted 244 lines · Ian Rogers
This patch series introduces the new 'perf inject --aslr' feature to remap virtual memory addresses or drop physical memory event leaks when profile record data is shared between machines. Bundled with this feature is a bug fix inside the core map tracking tool that hardens perf session analysis against concurrent lookup data races.
Detailed Mechanism of MMAP Mapping and ASLR virtual Address Allocation:
The ASLR tool virtualizes the address space of the recorded processes by intercepting MMAP and MMAP2 events to build a consistent translation database, which is subsequently used to rewrite sample addresses.
It maintains two primary lookup databases using hash maps:
1. 'remap_addresses': Maps an original mapping key to its new remapped base address. The key uses topological invariant coordinates:
(machine, dso, invariant). The invariant is computed as (start - pgoff) for DSO-backed mappings. This invariant remains constant even when perf's internal overlap-resolution splits a VMA into fragmented pieces, ensuring split maps resolve consistently back to the same remapped base.
2. 'top_addresses': Tracks the allocation state per process (machine, pid). It maintains 'remapped_max' (the highest allocated address in the virtualized space).For each MMAP/MMAP2 event:
- We look up the DSO and invariant key in 'remap_addresses'. If found, we reuse the translation, preserving the offset within the mapping.
- If not found, we allocate a new remapped address space:
- We usethread__find_mapto look up the mapping immediately preceding the new one in the original address space (at start - 1). If the preceding
mapping was also remapped, we place the new mapping contiguously after it in the remapped space. This preserves contiguity of split mappings (e.g., symbols split by HugeTLB, or anonymous .bss segments adjacent to initialized data).
- If no contiguous mapping is found, we insert a 1-page gap from the highest allocated address (remapped_max) to prevent accidental merging of unrelated VMAs.
- The event's start address (and pgoff for kernel maps) is rewritten, and the event is delegated to the output writer.To remain strictly conservative and guarantee security, the tool scrubs breakpoint addresses (
bp_addr) from all synthesized stream headers, completely dropsPERF_RECORD_TEXT_POKEevents to prevent absolute immediate pointer operands leaks, and drops unsupported complex payloads (such as user register stacks, raw tracepoints, and hardware AUX tracing frames).Verification is reinforced with shell test ('
inject_aslr.sh').Prerequisite Bug Fix (Patch 1). During development, a core map indexing issue was identified and resolved to prevent concurrent lookup data races during session analysis.
Changes since v19:
- Patch 1: Group lock and unlock operations insidemaps__mutate_mapping()into a single conditional block to resolve Clang 15 -Wthread-safety-analysis compilation errors.
- Patch 5: Skip kernel-based ASLR test cases (test_kernel_aslrandtest_kernel_report_aslr) on ARM architectures (aarch64 and arm*) to bypass high latency constraints and symbolization inconsistencies.Changes since v18:
- Patch 2 & 3: Squashed the bounds checking boundary fixes into the "Strip sample registers" patch. The array bounds checking now correctly uses 'orig_sample_type' to traverse the event payload, preventing heap corruption when dealing with events that have had their registers stripped by the ASLR tool pipeline.
- Patch 2 & 3: Rebased the commit series to properly isolate the sample address remapping logic from the register stripping logic.
- Patch 2 & 3: Expanded commit messages to extensively document the cross-endian behavior of 'perf inject'. Because 'perf inject' effectively acts as an endianness converter (writing a host-endianPERF_MAGICand flushing events exactly as they sit in memory after being byte-swapped byperf_event__all64_swap), all injected events must be perfectly constructed in the host's native endianness. Specifically,perf_event__all64_swapbyte-swaps the raw 64-bit payloads, which causes 32-bit sequential fields likePERF_SAMPLE_TID(containing pid and tid) to have their ordering reversed in memory (e.g., [BE_pid][BE_tid] becomes [LE_tid][LE_pid]). The ASLR tool's sample construction logic was expanded to explicitly unpack these fields and repack them sequentially via unions to guarantee a strictly host-endian layout that resolves these inversion anomalies. Similarly, branch stack flags (which are modified in-place to host-endian bitfields by the parser) are copied directly to the newly synthesized event, and 'needs_swap=false' is explicitly used when re-parsing the synthesized event to prevent erroneous double swapping.
- Series: Verified cross-endian robustness via the sashiko analyzer.Changes since v17:
- Patch 2: Reordered ksymbol deletion logic to ensureperf_event__process_ksymboldeletes the map *after* theaslr_tool__findnew_mappingtranslates the unregister offsets.
- Patch 2: Changedaslr_tool__deleteto cleanly handle guest machine deletion memory leaks.
- Patch 2: Resolved read-only segfaults on memory-mapped perf.data headers during attribute stripping by using deep copies inperf_event__repipe_attr.
- Patch 2: Fixed user space remap invariant logic to include(start - map__start(al.map))preventing negative overflows on module offset boundaries.
- Patch 3: Removed duplicatebswap_64payload byte-swapping inside the array logic, allowing the host endianness macrosCOPY_U64()to handle it dynamically.
- Patch 3: Fixed LBR branch sample starvation by explicitly reading branch counters instead of dropping the entire sample.
- Patch 5: Fixed test flakiness by grepping out physical hex addresses0x[0-9a-f]{8,}instead of matching exact address strings.
- Patch 5: Parameterized temp reports and updated test to scale with/dev/urandomcontinuous random reads.
- Patch Series: Added Signed-off-by tags uniformly and Assisted-by tags to track assistance.Changes since v16:
- Patch 2: Refactored inline ASLR stripping logic out ofbuiltin-inject.cand into dedicated helpers (aslr_tool__strip_attr_eventandaslr_tool__strip_evlist) inaslr.cto better separate concerns.
- Patch 2: Fixed guest machine allocation memory leak inaslr_tool__delete()wheremachines__exit()explicitly skipped freeing the guest processes tree.
- Patch 3: Fixed bounds-check violations during cross-endian parsing insideaslr_tool__process_sample()by correctly applyingbswap_64()to raw offsets, iteration counts, sizes, and addresses prior to logical evaluation whenorig_needs_swapis active.
- Patch 4: Fixed pipe mode parser misalignment bug by safely fetchingneeds_swapfrom the initialized evsel rather than blindly interceptingHEADER_ATTRevents prior to session parsing.
- Patch 4: Resolved checkpatch.pl line length warnings in thebswap_64endianness swapping logic.
- Patch Series: Reordered the final two patches. "perf aslr: Strip sample registers" is now Patch 4, and "perf test: Add inject ASLR test" is now Patch 5. This ensures the register stripping logic is fully introduced before the comprehensive shell tests validate it, preventing bisectability test failures and easing merge conflicts.
- Patch 5: Fixed "User registers stripping test" starvation when run as root by explicitly using '-e cycles:u' during recording, preventing the ring buffer from overflowing with kernel samples.Changes since v15:
- Patch 2: Added bounds checking forevent->header.sizebefore writing to breakpoint fields to avoid heap buffer overflow on older ABI events.
- Patch 2: Fixed asymmetric calculation bug inaslr_tool__findnew_mapping()where pgoff for anonymous kernel memory was not properly subtracted upon insertion, causing the lookup addition to overflow.
- Patch 2: Added detailed comments documenting the symmetric lookup and insertion math for unmapped and mapped memory blocks.
- Patch 5: Add missing kprobe and uprobe scrubbing of config1 and config2 duringaslr_tool__strip_evlist()to strictly conform with repipe constraints.Changes since v14:
- Patch 2: Removed unnecessary vertical whitespace inbuiltin-inject.c.
- Patch 2: Added comments explaining why pgoff is assigned for anonymous memory maps to prevent ASLR leaks.
- Patch 2: Removedorig_last_endtracking and refactored contiguous mapping detection to usethread__find_map(..., start - 1, ...)based on Gabriel's feedback.
- Patch 2: Scrub kprobe/uprobe event config1 and config2 fields to prevent address leaks.
- Patch 2: Overwrite pgoff with the remapped start address for anonymous mappings (detected viais_anon_memoryandis_no_dso_memory).
- Patch 3: Fix C90 mixed declaration error fororig_needs_swap.
- Patch 3: Temporarily disableevsel->needs_swapduring the secondaryevsel__parse_sample()call to prevent branch stack double-swapping bugs.Changes since v13:
- Patch 2: Added a NULL check for env before callingperf_env__kernel_is_64_bit(env)to prevent potential segfaults if the recorded environment has no headers.
- Patch 5: Fixedsample_sizeandid_posgoing out of sync duringaslr_tool__strip_evlist()andaslr_tool__restore_evlist(). Instead of usingevsel__reset_sample_bit(), which was acting as a no-op due to early bit clearing and corruptedsample_size, the tool now directly updatessample_typeand recomputes sample_size/id_pos dynamically. Addedorig_sample_sizetoaslr_evsel_privto correctly restore the state.Changes since v12:
- Patch 2: Fixed potential NULL pointer dereference inremap_addresses__hash()when handling unmapped memory events (key->dsois NULL) underREFCNT_CHECKING.
- Patch 2: Dynamically detect machine architecture bitness viaperf_env__kernel_is_64_bit()to select appropriatekernel_space_startboundaries, avoiding 64-bit address injection on 32-bit platforms.Changes since v11:
- Patch 1: Fixed struct dso name accessor inmaps.cby usingdso__name()instead of ->name.
- Patch 2: Fixed hash function inaslr.cto hash the underlying dso pointer usingRC_CHK_ACCESSto support reference count checking.Changes since v10:
- Patch 1: Added explicit tracking array logic inmaps__load_maps()to correctly accumulate valid maps (skipping NULL entries after failures) and safely return the exact populated count, resolving out-of-bounds pointer iteration panics.
- Patch 3: Fixed endianness bug during cross-endian sample parsing by passingevsel->needs_swapinstead of false to__evsel__parse_sampleinaslr.c, ensuring correct 32-bit field byte unswapping for packed fields. Refactoredevsel__parse_sampleto take aneeds_swapargument via__evsel__parse_sample.
- Patch 4: Fixedinject_aslr.shexit code handling in trap functions to capture and propagate the correct pipeline failure status code instead of unconditionally returning success or failing the test.Changes since v9:
- Patch 1: Added-ENOMEMerror check insidemaps__find_symbol_by_name()and returnNULLearly. Added map sorting state invalidation on early return inmaps__load_maps().
- Patch 2: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__findnew_mapping(). Addedpr_warning_oncewarning when raw auxtrace data is dropped.
- Patch 3: Fixed encapsulation by usingthread__maps()andthread__pid()accessors inaslr_tool__remap_address(). Wrappedevsel__parse_sample()to temporarily disableneeds_swapto avoid branch stack endianness corruption on cross-endian files. Fixed ISO C90 warning for declaration-after-statement fororig_needs_swap.
- Patch 4: Fixed duplicate cleanup by explicitly removing trap handlers (trap - EXIT TERM INT) inside thecleanup()function.
- Patch 5: Fixed heap corruption by adding size bounds checking before writing tosample_regs_userandsample_regs_intrfields. Added missing register mask clearing logic for theitracesynthesis path ofperf_event__repipe_attr().Ian Rogers (5):
perf maps: Addmaps__mutate_mapping
perf inject/aslr: Add ASLR tool infrastructure and MMAP tracking perf inject/aslr: Implement sample address remapping perf aslr: Strip sample registers
perf test: Add inject ASLR testtools/perf/builtin-inject.c | 81 +- tools/perf/tests/shell/inject_aslr.sh | 533 ++++++++++ tools/perf/util/Build | 1 + tools/perf/util/aslr.c | 1406 +++++++++++++++++++++++++ tools/perf/util/aslr.h | 44 + tools/perf/util/evsel.c | 6 +- tools/perf/util/evsel.h | 10 +- tools/perf/util/machine.c | 32 +- tools/perf/util/maps.c | 148 ++- tools/perf/util/maps.h | 3 + tools/perf/util/symbol-elf.c | 41 +- tools/perf/util/symbol.c | 17 +- 12 files changed, 2251 insertions(+), 71 deletions(-)
Quoted 4 lines
create mode 100755
tools/perf/tests/shell/inject_aslr.shcreate mode 100644tools/perf/util/aslr.ccreate mode 100644tools/perf/util/aslr.h