artifactize-foundation 0.27.2

Native file, path and executable primitives for Artifactize.
Documentation
use std::{ffi::CStr, os::unix::fs::PermissionsExt};

use super::*;
use crate::platform::private::{
    create_private_dir_all, create_private_file, is_owner_only, is_private_dir, is_private_file,
    private_tempdir_in, restrict_file,
};

// SAFETY: this declaration matches Darwin's sys/acl.h; the result is an owned acl_t.
unsafe extern "C" {
    fn acl_from_text(text: *const libc::c_char) -> *mut c_void;
}

fn apply_inheritable_grant(directory: &File) {
    // Darwin's well-known Everyone UUID. Deliberately grant access through an ACL while
    // leaving the POSIX mode unchanged; an ordinary child will inherit the grant.
    apply_acl(directory, c"!#acl 1\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:::allow,file_inherit,directory_inherit:read,execute\n");
}

fn apply_acl(file: &File, text: &CStr) {
    // SAFETY: text is terminated; check for NULL before owning the allocation.
    let acl = unsafe { acl_from_text(text.as_ptr()) };
    assert!(!acl.is_null(), "{}", io::Error::last_os_error());
    let acl = Acl(acl);
    // SAFETY: the directory and the parsed ACL are both live.
    status(unsafe { acl_set_fd_np(file.as_raw_fd(), acl.0, ACL_TYPE_EXTENDED) }).unwrap();
}

#[test]
fn acl_grants_are_rejected_and_do_not_survive_private_creation_or_restriction() {
    let parent = tempfile::Builder::new()
        .permissions(std::fs::Permissions::from_mode(0o700))
        .tempdir()
        .unwrap();
    let path = parent.path();
    apply_inheritable_grant(&File::open(path).unwrap());
    assert!(!is_private_dir(path).unwrap());

    let shared = path.join("ordinary child");
    let file = OpenOptions::new()
        .read(true)
        .write(true)
        .create_new(true)
        .mode(0o600)
        .open(&shared)
        .unwrap();
    assert!(!is_private_file(&file).unwrap());
    assert!(!is_owner_only(&file).unwrap());
    restrict_file(&file).unwrap();
    assert!(is_private_file(&file).unwrap());

    let file = create_private_file(&path.join("protected child")).unwrap();
    assert!(is_private_file(&file).unwrap());
    let nested = path.join("new parent/new child");
    create_private_dir_all(&nested).unwrap();
    assert!(is_private_dir(nested.parent().unwrap()).unwrap());
    assert!(is_private_dir(&nested).unwrap());
    let temporary = private_tempdir_in("acl-", path).unwrap();
    assert!(is_private_dir(temporary.path()).unwrap());
}

#[test]
fn mutation_acl_blocks_a_scratch_lease() {
    let parent = tempfile::tempdir().unwrap();
    apply_acl(
        &File::open(parent.path()).unwrap(),
        c"!#acl 1\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:::allow:delete_child\n",
    );
    let result = private_tempdir_in("acl-", parent.path());
    assert!(matches!(result, Err(error) if error.kind() == io::ErrorKind::PermissionDenied));
}

#[tokio::test]
async fn ordinary_spawn_allows_an_ancestor_that_gained_an_acl_grant_while_held() {
    use crate::platform::process::{resolved_command, spawn_gated};
    use std::fs;

    let root = tempfile::tempdir().unwrap();
    let dir = root.path().join("programs");
    create_private_dir_all(&dir).unwrap();
    let path = dir.join("fixture");
    fs::write(&path, b"#!/bin/sh\nexit 0\n").unwrap();
    const OWNER_EXECUTABLE: u32 = 0o700;
    fs::set_permissions(&path, fs::Permissions::from_mode(OWNER_EXECUTABLE)).unwrap();
    let command = resolved_command(&path, std::iter::empty::<&str>()).unwrap();
    let (gate, spawning) = spawn_gated(command).unwrap();
    let gate = gate.identify().await.unwrap();
    apply_acl(
        &File::open(dir).unwrap(),
        c"!#acl 1\ngroup:ABCDEFAB-CDEF-ABCD-EFAB-CDEF0000000C:::allow:add_file\n",
    );
    gate.admit().unwrap();
    let mut child = spawning.await.unwrap().unwrap();
    assert!(child.wait().await.unwrap().success());
    child.cleanup().await.unwrap();
}

#[test]
fn recursive_creation_tightens_an_existing_directory_with_a_macos_acl() {
    let parent = tempfile::tempdir().unwrap();
    let legacy = parent.path().join("legacy");
    std::fs::create_dir(&legacy).unwrap();
    apply_inheritable_grant(&File::open(&legacy).unwrap());
    std::fs::set_permissions(&legacy, std::fs::Permissions::from_mode(0o700)).unwrap();
    assert!(!is_private_dir(&legacy).unwrap());
    create_private_dir_all(&legacy).unwrap();
    assert!(is_private_dir(&legacy).unwrap());
    let descendant = legacy.join("ordinary child");
    std::fs::create_dir(&descendant).unwrap();
    assert!(has_no_grants(&File::open(descendant).unwrap()).unwrap());
}