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

check if procfs is the actual host procfs #1417

Merged
merged 3 commits into from
Oct 5, 2023
Merged

Conversation

kkourt
Copy link
Contributor

@kkourt kkourt commented Sep 1, 2023

See patches.

@kkourt kkourt requested a review from a team as a code owner September 1, 2023 16:13
@kkourt kkourt requested a review from tpapagian September 1, 2023 16:13
@kkourt kkourt marked this pull request as draft September 1, 2023 16:14
@kkourt kkourt added the release-note/misc This PR makes changes that have no direct user impact. label Sep 1, 2023
@kkourt kkourt force-pushed the pr/kkourt/kind-warning branch 4 times, most recently from 415d85e to a91bc91 Compare September 1, 2023 16:22
@kkourt kkourt marked this pull request as ready for review September 1, 2023 17:01
@tixxdz
Copy link
Member

tixxdz commented Sep 1, 2023

Maybe compare /proc/1/ns/pid with https://elixir.bootlin.com/linux/latest/source/include/linux/proc_ns.h#L46 IIRC it is an idr allocation that has been constant for a very long time... and init pid is statically declared anyway


ev.pid = get_current_pid_tgid() >> 32;
perf_event_output(ctx, &tg_hpid_event_map, BPF_F_CURRENT_CPU, &ev, sizeof(ev));
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like too much (using perf rb) for retrieving just one pid value.. could we just update map? or perhaps something like in pkg/sensors/map_update.go maybe the ebpf program could return the pid as return value, but I might be too optimistic ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used a perf rb to handle the case where we get >1 than one events. it's very unlikely to happen but it does not seem like a lot of trouble to handle it.

Copy link
Contributor Author

@kkourt kkourt Sep 11, 2023

Choose a reason for hiding this comment

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

maybe the ebpf program could return the pid as return value, but I might be too optimistic ;-)

That's what I tried first using BPF_PROG_RUN, but I couldn't make it to work. Specifically, I could not find a program type that would allow me to get the pid. So, instead, I used a system call. Is there a way to use get_current_pid_tgid() from a bpf program that is executed with BPF_PROG_RUN?

@kkourt kkourt force-pushed the pr/kkourt/kind-warning branch from a91bc91 to fc09bad Compare September 14, 2023 11:28
@netlify
Copy link

netlify bot commented Sep 14, 2023

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit 6d7f078
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/651d3e63f38eaa0008e30754
😎 Deploy Preview https://deploy-preview-1417--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.

Comment on lines 90 to 91
off: 4343,
whence: 5151,
Copy link
Contributor

Choose a reason for hiding this comment

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

If you were to make these two values completely random, then you'd have a 1 in 2^64 chance of colliding with another lseek call that also set the FD to -1 (highly unlikely). In fact, you could set the FD to a random negative value too (maximum number of open files appears to be much less than max(int) so overflow into negative numbers appears unlikely) and increase this to 1 in 2^95 which is reaching cryptographic collision avoidance territory.
Doing that would mean you wouldn't need the ring buffer and a simple map would suffice.

Copy link
Contributor

@kevsecurity kevsecurity left a comment

Choose a reason for hiding this comment

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

I tend to agree with not using the ring buffer and instead using a map to communicate the value to user space. Choosing random values for the magic numbers in the lseek call would avoid collisions with other lseek calls. Suggest this would simplify the code somewhat.

Copy link
Member

@tpapagian tpapagian left a comment

Choose a reason for hiding this comment

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

I believe that what Djalal said in a previous comment makes sense. i.e. to compare /<option.Config.ProcFS>/1/ns/pid with https://elixir.bootlin.com/linux/latest/source/include/linux/proc_ns.h#L46.

$ kind create cluster
[...]
$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                       NAMES
799c437d3f63   kindest/node:v1.27.3   "/usr/local/bin/entr…"   43 seconds ago   Up 36 seconds   127.0.0.1:41377->6443/tcp   kind-control-plane
$ docker exec -it kind-control-plane bash
root@kind-control-plane:/# ls -la /proc/1/ns/pid
lrwxrwxrwx 1 root root 0 Oct  3 09:10 /proc/1/ns/pid -> 'pid:[4026532359]'
root@kind-control-plane:/#
exit
$ sudo ls -la /proc/1/ns/pid
lrwxrwxrwx 1 root root 0 Oct  2 10:17 /proc/1/ns/pid -> 'pid:[4026531836]'

If we run on the host pid namespace the value of /proc/1/ns/pid should always be 4026531836 (0xEFFFFFFC) (or I miss something).

kkourt added 3 commits October 4, 2023 08:44
Signed-off-by: Kornilios Kourtis <[email protected]>
Tetragon requires access to host /proc. In certain environments (e.g.,
kind) this is not achieved by default. This patch adds a warning by
comparing the inode number of the pid namespace in the provided /proc
with the expected hard-coded value.

Signed-off-by: Kornilios Kourtis <[email protected]>
This patch modifies the localdev scripts to mount the actual host /proc
in the tetragon container when using kind.

This eliminates the warning introduced in the previous commits.

To this end, this patch introduces a new helm variable to specify the
path of the proc filesystem in the runtime.

Signed-off-by: Kornilios Kourtis <[email protected]>
@kkourt kkourt force-pushed the pr/kkourt/kind-warning branch from fc09bad to 6d7f078 Compare October 4, 2023 10:28
@kkourt
Copy link
Contributor Author

kkourt commented Oct 4, 2023

Pushed a new version using @tixxdz's suggestion. PTAL, thanks!

Copy link
Member

@tixxdz tixxdz left a comment

Choose a reason for hiding this comment

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

Thanks

@kkourt kkourt merged commit f7e1df3 into main Oct 5, 2023
@kkourt kkourt deleted the pr/kkourt/kind-warning branch October 5, 2023 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants