From da8f45d6caa48e25d25097fd7600acdae4a749db Mon Sep 17 00:00:00 2001 From: Tyler Finethy Date: Wed, 26 Feb 2025 17:11:30 -0500 Subject: [PATCH] [Dynamic Instrumentation] DI proctracker should not inspect itself (#34495) --- .../proctracker/proctracker.go | 5 +++ .../proctracker/proctracker_test.go | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 pkg/dynamicinstrumentation/proctracker/proctracker_test.go diff --git a/pkg/dynamicinstrumentation/proctracker/proctracker.go b/pkg/dynamicinstrumentation/proctracker/proctracker.go index 2489ee6904aee..ddb5130250bc2 100644 --- a/pkg/dynamicinstrumentation/proctracker/proctracker.go +++ b/pkg/dynamicinstrumentation/proctracker/proctracker.go @@ -95,6 +95,11 @@ func (pt *ProcessTracker) handleProcessStop(pid uint32) { } func (pt *ProcessTracker) inspectBinary(exePath string, pid uint32) { + // Avoid self-inspection. + if int(pid) == os.Getpid() { + return + } + serviceName := getServiceName(pid) if serviceName == "" { // if the expected env vars are not set we don't inspect the binary diff --git a/pkg/dynamicinstrumentation/proctracker/proctracker_test.go b/pkg/dynamicinstrumentation/proctracker/proctracker_test.go new file mode 100644 index 0000000000000..0a7d27df4e9f5 --- /dev/null +++ b/pkg/dynamicinstrumentation/proctracker/proctracker_test.go @@ -0,0 +1,33 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +//go:build linux_bpf + +package proctracker + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestProcessNotAdded(t *testing.T) { + pt := ProcessTracker{ + processes: make(map[pid]binaryID), + } + + // Use the current process ID for testing + pid := uint32(os.Getpid()) + + // Simulate process start + pt.inspectBinary("", pid) + + // Ensure the process ID is not added to pt.processes + pt.lock.RLock() + defer pt.lock.RUnlock() + _, exists := pt.processes[pid] + assert.False(t, exists, "process ID should not be added to pt.processes") +}