forge
Explore
Log in

Don't you dare go Hollow

Forge runner check run from an SSH shell fails the cgroup line with the reason and the systemd-run --user command to use, instead of passing it and then failing the sandbox run with a bare permission error

Browse files at this commit

9dc61f03231396fbbd73fc58788d73791da2cd51Ahmet Kaan GümüşParent: 810de83
Some checks failed
Show checks
  • ✗ci/buildworkspace: workspace io: No such file or directory (os error 2)Details
  • fix
    • forge runner check run from an SSH shell fails the cgroup line with the reason and the systemd-run --user command to use, instead of passing it and then failing the sandbox run with a bare permission error
Show full message
🐛 fix: forge runner check run from an SSH shell fails the cgroup line with the reason and the systemd-run --user command to use, instead of passing it and then failing the sandbox run with a bare permission error
crates/forge-runner/src/check.rsmodified+14 -13
@@ -14,6 +14,7 @@ use tokio::process::Command;
1414
1515 use crate::config::{Limits, UsernsMode};
1616 use crate::sandbox::SandboxRequest;
17+use crate::sandbox::helper::job_cgroup_base;
1718 use crate::sandbox::idmap::IdMap;
1819
1920 /// The probe job. It prints the seccomp mode of the job, then succeeds.
@@ -211,20 +212,20 @@ fn sandbox_lines(stderr: &str) -> Vec<&str> {
211212 .collect()
212213 }
213214
214-/// The cgroup v2 controllers enabled for the job cgroup, which is a child of the runner's parent
215-/// slice (see the helper). Returns the enabled list, or why it could not be read.
215+/// The cgroup v2 controllers enabled for job cgroups, read where the helper puts them. Fails when
216+/// that cgroup is not ours to write, as in an SSH session's scope.
216217 fn delegated_controllers() -> Result<Vec<String>, String> {
217- let text = fs::read_to_string("/proc/self/cgroup")
218- .map_err(|e| format!("cannot read /proc/self/cgroup: {e}"))?;
219- let own = text
220- .lines()
221- .find_map(|line| line.strip_prefix("0::"))
222- .ok_or_else(|| "cgroup v2 is not in use by this process".to_owned())?;
223- let own = PathBuf::from(own.trim());
224- let base = own.parent().unwrap_or(&own);
225- let file = Path::new("/sys/fs/cgroup")
226- .join(base.strip_prefix("/").unwrap_or(base))
227- .join("cgroup.subtree_control");
218+ let base = job_cgroup_base().map_err(|e| e.to_string())?;
219+ let dir = Path::new("/sys/fs/cgroup").join(base.strip_prefix("/").unwrap_or(&base));
220+ let probe = dir.join(format!("forge-check-{}", std::process::id()));
221+ fs::create_dir(&probe).map_err(|e| {
222+ format!(
223+ "cannot create a cgroup in {}: {e}; run under systemd --user, e.g. `systemd-run --user -P forge runner check`",
224+ dir.display()
225+ )
226+ })?;
227+ let _ = fs::remove_dir(&probe);
228+ let file = dir.join("cgroup.subtree_control");
228229 let enabled =
229230 fs::read_to_string(&file).map_err(|e| format!("cannot read {}: {e}", file.display()))?;
230231 Ok(enabled.split_whitespace().map(str::to_owned).collect())