From 1c0438a003241caa45b58b91df0ad2b70f66ed97 Mon Sep 17 00:00:00 2001 From: nikstur Date: Sat, 31 Dec 2022 02:51:52 +0100 Subject: [PATCH 1/3] lanzatool: simplify uefi path code --- rust/lanzatool/src/pe.rs | 51 +++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/rust/lanzatool/src/pe.rs b/rust/lanzatool/src/pe.rs index 2101b43..d5c7b59 100644 --- a/rust/lanzatool/src/pe.rs +++ b/rust/lanzatool/src/pe.rs @@ -32,7 +32,7 @@ pub fn lanzaboote_image( let kernel_path_file = write_to_tmp( target_dir, "kernel-esp-path", - esp_relative_path_string(esp, kernel_path)?, + esp_relative_uefi_path(esp, kernel_path)?, )?; let kernel_hash_file = write_to_tmp( target_dir, @@ -43,7 +43,7 @@ pub fn lanzaboote_image( let initrd_path_file = write_to_tmp( target_dir, "initrd-esp-path", - esp_relative_path_string(esp, initrd_path)?, + esp_relative_uefi_path(esp, initrd_path)?, )?; let initrd_hash_file = write_to_tmp( target_dir, @@ -167,17 +167,24 @@ fn write_to_tmp( Ok(path) } -fn esp_relative_path_string(esp: &Path, path: &Path) -> Result { +/// Convert a path to an UEFI path relative to the specified ESP. +fn esp_relative_uefi_path(esp: &Path, path: &Path) -> Result { let relative_path = path .strip_prefix(esp) - .with_context(|| format!("Failed to strip prefix: {:?} from path: {:?}", esp, path))? - .to_owned(); - let relative_path_string = relative_path - .into_os_string() - .into_string() - .expect("Failed to convert path '{}' to a relative string path") - .replace('/', "\\"); - Ok(format!("\\{}", &relative_path_string)) + .with_context(|| format!("Failed to strip esp prefix: {:?} from: {:?}", esp, path))?; + let uefi_path = uefi_path(relative_path)?; + Ok(format!("\\{}", &uefi_path)) +} + +/// Convert a path to a UEFI string representation. +/// +/// This might not _necessarily_ produce a valid UEFI path, since some UEFI implementations might +/// not support UTF-8 strings. A Rust String, however, is _always_ valid UTF-8. +fn uefi_path(path: &Path) -> Result { + path.to_str() + .to_owned() + .map(|x| x.replace('/', "\\")) + .with_context(|| format!("Failed to convert {:?} to an UEFI path", path)) } fn stub_offset(binary: &Path) -> Result { @@ -215,3 +222,25 @@ fn file_size(path: impl AsRef) -> Result { .metadata()? .size()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn convert_to_valid_uefi_path_relative_to_esp() { + let esp = Path::new("esp"); + let path = Path::new("esp/lanzaboote/is/great.txt"); + let converted_path = esp_relative_uefi_path(esp, path).unwrap(); + let expected_path = String::from("\\lanzaboote\\is\\great.txt"); + assert_eq!(converted_path, expected_path); + } + + #[test] + fn convert_to_valid_uefi_path() { + let path = Path::new("lanzaboote/is/great.txt"); + let converted_path = uefi_path(path).unwrap(); + let expected_path = String::from("lanzaboote\\is\\great.txt"); + assert_eq!(converted_path, expected_path); + } +} From b592d927440ecdf79e2d20224608462ec8ddc0a8 Mon Sep 17 00:00:00 2001 From: nikstur Date: Sat, 31 Dec 2022 02:55:11 +0100 Subject: [PATCH 2/3] lanzatool: don't open file to read metadata --- rust/lanzatool/src/pe.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rust/lanzatool/src/pe.rs b/rust/lanzatool/src/pe.rs index d5c7b59..166c3c0 100644 --- a/rust/lanzatool/src/pe.rs +++ b/rust/lanzatool/src/pe.rs @@ -212,14 +212,13 @@ fn image_base(pe: &PE) -> u64 { } fn file_size(path: impl AsRef) -> Result { - Ok(fs::File::open(&path) + Ok(fs::metadata(&path) .with_context(|| { format!( - "Failed to read file to calculate its size: {:?}", + "Failed to read file metadata to calculate its size: {:?}", path.as_ref() ) })? - .metadata()? .size()) } From 1e632c0d1dd5449b6269f2469e26b2c1148533ea Mon Sep 17 00:00:00 2001 From: nikstur Date: Sat, 31 Dec 2022 02:57:01 +0100 Subject: [PATCH 3/3] lanzatool: add context to sbsing output failure --- rust/lanzatool/src/signature.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rust/lanzatool/src/signature.rs b/rust/lanzatool/src/signature.rs index 5f70ba0..b3f953a 100644 --- a/rust/lanzatool/src/signature.rs +++ b/rust/lanzatool/src/signature.rs @@ -3,7 +3,7 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::process::Command; -use anyhow::Result; +use anyhow::{Context, Result}; pub struct KeyPair { pub private_key: PathBuf, @@ -32,7 +32,9 @@ impl KeyPair { let output = Command::new("sbsign").args(&args).output()?; if !output.status.success() { - std::io::stderr().write_all(&output.stderr).unwrap(); + std::io::stderr() + .write_all(&output.stderr) + .context("Failed to write output of sbsign to stderr")?; return Err(anyhow::anyhow!( "Failed to sign file using sbsign with args `{:?}`", &args