Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
601da0548b |
Generated
+1
@@ -74,6 +74,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"alkane-artifacts",
|
"alkane-artifacts",
|
||||||
"alkane-core",
|
"alkane-core",
|
||||||
|
"sha2",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ impl VerifiedArtifact {
|
|||||||
pub fn reader(&mut self) -> &mut File {
|
pub fn reader(&mut self) -> &mut File {
|
||||||
&mut self.file
|
&mut self.file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Recheck the same descriptor immediately before a guarded write.
|
||||||
|
pub fn revalidate(&mut self) -> Result<(), VerifyError> {
|
||||||
|
verify_open_file(&mut self.file, &self.record)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn seed_manifest() -> Result<Vec<ArtifactRecord>, VerifyError> {
|
pub fn seed_manifest() -> Result<Vec<ArtifactRecord>, VerifyError> {
|
||||||
@@ -118,9 +123,18 @@ fn signature_matches(format: &str, first: &[u8]) -> Result<bool, VerifyError> {
|
|||||||
|
|
||||||
pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtifact, VerifyError> {
|
pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtifact, VerifyError> {
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
|
verify_open_file(&mut file, record)?;
|
||||||
|
Ok(VerifiedArtifact {
|
||||||
|
file,
|
||||||
|
record: record.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn verify_open_file(file: &mut File, record: &ArtifactRecord) -> Result<(), VerifyError> {
|
||||||
if file.metadata()?.len() != record.size_bytes {
|
if file.metadata()?.len() != record.size_bytes {
|
||||||
return Err(VerifyError::Size);
|
return Err(VerifyError::Size);
|
||||||
}
|
}
|
||||||
|
file.seek(SeekFrom::Start(0))?;
|
||||||
let mut first = [0_u8; 16];
|
let mut first = [0_u8; 16];
|
||||||
let length = file.read(&mut first)?;
|
let length = file.read(&mut first)?;
|
||||||
if !signature_matches(&record.format, &first[..length])? {
|
if !signature_matches(&record.format, &first[..length])? {
|
||||||
@@ -147,10 +161,7 @@ pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtif
|
|||||||
return Err(VerifyError::Digest);
|
return Err(VerifyError::Digest);
|
||||||
}
|
}
|
||||||
file.seek(SeekFrom::Start(0))?;
|
file.seek(SeekFrom::Start(0))?;
|
||||||
Ok(VerifiedArtifact {
|
Ok(())
|
||||||
file,
|
|
||||||
record: record.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -189,9 +200,14 @@ mod tests {
|
|||||||
applicability: "test".to_owned(),
|
applicability: "test".to_owned(),
|
||||||
};
|
};
|
||||||
let mut verified = verify_file(&path, &record).unwrap();
|
let mut verified = verify_file(&path, &record).unwrap();
|
||||||
|
verified.revalidate().unwrap();
|
||||||
let mut replay = Vec::new();
|
let mut replay = Vec::new();
|
||||||
verified.reader().read_to_end(&mut replay).unwrap();
|
verified.reader().read_to_end(&mut replay).unwrap();
|
||||||
assert_eq!(replay, bytes);
|
assert_eq!(replay, bytes);
|
||||||
|
std::fs::write(&path, b"PK\x03\x04changed").unwrap();
|
||||||
|
assert!(matches!(verified.revalidate(), Err(VerifyError::Digest)));
|
||||||
|
std::fs::write(&path, bytes).unwrap();
|
||||||
|
verified.revalidate().unwrap();
|
||||||
record.size_bytes += 1;
|
record.size_bytes += 1;
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
verify_file(&path, &record),
|
verify_file(&path, &record),
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ license.workspace = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
alkane-core = { path = "../alkane-core" }
|
alkane-core = { path = "../alkane-core" }
|
||||||
alkane-artifacts = { path = "../alkane-artifacts" }
|
alkane-artifacts = { path = "../alkane-artifacts" }
|
||||||
|
sha2.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
//! Board transport adapters must supply serial, recovery and readback evidence.
|
//! Board transport adapters must supply serial, recovery and readback evidence.
|
||||||
|
|
||||||
use alkane_artifacts::VerifiedArtifact;
|
use alkane_artifacts::VerifiedArtifact;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
use std::io::{Read, Seek, SeekFrom};
|
use std::io::{Read, Seek, SeekFrom};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@@ -65,6 +66,10 @@ pub enum FlashError {
|
|||||||
ReadbackMismatch,
|
ReadbackMismatch,
|
||||||
#[error("verified image has an invalid digest")]
|
#[error("verified image has an invalid digest")]
|
||||||
InvalidDigest,
|
InvalidDigest,
|
||||||
|
#[error("image changed since verification")]
|
||||||
|
ArtifactChanged,
|
||||||
|
#[error("writer did not consume the complete image")]
|
||||||
|
IncompleteWrite,
|
||||||
#[error("write transport: {0}")]
|
#[error("write transport: {0}")]
|
||||||
Write(String),
|
Write(String),
|
||||||
#[error("readback transport: {0}")]
|
#[error("readback transport: {0}")]
|
||||||
@@ -85,6 +90,24 @@ fn digest_bytes(hex: &str) -> Result<[u8; 32], FlashError> {
|
|||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DigestReader<'a> {
|
||||||
|
inner: &'a mut dyn Read,
|
||||||
|
remaining: u64,
|
||||||
|
digest: Sha256,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Read for DigestReader<'_> {
|
||||||
|
fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
|
||||||
|
let limit = buffer
|
||||||
|
.len()
|
||||||
|
.min(usize::try_from(self.remaining).unwrap_or(usize::MAX));
|
||||||
|
let length = self.inner.read(&mut buffer[..limit])?;
|
||||||
|
self.remaining -= length as u64;
|
||||||
|
self.digest.update(&buffer[..length]);
|
||||||
|
Ok(length)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Stop before reboot. The caller must still prove live boot and persistence.
|
/// Stop before reboot. The caller must still prove live boot and persistence.
|
||||||
pub fn write_verified<W: Writer, R: IndependentReadback>(
|
pub fn write_verified<W: Writer, R: IndependentReadback>(
|
||||||
partition: &PartitionSpec,
|
partition: &PartitionSpec,
|
||||||
@@ -119,6 +142,9 @@ pub fn write_verified<W: Writer, R: IndependentReadback>(
|
|||||||
return Err(FlashError::RecoveryRequired);
|
return Err(FlashError::RecoveryRequired);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
artifact
|
||||||
|
.revalidate()
|
||||||
|
.map_err(|_| FlashError::ArtifactChanged)?;
|
||||||
let backup = writer
|
let backup = writer
|
||||||
.backup(partition)
|
.backup(partition)
|
||||||
.map_err(|e| FlashError::Write(e.to_string()))?;
|
.map_err(|e| FlashError::Write(e.to_string()))?;
|
||||||
@@ -133,9 +159,21 @@ pub fn write_verified<W: Writer, R: IndependentReadback>(
|
|||||||
.reader()
|
.reader()
|
||||||
.seek(SeekFrom::Start(0))
|
.seek(SeekFrom::Start(0))
|
||||||
.map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
.map_err(|e| FlashError::ArtifactIo(e.to_string()))?;
|
||||||
|
let mut stream = DigestReader {
|
||||||
|
inner: artifact.reader(),
|
||||||
|
remaining: size,
|
||||||
|
digest: Sha256::new(),
|
||||||
|
};
|
||||||
writer
|
writer
|
||||||
.write(partition, artifact.reader(), size)
|
.write(partition, &mut stream, size)
|
||||||
.map_err(|e| FlashError::Write(e.to_string()))?;
|
.map_err(|e| FlashError::Write(e.to_string()))?;
|
||||||
|
if stream.remaining != 0 {
|
||||||
|
return Err(FlashError::IncompleteWrite);
|
||||||
|
}
|
||||||
|
let written_digest: [u8; 32] = stream.digest.finalize().into();
|
||||||
|
if written_digest != expected {
|
||||||
|
return Err(FlashError::ArtifactChanged);
|
||||||
|
}
|
||||||
let actual = readback
|
let actual = readback
|
||||||
.sha256(partition, size)
|
.sha256(partition, size)
|
||||||
.map_err(|e| FlashError::Readback(e.to_string()))?;
|
.map_err(|e| FlashError::Readback(e.to_string()))?;
|
||||||
@@ -162,6 +200,7 @@ mod tests {
|
|||||||
serial: bool,
|
serial: bool,
|
||||||
recovery: bool,
|
recovery: bool,
|
||||||
backup_bytes: u64,
|
backup_bytes: u64,
|
||||||
|
max_read: Option<u64>,
|
||||||
writes: usize,
|
writes: usize,
|
||||||
contents: Vec<u8>,
|
contents: Vec<u8>,
|
||||||
}
|
}
|
||||||
@@ -192,9 +231,16 @@ mod tests {
|
|||||||
_: u64,
|
_: u64,
|
||||||
) -> Result<(), Self::Error> {
|
) -> Result<(), Self::Error> {
|
||||||
self.writes += 1;
|
self.writes += 1;
|
||||||
image
|
if let Some(limit) = self.max_read {
|
||||||
.read_to_end(&mut self.contents)
|
image
|
||||||
.map_err(|_| "read failed")?;
|
.take(limit)
|
||||||
|
.read_to_end(&mut self.contents)
|
||||||
|
.map_err(|_| "read failed")?;
|
||||||
|
} else {
|
||||||
|
image
|
||||||
|
.read_to_end(&mut self.contents)
|
||||||
|
.map_err(|_| "read failed")?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,6 +282,7 @@ mod tests {
|
|||||||
serial: true,
|
serial: true,
|
||||||
recovery: true,
|
recovery: true,
|
||||||
backup_bytes: 4096,
|
backup_bytes: 4096,
|
||||||
|
max_read: None,
|
||||||
writes: 0,
|
writes: 0,
|
||||||
contents: Vec::new(),
|
contents: Vec::new(),
|
||||||
}
|
}
|
||||||
@@ -322,4 +369,30 @@ mod tests {
|
|||||||
assert_eq!(writer.contents, IMAGE);
|
assert_eq!(writer.contents, IMAGE);
|
||||||
std::fs::remove_file(path).unwrap();
|
std::fs::remove_file(path).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn refuses_changed_or_partially_consumed_artifacts() {
|
||||||
|
let (path, mut image) = artifact("changed");
|
||||||
|
let target = partition();
|
||||||
|
let mut writer = writer();
|
||||||
|
let mut readback = MockReadback {
|
||||||
|
channel: "serial",
|
||||||
|
digest: digest_bytes(SHA).unwrap(),
|
||||||
|
};
|
||||||
|
std::fs::write(&path, b"PK\x03\x04changed").unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||||
|
Err(FlashError::ArtifactChanged)
|
||||||
|
));
|
||||||
|
assert_eq!(writer.writes, 0);
|
||||||
|
|
||||||
|
std::fs::write(&path, IMAGE).unwrap();
|
||||||
|
writer.max_read = Some(4);
|
||||||
|
assert!(matches!(
|
||||||
|
write_verified(&target, &mut image, &mut writer, &mut readback),
|
||||||
|
Err(FlashError::IncompleteWrite)
|
||||||
|
));
|
||||||
|
assert_eq!(writer.writes, 1);
|
||||||
|
std::fs::remove_file(path).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user