diff --git a/docs/portable-bundles.md b/docs/portable-bundles.md index c627872..38ee2f9 100644 --- a/docs/portable-bundles.md +++ b/docs/portable-bundles.md @@ -158,7 +158,11 @@ Executable validation currently supports little-endian Linux ELF (x86-64, 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. diff --git a/src/bundle.rs b/src/bundle.rs index 35a4cce..960efb2 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -940,6 +940,70 @@ mod tests { } } + #[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::>(); + 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, diff --git a/src/macho.rs b/src/macho.rs index c8cf6b7..42263ed 100644 --- a/src/macho.rs +++ b/src/macho.rs @@ -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()); diff --git a/src/verify.rs b/src/verify.rs index a038c35..e6e46fc 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -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::>(); + 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(()) + } } }