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

tetragon: Use do_task_dead probe for exit sensor #1012

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 49 additions & 3 deletions bpf/process/bpf_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,57 @@

char _license[] __attribute__((section("license"), used)) = "GPL";

__attribute__((section("tracepoint/sys_exit"), used)) int
event_exit(struct sched_execve_args *ctx)
/*
* Hooking on do_task_dead kernel function, which is the last one the
* task would execute after exiting. It's stable since v4.19, so it's
* safe to hook for us.
*
* To find out if we are the last thread of execution in the task we
* use current->signal->live counter (thanks Djalal! ;-) )
*
* It's initialized for thread leader:
*
* clone {
* copy_process
* copy_signal
* atomic_set(&sig->live, 1);
* }
*
* Incremented for each new thread:
*
* clone {
* copy_process
* atomic_inc(&current->signal->live);
* ...
* wake_up_new_task
* }
*
* Decremented for each exiting thread:
*
* do_exit {
* atomic_dec_and_test(&tsk->signal->live);
* ...
* do_task_dead
* __schedule
* BUG
* }
*
* If task->signal->live == 0 we are the last thread of execution and we
* won't race with another clone, because there's no other thread to call
* it (current thread is in do_exit).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice comment :)

*/
__attribute__((section("kprobe/do_task_dead"), used)) int
event_exit(struct pt_regs *ctx)
{
struct task_struct *task = (struct task_struct *)get_current_task();
__u64 pid_tgid = get_current_pid_tgid();
struct signal_struct *signal;
atomic_t live;

event_exit_send(ctx, pid_tgid);
probe_read(&signal, sizeof(signal), _(&task->signal));
probe_read(&live, sizeof(live), _(&signal->live));

if (live.counter == 0)
event_exit_send(ctx, pid_tgid >> 32);
return 0;
}
14 changes: 1 addition & 13 deletions bpf/process/bpf_exit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,9 @@ struct {
__type(value, struct msg_exit);
} exit_heap_map SEC(".maps");

static inline __attribute__((always_inline)) void event_exit_send(struct sched_execve_args *ctx, __u64 current)
static inline __attribute__((always_inline)) void event_exit_send(void *ctx, __u32 tgid)
{
struct execve_map_value *enter;
__u32 pid, tgid;

pid = current & 0xFFFFffff;
tgid = current >> 32;

/* We are only tracking group leaders so if tgid is not
* the same as the pid then this is an untracked child
* and we can skip the lookup/insert/delete cycle that
* would otherwise occur.
*/
if (pid != tgid)
return;

/* It is safe to do a map_lookup_event() here because
* we must have captured the execve case in order for an
Expand Down
6 changes: 3 additions & 3 deletions pkg/sensors/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ var (

Exit = program.Builder(
"bpf_exit.o",
"sched/sched_process_exit",
"tracepoint/sys_exit",
"do_task_dead",
"kprobe/do_task_dead",
"event_exit",
"tracepoint",
"kprobe",
)

Fork = program.Builder(
Expand Down
4 changes: 0 additions & 4 deletions pkg/sensors/exec/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ func TestExit(t *testing.T) {
}

func TestExitLeader(t *testing.T) {
t.Skip("due to github.com/cilium/tetragon/pull/987")

var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

Expand Down Expand Up @@ -132,8 +130,6 @@ func TestExitLeader(t *testing.T) {
//
// In our test we check that the parent of the /bin/echo command is the exit-tester program.
func TestExitZombie(t *testing.T) {
t.Skip("due to github.com/cilium/tetragon/pull/987")

var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

Expand Down
2 changes: 1 addition & 1 deletion pkg/testutils/sensors/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func mergeSensorMaps(t *testing.T, maps1, maps2 []SensorMap, progs1, progs2 []Se
func mergeInBaseSensorMaps(t *testing.T, sensorMaps []SensorMap, sensorProgs []SensorProg) ([]SensorMap, []SensorProg) {
var baseProgs = []SensorProg{
0: SensorProg{Name: "event_execve", Type: ebpf.TracePoint},
1: SensorProg{Name: "event_exit", Type: ebpf.TracePoint},
1: SensorProg{Name: "event_exit", Type: ebpf.Kprobe},
2: SensorProg{Name: "event_wake_up_new_task", Type: ebpf.Kprobe},
3: SensorProg{Name: "execve_send", Type: ebpf.TracePoint},
}
Expand Down