Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tetragon: Add kprobe args test #2005

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bpf/process/types/basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ enum {

syscall64_type = 28,

s16_ty = 29,
u16_ty = 30,
s8_ty = 31,
u8_ty = 32,

nop_s64_ty = -10,
nop_u64_ty = -11,
nop_u32_ty = -12,
Expand Down Expand Up @@ -2451,6 +2456,10 @@ read_call_arg(void *ctx, struct msg_generic_kprobe *e, int index, int type,
case int_type:
case s32_ty:
case u32_ty:
case s16_ty:
case u16_ty:
case s8_ty:
case u8_ty:
probe_read(args, sizeof(__u32), &arg);
size = sizeof(__u32);
break;
Expand Down
13 changes: 13 additions & 0 deletions pkg/generictypes/generictypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const (

GenericSyscall64 = 28

GenericS16Type = 29
GenericU16Type = 30
GenericS8Type = 31
GenericU8Type = 32

GenericNopType = -1
GenericInvalidType = -2
)
Expand Down Expand Up @@ -101,6 +106,14 @@ func GenericTypeFromString(arg string) int {
return GenericKernelModule
case "syscall64":
return GenericSyscall64
case "sint16", "int16":
return GenericS16Type
case "uint16":
return GenericU16Type
case "sint8", "int8":
return GenericS8Type
case "uint8":
return GenericU8Type
default:
return GenericInvalidType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down Expand Up @@ -183,6 +187,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down Expand Up @@ -823,6 +831,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down Expand Up @@ -183,6 +187,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down Expand Up @@ -823,6 +831,10 @@ spec:
enum:
- auto
- int
- int8
- uint8
- int16
- uint16
- uint32
- int32
- uint64
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/apis/cilium.io/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type KProbeArg struct {
// +kubebuilder:validation:Minimum=0
// Position of the argument.
Index uint32 `json:"index"`
// +kubebuilder:validation:Enum=auto;int;uint32;int32;uint64;int64;char_buf;char_iovec;size_t;skb;sock;string;fd;file;filename;path;nop;bpf_attr;perf_event;bpf_map;user_namespace;capability;kiocb;iov_iter;cred;load_info;module;syscall64;
// +kubebuilder:validation:Enum=auto;int;int8;uint8;int16;uint16;uint32;int32;uint64;int64;char_buf;char_iovec;size_t;skb;sock;string;fd;file;filename;path;nop;bpf_attr;perf_event;bpf_map;user_namespace;capability;kiocb;iov_iter;cred;load_info;module;syscall64;
// +kubebuilder:default=auto
// Argument type.
Type string `json:"type"`
Expand Down
52 changes: 52 additions & 0 deletions pkg/sensors/tracing/generickprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,58 @@ func handleMsgGenericKprobe(m *api.MsgGenericKprobe, gk *genericKprobe, r *bytes
arg.Value = output
arg.Label = a.label
unix.Args = append(unix.Args, arg)
case gt.GenericU16Type:
var output uint32
var arg api.MsgGenericKprobeArgUInt

err := binary.Read(r, binary.LittleEndian, &output)
if err != nil {
logger.GetLogger().WithError(err).Warnf("UInt type error")
}

arg.Index = uint64(a.index)
arg.Value = uint32(uint16(output))
arg.Label = a.label
unix.Args = append(unix.Args, arg)
case gt.GenericU8Type:
var output uint32
var arg api.MsgGenericKprobeArgUInt

err := binary.Read(r, binary.LittleEndian, &output)
if err != nil {
logger.GetLogger().WithError(err).Warnf("UInt type error")
}

arg.Index = uint64(a.index)
arg.Value = uint32(uint8(output))
arg.Label = a.label
unix.Args = append(unix.Args, arg)
case gt.GenericS16Type:
var output uint32
var arg api.MsgGenericKprobeArgInt

err := binary.Read(r, binary.LittleEndian, &output)
if err != nil {
logger.GetLogger().WithError(err).Warnf("Int type error")
}

arg.Index = uint64(a.index)
arg.Value = int32(int16(output))
arg.Label = a.label
unix.Args = append(unix.Args, arg)
case gt.GenericS8Type:
var output uint32
var arg api.MsgGenericKprobeArgInt

err := binary.Read(r, binary.LittleEndian, &output)
if err != nil {
logger.GetLogger().WithError(err).Warnf("Int type error")
}

arg.Index = uint64(a.index)
arg.Value = int32(int8(output))
arg.Label = a.label
unix.Args = append(unix.Args, arg)
case gt.GenericUserNamespace:
var output api.MsgGenericUserNamespace
var arg api.MsgGenericKprobeArgUserNamespace
Expand Down
195 changes: 195 additions & 0 deletions pkg/sensors/tracing/kprobe_args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package tracing

import (
"context"
"strconv"
"sync"
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
ec "github.com/cilium/tetragon/api/v1/tetragon/codegen/eventchecker"
"github.com/cilium/tetragon/pkg/ftrace"
"github.com/cilium/tetragon/pkg/jsonchecker"
lc "github.com/cilium/tetragon/pkg/matchers/listmatcher"
sm "github.com/cilium/tetragon/pkg/matchers/stringmatcher"
"github.com/cilium/tetragon/pkg/observer/observertesthelper"
tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/stretchr/testify/assert"
)

func trigger(t *testing.T) {
ins := asm.Instructions{
// Return SK_DROP
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
}

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Name: "test",
Type: ebpf.Tracing,
AttachType: ebpf.AttachTraceFEntry,
Instructions: ins,
License: "MIT",
AttachTo: "bpf_modify_return_test",
})
if err != nil {
t.Fatal(err)
}
defer prog.Close()

opts := ebpf.RunOptions{}
ret, err := prog.Run(&opts)
if err != nil {
t.Fatal(err)
}

if ret != 0 {
t.Fatalf("Expected return value to be 0, got %d", ret)
}
}

func TestKprobeArgs(t *testing.T) {
_, err := ftrace.ReadAvailFuncs("bpf_fentry_test1")
if err != nil {
t.Skip("Skipping test: could not find bpf_fentry_test1")
}

var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

ctx, cancel := context.WithTimeout(context.Background(), tus.Conf().CmdWaitTime)
defer cancel()

pidStr := strconv.Itoa(int(observertesthelper.GetMyPid()))
t.Logf("tester pid=%s\n", pidStr)

hook := `
apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
name: "sys-write"
spec:
kprobes:
- call: "bpf_fentry_test1"
syscall: false
args:
- index: 0
type: "int"
- call: "bpf_fentry_test2"
syscall: false
args:
- index: 0
type: "int"
- index: 1
type: "uint64"
- call: "bpf_fentry_test3"
syscall: false
args:
- index: 0
type: "int8"
- index: 1
type: "int"
- index: 2
type: "uint64"
- call: "bpf_fentry_test4"
syscall: false
args:
- index: 0
type: "uint64"
- index: 1
type: "int8"
- index: 2
type: "int"
- index: 3
type: "uint64"
- call: "bpf_fentry_test5"
syscall: false
args:
- index: 0
type: "uint64"
- index: 1
type: "uint64"
- index: 2
type: "int16"
- index: 3
type: "int"
- index: 4
type: "uint64"
selectors:
- matchPIDs:
- operator: In
followForks: true
isNamespacePID: false
values:
- ` + pidStr

createCrdFile(t, hook)

obs, err := observertesthelper.GetDefaultObserverWithFile(t, ctx, testConfigFile, tus.Conf().TetragonLib)
if err != nil {
t.Fatalf("GetDefaultObserverWithFile error: %s", err)
}
observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

trigger(t)

check1 := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("bpf_fentry_test1")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithOperator(lc.Ordered).
WithValues(
ec.NewKprobeArgumentChecker().WithIntArg(1),
))

check2 := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("bpf_fentry_test2")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithOperator(lc.Ordered).
WithValues(
ec.NewKprobeArgumentChecker().WithIntArg(2),
ec.NewKprobeArgumentChecker().WithSizeArg(3),
))

check3 := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("bpf_fentry_test3")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithOperator(lc.Ordered).
WithValues(
ec.NewKprobeArgumentChecker().WithIntArg(4),
ec.NewKprobeArgumentChecker().WithIntArg(5),
ec.NewKprobeArgumentChecker().WithSizeArg(6),
))

check4 := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("bpf_fentry_test4")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithOperator(lc.Ordered).
WithValues(
ec.NewKprobeArgumentChecker().WithSizeArg(7),
ec.NewKprobeArgumentChecker().WithIntArg(8),
ec.NewKprobeArgumentChecker().WithIntArg(9),
ec.NewKprobeArgumentChecker().WithSizeArg(10),
))

check5 := ec.NewProcessKprobeChecker("").
WithFunctionName(sm.Full("bpf_fentry_test5")).
WithArgs(ec.NewKprobeArgumentListMatcher().
WithOperator(lc.Ordered).
WithValues(
ec.NewKprobeArgumentChecker().WithSizeArg(11),
ec.NewKprobeArgumentChecker().WithSizeArg(12),
ec.NewKprobeArgumentChecker().WithIntArg(13),
ec.NewKprobeArgumentChecker().WithIntArg(14),
ec.NewKprobeArgumentChecker().WithSizeArg(15),
))

checker := ec.NewUnorderedEventChecker(check1, check2, check3, check4, check5)

err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}
Loading
Loading