Skip to content

Commit

Permalink
[CWS] remove gopacket dependency from cws-instrumentation (DataDog#32802
Browse files Browse the repository at this point in the history
)
  • Loading branch information
paulcacheux authored Jan 9, 2025
1 parent 3fbe9f7 commit af0d8cb
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 112 deletions.
4 changes: 2 additions & 2 deletions pkg/dynamicinstrumentation/proctracker/proctracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/network/go/bininspect"
"github.com/DataDog/datadog-agent/pkg/network/go/binversion"
"github.com/DataDog/datadog-agent/pkg/process/monitor"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -186,7 +186,7 @@ func (pt *ProcessTracker) registerProcess(binID binaryID, pid pid, mTime syscall
}

func getServiceName(pid uint32) string {
envVars, _, err := utils.EnvVars([]string{"DD"}, pid, model.MaxArgsEnvsSize)
envVars, _, err := utils.EnvVars([]string{"DD"}, pid, sharedconsts.MaxArgsEnvsSize)
if err != nil {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/security/proto/ebpfless/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/json"

"github.com/DataDog/datadog-agent/pkg/security/secl/containerutils"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"modernc.org/mathutil"
)

Expand Down Expand Up @@ -137,7 +137,7 @@ type ForkSyscallMsg struct {
// ExitSyscallMsg defines an exit message
type ExitSyscallMsg struct {
Code uint32
Cause model.ExitCause
Cause sharedconsts.ExitCause
}

// FileSyscallMsg defines a file message
Expand Down
8 changes: 4 additions & 4 deletions pkg/security/ptracer/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"time"

"github.com/DataDog/datadog-agent/pkg/security/proto/ebpfless"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"golang.org/x/sys/unix"
)

Expand Down Expand Up @@ -169,13 +169,13 @@ func (ctx *CWSPtracerCtx) handleExit(process *Process, waitStatus *syscall.WaitS
if process.Pid == process.Tgid && waitStatus != nil {
exitCtx := &ebpfless.ExitSyscallMsg{}
if waitStatus.Exited() {
exitCtx.Cause = model.ExitExited
exitCtx.Cause = sharedconsts.ExitExited
exitCtx.Code = uint32(waitStatus.ExitStatus())
} else if waitStatus.CoreDump() {
exitCtx.Cause = model.ExitCoreDumped
exitCtx.Cause = sharedconsts.ExitCoreDumped
exitCtx.Code = uint32(waitStatus.Signal())
} else if waitStatus.Signaled() {
exitCtx.Cause = model.ExitSignaled
exitCtx.Cause = sharedconsts.ExitSignaled
exitCtx.Code = uint32(waitStatus.Signal())
} else {
exitCtx.Code = uint32(waitStatus.Signal())
Expand Down
24 changes: 12 additions & 12 deletions pkg/security/ptracer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
usergrouputils "github.com/DataDog/datadog-agent/pkg/security/common/usergrouputils"
"github.com/DataDog/datadog-agent/pkg/security/proto/ebpfless"
"github.com/DataDog/datadog-agent/pkg/security/secl/containerutils"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/util/safeelf"
)

Expand Down Expand Up @@ -280,13 +280,13 @@ func getPidTTY(pid int) string {

func truncateArgs(list []string) ([]string, bool) {
truncated := false
if len(list) > model.MaxArgsEnvsSize {
list = list[:model.MaxArgsEnvsSize]
if len(list) > sharedconsts.MaxArgsEnvsSize {
list = list[:sharedconsts.MaxArgsEnvsSize]
truncated = true
}
for i, l := range list {
if len(l) > model.MaxArgEnvSize {
list[i] = l[:model.MaxArgEnvSize-4] + "..."
if len(l) > sharedconsts.MaxArgEnvSize {
list[i] = l[:sharedconsts.MaxArgEnvSize-4] + "..."
truncated = true
}
}
Expand Down Expand Up @@ -350,8 +350,8 @@ func truncateEnvs(it StringIterator) ([]string, bool) {
if len(text) > 0 {
envCounter++
if matchesOnePrefix(text, priorityEnvsPrefixes) {
if len(text) > model.MaxArgEnvSize {
text = text[:model.MaxArgEnvSize-4] + "..."
if len(text) > sharedconsts.MaxArgEnvSize {
text = text[:sharedconsts.MaxArgEnvSize-4] + "..."
truncated = true
}
priorityEnvs = append(priorityEnvs, text)
Expand All @@ -361,25 +361,25 @@ func truncateEnvs(it StringIterator) ([]string, bool) {

it.Reset()

if envCounter > model.MaxArgsEnvsSize {
envCounter = model.MaxArgsEnvsSize
if envCounter > sharedconsts.MaxArgsEnvsSize {
envCounter = sharedconsts.MaxArgsEnvsSize
}

// second pass collecting
envs := make([]string, 0, envCounter)
envs = append(envs, priorityEnvs...)

for it.Next() {
if len(envs) >= model.MaxArgsEnvsSize {
if len(envs) >= sharedconsts.MaxArgsEnvsSize {
return envs, true
}

text := it.Text()
if len(text) > 0 {
// if it matches one prefix, it's already in the envs through priority envs
if !matchesOnePrefix(text, priorityEnvsPrefixes) {
if len(text) > model.MaxArgEnvSize {
text = text[:model.MaxArgEnvSize-4] + "..."
if len(text) > sharedconsts.MaxArgEnvSize {
text = text[:sharedconsts.MaxArgEnvSize-4] + "..."
truncated = true
}
envs = append(envs, text)
Expand Down
4 changes: 2 additions & 2 deletions pkg/security/resolvers/envvars/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package envvars

import (
"github.com/DataDog/datadog-agent/pkg/security/probe/config"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/security/utils"
)

Expand Down Expand Up @@ -42,5 +42,5 @@ func (r *Resolver) ResolveEnvVars(pid uint32) ([]string, bool, error) {
// communicate the fact that it was truncated
return nil, true, nil
}
return utils.EnvVars(r.priorityEnvs, pid, model.MaxArgsEnvsSize)
return utils.EnvVars(r.priorityEnvs, pid, sharedconsts.MaxArgsEnvsSize)
}
3 changes: 2 additions & 1 deletion pkg/security/resolvers/process/resolver_ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
spath "github.com/DataDog/datadog-agent/pkg/security/resolvers/path"
"github.com/DataDog/datadog-agent/pkg/security/resolvers/usergroup"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/security/seclog"
"github.com/DataDog/datadog-agent/pkg/security/utils"
stime "github.com/DataDog/datadog-agent/pkg/util/ktime"
Expand Down Expand Up @@ -253,7 +254,7 @@ var argsEnvsInterner = utils.NewLRUStringInterner(argsEnvsValueCacheSize)
func parseStringArray(data []byte) ([]string, bool) {
truncated := false
values, err := model.UnmarshalStringArray(data)
if err != nil || len(data) == model.MaxArgEnvSize {
if err != nil || len(data) == sharedconsts.MaxArgEnvSize {
if len(values) > 0 {
values[len(values)-1] += "..."
}
Expand Down
9 changes: 2 additions & 7 deletions pkg/security/secl/model/args_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ package model
import (
"slices"
"strings"
)

const (
// MaxArgEnvSize maximum size of one argument or environment variable
MaxArgEnvSize = 256
// MaxArgsEnvsSize maximum number of args and/or envs
MaxArgsEnvsSize = 256
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
)

// ArgsEnvs raw value for args and envs
type ArgsEnvs struct {
ID uint64
Size uint32
ValuesRaw [MaxArgEnvSize]byte
ValuesRaw [sharedconsts.MaxArgEnvSize]byte
}

// ArgsEntry defines a args cache entry
Expand Down
27 changes: 5 additions & 22 deletions pkg/security/secl/model/consts_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"

"github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/usersession"
)

Expand Down Expand Up @@ -320,10 +321,10 @@ var (
}

// exitCauseConstants is the list of supported Exit causes
exitCauseConstants = map[string]ExitCause{
"EXITED": ExitExited,
"COREDUMPED": ExitCoreDumped,
"SIGNALED": ExitSignaled,
exitCauseConstants = map[string]sharedconsts.ExitCause{
"EXITED": sharedconsts.ExitExited,
"COREDUMPED": sharedconsts.ExitCoreDumped,
"SIGNALED": sharedconsts.ExitSignaled,
}

tlsVersionContants = map[string]uint16{
Expand All @@ -342,7 +343,6 @@ var (
l3ProtocolStrings = map[L3Protocol]string{}
l4ProtocolStrings = map[L4Protocol]string{}
addressFamilyStrings = map[uint16]string{}
exitCauseStrings = map[ExitCause]string{}
tlsVersionStrings = map[uint16]string{}
)

Expand Down Expand Up @@ -423,7 +423,6 @@ func initAddressFamilyConstants() {
func initExitCauseConstants() {
for k, v := range exitCauseConstants {
seclConstants[k] = &eval.IntEvaluator{Value: int(v)}
exitCauseStrings[v] = k
}
}

Expand Down Expand Up @@ -780,19 +779,3 @@ const (
// IPProtoRAW Raw IP packets
IPProtoRAW L4Protocol = 255
)

// ExitCause represents the cause of a process termination
type ExitCause uint32

func (cause ExitCause) String() string {
return exitCauseStrings[cause]
}

const (
// ExitExited Process exited normally
ExitExited ExitCause = iota
// ExitCoreDumped Process was terminated with a coredump signal
ExitCoreDumped
// ExitSignaled Process was terminated with a signal other than a coredump
ExitSignaled
)
14 changes: 14 additions & 0 deletions pkg/security/secl/model/sharedconsts/argsenvs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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.

// Package sharedconsts holds model related shared constants
package sharedconsts

const (
// MaxArgEnvSize maximum size of one argument or environment variable
MaxArgEnvSize = 256
// MaxArgsEnvsSize maximum number of args and/or envs
MaxArgsEnvsSize = 256
)
32 changes: 32 additions & 0 deletions pkg/security/secl/model/sharedconsts/exitcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

// Package sharedconsts holds model related shared constants
package sharedconsts

// ExitCause represents the cause of a process termination
type ExitCause uint32

func (cause ExitCause) String() string {
switch cause {
case ExitExited:
return "EXITED"
case ExitCoreDumped:
return "COREDUMPED"
case ExitSignaled:
return "SIGNALED"
default:
return "UNKNOWN"
}
}

const (
// ExitExited Process exited normally
ExitExited ExitCause = iota
// ExitCoreDumped Process was terminated with a coredump signal
ExitCoreDumped
// ExitSignaled Process was terminated with a signal other than a coredump
ExitSignaled
)
11 changes: 6 additions & 5 deletions pkg/security/secl/model/unmarshallers_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/DataDog/datadog-agent/pkg/security/secl/compiler/eval"
"github.com/DataDog/datadog-agent/pkg/security/secl/containerutils"
"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
)

func validateReadSize(size, read int) (int, error) {
Expand Down Expand Up @@ -316,14 +317,14 @@ func (e *ExitEvent) UnmarshalBinary(data []byte) (int, error) {

exitStatus := binary.NativeEndian.Uint32(data[0:4])
if exitStatus&0x7F == 0x00 { // process terminated normally
e.Cause = uint32(ExitExited)
e.Cause = uint32(sharedconsts.ExitExited)
e.Code = (exitStatus >> 8) & 0xFF
} else if exitStatus&0x7F != 0x7F { // process terminated because of a signal
if exitStatus&0x80 == 0x80 { // coredump signal
e.Cause = uint32(ExitCoreDumped)
e.Cause = uint32(sharedconsts.ExitCoreDumped)
e.Code = exitStatus & 0x7F
} else { // other signals
e.Cause = uint32(ExitSignaled)
e.Cause = uint32(sharedconsts.ExitSignaled)
e.Code = exitStatus & 0x7F
}
}
Expand Down Expand Up @@ -352,8 +353,8 @@ func (e *ArgsEnvsEvent) UnmarshalBinary(data []byte) (int, error) {

e.ID = binary.NativeEndian.Uint64(data[0:8])
e.Size = binary.NativeEndian.Uint32(data[8:12])
if e.Size > MaxArgEnvSize {
e.Size = MaxArgEnvSize
if e.Size > sharedconsts.MaxArgEnvSize {
e.Size = sharedconsts.MaxArgEnvSize
}

argsEnvSize := int(e.Size)
Expand Down
9 changes: 2 additions & 7 deletions pkg/security/seclwin/model/args_envs.go

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

Loading

0 comments on commit af0d8cb

Please sign in to comment.