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

Setup cgroup.subtree_control controllers when necessary in cgroupsv2 #208

Merged
merged 1 commit into from
Nov 22, 2022

Conversation

ndrewh
Copy link
Contributor

@ndrewh ndrewh commented Nov 13, 2022

This commit adds extra setup when cgroupsv2 is enabled. In particular, we make sure that the root cgroup (whatever cgroupv2_mount is set to) has setup cgroup.subtree_control with the controllers we need.

If the necessary controller are not listed, we have to move all processes out of the root cgroup before we can change this (the 'no internal processes' rule: https://unix.stackexchange.com/a/713343). Currently this handles the case where the nsjail process is the only process in the cgroup. It seems like this would be relatively rare, but since nsjail is frequently the root process in a Docker container (e.g. for hosting CTF challenges), I think this case is common enough to make it worth implementing.

This also adds --detect_cgroupv2, which will attempt to detect whether --cgroupv2_mount is a valid cgroupv2 mount, and will setuse_cgroupv2 accordingly. This is useful in containerized environments where you may not know the configuration ahead of time.

References:
https://github.com/redpwn/jail/blob/master/internal/cgroup/cgroup2.go

See #196

@ndrewh ndrewh force-pushed the cgroupsv2-fix branch 3 times, most recently from 62ce0a1 to 384b1e8 Compare November 17, 2022 22:07
This commit adds extra setup when cgroupsv2 is enabled. In particular,
we make sure that the root namespace has setup cgroup.subtree_control
with the controllers we need.

If the necessary controller are not listed, we have to move all
processes out of the root namespace before we can change this
(the 'no internal processes' rule:
https://unix.stackexchange.com/a/713343). Currently we only
handle the case where the nsjail process is the only process in
the cgroup. It seems like this would be relatively rare, but since
nsjail is frequently the root process in a Docker container (e.g.
for hosting CTF challenges), I think this case is common enough to
make it worth implementing.

This also adds `--detect_cgroupv2`, which will attempt to detect
whether `--cgroupv2_mount` is a valid cgroupv2 mount, and if so
it will set `use_cgroupv2`. This is useful in containerized
environments where you may not know the kernel version ahead of time.

References:
https://github.com/redpwn/jail/blob/master/internal/cgroup/cgroup2.go
@robertswiecki
Copy link
Collaborator

robertswiecki commented Nov 22, 2022

Thanks, I'll pull it. There're a couple of things which I might rework later - things like the style of comments, but it doesn't affect the logic of the PR, so let's do it.

@robertswiecki robertswiecki merged commit 4437810 into google:master Nov 22, 2022
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.

2 participants