Compare commits

...

2 Commits

Author SHA1 Message Date
Dandaedre 335abf6604 Verify deployment policy for Apple named bundles
Rust CI Cargo tests, coverage, and badge generation passed.
Gitea/rust_realease/pipeline/head This commit looks good Details
2026-10-20 18:31:54 +02:00
Dandaedre 531a059b51 Support macOS executables in named bundles 2026-10-20 18:11:06 +02:00
4 changed files with 172 additions and 7 deletions

View File

@ -155,9 +155,14 @@ and reopened to check its declared contents, modes and hashes. Partial staging i
removed on failure; successful invocation directories are retained under `dist/`.
Executable validation currently supports little-endian Linux ELF (x86-64,
aarch64, i686, riscv64gc) and Windows PE (x86-64, aarch64, i686). Unsupported
aarch64, i686, riscv64gc) Windows PE (x86-64, aarch64, i686), and thin 64-bit macOS Mach-O executables
(arm64 and x86-64). Unsupported
executable targets fail explicitly. This checks format and architecture, not ABI,
DLL completeness, license suitability or runtime behavior. Foreign executables
DLL completeness, license suitability or runtime behavior. For Apple named bundles, final verification also checks every executable against
`build.apple.deployment-target`: a dependency may have an older minimum, but not
a newer one, and only system dylib dependencies are accepted. Cargo outputs still
have their exact configured minimum checked during build/cache validation.
Foreign executables
are never executed automatically by the publisher. Trusted preparation scripts
must follow the same rule themselves.

View File

