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

Catch pids that are off a parent and not quite ready #581

Merged
merged 2 commits into from
Jan 29, 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
11 changes: 11 additions & 0 deletions pkg/internal/discover/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ func (m *matcher) filterCreated(obj processAttrs) (Event[ProcessMatch], bool) {
}, true
}
}

// We didn't match the process, but let's see if the parent PID is tracked, it might be the child hasn't opened the port yet
if _, ok := m.processHistory[PID(proc.PPid)]; ok {
m.log.Debug("found process by matching the process parent id", "pid", proc.Pid, "ppid", proc.PPid, "comm", proc.ExePath, "metadata", obj.metadata)
m.processHistory[obj.pid] = proc
return Event[ProcessMatch]{
Type: EventCreated,
Obj: ProcessMatch{Criteria: &m.criteria[0], Process: proc},
}, true
}

return Event[ProcessMatch]{}, false
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/internal/discover/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,48 @@ func TestCriteriaMatcher_MustMatchAllAttributes(t *testing.T) {
assert.Equal(t, "foons", m.Obj.Criteria.Namespace)
assert.Equal(t, services.ProcessInfo{Pid: 1, ExePath: "/bin/foo", OpenPorts: []uint32{8081}}, *m.Obj.Process)
}

func TestCriteriaMatcherMissingPort(t *testing.T) {
pipeConfig := pipe.Config{}
require.NoError(t, yaml.Unmarshal([]byte(`discovery:
services:
- name: port-only
namespace: foo
open_ports: 80
`), &pipeConfig))

matcherFunc, err := CriteriaMatcherProvider(CriteriaMatcher{Cfg: &pipeConfig})
require.NoError(t, err)
discoveredProcesses := make(chan []Event[processAttrs], 10)
filteredProcesses := make(chan []Event[ProcessMatch], 10)
go matcherFunc(discoveredProcesses, filteredProcesses)
defer close(discoveredProcesses)

// it will filter unmatching processes and return a ProcessMatch for these that match
processInfo = func(pp processAttrs) (*services.ProcessInfo, error) {
proc := map[PID]struct {
Exe string
PPid int32
}{
1: {Exe: "/bin/weird33", PPid: 0}, 2: {Exe: "/bin/weird33", PPid: 16}, 3: {Exe: "/bin/weird33", PPid: 1}}[pp.pid]
return &services.ProcessInfo{Pid: int32(pp.pid), ExePath: proc.Exe, PPid: proc.PPid, OpenPorts: pp.openPorts}, nil
}
discoveredProcesses <- []Event[processAttrs]{
{Type: EventCreated, Obj: processAttrs{pid: 1, openPorts: []uint32{80}}}, // this one is the parent, matches on port
{Type: EventDeleted, Obj: processAttrs{pid: 2, openPorts: []uint32{}}}, // we'll skip 2 since PPid is 16, not 1
{Type: EventCreated, Obj: processAttrs{pid: 3, openPorts: []uint32{}}}, // this one is the child, without port, but matches the parent by port
}

matches := testutil.ReadChannel(t, filteredProcesses, testTimeout)
require.Len(t, matches, 2)
m := matches[0]
assert.Equal(t, EventCreated, m.Type)
assert.Equal(t, "port-only", m.Obj.Criteria.Name)
assert.Equal(t, "foo", m.Obj.Criteria.Namespace)
assert.Equal(t, services.ProcessInfo{Pid: 1, ExePath: "/bin/weird33", OpenPorts: []uint32{80}, PPid: 0}, *m.Obj.Process)
m = matches[1]
assert.Equal(t, EventCreated, m.Type)
assert.Equal(t, "port-only", m.Obj.Criteria.Name)
assert.Equal(t, "foo", m.Obj.Criteria.Namespace)
assert.Equal(t, services.ProcessInfo{Pid: 3, ExePath: "/bin/weird33", OpenPorts: []uint32{}, PPid: 1}, *m.Obj.Process)
}
Loading