Skip to content

Commit 479ee22

Browse files
committed
Bmap file integrity check
Checks if the bmap hash is correct for the current bmap file. Bmap file checksum is calculated having that field as all 0s. Closes: #50 Signed-off-by: Rafael Garcia Ruiz <[email protected]>
1 parent 851e6b7 commit 479ee22

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

bmap-rs/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ anyhow = "1.0.66"
1212
nix = "0.26.1"
1313
flate2 = "1.0.24"
1414
clap = { version = "4.0.18", features = ["derive"] }
15-
indicatif = "0.17.1"
15+
indicatif = "0.17.1"
16+
sha2 = { version = "0.10.6", features = [ "asm" ] }
17+
hex = "0.4.3"

bmap-rs/src/main.rs

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use clap::Parser;
44
use flate2::read::GzDecoder;
55
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
66
use nix::unistd::ftruncate;
7+
use sha2::{Digest, Sha256};
78
use std::ffi::OsStr;
89
use std::fmt::Write;
910
use std::fs::File;
@@ -89,6 +90,23 @@ fn setup_input(path: &Path) -> Result<Decoder> {
8990
}
9091
}
9192

93+
fn bmap_integrity(checksum: String, xml: String) -> Result<()> {
94+
//Unset the checksum
95+
let mut bmap_hash = Sha256::new();
96+
let default = "0".repeat(64);
97+
let before_checksum = xml.replace(&checksum, &default);
98+
99+
//Compare given and created checksum
100+
bmap_hash.update(before_checksum);
101+
let digest = bmap_hash.finalize_reset();
102+
let new_checksum = hex::encode(digest.as_slice());
103+
if checksum != new_checksum {
104+
bail!("Bmap file doesn't match its checksum. It could be corrupted or compromised.")
105+
}
106+
println!("Bmap integrity checked!\n");
107+
Ok(())
108+
}
109+
92110
fn copy(c: Copy) -> Result<()> {
93111
if !c.image.exists() {
94112
bail!("Image file doesn't exist")
@@ -102,6 +120,7 @@ fn copy(c: Copy) -> Result<()> {
102120
b.read_to_string(&mut xml)?;
103121

104122
let bmap = Bmap::from_xml(&xml)?;
123+
bmap_integrity(bmap.bmap_file_checksum(), xml.clone())?;
105124
let output = std::fs::OpenOptions::new()
106125
.write(true)
107126
.create(true)

0 commit comments

Comments
 (0)