Skip to content

Commit

Permalink
tetragon: Add support to detect available exit probes
Browse files Browse the repository at this point in the history
Adding support to use disassociate_ctty exit probe in case
acct_process is missing in kernel.

Detecting both probes during base sensor setup and picking
up available function and adding new log line:

time="2023-12-11T14:44:28Z" level=info msg="Exit probe on acct_process" failed=false

The failed value will be true when the kallsyms detection
fails.

Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Dec 20, 2023
1 parent d5ca9c8 commit a250225
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
20 changes: 19 additions & 1 deletion bpf/process/bpf_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,28 @@ char _license[] __attribute__((section("license"), used)) = "Dual BSD/GPL";
* we are the last one of the thread group.
*/
__attribute__((section("kprobe/acct_process"), used)) int
event_exit(struct pt_regs *ctx)
event_exit_acct_process(struct pt_regs *ctx)
{
__u64 pid_tgid = get_current_pid_tgid();

event_exit_send(ctx, pid_tgid >> 32);
return 0;
}

/*
* Hooking on acct_process kernel function, which is called on the task's
* exit path once the task is the last one in the group. It's stable since
* v4.19, so it's safe to hook for us.
*
* It's called with on_exit argument != 0 when called from do_exit
* function with same conditions like for acct_process described above.
*/
__attribute__((section("kprobe/disassociate_ctty"), used)) int
event_exit_disassociate_ctty(struct pt_regs *ctx)
{
int on_exit = (int)PT_REGS_PARM1_CORE(ctx);

if (on_exit)
event_exit_send(ctx, get_current_pid_tgid() >> 32);
return 0;
}
9 changes: 9 additions & 0 deletions pkg/ksyms/ksyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,12 @@ func (k *Ksyms) getFnOffset(addr uint64) (*FnOffset, error) {
Offset: addr - sym.addr,
}, nil
}

func (k *Ksyms) IsAvailable(name string) bool {
for _, sym := range k.table {
if sym.name == name {
return true
}
}
return false
}
26 changes: 26 additions & 0 deletions pkg/sensors/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
package base

import (
"log"
"sync"

"github.com/cilium/tetragon/pkg/kernels"
"github.com/cilium/tetragon/pkg/ksyms"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/sensors"
"github.com/cilium/tetragon/pkg/sensors/program"
)
Expand Down Expand Up @@ -64,6 +69,26 @@ var (
sensorInit sync.Once
)

func setupExitProgram() {
ks, err := ksyms.KernelSymbols()
if err == nil {
has_acct_process := ks.IsAvailable("acct_process")
has_disassociate_ctty := ks.IsAvailable("disassociate_ctty")

/* Preffer acct_process over disassociate_ctty */
if has_acct_process {
Exit.Attach = "acct_process"
Exit.Label = "kprobe/acct_process"
} else if has_disassociate_ctty {
Exit.Attach = "disassociate_ctty"
Exit.Label = "kprobe/disassociate_ctty"
} else {
log.Fatal("Failed to detect exit probe symbol.")
}
}
logger.GetLogger().Infof("Exit probe on %s", Exit.Attach)
}

func GetExecveMap() *program.Map {
return ExecveMap
}
Expand Down Expand Up @@ -104,6 +129,7 @@ func GetDefaultMaps() []*program.Map {
// GetInitialSensor returns the base sensor
func GetInitialSensor() *sensors.Sensor {
sensorInit.Do(func() {
setupExitProgram()
sensor.Progs = GetDefaultPrograms()
sensor.Maps = GetDefaultMaps()
})
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 @@ -131,7 +131,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.Kprobe},
1: SensorProg{Name: "event_exit", Type: ebpf.Kprobe, Match: ProgMatchPartial},
2: SensorProg{Name: "event_wake_up_new_task", Type: ebpf.Kprobe},
3: SensorProg{Name: "execve_send", Type: ebpf.TracePoint},
4: SensorProg{Name: "tg_kp_bprm_committing_creds", Type: ebpf.Kprobe},
Expand Down

0 comments on commit a250225

Please sign in to comment.