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

legacy cgroup attachment functionality (needed for <= 5.6 kernels) #214

Merged
merged 3 commits into from
Aug 24, 2022
Merged

legacy cgroup attachment functionality (needed for <= 5.6 kernels) #214

merged 3 commits into from
Aug 24, 2022

Conversation

rafaeldtinoco
Copy link
Contributor

@rafaeldtinoco rafaeldtinoco commented Aug 22, 2022

I'm still working on this. Will finish the selftest and try to REAL use cases before moving away from draft. But feel free to review the work so far. Thanks.

@geyslan
Copy link
Member

geyslan commented Aug 23, 2022

LGTM so far. I can't run the tests on a generic/ubuntu2004 box, unfortunately.

vagrant@ubuntu2004:~/libbpfgo/selftest/cgroup-legacy$ uname -a
Linux ubuntu2004.localdomain 5.4.0-122-generic #138-Ubuntu SMP Wed Jun 22 15:00:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
vagrant@ubuntu2004:~/libbpfgo/selftest/cgroup-legacy$ pwd
/home/vagrant/libbpfgo/selftest/cgroup-legacy
vagrant@ubuntu2004:~/libbpfgo/selftest/cgroup-legacy$ git branch
* cgroup-attach-legacy
  main
  tinoco
vagrant@ubuntu2004:~/libbpfgo/selftest/cgroup-legacy$ make
make -C /home/vagrant/libbpfgo libbpfgo-static
make[1]: Entering directory '/home/vagrant/libbpfgo'
CC=clang \
	CGO_CFLAGS="-I/home/vagrant/libbpfgo/output" \
	CGO_LDFLAGS="-lelf -lz /home/vagrant/libbpfgo/output/libbpf.a" \
	GOOS=linux GOARCH=amd64 \
	go build \
	-tags netgo -ldflags '-w -extldflags "-static"' \
	.
make[1]: Leaving directory '/home/vagrant/libbpfgo'
make -C /home/vagrant/libbpfgo vmlinuxh
make[1]: Entering directory '/home/vagrant/libbpfgo'
make[1]: Leaving directory '/home/vagrant/libbpfgo'
clang -g -O2 -Wall -fpie -target bpf -D__TARGET_ARCH_amd64 -I../../output -c main.bpf.c -o main.bpf.o
In file included from main.bpf.c:3:
In file included from ../../output/bpf/bpf_helpers.h:11:
../../output/bpf/bpf_helper_defs.h:73:83: error: unknown type name '__u64'
static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, __u64 flags) = (void *) 2;
                                                                                  ^
../../output/bpf/bpf_helper_defs.h:97:42: error: unknown type name '__u32'
static long (*bpf_probe_read)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 4;
...

@rafaeldtinoco
Copy link
Contributor Author

vagrant@ubuntu2004:~/libbpfgo/selftest/cgroup-legacy$ uname -a
Linux ubuntu2004.localdomain 5.4.0-122-generic #138-Ubuntu SMP Wed Jun 22 15:00:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

If this is a Focal machine, please check tracee/packaging/Dockerfile.ubuntu-packaging file to see how to properly install needed toolchain (clang-12 needed, etc).

@rafaeldtinoco rafaeldtinoco marked this pull request as ready for review August 23, 2022 15:26
@grantseltzer grantseltzer changed the title WIP for legacy cgroup attachment functionality (needed for <= 5.6 kernels) legacy cgroup attachment functionality (needed for <= 5.6 kernels) Aug 23, 2022
libbpfgo.go Outdated Show resolved Hide resolved
libbpfgo.go Outdated Show resolved Hide resolved
libbpfgo.go Outdated Show resolved Hide resolved
libbpfgo.go Outdated Show resolved Hide resolved
libbpfgo.go Outdated Show resolved Hide resolved
@rafaeldtinoco
Copy link
Contributor Author

image

Tested with a vanilla 5.4 kernel and was able to attach to root cgroup on it.

- Add AttachCgroupLegacy()

AttachCgroupLegacy attaches the BPFProg to a cgroup described by given
fd. It first tries to use the most recent attachment method and, if that
does not work, instead of failing it tries the legacy way: to attach the
cgroup eBPF program without previously creating a link. Related to
upstream kernel commit af6eea57437a ("bpf: Implement bpf_link-based
cgroup BPF program attachment").

- Add DetachCgroupLegacy()

DetachCgroupLegacy detaches the BPFProg from a cgroup described by given
fd. This is needed because in legacy attachment there is no BPFLink,
just a fake one (kernel did not support it, nor libbpf). This function
should be called by the (*BPFLink)->Destroy() function, since BPFLink is
emulated (so user don´t need to distinguish between regular and legacy
cgroup detachments).
Note: this test will be skipped for kernels >= 5.4 (because its original
intent is to test the old attachment model, not the new one, already
being tested by other test(s)).
@rafaeldtinoco
Copy link
Contributor Author

The test still works in newer kernels but with a warning:

image

Because it is just testing the newer cgroup attachment type in those cases. When we run in kernels <= 5.4, then we get no warning and we test the interface for real. I thought about adding a flag to "force" using the legacy method but I don't think I should, so... it is what it is =).

Copy link
Contributor

@grantseltzer grantseltzer left a comment

Choose a reason for hiding this comment

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

LGTM! I like what you did with those latest changes (selftest running regardless, legacy struct)

@rafaeldtinoco rafaeldtinoco merged commit 5801272 into aquasecurity:main Aug 24, 2022
@rafaeldtinoco rafaeldtinoco deleted the cgroup-attach-legacy branch August 24, 2022 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants