Skip to content

Commit

Permalink
solving missing events issue by adding tracepoints on sys_enter_exit(…
Browse files Browse the repository at this point in the history
…+_group)
  • Loading branch information
0xmilkmix committed Dec 6, 2023
1 parent 1136585 commit 8a016c5
Show file tree
Hide file tree
Showing 3 changed files with 6,124 additions and 5,537 deletions.
34 changes: 34 additions & 0 deletions internal/pkg/daemon/bpfrecorder/bpf/recorder.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define PROBE_TYPE_WRITE 5
#define PROBE_TYPE_SOCKET 6
#define PROBE_TYPE_CAP 7
#define PROBE_TYPE_EXIT 8

enum
{
Expand Down Expand Up @@ -607,6 +608,39 @@ int BPF_KPROBE(trace_cap_capable)
return 0;
}

static __always_inline void handle_exit()
{
apparmor_event_data_t *event;

u32 pid = bpf_get_current_pid_tgid() >> 32;

u32 mntns = get_mntns();
if (!mntns)
return;

event = bpf_ringbuf_reserve(&apparmor_events, sizeof(apparmor_event_data_t), 0);
if (event) {
event->pid = pid;
event->mntns = mntns;
event->type = PROBE_TYPE_EXIT;
bpf_ringbuf_submit(event, 0);
}
}

SEC("tracepoint/syscalls/sys_enter_exit")
int syscall__exit(struct trace_event_raw_sys_enter *ctx)
{
handle_exit();
return 0;
}

SEC("tracepoint/syscalls/sys_enter_exit_group")
int syscall__exit_group(struct trace_event_raw_sys_enter *ctx)
{
handle_exit();
return 0;
}

SEC("tracepoint/raw_syscalls/sys_enter")
int sys_enter(struct trace_event_raw_sys_enter * args)
{
Expand Down
19 changes: 19 additions & 0 deletions internal/pkg/daemon/bpfrecorder/bpfrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
probeTypeWrite int = 5
probeTypeSocket int = 6
probeTypeCap int = 7
probeTypeExit int = 8
protRead int = 0x1
protWrite int = 0x2
protExec int = 0x4
Expand Down Expand Up @@ -111,6 +112,7 @@ type BpfRecorder struct {
lockRecordedSocketsUse sync.Mutex
recordedCapabilities []string
lockRecordedCapabilities sync.Mutex
lockAppArmorRecording sync.Mutex
}

type syscallTracepoint struct {
Expand Down Expand Up @@ -213,6 +215,16 @@ var apparmorSyscallTracepoints = []syscallTracepoint{
program: "syscall__socket",
name: "sys_enter_socket",
},
{
category: "syscalls",
program: "syscall__exit",
name: "sys_enter_exit",
},
{
category: "syscalls",
program: "syscall__exit_group",
name: "sys_enter_exit_group",
},
}

var capabilities = map[int]string{
Expand Down Expand Up @@ -303,6 +315,10 @@ func (b *BpfRecorder) Syscalls() *bpf.BPFMap {
func (b *BpfRecorder) GetAppArmorProcessed() BpfAppArmorProcessed {
var processed BpfAppArmorProcessed

// validating that the process exited.
// TODO: should this be subject to a flag for the Kubernetes controller integration?
b.lockAppArmorRecording.Lock()

processed.FileProcessed = b.processExecFsEvents()
processed.Socket = b.recordedSocketsUse
processed.Capabilities = b.recordedCapabilities
Expand Down Expand Up @@ -704,6 +720,7 @@ func (b *BpfRecorder) Load(startEventProcessor bool) (err error) {
return fmt.Errorf("init apparmor_events ringbuffer: %w", err)
}
b.PollRingBuffer(apparmorRingbuffer, timeout)
b.lockAppArmorRecording.Lock()
go b.handleAppArmorEvents(apparmorEvents)
}

Expand Down Expand Up @@ -979,6 +996,8 @@ func (b *BpfRecorder) handleAppArmorEvents(apparmorEvents chan []byte) {
b.handleAppArmorSocketEvents(apparmorEvent)
case uint8(probeTypeCap):
b.handleAppArmorCapabilityEvents(apparmorEvent)
case uint8(probeTypeExit):
b.lockAppArmorRecording.Unlock()
}
}
}
Expand Down
Loading

0 comments on commit 8a016c5

Please sign in to comment.