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

introduce in_init_tree flag for process events #3209

Merged
merged 7 commits into from
Dec 11, 2024

Conversation

will-isovalent
Copy link
Contributor

@will-isovalent will-isovalent commented Dec 10, 2024

This PR introduces a new flag for process events, in_init_tree, which is propagated from BPF via the execve map. The flag indicates whether a process is a member of its container's initial process tree, or whether it was spawned externally. For instance:

  • a host process' in_init_tree value is always false.
  • a container's entrypoint or any process spawned from it has an in_init_tree value of true.
  • a process injected into the container (e.g. via docker exec or kubectl exec or nsenter) has an in_init_tree value of false

To support unit testing this feature, we also introduce a new export filter for container IDs and use it to match container IDs in the unit test. This avoids flooding the unit test output with spurious events from the host system.

Changelog

- Introduce the in_init_tree flag for process events which indicates whether a process spawned from its container's init process tree
- Introduce a container_id export filter
- Introduce an in_init_tree export filter

Define a new field in the Container message to mark whether a containerized process is in
the container's "init" process tree. In other words, this field is true if and only if the
process exists in the container's PID namespace and has a direct lineage traceable to
PID=1 in that PID namespace. This is useful for example to filter for process events that
come from a kubectl exec or an nsenter.

Signed-off-by: William Findlay <[email protected]>
@will-isovalent will-isovalent added area/bpf This is related to BPF code release-note/minor This PR introduces a minor user-visible change area/filters labels Dec 10, 2024
@will-isovalent will-isovalent requested a review from a team as a code owner December 10, 2024 19:01
@will-isovalent will-isovalent changed the title Pr/will/is init tree introduce in_init_tree flag for process events Dec 10, 2024
Copy link

netlify bot commented Dec 10, 2024

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit e9bfc4d
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/6759bf14202a5d000728e3ce
😎 Deploy Preview https://deploy-preview-3209--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.

@will-isovalent
Copy link
Contributor Author

will-isovalent commented Dec 10, 2024

Whoops, bad find and replace messed up some field names. Fixing.

@will-isovalent will-isovalent force-pushed the pr/will/is_init_tree branch 2 times, most recently from c2a9df0 to 21aa867 Compare December 10, 2024 19:53
Set the EVENT_IN_INIT_TREE on a new child process on clone if it meets the criteria. We'll
later us this to set the corresponding flag in the process's container field in userspace.

Signed-off-by: William Findlay <[email protected]>
There were two flags missing from the flags array in pkg/reader. Add them here.

Signed-off-by: William Findlay <[email protected]>
Set Process.Pod.Container.InInitTree when a process has been marked as being in its
container's init process tree on the BPF side.

Signed-off-by: William Findlay <[email protected]>
Copy link
Contributor

@michi-covalent michi-covalent left a comment

Choose a reason for hiding this comment

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

api change looks good ✅

Copy link
Member

@mtardy mtardy left a comment

Choose a reason for hiding this comment

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

lgtm!

pkg/sensors/exec/exec_test.go Outdated Show resolved Hide resolved

readyWG.Wait()
observertesthelper.DockerStart(t, "in-init-tree-test")
time.Sleep(1 * time.Second)
Copy link
Member

Choose a reason for hiding this comment

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

I admit sleeping is easier. If this is one day flaky (because downloading the image took long on creation), with docker ps -a you can check that the container was created (image was downloaded) or that it was started with docker ps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm yeah, we're already sleeping in other docker tests. I'm gonna go through and refactor them all at some point so let's just fix that up then.

Implement a container_id filter, primarily to support its use in docker-based unit
testing.

Signed-off-by: William Findlay <[email protected]>
Write a unit test for in_init_tree. The test makes sure that processes descending from the
entrypoint are in_init_tree and that a docker exec'd process is not in_init_tree.

Signed-off-by: William Findlay <[email protected]>
Implement a new export filter for the process.in_init_tree field.

Signed-off-by: William Findlay <[email protected]>
@michi-covalent michi-covalent merged commit 3ede569 into main Dec 11, 2024
50 checks passed
@michi-covalent michi-covalent deleted the pr/will/is_init_tree branch December 11, 2024 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/bpf This is related to BPF code area/filters release-note/minor This PR introduces a minor user-visible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants