Skip to content

Commit 2608afa

Browse files
committed
Improve code style
1 parent 2ccc6a7 commit 2608afa

19 files changed

Lines changed: 275 additions & 51 deletions

File tree

Cargo.lock

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ct_monitor/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ impl Monitor {
140140

141141
fn validate_domain(domain: &str) -> Result<()> {
142142
let domain_regex =
143-
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$").unwrap();
143+
Regex::new(r"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$")
144+
.context("invalid regex")?;
144145
if !domain_regex.is_match(domain) {
145146
bail!("invalid domain name");
146147
}

dstack-mr/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ hex-literal.workspace = true
2424
fs-err.workspace = true
2525
bon.workspace = true
2626
log.workspace = true
27+
scale.workspace = true
28+
29+
[dev-dependencies]
30+
dstack-types.workspace = true
31+
reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] }
32+
flate2 = "1.0"
33+
tar = "0.4"

dstack-mr/cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ fn main() -> Result<()> {
118118
.context("Failed to measure machine configuration")?;
119119

120120
if config.json {
121-
println!("{}", serde_json::to_string_pretty(&measurements).unwrap());
121+
println!("{}", serde_json::to_string_pretty(&measurements)?);
122122
} else {
123123
println!("Machine measurements:");
124124
println!("MRTD: {}", hex::encode(measurements.mrtd));

dstack-mr/src/acpi.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ fn find_acpi_table(tables: &[u8], signature: &str) -> Result<(u32, u32, u32)> {
408408
}
409409

410410
let tbl_sig = &tables[offset..offset + 4];
411-
let tbl_len_bytes: [u8; 4] = tables[offset + 4..offset + 8].try_into().unwrap();
411+
let tbl_len_bytes: [u8; 4] = tables[offset + 4..offset + 8]
412+
.try_into()
413+
.expect("header len checked");
412414
let tbl_len = u32::from_le_bytes(tbl_len_bytes) as usize;
413415

414416
if tbl_sig == sig_bytes {

dstack-mr/src/kernel.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn patch_kernel(
129129

130130
let mut kd = kernel_data.to_vec();
131131

132-
let protocol = u16::from_le_bytes(kd[0x206..0x208].try_into().unwrap());
132+
let protocol = u16::from_le_bytes(kd[0x206..0x208].try_into().context("impossible failure")?);
133133

134134
let (real_addr, cmdline_addr) = if protocol < 0x200 || (kd[0x211] & 0x01) == 0 {
135135
(0x90000_u32, 0x9a000_u32)
@@ -158,14 +158,16 @@ fn patch_kernel(
158158
bail!("the kernel image is too old for ramdisk");
159159
}
160160
let mut initrd_max = if protocol >= 0x20c {
161-
let xlf = u16::from_le_bytes(kd[0x236..0x238].try_into().unwrap());
161+
let xlf =
162+
u16::from_le_bytes(kd[0x236..0x238].try_into().context("impossible failure")?);
162163
if (xlf & 0x40) != 0 {
163164
u32::MAX
164165
} else {
165166
0x37ffffff
166167
}
167168
} else if protocol >= 0x203 {
168-
let max = u32::from_le_bytes(kd[0x22c..0x230].try_into().unwrap());
169+
let max =
170+
u32::from_le_bytes(kd[0x22c..0x230].try_into().context("impossible failure")?);
169171
if max == 0 {
170172
0x37ffffff
171173
} else {

dstack-mr/src/tdvf.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use anyhow::{anyhow, bail, Context, Result};
66
use hex_literal::hex;
7+
use scale::Decode;
78
use sha2::{Digest, Sha384};
89

910
use crate::acpi::Tables;
@@ -24,7 +25,13 @@ pub enum PageAddOrder {
2425
SinglePass,
2526
}
2627

27-
#[derive(Debug)]
28+
/// Helper to decode little-endian integers from byte slice using scale codec
29+
fn decode_le<T: Decode>(data: &[u8], context: &str) -> Result<T> {
30+
T::decode(&mut &data[..])
31+
.with_context(|| format!("failed to decode {} as little-endian", context))
32+
}
33+
34+
#[derive(Debug, Decode)]
2835
struct TdvfSection {
2936
data_offset: u32,
3037
raw_data_size: u32,
@@ -34,6 +41,14 @@ struct TdvfSection {
3441
attributes: u32,
3542
}
3643

44+
#[derive(Debug, Decode)]
45+
struct TdvfDescriptor {
46+
signature: [u8; 4], // "TDVF"
47+
_length: u32,
48+
version: u32,
49+
num_sections: u32,
50+
}
51+
3752
#[derive(Debug)]
3853
pub(crate) struct Tdvf<'a> {
3954
fw: &'a [u8],
@@ -77,6 +92,11 @@ fn measure_tdx_efi_variable(vendor_guid: &str, var_name: &str) -> Result<Vec<u8>
7792
}
7893

7994
impl<'a> Tdvf<'a> {
95+
/// Parse TDVF firmware metadata
96+
///
97+
/// This function uses scale codec for clean, panic-free parsing.
98+
/// Correctness is verified by integration test in tests/tdvf_parse.rs
99+
/// which ensures identical measurements to the original implementation.
80100
pub fn parse(fw: &'a [u8]) -> Result<Tdvf<'a>> {
81101
const TDX_METADATA_OFFSET_GUID: &str = "e47a6535-984a-4798-865e-4685a7bf8ec2";
82102
const TABLE_FOOTER_GUID: &str = "96b582de-1fb2-45f7-baea-a366c55a082d";
@@ -99,8 +119,7 @@ impl<'a> Tdvf<'a> {
99119
if offset < 18 {
100120
bail!("TDVF firmware offset too small for tables length");
101121
}
102-
let tables_len =
103-
u16::from_le_bytes(fw[offset - 18..offset - 16].try_into().unwrap()) as usize;
122+
let tables_len = decode_le::<u16>(&fw[offset - 18..offset - 16], "tables length")? as usize;
104123
if tables_len == 0 || tables_len > offset.saturating_sub(18) {
105124
bail!("Failed to parse TDVF metadata: Invalid tables length");
106125
}
@@ -133,37 +152,34 @@ impl<'a> Tdvf<'a> {
133152
bail!("TDVF metadata data too small");
134153
}
135154
let tdvf_meta_offset_raw =
136-
u32::from_le_bytes(data[data.len() - 4..].try_into().unwrap()) as usize;
155+
decode_le::<u32>(&data[data.len() - 4..], "TDVF metadata offset")? as usize;
137156
if tdvf_meta_offset_raw > fw.len() {
138157
bail!("TDVF metadata offset exceeds firmware size");
139158
}
140159
let tdvf_meta_offset = fw.len() - tdvf_meta_offset_raw;
141-
let tdvf_meta_desc = &fw[tdvf_meta_offset..tdvf_meta_offset + 16];
142160

143-
if &tdvf_meta_desc[..4] != b"TDVF" {
161+
// Decode TDVF descriptor using scale codec
162+
let descriptor = TdvfDescriptor::decode(&mut &fw[tdvf_meta_offset..])
163+
.context("failed to decode TDVF descriptor")?;
164+
165+
if &descriptor.signature != b"TDVF" {
144166
bail!("Failed to parse TDVF metadata: Invalid TDVF descriptor");
145167
}
146-
let tdvf_version = u32::from_le_bytes(tdvf_meta_desc[8..12].try_into().unwrap());
147-
if tdvf_version != 1 {
168+
if descriptor.version != 1 {
148169
bail!("Failed to parse TDVF metadata: Unsupported TDVF version");
149170
}
150-
let num_sections = u32::from_le_bytes(tdvf_meta_desc[12..16].try_into().unwrap()) as usize;
171+
let num_sections = descriptor.num_sections as usize;
151172

152173
let mut meta = Tdvf {
153174
fw,
154175
sections: Vec::new(),
155176
};
177+
178+
// Decode all sections using scale codec
156179
for i in 0..num_sections {
157180
let sec_offset = tdvf_meta_offset + 16 + 32 * i;
158-
let sec_data = &fw[sec_offset..sec_offset + 32];
159-
let s = TdvfSection {
160-
data_offset: u32::from_le_bytes(sec_data[0..4].try_into().unwrap()),
161-
raw_data_size: u32::from_le_bytes(sec_data[4..8].try_into().unwrap()),
162-
memory_address: u64::from_le_bytes(sec_data[8..16].try_into().unwrap()),
163-
memory_data_size: u64::from_le_bytes(sec_data[16..24].try_into().unwrap()),
164-
sec_type: u32::from_le_bytes(sec_data[24..28].try_into().unwrap()),
165-
attributes: u32::from_le_bytes(sec_data[28..32].try_into().unwrap()),
166-
};
181+
let s = TdvfSection::decode(&mut &fw[sec_offset..])
182+
.with_context(|| format!("failed to decode TDVF section {}", i))?;
167183

168184
if s.memory_address % PAGE_SIZE != 0 {
169185
bail!("Failed to parse TDVF metadata: Section memory address not aligned");

0 commit comments

Comments
 (0)