8000 BTFHub support by rafaeldtinoco · Pull Request #1125 · aquasecurity/tracee · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

BTFHub support #1125

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

Closed
wants to merge 1 commit into from
Closed

BTFHub support #1125

wants to merge 1 commit into from

Conversation

rafaeldtinoco
Copy link
Contributor
  • tracee-ebpf: add BTFHub support
  • tracee: new AttachTracepoint prototype (needed because of new libbpfgo)
  • libbpfgo: update libbpfgo version due to osinfo arch support

@rafaeldtinoco
Copy link
Contributor Author

BTFHub was added as a submodule inside 3rdparty/ directory. After initializing all submodules, you're able to either compile tracee without BTF files embedded (BTFHUB=0, which is the default option) or opt to set BTFHUB=1 and compile it with embedded BTF files.

[rafaeldtinoco@fujitsu:~/.../tracee/tracee-ebpf][btfembed]$ CORE=1 STATIC=1 BTFHUB=1 make all

With the following snippet:

.PHONY: btfhub
btfhub: $(OUT_BTFHUB)
ifeq ($(BTFHUB), 1)
	@test -f $(BTFHUB_SRC)/README.md || (echo "missing btfhub submodule initialization"; false)
	@test -f $(OUT_BPF_CORE) || (echo "you must compile CO-RE object first"; false)
	$(BTFHUB_SRC)/tools/extract.sh -a $(ARCH)
	@echo "generating customized BTF files according to eBPF object..."
	$(BTFHUB_SRC)/tools/btfgen.sh -a $(ARCH) -o $(abspath .)/$(OUT_BPF_CORE)
	@echo "moving generated BTF files to correct place..."
	@rm -rf $(OUT_BTFHUB)/*
	@mv $(BTFHUB_SRC)/custom-archive/* $(OUT_BTFHUB)
endif

All the BTF files from BTFHub are extracted, then tailored (to tracee.bpf.o) BTF files are encoded and, at the end, all the tailored BTF files are embedded into the go binary file.

@rafaeldtinoco
Copy link
Contributor Author
[rafaeldtinoco@bionic:~/.../tracee/tracee-ebpf][btfembed]$ uname -a
Linux bionic 5.4.0-87-generic #98~18.04.1-Ubuntu SMP Wed Sep 22 10:45:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

The correct embedded BTF file is written (and read) according to the running environment:

[rafaeldtinoco@bionic:~/.../tracee/tracee-ebpf][btfembed]$ sudo ./dist/tracee-ebpf --debug --trace event=execve,execveat
OSInfo: VERSION_CODENAME: bionic
OSInfo: KERNEL_RELEASE: 5.4.0-87-generic
OSInfo: ARCH: x86_64
OSInfo: VERSION: "18.04.6 LTS (Bionic Beaver)"
OSInfo: ID: ubuntu
OSInfo: ID_LIKE: debian
OSInfo: PRETTY_NAME: "Ubuntu 18.04.6 LTS"
OSInfo: VERSION_ID: "18.04"
BTF: bpfenv = false, btfenv = false, vmlinux = false
BTF: using BTF file from embedded btfhub: /tmp/tracee/tracee.btf
unpacked CO:RE bpf object file into memory
TIME             UID    COMM             PID     TID     RET              EVENT                ARGS
01:40:29:149536  1000   bash             4502    4502    0                execve               pathname: /bin/ps, argv: [ps -ef]

End of events stream
Stats: {EventCount:1 ErrorCount:0 LostEvCount:0 LostWrCount:0 LostNtCount:0}

@rafaeldtinoco
Copy link
Contributor Author

Somethings worth mentioning.

  • Currently, within BTFHub, the only Linux distribution that is still releasing kernel images without BTF support is Ubuntu (Bionic and Focal). Bionic supports BPF only with its HWE kernel (5.4). Focal supports BPF with its regular release kernel (5.4) AND with HWE kernels (5.8, 5.11 and, in the future, 5.13). Both will start to release patched kernel versions with BTF support (https://bugs.launchpad.net/ubuntu/+source/dwarves-dfsg/+bug/1912811) soon.

  • After all BTFHub supported distributions have BTF-enabled-kernels only being released, BTFHub won't need to keep updating its repository for newer kernel versions. It will start to behave as a "legacy kernels" BTF files repository (allowing projects to support all released kernels up to the date when there was no need for external BTF files).

@itaysk

After Ubuntu starts releasing BTF enabled kernels for all its LTS releases, we will be capable of embedding all BTF files needed in order to support the older kernels... and the new kernels won't need external BTF files. This would make the "BTFHub API" idea to loose strength since we wouldn't need to download BTF files from an online API. Everything will be already embedded.

WDYT ?

@yanivagman
Copy link
Collaborator

@rafaeldtinoco does this PR handle one of the following (or both):
#833
#1065

If so, let's link them to this PR (so they will also be closed when this is merged)

@grantseltzer
Copy link
Contributor

Is there a way to test this on a machine with BTF enabled?

@rafaeldtinoco
Copy link
Contributor Author

Is there a way to test this on a machine with BTF enabled?

not really.. Tracee won't try to load embedded file if there is a /sys/kernel/btf/vmlinux file available.. even if the go binary had the BTF file version for the running kernel (which is not the case for kernels that are BTF enabled).

func unpackBTFHub(outFilePath string, OSInfo *helpers.OSInfo) error {
var btfFilePath string

osId := OSInfo.GetOSReleaseFieldValue(helpers.OS_ID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be a problem to use this API of OSInfo in tracee container (alpine based) as we will get the wrong distro release info (aquasecurity/libbpfgo#92)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's discuss this at: aquasecurity/libbpfgo#92 (comment) so we have a single point to talk about it. IIRC we already addressed this in the past, but we can discuss in that issue you opened if you are okay.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Even if we agree to use the mock osrelease solution, this PR will have to be updated, or the tracee container would not detect the correct osrelease (as it is based on an alpine container)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I understood correctly what you are saying, you mean that we have to make sure the mock is done to tracee container within this PR (and not other). This way, tracee container would run by default with LIBBPFGO_OSRELEASE_FILE setting correctly having the host information.

Is that what you meant ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, exactly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This still needs to be addressed.

- embed files, or not, based in build tags (by @grantseltzer)
- Makefile: CORE and STATIC as build time options
- CORE=1 does not exist anymore: always embed CORE bpf object
- STATIC=1 now really sets go build options for STATIC binary
- BTFHUB=1 sets go build options for embedding BTFhub files
@rafaeldtinoco
Copy link
Contributor Author

I'm closing this PR and opening a new one in order to have a cleaner view of reviews. I'll point this one there to keep history of discussions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants
0