Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf recorder: track child pids on clone() #2644

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/pkg/daemon/bpfrecorder/bpf/recorder.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int sched_process_exec(struct trace_event_raw_sched_process_exec * ctx)

if (is_child || matches_filter(comm)) {
u32 pid = bpf_get_current_pid_tgid() >> 32;
bpf_printk("adding child pid: %u\n", pid);
bpf_printk("adding child pid: %u", pid);
bpf_map_update_elem(&child_pids, &pid, &TRUE, BPF_ANY);
}
return 0;
Expand Down Expand Up @@ -378,6 +378,24 @@ int sched_process_exit(void * ctx)
return 0;
}

// Detect clone() from PIDs in child_pids and add the new PIDs to the map.
SEC("tracepoint/syscalls/sys_exit_clone")
int sys_exit_clone(struct trace_event_raw_sys_exit * ctx)
{
u32 ret = ctx->ret;
// We only need the fork, the existing process is already traced.
if (ret == 0)
return 0;

u32 pid = bpf_get_current_pid_tgid() >> 32;
bool is_child = bpf_map_lookup_elem(&child_pids, &pid) != NULL;
if (is_child) {
bpf_printk("adding child pid from clone: %u", ret);
bpf_map_update_elem(&child_pids, &ret, &TRUE, BPF_ANY);
}
return 0;
}

SEC("tracepoint/raw_syscalls/sys_enter")
int sys_enter(struct trace_event_raw_sys_enter * args)
{
Expand Down
Binary file modified internal/pkg/daemon/bpfrecorder/bpf/recorder.bpf.o.amd64
Binary file not shown.
Binary file modified internal/pkg/daemon/bpfrecorder/bpf/recorder.bpf.o.arm64
Binary file not shown.
1 change: 1 addition & 0 deletions internal/pkg/daemon/bpfrecorder/bpfrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ func (b *BpfRecorder) getMntnsForProfile(profile string) (uint32, bool) {

var baseHooks = []string{
"sys_enter",
"sys_exit_clone",
"sched_process_exec",
"sched_process_exit",
}
Expand Down
Loading