diff --git a/dstack/dstack-types/src/lib.rs b/dstack/dstack-types/src/lib.rs index 5317c9bcb..49db6f97c 100644 --- a/dstack/dstack-types/src/lib.rs +++ b/dstack/dstack-types/src/lib.rs @@ -1562,46 +1562,6 @@ pub fn image_hash_from_sha256sum(checksum_file: &[u8]) -> [u8; 32] { sha256(checksum_file) } -pub fn sha256sum_entry_hash(checksum_file: &[u8], filename: &str) -> Result<[u8; 32], String> { - let text = std::str::from_utf8(checksum_file) - .map_err(|e| format!("sha256sum.txt is not valid UTF-8: {e}"))?; - let mut found = None; - for (line_no, line) in text.lines().enumerate() { - let line = line.trim(); - if line.is_empty() { - continue; - } - let mut parts = line.split_whitespace(); - let Some(hash_hex) = parts.next() else { - continue; - }; - let Some(path) = parts.next() else { - return Err(format!( - "sha256sum.txt line {} is missing filename", - line_no + 1 - )); - }; - if path != filename { - continue; - } - if found.is_some() { - return Err(format!( - "sha256sum.txt contains duplicate {filename} entries" - )); - } - let hash = hex::decode(hash_hex) - .map_err(|e| format!("sha256sum.txt {filename} hash is not valid hex: {e}"))?; - let hash: [u8; 32] = hash.try_into().map_err(|hash: Vec| { - format!( - "sha256sum.txt {filename} hash has invalid length {}, expected 32", - hash.len() - ) - })?; - found = Some(hash); - } - found.ok_or_else(|| format!("sha256sum.txt is missing {filename}")) -} - pub fn verify_measurement_material( os_image_hash: &[u8], checksum_file: &[u8], @@ -1615,7 +1575,7 @@ pub fn verify_measurement_material( hex::encode(image_hash_from_sha256sum(checksum_file)) )); } - let expected_measurement_hash = sha256sum_entry_hash(checksum_file, filename)?; + let expected_measurement_hash = sha256sum::entry_hash(checksum_file, filename)?; let actual_measurement_hash = sha256(measurement); if expected_measurement_hash != actual_measurement_hash { return Err(format!( @@ -2484,6 +2444,7 @@ pub struct ImageInfo { } pub mod mr_config; +pub mod sha256sum; pub mod shared_filenames; pub mod version; diff --git a/dstack/dstack-types/src/sha256sum.rs b/dstack/dstack-types/src/sha256sum.rs new file mode 100644 index 000000000..8165b3343 --- /dev/null +++ b/dstack/dstack-types/src/sha256sum.rs @@ -0,0 +1,142 @@ +// SPDX-FileCopyrightText: © 2026 Phala Network +// +// SPDX-License-Identifier: Apache-2.0 + +//! Parser for `sha256sum.txt`, the file whose digest is `os_image_hash`. +//! +//! The file is the only thing binding `os_image_hash` to the bytes that get +//! measured, so every consumer must read it exactly the same way. We generate +//! it ourselves with `sha256sum ` (`os/image/assemble.sh`), so only that +//! output shape is accepted: one ` \n` line per file. +//! Everything else GNU `sha256sum` understands -- binary-mode `*name`, +//! `\`-escaped lines, paths -- is rejected rather than interpreted. + +/// One `sha256sum.txt` entry: a file name and the digest it is bound to. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Entry { + pub hash: [u8; 32], + pub name: String, +} + +pub fn parse(checksum_file: &[u8]) -> Result, String> { + let body = std::str::from_utf8(checksum_file) + .ok() + .and_then(|text| text.strip_suffix('\n')) + .ok_or("sha256sum.txt is not newline-terminated text")?; + let mut entries: Vec = Vec::new(); + for (index, line) in body.split('\n').enumerate() { + let line_no = index + 1; + let entry = parse_line(line).ok_or_else(|| { + format!("sha256sum.txt line {line_no} is not ` `") + })?; + if entries.iter().any(|e| e.name == entry.name) { + return Err(format!( + "sha256sum.txt line {line_no} repeats {}", + entry.name + )); + } + entries.push(entry); + } + Ok(entries) +} + +pub fn entry_hash(checksum_file: &[u8], filename: &str) -> Result<[u8; 32], String> { + parse(checksum_file)? + .into_iter() + .find(|entry| entry.name == filename) + .map(|entry| entry.hash) + .ok_or_else(|| format!("sha256sum.txt is missing {filename}")) +} + +fn parse_line(line: &str) -> Option { + let (hash_hex, name) = line.split_once(" ")?; + if !hash_hex + .bytes() + .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) + || !is_valid_name(name) + { + return None; + } + let mut hash = [0; 32]; + hex::decode_to_slice(hash_hex, &mut hash).ok()?; + Some(Entry { + hash, + name: name.into(), + }) +} + +/// A file in the image root, e.g. `bzImage` or `measurement.tdx.cbor`. The +/// manifest cannot list itself: its digest is the image identity. +fn is_valid_name(name: &str) -> bool { + name.starts_with(|c: char| c.is_ascii_alphanumeric()) + && name + .bytes() + .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-')) + && name != "sha256sum.txt" +} + +#[cfg(test)] +mod tests { + use super::*; + + const DIGEST: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + + #[test] + fn accepts_sha256sum_output() { + let names = [ + "ovmf.fd", + "bzImage", + "initramfs.cpio.gz", + "metadata.json", + "measurement.aws.replay.json", + "rootfs_v2-x86", + ]; + let doc: String = names.iter().map(|n| format!("{DIGEST} {n}\n")).collect(); + let entries = parse(doc.as_bytes()).unwrap(); + assert_eq!( + entries.iter().map(|e| e.name.as_str()).collect::>(), + names + ); + assert_eq!(hex::encode(entries[0].hash), DIGEST); + assert_eq!(entry_hash(doc.as_bytes(), "bzImage"), Ok(entries[1].hash)); + assert!(entry_hash(doc.as_bytes(), "missing").is_err()); + } + + #[test] + fn rejects_anything_else() { + let upper = DIGEST.to_uppercase(); + for doc in [ + String::new(), + "\n".into(), + format!("{DIGEST} bzImage"), + format!("{DIGEST} bzImage\n\n"), + format!("\n{DIGEST} bzImage\n"), + format!("{DIGEST} bzImage\r\n"), + format!("{DIGEST} bzImage\n{DIGEST} bzImage\n"), + format!("{upper} bzImage\n"), + format!("{} bzImage\n", &DIGEST[2..]), + format!("{DIGEST}00 bzImage\n"), + format!("{DIGEST} bzImage\n"), + format!("{DIGEST} *bzImage\n"), + format!("\\{DIGEST} bzImage\n"), + format!("{DIGEST} bzImage\n"), + format!("{DIGEST} bzImage junk\n"), + format!("{DIGEST} bz Image\n"), + format!("{DIGEST} \n"), + format!("{DIGEST} ./bzImage\n"), + format!("{DIGEST} ../bzImage\n"), + format!("{DIGEST} sub/bzImage\n"), + format!("{DIGEST} /bzImage\n"), + format!("{DIGEST} .\n"), + format!("{DIGEST} ..\n"), + format!("{DIGEST} .hidden\n"), + format!("{DIGEST} -rf\n"), + format!("{DIGEST} bz*Image\n"), + format!("{DIGEST} bzImage\u{200b}\n"), + format!("{DIGEST} sha256sum.txt\n"), + ] { + assert!(parse(doc.as_bytes()).is_err(), "accepted {doc:?}"); + } + assert!(parse(b"\xff\n").is_err()); + } +} diff --git a/dstack/verifier/src/verification.rs b/dstack/verifier/src/verification.rs index 91442b3cd..8337dca81 100644 --- a/dstack/verifier/src/verification.rs +++ b/dstack/verifier/src/verification.rs @@ -18,7 +18,7 @@ use cc_eventlog::{ TdxEvent, }; use dstack_mr::{tdx::TdxRtmr0AcpiHashes, TdxMeasurements}; -use dstack_types::{TdxAttestationVariant, VmConfig}; +use dstack_types::{sha256sum, TdxAttestationVariant, VmConfig}; use hex_literal::hex; use ra_tls::attestation::{ AppInfo, Attestation, AttestationQuote, AttestationVerifier, DstackVerifiedReport, NitroPcrs, @@ -26,7 +26,7 @@ use ra_tls::attestation::{ }; use serde::{Deserialize, Serialize}; use sha2::{Digest as _, Sha256}; -use tokio::{io::AsyncWriteExt, process::Command}; +use tokio::io::AsyncWriteExt; use tpm_qvl::verify::VerifiedReport as TpmVerifiedReport; use tracing::{debug, info, warn}; @@ -331,39 +331,25 @@ impl CvmVerifier { .all(|component| matches!(component, Component::Normal(_) | Component::CurDir)) } - /// A manifest name must be literally a file name, because - /// `prune_unlisted_image_files` matches manifest entries against the - /// `file_name()` of each top-level directory entry, and `sha256sum -c` - /// resolves them relative to the extraction root. - fn is_flat_manifest_name(name: &str) -> bool { - Path::new(name) - .file_name() - .is_some_and(|file_name| file_name == OsStr::new(name)) - } - - fn validate_image_manifest_paths(files_doc: &str) -> Result<()> { - for (line_index, line) in files_doc.lines().enumerate() { - if line.trim().is_empty() { - continue; - } - let mut fields = line.split_whitespace(); - let _digest = fields - .next() - .context("image manifest entry is missing a digest")?; - let name = fields - .next() - .context("image manifest entry is missing a path")?; - if fields.next().is_some() { - bail!("image manifest line {} has extra fields", line_index + 1); - } - if !Self::is_flat_manifest_name(name) { - bail!("image manifest line {} has an unsafe path", line_index + 1); - } - if name == "sha256sum.txt" { - bail!("image manifest must not recursively list sha256sum.txt"); + /// Check every file the image manifest lists against the digest it binds. + /// This is the only content check on `bzImage`, `ovmf.fd` and the initrd + /// before they are measured. + fn verify_image_manifest( + extracted_dir: &Path, + files_doc: &str, + ) -> Result> { + let entries = sha256sum::parse(files_doc.as_bytes()).map_err(anyhow::Error::msg)?; + for entry in &entries { + let mut file = fs_err::File::open(extracted_dir.join(&entry.name)) + .with_context(|| format!("image is missing manifest entry {}", entry.name))?; + let mut hasher = Sha256::new(); + std::io::copy(&mut file, &mut hasher) + .with_context(|| format!("failed to read manifest entry {}", entry.name))?; + if hasher.finalize().as_slice() != entry.hash { + bail!("{} does not match its digest in sha256sum.txt", entry.name); } } - Ok(()) + Ok(entries) } fn extract_image_archive(tarball_path: &Path, extracted_dir: &Path) -> Result<()> { @@ -398,12 +384,11 @@ impl CvmVerifier { Ok(()) } - fn prune_unlisted_image_files(extracted_dir: &Path, files_doc: &str) -> Result<()> { - let listed_files: Vec<&OsStr> = files_doc - .lines() - .flat_map(|line| line.split_whitespace().nth(1)) - .map(|s| s.as_ref()) - .collect(); + fn prune_unlisted_image_files( + extracted_dir: &Path, + manifest: &[sha256sum::Entry], + ) -> Result<()> { + let listed_files: Vec<&OsStr> = manifest.iter().map(|entry| entry.name.as_ref()).collect(); let files = fs_err::read_dir(extracted_dir).context("Failed to read directory")?; for file in files { let file = file.context("Failed to read directory entry")?; @@ -1037,26 +1022,10 @@ impl CvmVerifier { let sha256sum_path = extracted_dir.join("sha256sum.txt"); let files_doc = fs_err::read_to_string(&sha256sum_path).context("Failed to read sha256sum.txt")?; - Self::validate_image_manifest_paths(&files_doc)?; - - // Verify checksum - let output = Command::new("sha256sum") - .arg("-c") - .arg("sha256sum.txt") - .current_dir(&extracted_dir) - .output() - .await - .context("Failed to verify checksum")?; - - if !output.status.success() { - bail!( - "Checksum verification failed: {}", - String::from_utf8_lossy(&output.stderr) - ); - } + let manifest = Self::verify_image_manifest(&extracted_dir, &files_doc)?; // Remove the files that are not listed in sha256sum.txt - Self::prune_unlisted_image_files(&extracted_dir, &files_doc)?; + Self::prune_unlisted_image_files(&extracted_dir, &manifest)?; // All image modes are addressed by sha256(sha256sum.txt). Extra // measurement CBOR files are ordinary sha256sum.txt entries and do not @@ -1705,6 +1674,17 @@ mod tests { assert_eq!(entries.len(), 1, "temporary cache files must not survive"); } + /// Lay out an extracted image whose manifest is `files_doc`, with each + /// named file holding the payload given for it. + fn image_dir_with_manifest(files_doc: &str, files: &[(&str, &[u8])]) -> tempfile::TempDir { + let dir = tempfile::tempdir().expect("temp image directory"); + fs_err::write(dir.path().join("sha256sum.txt"), files_doc).unwrap(); + for (name, payload) in files { + fs_err::write(dir.path().join(name), payload).unwrap(); + } + dir + } + #[test] fn measurement_cache_key_ignores_unmeasured_fields() { let base: VmConfig = serde_json::from_value(serde_json::json!({ @@ -1739,13 +1719,14 @@ mod tests { #[test] fn image_cache_pruning_keeps_checksum_identity() { - let dir = tempfile::tempdir().expect("temp image directory"); - let files_doc = "00 metadata.json\n"; - fs_err::write(dir.path().join("sha256sum.txt"), files_doc).unwrap(); - fs_err::write(dir.path().join("metadata.json"), "{}").unwrap(); - fs_err::write(dir.path().join("unmeasured"), "remove me").unwrap(); + let files_doc = format!("{} metadata.json\n", hex::encode(Sha256::digest(b"{}"))); + let dir = image_dir_with_manifest( + &files_doc, + &[("metadata.json", b"{}"), ("unmeasured", b"remove me")], + ); - CvmVerifier::prune_unlisted_image_files(dir.path(), files_doc).unwrap(); + let manifest = CvmVerifier::verify_image_manifest(dir.path(), &files_doc).unwrap(); + CvmVerifier::prune_unlisted_image_files(dir.path(), &manifest).unwrap(); assert!(dir.path().join("sha256sum.txt").exists()); assert!(dir.path().join("metadata.json").exists()); @@ -1753,7 +1734,37 @@ mod tests { } #[test] - fn image_paths_must_be_confined_and_manifest_paths_must_be_flat() { + fn every_manifest_entry_is_checked_before_the_image_is_accepted() { + let good = hex::encode(Sha256::digest(b"{}")); + let zero = "00".repeat(32); + let files: &[(&str, &[u8])] = &[("metadata.json", b"{}"), ("bzImage", b"kernel")]; + + for files_doc in [ + format!("{good} metadata.json\n{zero} bzImage\n"), + format!("{good} metadata.json\n{zero} missing\n"), + ] { + let dir = image_dir_with_manifest(&files_doc, files); + assert!( + CvmVerifier::verify_image_manifest(dir.path(), &files_doc).is_err(), + "accepted {files_doc:?}" + ); + } + + let good_doc = format!( + "{good} metadata.json\n{} bzImage\n", + hex::encode(Sha256::digest(b"kernel")) + ); + let dir = image_dir_with_manifest(&good_doc, files); + assert_eq!( + CvmVerifier::verify_image_manifest(dir.path(), &good_doc) + .unwrap() + .len(), + 2 + ); + } + + #[test] + fn image_archive_paths_must_be_confined() { for path in ["../escape", "/absolute", "nested/../escape"] { assert!( !CvmVerifier::is_confined_archive_path(Path::new(path)), @@ -1769,25 +1780,6 @@ mod tests { "{path}" ); } - - let digest = "00".repeat(32); - assert!( - CvmVerifier::validate_image_manifest_paths(&format!("{digest} metadata.json\n")) - .is_ok() - ); - for path in [ - "../escape", - "/absolute", - "nested/artifact", - "./metadata.json", - ".", - "sha256sum.txt", - ] { - assert!( - CvmVerifier::validate_image_manifest_paths(&format!("{digest} {path}\n")).is_err(), - "{path}" - ); - } } #[test]