Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions updatehub/src/object/installer/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
10 changes: 3 additions & 7 deletions updatehub/src/object/installer/imxkobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,19 @@ 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 {
write!(cmd, " --search_exponent={}", self.search_exponent).unwrap();
}

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";
Expand Down
10 changes: 7 additions & 3 deletions updatehub/src/object/installer/ubifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand All @@ -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(())
Expand Down
33 changes: 32 additions & 1 deletion updatehub/src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<std::ffi::OsStr>) -> Result<String> {
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<MountGuard> {
let directory = tempfile::tempdir()?;
let dest = directory.path();
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions updatehub/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 6 additions & 2 deletions updatehub/src/utils/mtd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub(crate) fn target_device_from_ubi_volume_name(volume: &str) -> Result<PathBuf
.filter_map(std::result::Result::ok)
.find_map(|entry| {
let path = entry.path();
let output =
easy_process::run(&format!("ubinfo {} -N {}", path.display(), volume)).ok()?;
let output = easy_process::run(&format!(
"ubinfo {} -N {}",
super::fs::shell_quote(path).ok()?,
super::fs::shell_quote(volume).ok()?
))
.ok()?;

let line = output.stdout.lines().next()?;
let re_match = re.captures(line)?;
Expand Down