Skip to content

Commit

Permalink
libbpf-tools: Fix misaligned pointer accesses in exitsnoop
Browse files Browse the repository at this point in the history
The perf buffer in exit snoop doesn't maintain 8 byte alignment for
start_time and exit_time in struct event. When building with
"-fsanitize=alignment -fsanitize-undefined-trap-on-error" failures
happen in handle_event. Fix these by copying the event from the perf
buffer before accessing.

Signed-off-by: Ian Rogers <irogers@google.com>
  • Loading branch information
captain5050 authored and yonghong-song committed Nov 14, 2023
1 parent 1d659c7 commit 6617113
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions libbpf-tools/exitsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,39 @@ static void sig_int(int signo)

static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
{
struct event *e = data;
struct event e;
time_t t;
struct tm *tm;
char ts[32];
double age;
int sig, coredump;

if (data_sz < sizeof(e)) {
printf("Error: packet too small\n");
return;
}
/* Copy data as alignment in the perf buffer isn't guaranteed. */
memcpy(&e, data, sizeof(e));

if (emit_timestamp) {
time(&t);
tm = localtime(&t);
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
printf("%8s ", ts);
}

age = (e->exit_time - e->start_time) / 1e9;
age = (e.exit_time - e.start_time) / 1e9;
printf("%-16s %-7d %-7d %-7d %-7.2f ",
e->comm, e->pid, e->ppid, e->tid, age);
e.comm, e.pid, e.ppid, e.tid, age);

if (!e->sig) {
if (!e->exit_code)
if (!e.sig) {
if (!e.exit_code)
printf("0\n");
else
printf("code %d\n", e->exit_code);
printf("code %d\n", e.exit_code);
} else {
sig = e->sig & 0x7f;
coredump = e->sig & 0x80;
sig = e.sig & 0x7f;
coredump = e.sig & 0x80;
if (sig)
printf("signal %d (%s)", sig, strsignal(sig));
if (coredump)
Expand Down

0 comments on commit 6617113

Please sign in to comment.