Skip to content

Commit

Permalink
bpf: Fix Prefix operator for matchBinaries
Browse files Browse the repository at this point in the history
If path larger than 256 bytes need to copy prefix from args.

Signed-off-by: Andrei Fedotov <[email protected]>
  • Loading branch information
anfedotoff committed Jul 23, 2024
1 parent 358f397 commit ac9d609
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions bpf/process/bpf_execve_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ execve_send(void *ctx)
{
struct msg_execve_event *event;
struct execve_map_value *curr;
struct execve_heap *heap;
struct msg_process *p;
__u32 zero = 0;
uint64_t size;
Expand Down Expand Up @@ -329,19 +330,29 @@ execve_send(void *ctx)
memset(&curr->bin, 0, sizeof(curr->bin));
#ifdef __LARGE_BPF_PROG
// read from proc exe stored at execve time
if (event->exe.len <= BINARY_PATH_MAX_LEN) {
if (event->exe.len <= BINARY_PATH_MAX_LEN && !event->exe.error) {
curr->bin.path_length = probe_read(curr->bin.path, event->exe.len, event->exe.off);
if (curr->bin.path_length == 0)
curr->bin.path_length = event->exe.len;
} else {
heap = map_lookup_elem(&execve_heap, &zero);
if (heap) {
curr->bin.path_length = probe_read_str(curr->bin.path, BINARY_PATH_MAX_LEN, &heap->maxpath);
if (curr->bin.path_length > 1) {
// don't include the NULL byte in the length
curr->bin.path_length--;
}
}
}
#else
// reuse p->args first string that contains the filename, this can't be
// above 256 in size (otherwise the complete will be send via data msg)
// which is okay because we need the 256 first bytes.
curr->bin.path_length = probe_read_str(curr->bin.path, BINARY_PATH_MAX_LEN, &p->args);
if (curr->bin.path_length > 1) {
// don't include the NULL byte in the length
curr->bin.path_length--;
// reuse heap->maxpath that contains the filename.
heap = map_lookup_elem(&execve_heap, &zero);
if (heap) {
curr->bin.path_length = probe_read_str(curr->bin.path, BINARY_PATH_MAX_LEN, &heap->maxpath);
if (curr->bin.path_length > 1) {
// don't include the NULL byte in the length
curr->bin.path_length--;
}
}
#endif
}
Expand Down

0 comments on commit ac9d609

Please sign in to comment.