tetragon icon indicating copy to clipboard operation
tetragon copied to clipboard

tetragon: extract struct dentry member using CO:RE

Open dwindsor opened this issue 1 year ago • 23 comments

Thanks for contributing! Please ensure your pull request adheres to the following guidelines:

  • [ ] All commits contain a well written commit message and are signed-off (see Submitting a pull request).
  • [ ] All code is covered by unit and/or end-to-end tests where feasible.
  • [ ] All generated files are updated if needed (see Making changes).
  • [ ] Provide a title or release-note blurb suitable for the release notes (see guidelines).
  • [ ] Update documentation and write an upgrade note if needed (see guidelines).
  • [ ] Are you a user of Tetragon? Please add yourself to the Users doc in the Cilium repository.

Fixes: #2573

<!-- Enter the release note text here if needed or remove this section! -->

dwindsor avatar Jun 17 '24 16:06 dwindsor

Deploy Preview for tetragon ready!

Name Link
Latest commit 6e29ad2b1e115ed112ff8bfe2d87daf7b1c626e5
Latest deploy log https://app.netlify.com/sites/tetragon/deploys/66992639e725e50008a84182
Deploy Preview https://deploy-preview-2574--tetragon.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Jun 17 '24 16:06 netlify[bot]

Hi! I'm encountering the following error when attempting to build these changes, and am not quite sure why... 🤔

Following the same procedures as 9889f27, I'm getting this error:

shell time="2024-06-17T12:16:50-04:00" level=fatal msg="Failed to start tetragon" error="failed loading tracing policy file \"/home/dave/src/github.com/dwindsor/tetragon/examples/tracingpolicy/security_inode_follow_link.yaml\": validation failed: \"follow-symlink\": validation failure list:\nspec.kprobes[0].args[0].type in body should be one of [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 kernel_cap_t cap_inheritable cap_permitted cap_effective linux_binprm data_loc net_device]"

Using the following Tracing Policy:

apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
  name: "follow-symlink"
spec:
  kprobes:
  - call: "security_inode_follow_link"
    syscall: false
    args:
    - index: 0
      type: "dentry"
    returnArg:
      index: 0
      type: "int"
    selectors:
    - matchArgs:
      - index: 0
        operator: "Equal"
        values:
        - "/tmp/softlink"

It appears the procedure has changed at least slightly since 9889f27, as 577a2ac introduced some changes.

i've tried following the same procedure as 9268fd0, but haven't had luck so far. I'll keep trying but wanted to see if anyone had any pointers?

cc: @kevsecurity @mtardy @jrfastab

dwindsor avatar Jun 17 '24 16:06 dwindsor

I forgot to run make crds; not sure if that target was around last time?

Either way, above errors are fixed =)

dwindsor avatar Jun 17 '24 17:06 dwindsor

I forgot to run make crds; not sure if that target was around last time?

It was renamed from make generate to make crds recently: https://github.com/cilium/tetragon/commit/c26e483f7a3901eb3c5bc86671ba46f49c4b2481

Either way, above errors are fixed =)

Thanks! I plan to review this tomorrow.

Could you also fix the static check failures? Running make check locally should perform the same tests. This is not required for the review, but it's something that needs to be fixed before merging.

kkourt avatar Jun 20 '24 14:06 kkourt

Thanks for the review @kkourt! I've responded to your comments and fixed the formatting issues, but I'm having trouble getting the test to work.

Running the test program on my system, I see the relevant event being produced in tetra:

{
  "process_kprobe": {
    "process": {
      "exec_id": "bGF0ZXJhbHVzOjI0ODQ0MzUwNjU1MzM4OjMyNjQ2MQ==",
      "pid": 326461,
      "uid": 1000,
      "cwd": "/home/dave/src/github.com/dwindsor/tetragon/contrib/tester-progs",
      "binary": "/home/dave/src/github.com/dwindsor/tetragon/contrib/tester-progs/symlink-tester",
      "flags": "execve clone",
      "start_time": "2024-06-27T21:39:16.896790982Z",
      "auid": 1000,
      "parent_exec_id": "bGF0ZXJhbHVzOjU2NTkyMTAwMDAwMDA6ODUyNjE=",
      "refcnt": 1,
      "tid": 326461
    },
    "parent": {
      "exec_id": "bGF0ZXJhbHVzOjU2NTkyMTAwMDAwMDA6ODUyNjE=",
      "pid": 85261,
      "uid": 1000,
      "cwd": "/tmp",
      "binary": "/usr/bin/bash",
      "flags": "procFS auid",
      "start_time": "2024-06-27T16:19:31.756135483Z",
      "auid": 1000,
      "parent_exec_id": "bGF0ZXJhbHVzOjU2NTkyMDAwMDAwMDA6ODUyNjA=",
      "tid": 85261
    },
    "function_name": "security_inode_follow_link",
    "args": [
      {
        "dentry_arg": {
          "name": "/tmp/id"
        }
      }
    ],
    "action": "KPROBE_ACTION_POST",
    "policy_name": "path-traversal-block",
    "return_action": "KPROBE_ACTION_POST"
  },
  "node_name": "lateralus",
  "time": "2024-06-27T21:39:16.897100809Z"
}

