Skip to content

Commit 3834e84

Browse files
RoyLinRoyLin
authored andcommitted
fix: sanitize kernel cmdline to prevent InvalidAscii panic on macOS
Add insert_str_safe() that replaces non-printable ASCII characters with '?' before inserting into kernel cmdline. This prevents InvalidAscii errors when macOS system info contains emoji.
1 parent 47478c1 commit 3834e84

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/kernel/src/cmdline/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,26 @@ impl Cmdline {
154154
Ok(())
155155
}
156156

157+
/// Inserts a string, replacing non-printable ASCII characters with '?'.
158+
/// This is safe for use with strings that may contain emoji or special characters
159+
/// (e.g., from macOS system info) that would otherwise cause InvalidAscii errors.
160+
pub fn insert_str_safe<T: AsRef<str>>(&mut self, slug: T) -> Result<()> {
161+
let s = slug.as_ref();
162+
let sanitized: String = s.chars().map(|c| if valid_char(c) { c } else { '?' }).collect();
163+
let trimmed = sanitized.trim();
164+
if trimmed.is_empty() {
165+
return Ok(());
166+
}
167+
168+
self.has_capacity(trimmed.len())?;
169+
170+
self.start_push();
171+
self.line.push_str(trimmed);
172+
self.end_push();
173+
174+
Ok(())
175+
}
176+
157177
/// Returns the cmdline in progress without nul termination.
158178
pub fn as_str(&self) -> &str {
159179
self.line.as_str()

src/vmm/src/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,9 +1948,9 @@ pub fn build_microvm(
19481948
#[allow(unused_mut)]
19491949
let mut kernel_cmdline = Cmdline::new(arch::CMDLINE_MAX_SIZE);
19501950
if let Some(cmdline) = payload_config.kernel_cmdline {
1951-
kernel_cmdline.insert_str(cmdline.as_str()).unwrap();
1951+
kernel_cmdline.insert_str_safe(cmdline.as_str()).unwrap();
19521952
} else if let Some(cmdline) = &vm_resources.kernel_cmdline.prolog {
1953-
kernel_cmdline.insert_str(cmdline).unwrap();
1953+
kernel_cmdline.insert_str_safe(cmdline).unwrap();
19541954
} else {
19551955
kernel_cmdline.insert_str(DEFAULT_KERNEL_CMDLINE).unwrap();
19561956
}
@@ -1978,7 +1978,7 @@ pub fn build_microvm(
19781978
format!("console={kernel_console}").as_str(),
19791979
);
19801980
kernel_cmdline = Cmdline::new(arch::CMDLINE_MAX_SIZE);
1981-
kernel_cmdline.insert_str(cmdline).unwrap();
1981+
kernel_cmdline.insert_str_safe(cmdline).unwrap();
19821982
}
19831983

19841984
#[cfg(target_os = "windows")]
@@ -2547,7 +2547,7 @@ pub fn build_microvm(
25472547
}
25482548

25492549
if let Some(s) = &vm_resources.kernel_cmdline.epilog {
2550-
vmm.kernel_cmdline.insert_str(s).unwrap();
2550+
vmm.kernel_cmdline.insert_str_safe(s).unwrap();
25512551
};
25522552

25532553
#[cfg(all(target_arch = "x86_64", target_os = "windows"))]

0 commit comments

Comments
 (0)