-
Notifications
You must be signed in to change notification settings - Fork 400
feat(agent): enforce per-sandbox cgroup v2 memory limits #3142
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
Open
daryllimyt
wants to merge
15
commits into
main
Choose a base branch
from
feat/agent-sandbox-cgroup-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
94fa42c
feat(agent): enforce per-sandbox cgroup v2 memory limits
daryllimyt 842e26f
fix(agent): clear stale readiness sentinel on startup
daryllimyt 886454a
fix(config): treat blank agent executor ready-file env as unset
daryllimyt 72679fb
fix(agent): derive cgroup root and enforce sandbox memory budget
daryllimyt 7a037f5
fix(agent): honor ready-file override in compose healthchecks
daryllimyt ed5ce8b
fix(agent): clear stale readiness sentinel before startup validation
daryllimyt ffbe671
feat(agent): delegate cgroup subtree to apiuser in sandbox overlay
daryllimyt 17012bb
fix(agent): set apiuser identity env when dropping privileges
daryllimyt 080c889
Merge origin/main: move cgroup delegation to dedicated entrypoint
daryllimyt f544e7e
fix(agent): harden cgroup detection edge cases
daryllimyt 17c69a9
fix(agent): honor cgroup-enabled flag in delegation entrypoint
daryllimyt ab6c868
fix(agent): honor ancestor cgroup limits in memory budget
daryllimyt 7276d2f
fix(agent): enforce sandbox cgroup limits
daryllimyt cb99c56
fix(agent): use sandbox overlay for compose nsjail
daryllimyt 594c9c5
fix(agent): gate root cgroup delegation on nsjail being enabled
daryllimyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| # Agent-executor entrypoint for deployments that enable per-sandbox cgroup | ||
| # memory limits. Start the container as root with this entrypoint (compose: | ||
| # user "0:0"; Kubernetes: runAsUser 0) and it hands the container's own | ||
| # cgroup v2 directory to apiuser so the worker can prepare nsjail child | ||
| # cgroups without root, then drops privileges before starting it. Where the | ||
| # needed privileges are absent the worker starts unchanged and falls back to | ||
| # rlimit-only sandbox limits. Non-root invocations pass straight through. | ||
| if [[ "$(id -u)" == "0" ]]; then | ||
|
daryllimyt marked this conversation as resolved.
|
||
| # Mirror config.env_bool falsy values so disabling the feature also skips | ||
| # the root-side delegation, not just the Python-side preparation. The | ||
| # privilege drop below is unconditional: root never reaches the worker. | ||
| cgroup_enabled="$(printf '%s' "${TRACECAT__AGENT_SANDBOX_CGROUP_ENABLED:-true}" | tr '[:upper:]' '[:lower:]')" | ||
| case "$cgroup_enabled" in 0 | false | no | off) cgroup_enabled=false ;; *) cgroup_enabled=true ;; esac | ||
|
|
||
| if [[ "$cgroup_enabled" == "true" ]]; then | ||
|
daryllimyt marked this conversation as resolved.
Outdated
|
||
| # Resolve this container's cgroup v2 directory: the mount root under a | ||
| # private cgroup namespace, a subpath of the host cgroupfs otherwise | ||
| # (e.g. privileged Kubernetes). Never touch anything above it, and | ||
| # never touch anything at all without a unified v2 entry — on a | ||
| # cgroup v1 host an empty match would otherwise point at the | ||
| # cgroupfs root. | ||
| cgroup_path="$(sed -n 's/^0:://p' /proc/self/cgroup | head -n 1)" | ||
| if [[ -z "$cgroup_path" ]]; then | ||
| echo "No cgroup v2 entry in /proc/self/cgroup; agent sandbox" \ | ||
| "cgroup limits will be unavailable." >&2 | ||
| else | ||
| cgroup_rel="${cgroup_path#/}" | ||
| cgroup_dir="/sys/fs/cgroup${cgroup_rel:+/$cgroup_rel}" | ||
| mount -o remount,rw /sys/fs/cgroup 2>/dev/null || true | ||
| if [[ -f "$cgroup_dir/cgroup.controllers" ]] && | ||
| chown apiuser:apiuser "$cgroup_dir" "$cgroup_dir/cgroup.procs" \ | ||
| "$cgroup_dir/cgroup.subtree_control" "$cgroup_dir/cgroup.threads"; then | ||
| echo "Delegated $cgroup_dir to apiuser." | ||
| else | ||
| echo "Unable to delegate $cgroup_dir to apiuser; agent sandbox" \ | ||
| "cgroup limits will be unavailable." >&2 | ||
| fi | ||
| fi | ||
| fi | ||
| # setpriv changes only IDs; fix the identity env vars ourselves instead of | ||
| # --reset-env, which would clear the service configuration environment. | ||
| export HOME=/home/apiuser USER=apiuser LOGNAME=apiuser | ||
| exec setpriv --reuid=apiuser --regid=apiuser --init-groups "$@" | ||
| fi | ||
|
|
||
| exec "$@" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.