Skip to content

Commit

Permalink
Filter out host processes by host mntns to prevent ebpf map from fill…
Browse files Browse the repository at this point in the history
…ing up during recording
  • Loading branch information
neblen committed Sep 16, 2022
1 parent 9a9836f commit 905a7a4
Show file tree
Hide file tree
Showing 6 changed files with 134,270 additions and 3,824 deletions.
15 changes: 15 additions & 0 deletions internal/pkg/daemon/bpfrecorder/bpf/recorder.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define MAX_ENTRIES 8 * 1024
#define MAX_SYSCALLS 1024
#define MAX_COMM_LEN 64
#define MAX_MNTNS_LEN 1

char LICENSE[] SEC("license") = "Dual BSD/GPL";

Expand All @@ -24,6 +25,13 @@ struct {
__type(value, char[MAX_COMM_LEN]); // command name
} comms SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, MAX_MNTNS_LEN);
__type(key, u32); // PID
__type(value, u64); // mntns ID
} system_mntns SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 1 << 24);
Expand All @@ -50,6 +58,13 @@ int sys_enter(struct trace_event_raw_sys_enter * args)
if (mntns == 0) {
return 0;
}
// Filter mntns by hostmntns to exclude host processes
u32 hostPid = 1;
u64 * host_mntns;
host_mntns = bpf_map_lookup_elem(&system_mntns, &hostPid);
if (host_mntns != NULL && *host_mntns == mntns) {
return 0;
}

// Update the command name if required
char comm[MAX_COMM_LEN];
Expand Down
30 changes: 21 additions & 9 deletions internal/pkg/daemon/bpfrecorder/bpfrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type BpfRecorder struct {
startRequests int64
syscalls *bpf.BPFMap
comms *bpf.BPFMap
mntns *bpf.BPFMap
btfPath string
syscallNamesForIDCache *ttlcache.Cache[string, string]
containerIDCache *ttlcache.Cache[string, string]
Expand Down Expand Up @@ -423,6 +424,11 @@ func (b *BpfRecorder) load(startEventProcessor bool) (err error) {
if err != nil {
return fmt.Errorf("get comms map: %w", err)
}
b.logger.Info("Getting system_mntns map")
mntns, err := b.GetMap(module, "system_mntns")
if err != nil {
return fmt.Errorf("get mntns_map: %w", err)
}

events := make(chan []byte)
ringbuffer, err := b.InitRingBuf(module, "events", events)
Expand All @@ -433,6 +439,11 @@ func (b *BpfRecorder) load(startEventProcessor bool) (err error) {

b.syscalls = syscalls
b.comms = comms
b.mntns = mntns

// Update mntns to system_mntns
b.updateSystemMntns()

if startEventProcessor {
go b.processEvents(events)
}
Expand Down Expand Up @@ -564,6 +575,16 @@ func (b *BpfRecorder) processEvents(events chan []byte) {
}
}

func (b *BpfRecorder) updateSystemMntns() {
mntnsByte := make([]byte, 8)
binary.LittleEndian.PutUint64(mntnsByte, b.systemMountNamespace)
var hostPid uint32 = 1
err := b.UpdateValue(b.mntns, hostPid, mntnsByte)
if err != nil {
b.logger.Error(err, "update system_mntns map failed")
}
}

func (b *BpfRecorder) handleEvent(event []byte) {
// Newly arrived PIDs
const eventLen = 16
Expand All @@ -575,15 +596,6 @@ func (b *BpfRecorder) handleEvent(event []byte) {
pid := binary.LittleEndian.Uint32(event)
mntns := binary.LittleEndian.Uint64(event[8:])

// Filter out non-containers
if mntns == b.systemMountNamespace {
b.logger.V(config.VerboseLevel).Info(
"Skipping PID, because it's on the system mount namespace",
"pid", pid, "mntns", mntns,
)
return
}

// Blocking from syscall retrieval when PIDs are currently being analyzed
b.pidLock.Lock()
defer b.pidLock.Unlock()
Expand Down
24 changes: 0 additions & 24 deletions internal/pkg/daemon/bpfrecorder/bpfrecorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,30 +725,6 @@ func TestProcessEvents(t *testing.T) {
require.True(t, success)
},
},
{ // system mount namespace
prepare: func(sut *BpfRecorder, mock *bpfrecorderfakes.FakeImpl) []byte {
mntns := binary.LittleEndian.Uint64([]byte{1, 0, 0, 0, 0, 0, 0, 0})
sut.systemMountNamespace = mntns
sut.profileForMountNamespace.Store(mntns, "profile.json")
return []byte{
1, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
}
},
assert: func(sut *BpfRecorder, logger *Logger) {
success := false
for i := 0; i < 100; i++ {
logger.mutex.RLock()
success = util.Contains(logger.messages, "Skipping PID, because it's on the system mount namespace")
logger.mutex.RUnlock()
if success {
break
}
time.Sleep(100 * time.Millisecond)
}
require.True(t, success)
},
},
{ // invalid event length
prepare: func(sut *BpfRecorder, mock *bpfrecorderfakes.FakeImpl) []byte {
return []byte{1, 0, 0}
Expand Down
83 changes: 83 additions & 0 deletions internal/pkg/daemon/bpfrecorder/bpfrecorderfakes/fake_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 905a7a4

Please sign in to comment.