dwindsor avatar Jun 27 '24 21:06 dwindsor

Hi,

Thanks for the review @kkourt! I've responded to your comments and fixed the formatting issues, but I'm having trouble getting the test to work.

Thanks.

I think the issue might be that you are killing the test program before it has a chance to do the operation. I've tried the test locally, and this seems to work for me:

diff --git a/pkg/sensors/tracing/kprobe_test.go b/pkg/sensors/tracing/kprobe_test.go                                                                                                                                                                                                                                           
index db8a53f0b..55fcbbaa6 100644                                                                                                                                                                                                                                                                                              
--- a/pkg/sensors/tracing/kprobe_test.go                                                                                                                                                                                                                                                                                       
+++ b/pkg/sensors/tracing/kprobe_test.go                                                                                                                                                                                                                                                                                       
@@ -6049,7 +6049,6 @@ func TestDentryExtractPath(t *testing.T) {                                                                                                                                                                                                                                                               
        ops := func() {                                                                                                                                                                                                                                                                                                        
                err = command.Start()                                                                                                                                                                                                                                                                                          
                assert.NoError(t, err)                                                                                                                                                                                                                                                                                         
-               defer command.Process.Kill()                                                                                                                                                                                                                                                                                   
        }                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                               
        events := perfring.RunTestEvents(t, ctx, ops)         
$ sudo $(which go) test ./pkg/sensors/tracing -bpf-lib $(pwd)/bpf/objs -test.run TestDentryExtractPath                                                                                                                                                                       
ok      github.com/cilium/tetragon/pkg/sensors/tracing  1.325s                                                                                                                                                                                                                                                                     

Could you also rebase to latest main? I believe it should address some of the e2e test failures.

kkourt avatar Jul 03 '24 08:07 kkourt

could you please split the change into separate logical parts? like at least bpf bits. tests, schema changes.. anything that makes sense to split, thnx

olsajiri avatar Jul 10 '24 07:07 olsajiri

Hi @kkourt, thanks for the pointers! Your suggestion worked.

@olsajiri I've split the PR into logical patches. Some CI is failing now but for unrelated reasons.

Thanks!

dwindsor avatar Jul 18 '24 15:07 dwindsor

Hi @kkourt, thanks for the pointers! Your suggestion worked.

@olsajiri I've split the PR into logical patches. Some CI is failing now but for unrelated reasons.

Thanks, can you also rebase? There seem to be conflicts. The conflicts seem to be in the auto-generated files, so it should be easy to resolve.

kkourt avatar Jul 22 '24 07:07 kkourt

Hi @kkourt, thanks for the pointers! Your suggestion worked. @olsajiri I've split the PR into logical patches. Some CI is failing now but for unrelated reasons.

Thanks, can you also rebase? There seem to be conflicts. The conflicts seem to be in the auto-generated files, so it should be easy to resolve.

Thanks! I've tried running make crds, but it shows no differences to push

dwindsor avatar Jul 25 '24 15:07 dwindsor

Hi @kkourt, thanks for the pointers! Your suggestion worked. @olsajiri I've split the PR into logical patches. Some CI is failing now but for unrelated reasons.

Thanks, can you also rebase? There seem to be conflicts. The conflicts seem to be in the auto-generated files, so it should be easy to resolve.

Thanks! I've tried running make crds, but it shows no differences to push

Have you tried running make protogen as well? The conflicts seem to be related to protobuf definitions.

kkourt avatar Jul 26 '24 09:07 kkourt

Moving this to draft since there are many conflicts.

kkourt avatar Oct 07 '24 07:10 kkourt

Any progress on this?

bentekkie avatar Jan 29 '25 16:01 bentekkie

Any progress on this?

I believe what this PR is trying to do can be achieved via https://github.com/cilium/tetragon/pull/3143, which is a more generic approach.

kkourt avatar Jan 29 '25 16:01 kkourt

Rebasing and running make generate and make codegen should be all that is needed, I made an updated pr here https://github.com/cilium/tetragon/pull/3353

bentekkie avatar Jan 29 '25 17:01 bentekkie

Any progress on this?

I believe what this PR is trying to do can be achieved via #3143, which is a more generic approach.

Oh nice, will that go in soon?

bentekkie avatar Jan 29 '25 17:01 bentekkie

Oh nice, will that go in soon?

probably, it looks good

olsajiri avatar Feb 03 '25 09:02 olsajiri

Any progress on this?

I believe what this PR is trying to do can be achieved via #3143, which is a more generic approach.

use resolve: "d_name.name" to get dentry? but how to get the full path? like /tmp/id,use d_name.name, only get id.

panzhenyu12 avatar Feb 17 '25 08:02 panzhenyu12

use resolve: "d_name.name" to get dentry? but how to get the full path? like /tmp/id,use d_name.name, only get id.

hum, could you please share your spec? at least which hook are you attached to? thanks

olsajiri avatar Feb 17 '25 13:02 olsajiri

use resolve: "d_name.name" to get dentry? but how to get the full path? like /tmp/id,use d_name.name, only get id.

hum, could you please share your spec? at least which hook are you attached to? thanks

kind: TracingPolicy
metadata:
  name: "file-tamper-mknod"
spec:
  kprobes:
  - call: "security_path_mknod"
    syscall: false
    return: true
    args:
    - index: 0
      type: "path"
    - index: 1
      type: "string"
      resolve: "d_name.name"
    returnArg:
      index: 0
      type: "int"

my TracingPolicy like this, use d_name.name or d_iname only retrieve the current part of the path, not the full path.

The result is as shown in the picture: 截屏2025-02-18 09 12 18

panzhenyu12 avatar Feb 18 '25 01:02 panzhenyu12

use resolve: "d_name.name" to get dentry? but how to get the full path? like /tmp/id,use d_name.name, only get id.

hum, could you please share your spec? at least which hook are you attached to? thanks

kind: TracingPolicy
metadata:
  name: "file-tamper-mknod"
spec:
  kprobes:
  - call: "security_path_mknod"
    syscall: false
    return: true
    args:
    - index: 0
      type: "path"
    - index: 1
      type: "string"
      resolve: "d_name.name"
    returnArg:
      index: 0
      type: "int"

my TracingPolicy like this, use d_name.name or d_iname only retrieve the current part of the path, not the full path.

ok, for that we'd need the 'dentry' type ... you have path and dentry arguments that give the full path, but if you want to get full path from the dentry argument alone I think we need the dentry type .. we can revive this PR

olsajiri avatar Feb 18 '25 08:02 olsajiri

use resolve: "d_name.name" to get dentry? but how to get the full path? like /tmp/id,use d_name.name, only get id.

hum, could you please share your spec? at least which hook are you attached to? thanks

kind: TracingPolicy
metadata:
  name: "file-tamper-mknod"
spec:
  kprobes:
  - call: "security_path_mknod"
    syscall: false
    return: true
    args:
    - index: 0
      type: "path"
    - index: 1
      type: "string"
      resolve: "d_name.name"
    returnArg:
      index: 0
      type: "int"

my TracingPolicy like this, use d_name.name or d_iname only retrieve the current part of the path, not the full path.

ok, for that we'd need the 'dentry' type ... you have path and dentry arguments that give the full path, but if you want to get full path from the dentry argument alone I think we need the dentry type .. we can revive this PR

Any progress on this? Is there anything I can help with? I'd be happy to.

panzhenyu12 avatar Feb 20 '25 09:02 panzhenyu12

ok, for that we'd need the 'dentry' type ... you have path and dentry arguments that give the full path, but if you want to get full path from the dentry argument alone I think we need the dentry type .. we can revive this PR

Any progress on this? Is there anything I can help with? I'd be happy to.

I refreshed that and have draft version in here https://github.com/cilium/tetragon/pull/3423, it does not support walking through the different mounts ATM, need to figure that out, hopfuly this week

olsajiri avatar Feb 23 '25 12:02 olsajiri