use std::path::{Path, PathBuf};
pub const CANARY_DIR_FROM_HOME: &str = ".local/share/cortexkit/aft/gh-shim-worker-jail";
pub const CANARY_FILE: &str = "canary";
#[cfg(unix)]
const CANARY_CONTENTS: &[u8] =
b"AFT worker-jail canary. Worker shells cannot read this file; the gh shim uses that to recognize them.\n";
#[doc(hidden)]
#[cfg(unix)]
pub fn account_home() -> std::io::Result<PathBuf> {
crate::production_storage::account_home()
}
pub fn dir_for_home(home: &Path) -> PathBuf {
home.join(CANARY_DIR_FROM_HOME)
}
pub fn path_for_home(home: &Path) -> PathBuf {
dir_for_home(home).join(CANARY_FILE)
}
#[cfg(unix)]
pub fn prepare(home: &Path) -> Result<PathBuf, String> {
use std::fs;
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
let dir = dir_for_home(home);
fs::DirBuilder::new()
.recursive(true)
.mode(0o700)
.create(&dir)
.map_err(|error| {
format!(
"cannot create worker-jail canary {}: {error}",
dir.display()
)
})?;
let metadata = fs::symlink_metadata(&dir).map_err(|error| {
format!(
"cannot inspect worker-jail canary {}: {error}",
dir.display()
)
})?;
if !metadata.is_dir() {
return Err(format!(
"worker-jail canary directory is not a real directory: {}",
dir.display()
));
}
fs::set_permissions(&dir, fs::Permissions::from_mode(0o700))
.map_err(|error| format!("cannot restore canary directory mode: {error}"))?;
let file = dir.join(CANARY_FILE);
let intact = fs::symlink_metadata(&file).is_ok_and(|metadata| metadata.is_file())
&& fs::read(&file).is_ok_and(|bytes| bytes == CANARY_CONTENTS);
if !intact {
let staging = dir.join(format!(".{CANARY_FILE}.{}", std::process::id()));
let _ = fs::remove_file(&staging);
fs::write(&staging, CANARY_CONTENTS)
.and_then(|()| fs::rename(&staging, &file))
.map_err(|error| {
format!(
"cannot write worker-jail canary {}: {error}",
file.display()
)
})?;
}
fs::set_permissions(&file, fs::Permissions::from_mode(0o600))
.map_err(|error| format!("cannot restore canary mode: {error}"))?;
if fs::read(&file).ok().as_deref() != Some(CANARY_CONTENTS) {
return Err(format!(
"worker-jail canary did not verify after writing: {}",
file.display()
));
}
dir.canonicalize()
.map_err(|error| format!("cannot canonicalize worker-jail canary: {error}"))
}
#[cfg(unix)]
pub fn prepare_homes(daemon_home: &Path, fenced: bool) -> Result<Vec<PathBuf>, String> {
#[cfg(not(any(test, feature = "test-support")))]
let account = crate::production_storage::account_home();
#[cfg(any(test, feature = "test-support"))]
let account = TEST_PREPARATION_HOME.with(|slot| {
let mut slot = slot.borrow_mut();
match slot.as_mut() {
Some((home, reads)) => {
*reads += 1;
Ok(home.clone())
}
None => crate::production_storage::account_home(),
}
});
prepare_homes_with_account(daemon_home, account, fenced)
}
#[cfg(all(any(test, feature = "test-support"), unix))]
thread_local! {
static TEST_PREPARATION_HOME: std::cell::RefCell<Option<(PathBuf, usize)>> = const { std::cell::RefCell::new(None) };
}
#[cfg(all(any(test, feature = "test-support"), unix))]
pub fn with_test_preparation_home<R>(
home: &Path,
expected_reads: usize,
run: impl FnOnce() -> R,
) -> R {
struct Restore {
previous: Option<(PathBuf, usize)>,
expected_reads: usize,
_same_thread: std::marker::PhantomData<std::rc::Rc<()>>,
}
impl Drop for Restore {
fn drop(&mut self) {
let current = TEST_PREPARATION_HOME.with(|slot| slot.replace(self.previous.take()));
if !std::thread::panicking() {
assert_eq!(
current.unwrap().1,
self.expected_reads,
"account-home preparation escaped its fixture thread or did not run"
);
}
}
}
let previous = TEST_PREPARATION_HOME.with(|slot| slot.replace(Some((home.to_path_buf(), 0))));
let _restore = Restore {
previous,
expected_reads,
_same_thread: std::marker::PhantomData,
};
run()
}
#[cfg(unix)]
fn prepare_homes_with_account(
daemon_home: &Path,
account: std::io::Result<PathBuf>,
fenced: bool,
) -> Result<Vec<PathBuf>, String> {
let mut homes = match account {
Ok(home) => vec![home],
Err(error) if fenced => {
return Err(format!("cannot resolve worker-jail account home: {error}"))
}
Err(_) => Vec::new(),
};
homes.push(daemon_home.to_path_buf());
let mut dirs = Vec::new();
for home in homes {
match prepare(&home) {
Ok(dir) if !dirs.contains(&dir) => dirs.push(dir),
Err(error) if fenced => return Err(error),
_ => {}
}
}
Ok(dirs)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CanaryAccess {
Denied,
Readable,
Missing,
Unproven,
}
pub fn access(path: &Path) -> CanaryAccess {
probe(path).access
}
struct Probe {
access: CanaryAccess,
detail: String,
}
fn probe(path: &Path) -> Probe {
#[cfg(target_os = "linux")]
{
linux_probe(path)
}
#[cfg(target_os = "macos")]
{
macos_probe(path)
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
Probe {
access: CanaryAccess::Missing,
detail: format!("{}: no canary probe on this platform", path.display()),
}
}
}
#[cfg(unix)]
fn errno_of(error: &std::io::Error) -> i32 {
error.raw_os_error().unwrap_or(-1)
}
#[cfg(target_os = "linux")]
fn linux_probe(path: &Path) -> Probe {
let (access, detail) = match std::fs::metadata(path) {
Err(error) if error.raw_os_error() == Some(libc::EACCES) => (
CanaryAccess::Denied,
format!("stat errno={}", errno_of(&error)),
),
Err(error) => (
CanaryAccess::Missing,
format!("stat errno={}", errno_of(&error)),
),
Ok(metadata) if !metadata.is_file() => (CanaryAccess::Missing, "stat: not a file".into()),
Ok(_) => match std::fs::File::open(path) {
Ok(_) => (CanaryAccess::Readable, "stat ok, open ok".into()),
Err(error) if error.raw_os_error() == Some(libc::EACCES) => (
CanaryAccess::Denied,
format!("stat ok, open errno={}", errno_of(&error)),
),
Err(error) => (
CanaryAccess::Missing,
format!("stat ok, open errno={}", errno_of(&error)),
),
},
};
Probe {
access,
detail: format!("{}: {detail} -> {access:?}", path.display()),
}
}
#[cfg(target_os = "macos")]
mod seatbelt {
unsafe extern "C" {
pub fn sandbox_check(
pid: libc::pid_t,
operation: *const libc::c_char,
filter: libc::c_int,
...
) -> libc::c_int;
pub static SANDBOX_CHECK_NO_REPORT: libc::c_int;
}
pub const SANDBOX_FILTER_PATH: libc::c_int = 1;
}
#[cfg(target_os = "macos")]
fn resolve_existing_prefix(path: &Path) -> PathBuf {
let mut rest = Vec::new();
let mut prefix = path;
loop {
if let Ok(resolved) = prefix.canonicalize() {
return rest
.iter()
.rev()
.fold(resolved, |path, name| path.join(name));
}
match (prefix.parent(), prefix.file_name()) {
(Some(parent), Some(name)) => {
rest.push(name.to_os_string());
prefix = parent;
}
_ => return path.to_path_buf(),
}
}
}
#[cfg(target_os = "macos")]
fn macos_probe(path: &Path) -> Probe {
use std::os::unix::ffi::OsStrExt;
let resolved = resolve_existing_prefix(path);
let probe = |access: CanaryAccess, detail: String| Probe {
access,
detail: format!(
"{} (resolved {}): {detail} -> {access:?}",
path.display(),
resolved.display()
),
};
let metadata = std::fs::symlink_metadata(&resolved);
let stat = match &metadata {
Ok(metadata) if metadata.is_file() => "stat ok".to_string(),
Ok(_) => return probe(CanaryAccess::Missing, "stat: not a file".into()),
Err(error) if matches!(error.raw_os_error(), Some(libc::ENOENT | libc::ENOTDIR)) => {
return probe(
CanaryAccess::Missing,
format!("stat errno={}", errno_of(error)),
)
}
Err(error) => format!("stat errno={}", errno_of(error)),
};
let Ok(c_path) = std::ffi::CString::new(resolved.as_os_str().as_bytes()) else {
return probe(CanaryAccess::Missing, "path contains NUL".into());
};
let (result, errno) = unsafe {
*libc::__error() = 0;
let result = seatbelt::sandbox_check(
libc::getpid(),
c"file-read-data".as_ptr(),
seatbelt::SANDBOX_FILTER_PATH | seatbelt::SANDBOX_CHECK_NO_REPORT,
c_path.as_ptr(),
);
(result, *libc::__error())
};
let detail = format!("{stat}, sandbox_check={result} errno={errno}");
match result {
0 if metadata.is_ok() => probe(CanaryAccess::Readable, detail),
0 => probe(CanaryAccess::Unproven, detail),
1 => probe(CanaryAccess::Denied, detail),
_ => probe(CanaryAccess::Unproven, detail),
}
}
pub fn diagnostics() -> String {
let mut lines = Vec::new();
#[cfg(target_os = "linux")]
lines.push(format!("no_new_privs={}", unsafe {
libc::prctl(libc::PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)
}));
for home in candidate_homes() {
lines.push(probe(&path_for_home(&home)).detail);
}
lines.join("; ")
}
pub fn inside_worker_jail() -> bool {
#[cfg(target_os = "linux")]
{
inside_worker_jail_with_no_new_privs(unsafe {
libc::prctl(libc::PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0)
})
}
#[cfg(not(target_os = "linux"))]
denied_candidate()
}
#[rustfmt::skip]
test_support_visibility! {
#[cfg(target_os = "linux")]
pub(crate) fn inside_worker_jail_with_no_new_privs(result: i32) -> bool {
if result == 0 {
return false;
}
denied_candidate()
}
}
fn denied_candidate() -> bool {
candidate_homes()
.iter()
.any(|home| access(&path_for_home(home)) == CanaryAccess::Denied)
}
fn candidate_homes() -> Vec<PathBuf> {
#[cfg(any(test, feature = "test-support"))]
if let Some(homes) = TEST_HOMES.with(|slot| slot.borrow().clone()) {
return homes;
}
let mut homes = Vec::new();
#[cfg(unix)]
if let Ok(home) = crate::production_storage::account_home() {
homes.push(home);
}
if let Some(home) = std::env::var_os("HOME")
.filter(|value| !value.is_empty())
.map(PathBuf::from)
.filter(|home| home.is_absolute())
{
if !homes.contains(&home) {
homes.push(home);
}
}
homes
}
#[cfg(any(test, feature = "test-support"))]
thread_local! {
static TEST_HOMES: std::cell::RefCell<Option<Vec<PathBuf>>> =
const { std::cell::RefCell::new(None) };
}
#[cfg(all(any(test, feature = "test-support"), unix))]
pub fn with_test_homes<R>(homes: Vec<PathBuf>, run: impl FnOnce() -> R) -> R {
let previous = TEST_HOMES.with(|slot| slot.replace(Some(homes)));
struct Restore(Option<Vec<PathBuf>>);
impl Drop for Restore {
fn drop(&mut self) {
TEST_HOMES.with(|slot| *slot.borrow_mut() = self.0.take());
}
}
let _restore = Restore(previous);
run()
}
pub fn verify_denied_after_apply(path: &Path) -> Result<(), String> {
match access(path) {
CanaryAccess::Denied => Ok(()),
other => Err(format!(
"the worker jail does not deny its gh-shim canary {} ({other:?}); refusing to start an unfenced worker shell",
path.display()
)),
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
use std::os::unix::fs::PermissionsExt;
#[test]
fn prepare_creates_and_repairs_the_canary() {
let home = tempfile::tempdir().unwrap();
let dir = prepare(home.path()).unwrap();
assert_eq!(dir, dir_for_home(home.path()).canonicalize().unwrap());
let file = dir.join(CANARY_FILE);
assert_eq!(std::fs::read(&file).unwrap(), CANARY_CONTENTS);
std::fs::write(&file, b"tampered").unwrap();
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o644)).unwrap();
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();
prepare(home.path()).unwrap();
assert_eq!(std::fs::read(&file).unwrap(), CANARY_CONTENTS);
let mode = |path: &Path| std::fs::metadata(path).unwrap().permissions().mode() & 0o777;
assert_eq!(mode(&file), 0o600);
assert_eq!(mode(&dir), 0o700);
}
#[test]
fn distinct_account_and_daemon_homes_are_prepared_for_workers_and_heads() {
let account = tempfile::tempdir().unwrap();
let daemon = tempfile::tempdir().unwrap();
for fenced in [true, false] {
let dirs = prepare_homes_with_account(daemon.path(), Ok(account.path().into()), fenced)
.unwrap();
assert_eq!(
dirs,
vec![
dir_for_home(account.path()).canonicalize().unwrap(),
dir_for_home(daemon.path()).canonicalize().unwrap()
]
);
}
let dirs =
prepare_homes_with_account(account.path(), Ok(account.path().into()), true).unwrap();
assert_eq!(dirs.len(), 1);
}
#[test]
fn missing_account_home_refuses_fenced_workers_but_not_heads() {
let daemon = tempfile::tempdir().unwrap();
assert!(prepare_homes_with_account(
daemon.path(),
Err(std::io::Error::other("no passwd entry")),
true
)
.is_err());
assert_eq!(
prepare_homes_with_account(
daemon.path(),
Err(std::io::Error::other("no passwd entry")),
false
)
.unwrap()
.len(),
1
);
}
#[test]
fn unwritable_account_home_refuses_fenced_workers() {
let fixture = tempfile::tempdir().unwrap();
let account = fixture.path().join("account");
std::fs::write(&account, "not a directory").unwrap();
assert!(prepare_homes_with_account(fixture.path(), Ok(account), true).is_err());
}
#[test]
fn prepare_refuses_a_canary_directory_replaced_by_a_symlink() {
let home = tempfile::tempdir().unwrap();
let elsewhere = tempfile::tempdir().unwrap();
let dir = dir_for_home(home.path());
std::fs::create_dir_all(dir.parent().unwrap()).unwrap();
std::os::unix::fs::symlink(elsewhere.path(), &dir).unwrap();
assert!(prepare(home.path()).is_err());
}
#[test]
fn an_unconfined_process_sees_a_readable_or_missing_canary() {
let home = tempfile::tempdir().unwrap();
assert_eq!(access(&path_for_home(home.path())), CanaryAccess::Missing);
prepare(home.path()).unwrap();
assert_eq!(access(&path_for_home(home.path())), CanaryAccess::Readable);
with_test_homes(vec![home.path().to_path_buf()], || {
assert!(!inside_worker_jail());
});
assert!(verify_denied_after_apply(&path_for_home(home.path())).is_err());
}
}