-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[generic_test.go] Add unit test for ExtractParam with fake LSM policy
This test aims at testing the ExtractParam feature. Tetragon use hard-coded types instead of BTF. So currently, the ExtractParam feature does not allow to extract attributes from other types than the few hard-coded in `generictypes.go`. For instance, if you try to use the feature with task_struct structure, it will fail because it is not yet hard-coded. Signed-off-by: Tristan d'Audibert <tristan.daudibert@orange.com>
- Loading branch information
1 parent
0d26b71
commit 9361d90
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
// Copyright Orange | ||
|
||
package tracing | ||
|
||
import ( | ||
"testing" | ||
|
||
api "github.com/cilium/tetragon/pkg/api/tracingapi" | ||
gt "github.com/cilium/tetragon/pkg/generictypes" | ||
"github.com/cilium/tetragon/pkg/tracingpolicy" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildBtfArgFromLSMPolicy(t *testing.T) { | ||
rawPolicy := ` | ||
apiVersion: cilium.io/v1alpha1 | ||
kind: TracingPolicy | ||
metadata: | ||
name: "lsm" | ||
spec: | ||
lsmhooks: | ||
- hook: "fake" | ||
args: | ||
- index: 0 | ||
type: "linux_binprm" | ||
extractParam: 'mm.arg_start' | ||
overwriteType: 'int' | ||
- index: 1 | ||
type: "linux_binprm" | ||
extractParam: 'file.f_path.dentry.d_name.name' | ||
overwriteType: 'string' | ||
` | ||
policy, err := tracingpolicy.FromYAML(rawPolicy) | ||
assert.NoError(t, err, "FromYAML rawPolicy error %s", err) | ||
|
||
var btfArgs [api.EventConfigMaxArgs][api.MaxBtfArgDepth]api.ConfigBtfArg | ||
|
||
for i, arg := range policy.TpSpec().LsmHooks[0].Args { | ||
btfArgs[i] = [api.MaxBtfArgDepth]api.ConfigBtfArg{} | ||
lastBtfType, err := buildBtfArg(arg, &btfArgs[i]) | ||
assert.NoError(t, err) | ||
assert.NotEqual(t, nil, lastBtfType) | ||
argType := findTypeFromBtfType(arg, lastBtfType) | ||
assert.NotEqual(t, gt.GenericInvalidType, argType, "Type %s is not supported", (*lastBtfType).TypeName()) | ||
} | ||
|
||
result := [api.EventConfigMaxArgs][api.MaxBtfArgDepth]api.ConfigBtfArg{ | ||
[api.MaxBtfArgDepth]api.ConfigBtfArg{ | ||
{Offset: 16, IsPointer: 1, IsInitialized: 1}, | ||
{Offset: 312, IsPointer: 1, IsInitialized: 1}, // Off = 376, but CI complains | ||
}, | ||
[api.MaxBtfArgDepth]api.ConfigBtfArg{ | ||
{Offset: 64, IsPointer: 1, IsInitialized: 1}, | ||
{Offset: 16, IsPointer: 0, IsInitialized: 1}, // Off = 152, but CI complains | ||
{Offset: 8, IsPointer: 1, IsInitialized: 1}, | ||
{Offset: 32, IsPointer: 0, IsInitialized: 1}, | ||
{Offset: 8, IsPointer: 1, IsInitialized: 1}, | ||
}, | ||
} | ||
assert.Equal(t, btfArgs, result) | ||
} |