@ -496,6 +496,14 @@ fn architecture(path: &Path, target: &str) -> Result<(), Error> {
"riscv64gc" => Some(243),
_ => None,
}
} else if target.ends_with("-apple-darwin") && h[..4] == [0xcf, 0xfa, 0xed, 0xfe] {
u32::from_le_bytes(h[12..16].try_into().unwrap()) == 2
&& Some(u32::from_le_bytes(h[4..8].try_into().unwrap()))
== match arch {
"aarch64" => Some(0x0100_000c),
"x86_64" => Some(0x0100_0007),
_ => None,
}
} else {
false
};
@ -899,6 +907,103 @@ pub fn verify(path: &Path, root: &str, format: Format, entries: &[Entry]) -> Res
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bundle_macho_requires_matching_thin_64_bit_executable() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ffprobe");
for (target, cpu) in [
("aarch64-apple-darwin", 0x0100_000cu32),
("x86_64-apple-darwin", 0x0100_0007u32),
] {
let mut bytes = [0u8; 64];
bytes[..4].copy_from_slice(&[0xcf, 0xfa, 0xed, 0xfe]);
bytes[4..8].copy_from_slice(&cpu.to_le_bytes());
bytes[12..16].copy_from_slice(&2u32.to_le_bytes());
fs::write(&path, bytes).unwrap();
architecture(&path, target).unwrap();
let wrong = if target.starts_with("aarch64") {
"x86_64-apple-darwin"
} else {
"aarch64-apple-darwin"
};
assert!(architecture(&path, wrong).is_err());
assert!(architecture(&path, "x86_64-unknown-linux-gnu").is_err());
bytes[12] = 6; // A dylib is not a runnable program.
fs::write(&path, bytes).unwrap();
assert!(architecture(&path, target).is_err());
bytes[12] = 2;
bytes[..4].copy_from_slice(&[0xca, 0xfe, 0xba, 0xbe]);
fs::write(&path, bytes).unwrap();
assert!(architecture(&path, target).is_err());
fs::write(&path, &bytes[..16]).unwrap();
assert!(architecture(&path, target).is_err());
}
}
#[test]
fn apple_bundle_verification_checks_every_executable_in_zip_and_tgz() {
use std::io::Write;
let dir = tempfile::tempdir().unwrap();
let tree = dir.path().join("tree");
fs::create_dir(&tree).unwrap();
let dependency = b"/usr/lib/libSystem.B.dylib\0";
let length = (24 + dependency.len()).div_ceil(8) * 8;
let mut bytes = vec![0u8; 56 + length];
for (offset, value) in [
(0, 0xfeedfacfu32),
(4, 0x100000c),
(12, 2),
(16, 2),
(20, 24 + length as u32),
(32, 0x32),
(36, 24),
(40, 1),
(44, 13 << 16),
(56, 0xc),
(60, length as u32),
(64, 24),
] {
bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}
bytes[80..80 + dependency.len()].copy_from_slice(dependency);
for format in [Format::Zip, Format::Tgz] {
for (minimum, should_pass) in [(12u32, true), (14u32, false)] {
fs::write(tree.join("app"), &bytes).unwrap();
let mut helper = bytes.clone();
helper[44..48].copy_from_slice(&(minimum << 16).to_le_bytes());
fs::write(tree.join("ffprobe"), helper).unwrap();
fs::write(tree.join("LICENSE"), b"notice, not executable").unwrap();
let entries = [("app", 0o755), ("ffprobe", 0o755), ("LICENSE", 0o644)]
.into_iter()
.map(|(name, mode)| Entry {
destination: name.into(),
mode,
sha256: package::checksum(&tree.join(name)).unwrap(),
})
.collect::<Vec<_>>();
let path = dir.path().join("bundle");
write_archive(&path, &tree, "root", format, &entries).unwrap();
let artifact = Artifact {
target: "aarch64-apple-darwin".into(),
sha256: package::checksum(&path).unwrap(),
path: path.clone(),
contents: ArtifactContents::Bundle {
root: "root".into(),
format,
entries,
},
};
assert_eq!(
crate::verify::verify_apple_artifact(&path, &artifact, "13.0").is_ok(),
should_pass
);
let mut broken = fs::OpenOptions::new().write(true).open(&path).unwrap();
broken.write_all(b"broken").unwrap();
assert!(crate::verify::verify_apple_artifact(&path, &artifact, "13.0").is_err());
}
}
}
fn fixture() -> (
tempfile::TempDir,
SelectedPackage,

View File

@ -15,6 +15,15 @@ pub fn verify_path(path: &Path, target: &str, minimum: &str) -> Result<(), Error
}
pub fn verify(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
verify_policy(bytes, target, minimum, true)
}
/// Bundled runtime tools may support an older macOS version than the application.
pub fn verify_compatible(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
verify_policy(bytes, target, minimum, false)
}
fn verify_policy(bytes: &[u8], target: &str, minimum: &str, exact: bool) -> Result<(), Error> {
if bytes.len() < 32 || bytes[..4] != MH_MAGIC_64_LE {
return Err(Error(
"expected a little-endian 64-bit Mach-O executable".into(),
@ -86,9 +95,9 @@ pub fn verify(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
return Err(Error("Mach-O load command count/size mismatch".into()));
}
let found = found_minimum.ok_or_else(|| Error("Mach-O has no LC_BUILD_VERSION".into()))?;
if found != expected_minimum {
if (exact && found != expected_minimum) || (!exact && found > expected_minimum) {
return Err(Error(format!(
"Mach-O deployment minimum {} does not match configured {minimum}",
"Mach-O deployment minimum {} is incompatible with configured {minimum}",
display_version(found)
)));
}
@ -165,6 +174,8 @@ mod tests {
fn validates_architecture_deployment_minimum_and_system_dependencies() {
let arm = fixture(0x0100_000c, 13 << 16, "/usr/lib/libSystem.B.dylib");
verify(&arm, "aarch64-apple-darwin", "13.0").unwrap();
verify_compatible(&arm, "aarch64-apple-darwin", "14.0").unwrap();
assert!(verify_compatible(&arm, "aarch64-apple-darwin", "12.0").is_err());
assert!(verify(&arm, "x86_64-apple-darwin", "13.0").is_err());
assert!(verify(&arm, "aarch64-apple-darwin", "14.0").is_err());

View File

@ -152,9 +152,53 @@ pub fn verify_apple_artifact(
}
Ok(())
}
ArtifactContents::Bundle { .. } => Err(Error(
"Apple named bundles require an explicit Mach-O deployment policy".into(),
)),
ArtifactContents::Bundle {
root,
format,
entries,
} => {
// Recheck the declared tree, hashes and modes before interpreting executable bytes.
crate::bundle::verify(path, root, *format, entries)?;
let mut expected = entries
.iter()
.filter(|entry| entry.mode & 0o111 != 0)
.map(|entry| format!("{root}/{}", entry.destination))
.collect::<std::collections::BTreeSet<_>>();
let inspect = |reader: &mut dyn Read| -> Result<(), Error> {
let mut bytes = Vec::new();
reader
.read_to_end(&mut bytes)
.map_err(|e| Error(format!("cannot read Apple bundle executable: {e}")))?;
crate::macho::verify_compatible(&bytes, &artifact.target, deployment_target)
};
match format {
crate::bundle::Format::Zip => {
let mut archive = zip::ZipArchive::new(File::open(path)?)
.map_err(|e| Error(format!("invalid Apple ZIP: {e}")))?;
for name in &expected {
let mut entry = archive
.by_name(name)
.map_err(|e| Error(format!("missing Apple executable {name}: {e}")))?;
inspect(&mut entry)?;
}
expected.clear();
}
crate::bundle::Format::Tgz => {
let mut archive = tar::Archive::new(GzDecoder::new(File::open(path)?));
for entry in archive.entries()? {
let mut entry = entry?;
let name = entry.path()?.to_string_lossy().into_owned();
if expected.remove(&name) {
inspect(&mut entry)?;
}
}
}
}
if !expected.is_empty() {
return Err(Error("Missing Apple bundle executables".into()));
}
Ok(())
}
}
}