Skip to content

Commit

Permalink
Fix process name
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Dec 10, 2024
1 parent c7a57c2 commit 29371a1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/agent/daemon/state/events_pipeline.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state

import (
"bytes"
"context"
"encoding/json"
"net/netip"
Expand All @@ -10,6 +9,7 @@ import (
castpb "github.com/castai/kvisor/api/v1/runtime"
"github.com/castai/kvisor/cmd/agent/daemon/enrichment"
"github.com/castai/kvisor/cmd/agent/daemon/metrics"
"github.com/castai/kvisor/pkg/ebpftracer/decoder"
ebpftypes "github.com/castai/kvisor/pkg/ebpftracer/types"
"github.com/cespare/xxhash/v2"
"github.com/elastic/go-freelru"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (c *Controller) toProtoEvent(e *ebpftypes.Event) *castpb.Event {
event := &castpb.Event{
EventType: 0,
Timestamp: e.Context.Ts,
ProcessName: string(bytes.TrimRight(e.Context.Comm[:], "\x00")),
ProcessName: decoder.ProcessNameString(e.Context.Comm[:]),
Namespace: e.Container.PodNamespace,
PodUid: e.Container.PodUID,
PodName: e.Container.PodName,
Expand Down
10 changes: 10 additions & 0 deletions pkg/ebpftracer/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,16 @@ func (decoder *Decoder) ReadProtoDNS() (*types.ProtoDNS, error) {
return ToProtoDNS(&details, dnsPacketParser), nil
}

// ProcessNameString converts raw process name to readable string.
// Since it's a C-like string it can contain NUL byte.
func ProcessNameString(raw []byte) string {
nulByteIndex := bytes.IndexByte(raw[:], 0)
if nulByteIndex == -1 {
return string(raw)
}
return string(raw[:nulByteIndex])
}

func ToProtoDNS(details *packet.PacketDetails, dnsPacketParser *layers.DNS) *castpb.DNS {
pbDNS := &castpb.DNS{
Answers: make([]*castpb.DNSAnswers, len(dnsPacketParser.Answers)),
Expand Down
24 changes: 24 additions & 0 deletions pkg/ebpftracer/decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,30 @@ func TestDecodeContext(t *testing.T) {
}, eventCtx)
}

func TestProcessNameString(t *testing.T) {
tests := []struct {
name string
value []byte
expected string
}{
{
name: "no null terminator",
value: []byte("curl"),
expected: "curl",
},
{
name: "truncate at first null terminator",
value: []byte{116, 101, 115, 116, 0, 120, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
expected: "test",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expected, ProcessNameString(test.value))
})
}
}

var sshRequestData = []byte{
// Payload size
0x5e, 0x00, 0x00, 0x00,
Expand Down
4 changes: 2 additions & 2 deletions pkg/ebpftracer/signature/signature.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package signature

import (
"bytes"
"context"
"time"

castpb "github.com/castai/kvisor/api/v1/runtime"
"github.com/castai/kvisor/pkg/ebpftracer/decoder"
"github.com/castai/kvisor/pkg/ebpftracer/events"
"github.com/castai/kvisor/pkg/ebpftracer/types"
"github.com/castai/kvisor/pkg/logging"
Expand Down Expand Up @@ -127,7 +127,7 @@ func (e *SignatureEngine) handleEvent(event *types.Event) {
e.eventsChan <- &castpb.Event{
EventType: castpb.EventType_EVENT_SIGNATURE,
Timestamp: uint64(time.Now().UTC().UnixNano()), // nolint:gosec
ProcessName: string(bytes.Trim(event.Context.Comm[:], "\x00")),
ProcessName: decoder.ProcessNameString(event.Context.Comm[:]),
Namespace: event.Container.PodNamespace,
PodName: event.Container.PodName,
ContainerName: event.Container.Name,
Expand Down
3 changes: 2 additions & 1 deletion pkg/ebpftracer/tracer_playground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/castai/kvisor/pkg/cgroup"
"github.com/castai/kvisor/pkg/containers"
"github.com/castai/kvisor/pkg/ebpftracer"
"github.com/castai/kvisor/pkg/ebpftracer/decoder"
"github.com/castai/kvisor/pkg/ebpftracer/events"
"github.com/castai/kvisor/pkg/ebpftracer/signature"
"github.com/castai/kvisor/pkg/ebpftracer/types"
Expand Down Expand Up @@ -278,7 +279,7 @@ var ingoredProcesses = map[string]struct{}{

func printEvent(tr *ebpftracer.Tracer, e *types.Event) {
eventName := tr.GetEventName(e.Context.EventID)
procName := string(bytes.TrimRight(e.Context.Comm[:], "\x00"))
procName := decoder.ProcessNameString(e.Context.Comm[:])
if _, ignored := ingoredProcesses[procName]; ignored {
return
}
Expand Down

0 comments on commit 29371a1

Please sign in to comment.