diff --git a/updatehub/src/object/installer/flash.rs b/updatehub/src/object/installer/flash.rs index dd8a8bba..2d16c9de 100644 --- a/updatehub/src/object/installer/flash.rs +++ b/updatehub/src/object/installer/flash.rs @@ -52,12 +52,13 @@ impl Installer for objects::Flash { let is_nand = utils::mtd::is_nand(&target)?; - easy_process::run(&format!("flash_erase {target:?} 0 0"))?; + let (target, source) = (utils::fs::shell_quote(&target)?, utils::fs::shell_quote(&source)?); + easy_process::run(&format!("flash_erase {target} 0 0"))?; if is_nand { - easy_process::run(&format!("nandwrite -p {target:?} {source:?}"))?; + easy_process::run(&format!("nandwrite -p {target} {source}"))?; } else { - easy_process::run(&format!("flashcp {source:?} {target:?}"))?; + easy_process::run(&format!("flashcp {source} {target}"))?; } Ok(()) diff --git a/updatehub/src/object/installer/imxkobs.rs b/updatehub/src/object/installer/imxkobs.rs index fc853280..58fdbfa6 100644 --- a/updatehub/src/object/installer/imxkobs.rs +++ b/updatehub/src/object/installer/imxkobs.rs @@ -58,11 +58,7 @@ impl Installer for objects::Imxkobs { cmd += "-x "; } - cmd += context - .download_dir - .join(self.sha256sum()) - .to_str() - .ok_or(Error::InvalidPath) + cmd += &utils::fs::shell_quote(context.download_dir.join(self.sha256sum())) .log_error_msg("invalid path from download_dir for kobs-ng command")?; if self.search_exponent > 0 { @@ -70,11 +66,11 @@ impl Installer for objects::Imxkobs { } if let Some(chip_0) = &self.chip_0_device_path { - write!(cmd, " --chip_0_device_path={}", chip_0.display()).unwrap(); + write!(cmd, " --chip_0_device_path={}", utils::fs::shell_quote(chip_0)?).unwrap(); } if let Some(chip_1) = &self.chip_1_device_path { - write!(cmd, " --chip_1_device_path={}", chip_1.display()).unwrap(); + write!(cmd, " --chip_1_device_path={}", utils::fs::shell_quote(chip_1)?).unwrap(); } cmd += " -v"; diff --git a/updatehub/src/object/installer/ubifs.rs b/updatehub/src/object/installer/ubifs.rs index 4ef7efe8..f6338a8a 100644 --- a/updatehub/src/object/installer/ubifs.rs +++ b/updatehub/src/object/installer/ubifs.rs @@ -41,7 +41,7 @@ impl Installer for objects::Ubifs { if self.compressed { easy_process::run_with_stdin( - &format!("ubiupdatevol {} -", target.display()), + &format!("ubiupdatevol {} -", utils::fs::shell_quote(&target)?), |stdin| { let mut file = std::fs::File::open(source).log_error_msg("failed open object")?; @@ -52,8 +52,12 @@ impl Installer for objects::Ubifs { ) .log_error_msg("ubiupdatevol failed to run")?; } else { - easy_process::run(&format!("ubiupdatevol {} {}", target.display(), source.display())) - .log_error_msg("ubiupdatevol failed to run")?; + easy_process::run(&format!( + "ubiupdatevol {} {}", + utils::fs::shell_quote(&target)?, + utils::fs::shell_quote(&source)? + )) + .log_error_msg("ubiupdatevol failed to run")?; } Ok(()) diff --git a/updatehub/src/utils/fs.rs b/updatehub/src/utils/fs.rs index c7befaf1..ab473862 100644 --- a/updatehub/src/utils/fs.rs +++ b/updatehub/src/utils/fs.rs @@ -389,7 +389,7 @@ pub(crate) fn format(target: &Path, fs: Filesystem, options: Option<&str>) -> Re ensure_not_mounted(target)?; trace!("formating {:?} as {}", target, fs); - let target = target.display(); + let target = shell_quote(target)?; let options = options.unwrap_or_default(); let cmd = match fs { @@ -419,6 +419,18 @@ pub(crate) fn format(target: &Path, fs: Filesystem, options: Option<&str>) -> Re Ok(()) } +/// Quotes `arg` so `easy_process` passes it as one argument. +/// +/// Its parser is not sh: a backslash escapes the next character even inside +/// single quotes, and an unknown escape drops both characters. A lossy +/// conversion could name a different node, so non-UTF-8 is refused. +pub(crate) fn shell_quote(arg: impl AsRef) -> Result { + let arg = arg.as_ref(); + let arg = arg.to_str().ok_or_else(|| Error::NonUtf8Path(arg.into()))?; + + Ok(format!("'{}'", arg.replace('\\', r"\\").replace('\'', r"\'"))) +} + pub(crate) fn mount(source: &Path, fs: Filesystem, options: &str) -> io::Result { let directory = tempfile::tempdir()?; let dest = directory.path(); @@ -520,6 +532,25 @@ pub(crate) mod tests { format(target.path(), Filesystem::Ext4, None).unwrap(); } + #[test] + fn quoted_argument_reaches_the_command_whole() { + for arg in [r"/dev/disk/by-label/it's a\x20disk", r"/dev/disk\q1", r"trailing\"] { + let quoted = shell_quote(arg).unwrap(); + let output = easy_process::run(&format!("printf %s {quoted}")).unwrap(); + + assert_eq!(output.stdout, arg); + } + } + + #[test] + fn non_utf8_argument_is_refused() { + use std::os::unix::ffi::OsStrExt; + + let arg = std::ffi::OsStr::from_bytes(b"/dev/disk\xff"); + + assert!(matches!(shell_quote(arg), Err(Error::NonUtf8Path(_)))); + } + #[test] fn search_path_override_replaces_the_process_path() { // `sh` is on the real PATH of every host that runs this suite. diff --git a/updatehub/src/utils/mod.rs b/updatehub/src/utils/mod.rs index a6bc51b0..d1d5de89 100644 --- a/updatehub/src/utils/mod.rs +++ b/updatehub/src/utils/mod.rs @@ -66,6 +66,10 @@ pub enum Error { #[from(ignore)] UnresolvedBootEmmc(#[error(not(source))] std::path::PathBuf), + #[display("{_0:?} is not valid UTF-8 and cannot be passed to a command")] + #[from(ignore)] + NonUtf8Path(#[error(not(source))] std::path::PathBuf), + #[display("user doesn't have write permission on target device: {_0:?}")] #[from(ignore)] MissingWritePermission(#[error(not(source))] std::path::PathBuf), diff --git a/updatehub/src/utils/mtd.rs b/updatehub/src/utils/mtd.rs index f8c58fe8..6e88dcbc 100644 --- a/updatehub/src/utils/mtd.rs +++ b/updatehub/src/utils/mtd.rs @@ -21,8 +21,12 @@ pub(crate) fn target_device_from_ubi_volume_name(volume: &str) -> Result