@@ -159,10 +159,21 @@ pub fn write_rootfs_image_file(
159159
160160 let tmp_path = temporary_injection_path(image_path);
161161 fs::write(&tmp_path, contents).map_err(|e| format!("write {}: {e}", tmp_path.display()))?;
162- let _ = run_debugfs(image_path, &format!("rm {guest_path}"));
162+ let Some(quoted_guest_path) = debugfs_quote_absolute_path(guest_path) else {
163+ let _ = fs::remove_file(&tmp_path);
164+ return Err(format!("invalid debugfs guest path '{guest_path}'"));
165+ };
166+ let Some(quoted_tmp_path) = debugfs_quote_argument(&tmp_path.to_string_lossy()) else {
167+ let _ = fs::remove_file(&tmp_path);
168+ return Err(format!(
169+ "invalid debugfs injection path '{}'",
170+ tmp_path.display()
171+ ));
172+ };
173+ let _ = run_debugfs(image_path, &format!("rm {quoted_guest_path}"));
163174 let result = run_debugfs(
164175 image_path,
165- &format!("write {} {}", tmp_path.display(), guest_path ),
176+ &format!("write {quoted_tmp_path } {quoted_guest_path}" ),
166177 );
167178 let _ = fs::remove_file(&tmp_path);
168179 result
@@ -174,9 +185,12 @@ pub fn set_rootfs_image_file_mode(
174185 mode: u32,
175186) -> Result<(), String> {
176187 let regular_file_mode = 0o100_000 | (mode & 0o7777);
188+ let Some(quoted_guest_path) = debugfs_quote_absolute_path(guest_path) else {
189+ return Err(format!("invalid debugfs guest path '{guest_path}'"));
190+ };
177191 run_debugfs(
178192 image_path,
179- &format!("set_inode_field {guest_path } mode 0{regular_file_mode:o}"),
193+ &format!("set_inode_field {quoted_guest_path } mode 0{regular_file_mode:o}"),
180194 )
181195}
182196
@@ -539,7 +553,7 @@ fn collect_sandbox_owner_commands(
539553 return Ok(true);
540554 }
541555
542- let Some(quoted_guest_path) = debugfs_quote_path (guest_path) else {
556+ let Some(quoted_guest_path) = debugfs_quote_absolute_path (guest_path) else {
543557 return Ok(false);
544558 };
545559 commands.push(format!("set_inode_field {quoted_guest_path} uid {uid}"));
@@ -579,14 +593,22 @@ fn collect_sandbox_owner_commands(
579593 Ok(true)
580594}
581595
582- fn debugfs_quote_path (path: &str) -> Option<String> {
596+ fn debugfs_quote_absolute_path (path: &str) -> Option<String> {
583597 if path.is_empty() || !path.starts_with('/') {
584598 return None;
585599 }
586600
587- let mut quoted = String::with_capacity(path.len() + 2);
601+ debugfs_quote_argument(path)
602+ }
603+
604+ fn debugfs_quote_argument(argument: &str) -> Option<String> {
605+ if argument.is_empty() {
606+ return None;
607+ }
608+
609+ let mut quoted = String::with_capacity(argument.len() + 2);
588610 quoted.push('"');
589- for ch in path .chars() {
611+ for ch in argument .chars() {
590612 match ch {
591613 '\0' | '\n' | '\r' => return None,
592614 '\\' => quoted.push_str("\\\\"),
@@ -1084,6 +1106,19 @@ mod tests {
10841106 let _ = fs::remove_dir_all(&dir);
10851107 }
10861108
1109+ #[test]
1110+ fn debugfs_quote_argument_quotes_source_paths_with_spaces() {
1111+ assert_eq!(
1112+ debugfs_quote_argument("/tmp/openshell state/.openshell-rootfs-inject-123-0"),
1113+ Some("\"/tmp/openshell state/.openshell-rootfs-inject-123-0\"".to_string())
1114+ );
1115+ assert_eq!(
1116+ debugfs_quote_argument("/tmp/path/with\\backslash/and\"quote"),
1117+ Some("\"/tmp/path/with\\\\backslash/and\\\"quote\"".to_string())
1118+ );
1119+ assert_eq!(debugfs_quote_argument("/tmp/bad\npath"), None);
1120+ }
1121+
10871122 fn unique_temp_dir() -> PathBuf {
10881123 static COUNTER: AtomicU64 = AtomicU64::new(0);
10891124 let nanos = SystemTime::now()
0 commit comments