From 74afcb1eea6a1551aee0adcc74357a2b6d9c6382 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Wed, 28 Dec 2022 23:33:01 +0100 Subject: [PATCH 1/4] lanzatool: remove Path -> String conversion from pe module ... by using OsString, which can handle broken UTF-8 in file names. --- rust/lanzatool/src/pe.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/rust/lanzatool/src/pe.rs b/rust/lanzatool/src/pe.rs index a91851c..0111585 100644 --- a/rust/lanzatool/src/pe.rs +++ b/rust/lanzatool/src/pe.rs @@ -1,3 +1,4 @@ +use std::ffi::OsString; use std::fs; use std::io::Write; use std::os::unix::fs::MetadataExt; @@ -8,8 +9,6 @@ use std::process::Command; use anyhow::{Context, Result}; use goblin::pe::PE; -use crate::utils; - use tempfile::TempDir; /// Attach all information that lanzaboote needs into the PE binary. @@ -94,12 +93,11 @@ fn wrap_in_pe( .open(&image_path) .context("Failed to generate named temp file")?; - let mut args: Vec = sections.iter().flat_map(Section::to_objcopy).collect(); - let extra_args = vec![ - utils::path_to_string(stub), - utils::path_to_string(&image_path), - ]; - args.extend(extra_args); + let mut args: Vec = sections.iter().flat_map(Section::to_objcopy).collect(); + + [stub.as_os_str(), image_path.as_os_str()] + .iter() + .for_each(|a| args.push(a.into())); let status = Command::new("objcopy") .args(&args) @@ -122,12 +120,19 @@ struct Section { } impl Section { - fn to_objcopy(&self) -> Vec { + /// Create objcopy `-add-section` command line parameters that + /// attach the section to a PE file. + fn to_objcopy(&self) -> Vec { + // There is unfortunately no format! for OsString, so we cannot + // just format a path. + let mut map_str: OsString = format!("{}=", self.name).into(); + map_str.push(&self.file_path); + vec![ - String::from("--add-section"), - format!("{}={}", self.name, utils::path_to_string(&self.file_path)), - String::from("--change-section-vma"), - format!("{}={:#x}", self.name, self.offset), + OsString::from("--add-section"), + map_str, + OsString::from("--change-section-vma"), + format!("{}={:#x}", self.name, self.offset).into(), ] } } From b762de9fec4adaa57f515f01bb098e8ce47b27e3 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Wed, 28 Dec 2022 23:49:45 +0100 Subject: [PATCH 2/4] lanzatool: remove Path -> String conversions in signature module --- rust/lanzatool/src/signature.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/rust/lanzatool/src/signature.rs b/rust/lanzatool/src/signature.rs index e910c9d..d7ac85f 100644 --- a/rust/lanzatool/src/signature.rs +++ b/rust/lanzatool/src/signature.rs @@ -1,10 +1,9 @@ +use std::ffi::OsString; use std::path::{Path, PathBuf}; use std::process::Command; use anyhow::Result; -use crate::utils; - pub struct KeyPair { pub private_key: PathBuf, pub public_key: PathBuf, @@ -19,14 +18,14 @@ impl KeyPair { } pub fn sign_and_copy(&self, from: &Path, to: &Path) -> Result<()> { - let args = vec![ - String::from("--key"), - utils::path_to_string(&self.private_key), - String::from("--cert"), - utils::path_to_string(&self.public_key), - utils::path_to_string(from), - String::from("--output"), - utils::path_to_string(to), + let args: Vec = vec![ + OsString::from("--key"), + self.private_key.clone().into(), + OsString::from("--cert"), + self.public_key.clone().into(), + from.as_os_str().to_owned(), + OsString::from("--output"), + to.as_os_str().to_owned(), ]; let output = Command::new("sbsign").args(&args).output()?; From f07618b64c9a81a1f45c2d7b4a880882ecceae62 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Wed, 28 Dec 2022 23:50:57 +0100 Subject: [PATCH 3/4] lanzatool: remove unused utils module --- rust/lanzatool/src/main.rs | 1 - rust/lanzatool/src/utils.rs | 11 ----------- 2 files changed, 12 deletions(-) delete mode 100644 rust/lanzatool/src/utils.rs diff --git a/rust/lanzatool/src/main.rs b/rust/lanzatool/src/main.rs index 07cdb65..80513ab 100644 --- a/rust/lanzatool/src/main.rs +++ b/rust/lanzatool/src/main.rs @@ -4,7 +4,6 @@ mod generation; mod install; mod pe; mod signature; -mod utils; use anyhow::Result; use clap::Parser; diff --git a/rust/lanzatool/src/utils.rs b/rust/lanzatool/src/utils.rs deleted file mode 100644 index c9ac877..0000000 --- a/rust/lanzatool/src/utils.rs +++ /dev/null @@ -1,11 +0,0 @@ -use std::path::Path; - -// All Linux file paths should be convertable to strings -pub fn path_to_string(path: impl AsRef) -> String { - String::from(path.as_ref().to_str().unwrap_or_else(|| { - panic!( - "Failed to convert path '{}' to a string", - path.as_ref().display() - ) - })) -} From f6ae3735008bb08193bb9ed5fcd481c7ee3e7cd8 Mon Sep 17 00:00:00 2001 From: Julian Stecklina Date: Wed, 28 Dec 2022 23:51:51 +0100 Subject: [PATCH 4/4] lanzatool: apply rustfmt to install.rs --- rust/lanzatool/src/install.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/lanzatool/src/install.rs b/rust/lanzatool/src/install.rs index bb33486..a4bdc3d 100644 --- a/rust/lanzatool/src/install.rs +++ b/rust/lanzatool/src/install.rs @@ -36,9 +36,8 @@ impl Installer { pub fn install(&self) -> Result<()> { for toplevel in &self.generations { - let generation_result = Generation::from_toplevel(toplevel).with_context(|| { - format!("Failed to build generation from toplevel: {toplevel:?}") - }); + let generation_result = Generation::from_toplevel(toplevel) + .with_context(|| format!("Failed to build generation from toplevel: {toplevel:?}")); let generation = match generation_result { Ok(generation) => generation, @@ -47,7 +46,7 @@ impl Installer { continue; } }; - + println!("Installing generation {generation}"); self.install_generation(&generation)