Skip to main content

aff4_image/
set_verify.rs

1//! Verification retains the volume containing each reference as its context.
2use super::super::integrity::{absent, compare};
3use super::super::verify_all::{algorithm, declined};
4use super::*;
5
6/// Integrity checks recorded in one supplied volume's metadata context.
7#[derive(Debug, Clone, serde::Serialize)]
8pub struct SetVolumeVerification {
9    /// Declared volume identifier.
10    pub volume: String,
11    /// Caller-supplied path.
12    pub path: PathBuf,
13    /// Exact-byte metadata integrity; missing references remain explicit.
14    pub metadata: Option<MetadataVerification>,
15    /// Failure to interpret metadata integrity references.
16    pub metadata_error: Option<String>,
17    /// Linear, block, map and composite checks from this volume.
18    pub checks: Vec<IntegrityCheck>,
19}
20
21/// Selected physical image plus all supplied-volume integrity references.
22/// Internal matches do not establish independent authenticity.
23#[derive(Debug, Clone, serde::Serialize)]
24pub struct SetVerification {
25    /// Selected primary image.
26    pub image: String,
27    /// Complete assembled-image digest; absent on an incomplete read.
28    pub assembled: Option<SetDigest>,
29    /// Assembled-image read or geometry failure.
30    pub image_error: Option<String>,
31    /// Stored, explicitly described, and implicit gap bytes of the image.
32    pub coverage: Option<ByteCoverage>,
33    /// Contributor volume identifiers in caller-supplied stripe order.
34    pub stripe_order: Vec<String>,
35    /// Every owned ImageStream, verified once for its linear content.
36    pub streams: Vec<ResourceVerification>,
37    /// References stay scoped to the volume that records them.
38    pub volumes: Vec<SetVolumeVerification>,
39}
40
41impl SetVerification {
42    /// True only for complete bytes and matching metadata/content references.
43    pub fn all_match(&self) -> bool {
44        self.assembled
45            .as_ref()
46            .is_some_and(|v| v.external_match != Some(false))
47            && self.image_error.is_none()
48            && self.coverage.is_some()
49            && self
50                .streams
51                .iter()
52                .all(|r| r.error.is_none() && r.verification.is_some())
53            && self.volumes.iter().all(|v| {
54                v.metadata_error.is_none()
55                    && v.metadata
56                        .as_ref()
57                        .is_some_and(MetadataVerification::all_match)
58                    && v.checks.iter().all(|c| c.outcome == CheckOutcome::Match)
59            })
60    }
61}
62
63fn linear_check(id: &str, p: &Property, value: Option<&Verification>) -> IntegrityCheck {
64    let mut check = declined(id, p, &Error::Unsupported("linear digest algorithm".into()));
65    let Some(v) = value else {
66        check.outcome = CheckOutcome::Unreadable;
67        check.detail = Some("resource content was not completely verified".into());
68        return check;
69    };
70    let computed = match algorithm(p) {
71        "MD5" => &v.md5,
72        "SHA1" => &v.sha1,
73        "SHA256" => &v.sha256,
74        "SHA512" => &v.sha512,
75        "Blake2b" | "blake2b" => &v.blake2b,
76        _ => return check,
77    };
78    check.computed = Some(computed.clone());
79    check.detail = None;
80    check.outcome =
81        if p.value.len() != computed.len() || !p.value.bytes().all(|b| b.is_ascii_hexdigit()) {
82            CheckOutcome::Unreadable
83        } else if computed.eq_ignore_ascii_case(&p.value) {
84            CheckOutcome::Match
85        } else {
86            CheckOutcome::Mismatch
87        };
88    check
89}
90
91impl VolumeSet {
92    /// Verifies the selected image, every owned stream, and each volume's recorded
93    /// integrity structures. Missing/unsupported references prevent all_match.
94    /// Striped roots use the explicit supplied order; no ordering is guessed.
95    /// Other images' linear/root hashes are reported as unsupported. Metadata
96    /// references are internal; expected_image_sha256 is an external media digest.
97    pub fn verify_full(
98        &mut self,
99        image: &str,
100        expected_image_sha256: Option<&str>,
101        mut progress: impl FnMut(&str, u64, u64) -> ControlFlow<()>,
102    ) -> Result<SetVerification> {
103        let mut remaining = self.volumes[0].limits.verification_bytes;
104        let mut report = SetVerification {
105            image: image.into(),
106            assembled: None,
107            image_error: None,
108            coverage: None,
109            stripe_order: self
110                .volumes
111                .iter()
112                .filter(|v| v.has_type(image, "DiskImage"))
113                .map(|v| v.volume.clone())
114                .collect(),
115            streams: Vec::new(),
116            volumes: Vec::new(),
117        };
118        let result = self.size(image).and_then(|size| {
119            remaining = remaining
120                .checked_sub(size)
121                .ok_or_else(|| malformed("verification byte limit exceeded"))?;
122            self.scan_image(image, expected_image_sha256, true, |done, size| {
123                progress(image, done, size)
124            })
125        });
126        let image_hashes = match result {
127            Err(Error::Aborted) => return Err(Error::Aborted),
128            Err(e) => {
129                report.image_error = Some(e.to_string());
130                None
131            }
132            Ok((digest, hashes)) => {
133                let (map, size) = self.image_map(image)?;
134                let mut coverage = ByteCoverage::default();
135                let mut end = 0;
136                for range in &map.ranges {
137                    coverage.gap_filled += range.start - end;
138                    if is_symbolic(&range.target) {
139                        coverage.described += range.end - range.start;
140                    } else {
141                        coverage.stored += range.end - range.start;
142                    }
143                    end = range.end;
144                }
145                coverage.gap_filled += size - end;
146                report.coverage = Some(coverage);
147                report.assembled = Some(digest);
148                hashes
149            }
150        };
151        let owners = self.owners.clone();
152        let mut hashes = BTreeMap::new();
153        for (id, &owner) in &owners {
154            if progress(id, 0, 0).is_break() {
155                return Err(Error::Aborted);
156            }
157            let value = self.volumes[owner].size(id).and_then(|size| {
158                remaining = remaining
159                    .checked_sub(size)
160                    .ok_or_else(|| malformed("verification byte limit exceeded"))?;
161                self.volumes[owner].verify(id, |done, size| progress(id, done, size))
162            });
163            let (verification, coverage, error) = match value {
164                Err(Error::Aborted) => return Err(Error::Aborted),
165                Err(e) => (None, None, Some(e.to_string())),
166                Ok(v) => {
167                    let coverage = ByteCoverage {
168                        stored: v.bytes_verified,
169                        ..Default::default()
170                    };
171                    hashes.insert(id.clone(), v.clone());
172                    (Some(v), Some(coverage), None)
173                }
174            };
175            report.streams.push(ResourceVerification {
176                resource: id.clone(),
177                verification,
178                coverage,
179                error,
180            });
181            self.volumes[owner].cache = None;
182            self.volumes[owner].index_cache = None;
183        }
184        for context in 0..self.volumes.len() {
185            let volume = &mut self.volumes[context];
186            if progress(&volume.volume, 0, 0).is_break() {
187                return Err(Error::Aborted);
188            }
189            let (metadata, metadata_error) = match volume.verify_metadata(None) {
190                Ok(v) => (Some(v), None),
191                Err(e) => (None, Some(e.to_string())),
192            };
193            let mut result = SetVolumeVerification {
194                volume: volume.volume.clone(),
195                path: self.paths[context].clone(),
196                metadata,
197                metadata_error,
198                checks: Vec::new(),
199            };
200            let subjects: Vec<_> = volume.graph.keys().cloned().collect();
201            let local: BTreeSet<_> = owners
202                .iter()
203                .filter(|(_, n)| **n == context)
204                .map(|(id, _)| id.clone())
205                .collect();
206            for id in subjects {
207                if progress(&id, 0, 0).is_break() {
208                    return Err(Error::Aborted);
209                }
210                let properties = self.volumes[context].graph[&id].clone();
211                for p in properties.iter().filter(|p| is_integrity_property(p)) {
212                    let is_hash = is_property(&p.predicate, "hash");
213                    let is_root = is_hash
214                        && algorithm(p).starts_with("blockMapHash")
215                        && !self.volumes[context].has_type(&id, "Map");
216                    let check = if is_root {
217                        if id != image {
218                            declined(&id, p, &Error::Unsupported("unselected image root".into()))
219                        } else {
220                            match self.root_input(image, p) {
221                                Ok((input, alg)) => {
222                                    compare(&id, &p.predicate, &alg, &p.value, &input)
223                                }
224                                Err(e) => declined(&id, p, &e),
225                            }
226                        }
227                    } else if is_hash
228                        && !self.volumes[context].has_type(&id, "BlockHashes")
229                        && !algorithm(p).starts_with("blockMapHash")
230                    {
231                        let value = if id == image {
232                            image_hashes.as_ref()
233                        } else {
234                            hashes.get(&id)
235                        };
236                        linear_check(&id, p, value)
237                    } else {
238                        match self.volumes[context].structural_input_scoped(&id, p, Some(&local)) {
239                            Ok((input, alg)) => compare(&id, &p.predicate, &alg, &p.value, &input),
240                            Err(e) => declined(&id, p, &e),
241                        }
242                    };
243                    result.checks.push(check);
244                }
245                if self.volumes[context].has_type(&id, "ImageStream") {
246                    let blocks =
247                        match owners.get(&id).copied() {
248                            None => Err(malformed("stream has no owning volume")),
249                            Some(owner) if owner == context => self.volumes[context]
250                                .check_blocks_from(&id, None, &mut remaining, &mut progress, None),
251                            Some(owner) => {
252                                let (reference, data) =
253                                    two_volumes(&mut self.volumes, context, owner);
254                                reference.check_blocks_from(
255                                    &id,
256                                    Some(data),
257                                    &mut remaining,
258                                    &mut progress,
259                                    None,
260                                )
261                            }
262                        };
263                    match blocks {
264                        Err(Error::Aborted) => return Err(Error::Aborted),
265                        Err(e) => {
266                            result
267                                .checks
268                                .push(declined(&id, &placeholder("block hashes"), &e))
269                        }
270                        Ok(checks) => {
271                            if checks.is_empty()
272                                && local.contains(&id)
273                                && !properties.iter().any(|p| is_property(&p.predicate, "hash"))
274                            {
275                                result
276                                    .checks
277                                    .push(absent(&id, "linear or block content digest"));
278                            }
279                            result.checks.extend(checks);
280                        }
281                    }
282                    if let Some(&owner) = owners.get(&id) {
283                        self.volumes[owner].cache = None;
284                        self.volumes[owner].index_cache = None;
285                    }
286                }
287                if self.volumes[context].has_type(&id, "Map") {
288                    if let Err(e) = self.validate_context_map(context, &id) {
289                        result
290                            .checks
291                            .push(declined(&id, &placeholder("map geometry"), &e));
292                    }
293                    if !properties.iter().any(|p| {
294                        ["hash", "mapHash", "blockMapHash"]
295                            .iter()
296                            .any(|key| is_property(&p.predicate, key))
297                    }) {
298                        for key in ["mapPointHash", "mapIdxHash"] {
299                            if !properties.iter().any(|p| is_property(&p.predicate, key)) {
300                                result.checks.push(absent(&id, key));
301                            }
302                        }
303                    }
304                }
305                if ["FileImage", "FileSubStream"]
306                    .iter()
307                    .any(|kind| self.volumes[context].has_type(&id, kind))
308                {
309                    result.checks.push(declined(
310                        &id,
311                        &placeholder("logical resource"),
312                        &Error::Unsupported(
313                            "logical resources in physical volume-set verification".into(),
314                        ),
315                    ));
316                }
317                if id == image && !properties.iter().any(|p| is_property(&p.predicate, "hash")) {
318                    result.checks.push(absent(image, "image digest"));
319                }
320            }
321            // Clear all payload caches before moving to the next context.
322            for volume in &mut self.volumes {
323                volume.cache = None;
324                volume.index_cache = None;
325                volume.maps.clear();
326            }
327            report.volumes.push(result);
328        }
329        Ok(report)
330    }
331
332    fn validate_context_map(&mut self, context: usize, id: &str) -> Result<()> {
333        self.volumes[context].load_map(id)?;
334        let map = self.volumes[context].maps[id].clone();
335        let mut end = 0;
336        for range in &map.ranges {
337            if range.start > end {
338                self.validate_set_range(&map.gap, end, range.start - end)?;
339            }
340            self.validate_set_range(&range.target, range.offset, range.end - range.start)?;
341            end = range.end;
342        }
343        let size = self.volumes[context].size(id)?;
344        if end < size {
345            self.validate_set_range(&map.gap, end, size - end)?;
346        }
347        Ok(())
348    }
349
350    fn validate_set_range(&mut self, target: &str, offset: u64, length: u64) -> Result<()> {
351        if is_symbolic(target) {
352            return self.volumes[0].read_inner(target, &mut [], 0, &mut Vec::new(), false);
353        }
354        let owner = self
355            .owners
356            .get(target)
357            .copied()
358            .ok_or_else(|| malformed("map target has no owner"))?;
359        let size = self.volumes[owner].size(target)?;
360        if offset.checked_add(length).is_none_or(|end| end > size) {
361            return Err(malformed("map exceeds owned stream"));
362        }
363        Ok(())
364    }
365
366    fn root_input(&mut self, image: &str, p: &Property) -> Result<(Vec<u8>, String)> {
367        let alg = match algorithm(p) {
368            "blockMapHashSHA512" => "SHA512",
369            "blockMapHashSHA256" => "SHA256",
370            _ => return Err(Error::Unsupported("striped root algorithm".into())),
371        };
372        let contexts: Vec<_> = self
373            .volumes
374            .iter()
375            .enumerate()
376            .filter(|(_, v)| v.has_type(image, "DiskImage"))
377            .map(|(n, _)| n)
378            .collect();
379        if contexts.is_empty() {
380            return Err(malformed("image has no declaring volume"));
381        }
382        if contexts.len() > 1 && alg != "SHA512" {
383            return Err(Error::Unsupported(
384                "SHA256 striped root requires an independent vector".into(),
385            ));
386        }
387        let mut result = Vec::new();
388        for &context in &contexts {
389            let map = self.volumes[context]
390                .value(image, "dataStream")?
391                .ok_or_else(|| malformed("stripe has no local map"))?;
392            let local = self
393                .owners
394                .iter()
395                .filter(|(_, n)| **n == context)
396                .map(|(id, _)| id.clone())
397                .collect();
398            let property = Property {
399                predicate: format!("{NS}blockMapHash"),
400                value: String::new(),
401                datatype: Some(format!("{NS}{alg}")),
402                language: None,
403            };
404            let (input, _) =
405                self.volumes[context].structural_input_scoped(&map, &property, Some(&local))?;
406            if contexts.len() == 1 {
407                return Ok((input, alg.into()));
408            }
409            if alg == "SHA512" {
410                result.extend(sha2::Sha512::digest(&input));
411            } else {
412                result.extend(Sha256::digest(&input));
413            }
414        }
415        Ok((result, alg.into()))
416    }
417}
418
419fn is_integrity_property(p: &Property) -> bool {
420    [
421        "hash",
422        "mapHash",
423        "mapIdxHash",
424        "mapPointHash",
425        "mapPathHash",
426        "blockMapHash",
427        "imageStreamHash",
428        "imageStreamIndexHash",
429    ]
430    .iter()
431    .any(|key| is_property(&p.predicate, key))
432}
433fn placeholder(name: &str) -> Property {
434    Property {
435        predicate: name.into(),
436        value: String::new(),
437        datatype: None,
438        language: None,
439    }
440}
441fn two_volumes(volumes: &mut [Container], a: usize, b: usize) -> (&mut Container, &mut Container) {
442    if a < b {
443        let (left, right) = volumes.split_at_mut(b);
444        (&mut left[a], &mut right[0])
445    } else {
446        let (left, right) = volumes.split_at_mut(a);
447        (&mut right[0], &mut left[b])
448    }
449}