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

LSM sensor #2566

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

LSM sensor #2566

wants to merge 3 commits into from

Conversation

anfedotoff
Copy link
Contributor

@anfedotoff anfedotoff commented Jun 16, 2024

LSM sensor support allows to use LSM BPF programs the way we use BPF programs for kprobes/tracepoints/uprobes.

TracingPolicy example:

apiVersion: cilium.io/v1alpha1
kind: TracingPolicy
metadata:
  name: "lsm"
spec:
  lsmhooks:
  - hook: "file_open"
    args:
      - index: 0
        type: "file"
    selectors:
    - matchBinaries:
      - operator: "In"
        values:
        - "/usr/bin/cat"

Event example:

{                                                                                                     
  "process_lsm": {                                                                                    
    "process": {                                                                                      
      "exec_id": "dXNlci1uaXg6MTk5MzE1NDk0NTk3MzM6MzIwMTA4",                                  
      "pid": 320108,                                                                                  
      "uid": 1000,                                                                                    
      "cwd": "/home/user/go/src/github.com/cilium/tetragon",                                    
      "binary": "/usr/bin/cat",                                                                       
      "arguments": "/etc/passwd",                                                                     
      "flags": "execve clone",                                                                        
      "start_time": "2024-06-15T18:03:29.742161520Z",                                                                                                                                                       
      "auid": 1000,                                                                                   
      "parent_exec_id": "dXNlci1uaXg6ODg1NjMwMDAwMDAwMDoxNDc4MTI=",                                                                                                                                 
      "refcnt": 1,                                                                                    
      "tid": 320108,                                                                                  
      "user": {                                                                                       
        "name": "user"                                                                          
      }                                                                                               
    },                                                                                                
    "parent": {                                                                                       
      "exec_id": "dXNlci1uaXg6ODg1NjMwMDAwMDAwMDoxNDc4MTI=",                                  
      "pid": 147812,                                                                                  
      "uid": 1000,                                                                                    
      "cwd": "/home/user/go/src/github.com/cilium/tetragon",                                                                                                                                          
      "binary": "/usr/bin/zsh",                                                                                                                                                                             
      "flags": "procFS auid",                                                                         
      "start_time": "2024-06-15T14:37:33.597296165Z",
      "auid": 1000,
      "parent_exec_id": "dXNlci1uaXg6MTM1ODA0MDAwMDAwMDozMTQ2",
      "tid": 147812
    },
    "function_name": "file_open",
    "policy_name": "lsm",
    "args": [
      {
        "file_arg": {
          "path":"/etc/passwd",
          "permission":"-rw-r--r--"
        }
      }
    ],
    "action": "KPROBE_ACTION_POST"
  },
  "node_name": "user-nix",
  "time": "2024-06-15T18:03:29.743030933Z"
}

This is also necessary for #2409.

@anfedotoff anfedotoff requested a review from a team as a code owner June 16, 2024 09:07
@anfedotoff anfedotoff requested a review from kkourt June 16, 2024 09:07
Copy link

netlify bot commented Jun 16, 2024

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit e0f9144
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/6686edcd56030e00097020d9
😎 Deploy Preview https://deploy-preview-2566--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.

@anfedotoff anfedotoff marked this pull request as draft June 16, 2024 09:09
@anfedotoff
Copy link
Contributor Author

There are things to do:

  • Fix tests
  • Add lsm test
  • Decide minimal kernel version for LSM sensor support (LSM BPF is needed 5.7 kernel).
  • Fix some bugs in code and make it more neat.

I managed to load LSM BPF programs, tail calls also work for LSM programs! The most terrifying problems are solved, I think. I managed to catch some Lsm events).

@anfedotoff anfedotoff force-pushed the lsm-sensor branch 7 times, most recently from a9f28bf to 1f5d707 Compare June 26, 2024 12:50
@anfedotoff
Copy link
Contributor Author

anfedotoff commented Jun 26, 2024

For now I managed to fix problem with args resolving. LSM BPF programs get args the way raw tracepoint programs do. So, LSM events now are looking good. It would be nice to start code review.

Some open questions:

  • ./verify/verify.sh . I made a hack for loading generic LSM programs. But I need to install llvm-objcopy in CI
  • Tests. I think we need some tests. Maybe more than one. I'll work on it. Maybe we discuss what kind of tests it's good to have?
  • Enforce mode. If LSM BPF returns the value that is not qual to zero, than operation is not permitted. Maybe we should add an action for that? For example, if policy is violated just return -EPERM, instead of sending SIGKILL.

@kkourt, @olsajiri, @mtardy please could you have a look?

@anfedotoff anfedotoff marked this pull request as ready for review June 26, 2024 13:11
@anfedotoff anfedotoff requested a review from mtardy as a code owner June 26, 2024 13:11
@anfedotoff anfedotoff force-pushed the lsm-sensor branch 2 times, most recently from f17edbc to 03cb7db Compare June 26, 2024 13:48
@anfedotoff anfedotoff changed the title wip: LSM sensor LSM sensor Jun 26, 2024
@anfedotoff anfedotoff force-pushed the lsm-sensor branch 6 times, most recently from 0127073 to 1cc6efb Compare July 2, 2024 11:56
@kkourt kkourt added the release-note/major This PR introduces major new functionality label Jul 3, 2024
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Overall, this looks good to me! I've left some minor comments.

./verify/verify.sh . I made a hack for loading generic LSM programs. But I need to install llvm-objcopy in CI

Looks good to me. Yap, let's install it. I'm guessing it should be part of our clang container so we can just use that.

Tests. I think we need some tests. Maybe more than one. I'll work on it. Maybe we discuss what kind of tests it's good to have?

That's a good question. I would say start simple, and have one or two tests for the basic functionality (observability and enforcement)

Enforce mode. If LSM BPF returns the value that is not qual to zero, than operation > is not permitted. Maybe we should add an action for that? For example, if policy is > violated just return -EPERM, instead of sending SIGKILL.

Yap, I think that makes sense. We also have an override action that does something similar, but having a separate action for lsm (as you do in your PR) makes sense to me.

bpf/Makefile Show resolved Hide resolved
pkg/sensors/tracing/genericlsm.go Outdated Show resolved Hide resolved
progs = append(progs, load)

fdinstall := program.MapBuilderPin("fdinstall_map", sensors.PathJoin(sensorPath, "fdinstall_map"), load)
maps = append(maps, fdinstall)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been nice to use the new interface, but it's not clear when it's going to be ready: #2128.

CC: @olsajiri

pkg/sensors/tracing/genericlsm.go Show resolved Hide resolved
pkg/sensors/tracing/lsm_test.go Outdated Show resolved Hide resolved
@kkourt kkourt requested a review from olsajiri July 3, 2024 10:48
@kkourt
Copy link
Contributor

kkourt commented Jul 3, 2024

@olsajiri I added you to the reviewers. It would be great if you could have a look when you get a chance. Thanks!

Signed-off-by: Andrey Fedotov <anfedotoff@yandex-team.ru>
Signed-off-by: Andrey Fedotov <anfedotoff@yandex-team.ru>
Signed-off-by: Andrey Fedotov <anfedotoff@yandex-team.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/major This PR introduces major new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants