Skip to content

Commit

Permalink
Merge pull request #415 from kubescape/bugfix/empty-exec
Browse files Browse the repository at this point in the history
Bugfix/empty exec
  • Loading branch information
amitschendel authored Nov 26, 2024
2 parents 41468e4 + 52b9628 commit 1b532d6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,6 @@ func (am *ApplicationProfileManager) ReportCapability(k8sContainerID, capability
}

func (am *ApplicationProfileManager) ReportFileExec(k8sContainerID, path string, args []string) {
// skip empty path
if path == "" {
return
}
if err := am.waitForContainer(k8sContainerID); err != nil {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestApplicationProfileManager(t *testing.T) {
// report capability
go am.ReportCapability("ns/pod/cont", "NET_BIND_SERVICE")
// report file exec
go am.ReportFileExec("ns/pod/cont", "", []string{"ls"}) // will not be reported
go am.ReportFileExec("ns/pod/cont", "/bin/bash", []string{"-c", "ls"})
go am.ReportFileExec("ns/pod/cont", "/bin/bash", []string{"-c", "ls"}) // duplicate - not reported
go am.ReportFileExec("ns/pod/cont", "/bin/bash", []string{"-c", "ls", "-l"}) // additional arg - reported
Expand Down
9 changes: 8 additions & 1 deletion pkg/containerwatcher/v1/container_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,15 @@ func CreateIGContainerWatcher(cfg config.Config, applicationProfileManager appli

path := event.Comm
if len(event.Args) > 0 {
path = event.Args[0]
if event.Args[0] != "" {
path = event.Args[0]
}
}

if path == "" {
return
}

metrics.ReportEvent(utils.ExecveEventType)
processManager.ReportEvent(utils.ExecveEventType, &event)
applicationProfileManager.ReportFileExec(k8sContainerID, path, event.Args)
Expand Down
4 changes: 3 additions & 1 deletion pkg/ruleengine/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ var (

func getExecPathFromEvent(event *tracerexectype.Event) string {
if len(event.Args) > 0 {
return event.Args[0]
if event.Args[0] != "" {
return event.Args[0]
}
}
return event.Comm
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/ruleengine/v1/r0001_unexpected_process_launched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ func TestR0001UnexpectedProcessLaunched(t *testing.T) {
t.Errorf("Expected ruleResult to not be nil since exec is not whitelisted")
}

// Test /bin/sh
profile.Spec.Containers[0].Execs = append(profile.Spec.Containers[0].Execs, v1beta1.ExecCalls{
Path: "/bin/sh",
Args: []string{"/bin/sh", "-s", "unix:cmd"},
})
objCache.SetApplicationProfile(profile)

e.Comm = "sh"
e.Args = []string{"/bin/sh", "-s", "unix:cmd"}
ruleResult = r.ProcessEvent(utils.ExecveEventType, e, &objCache)
if ruleResult != nil {
t.Errorf("Expected ruleResult to be nil since exec is whitelisted")
}
}

func TestR0001UnexpectedProcessLaunchedArgCompare(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ func GetHostFilePathFromEvent(event K8sEvent, containerPid uint32) (string, erro
// Get the path of the executable from the given event.
func GetExecPathFromEvent(event *tracerexectype.Event) string {
if len(event.Args) > 0 {
return event.Args[0]
if event.Args[0] != "" {
return event.Args[0]
}
}
return event.Comm
}
Expand Down

0 comments on commit 1b532d6

Please sign in to comment.