From a91f8f79162e04c271592e894114f82cfea995d2 Mon Sep 17 00:00:00 2001 From: Sam Leffler Date: Mon, 4 Oct 2021 21:50:21 +0000 Subject: [PATCH] kata-proc-interface: marshal bundle_id params correctly Over-optimized the marhaling logic--we Need to convert &str's to CString's to get the trailing \0 (same lesson learned doing StorageManager). Change-Id: I97e8d35947dcb32989dc68f11dd422433c00aa02 GitOrigin-RevId: c775bfc3c568ee3dcae32928a422c5178495e77f --- .../kata-proc-interface/Cargo.toml | 2 +- .../kata-proc-interface/src/lib.rs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apps/system/components/ProcessManager/kata-proc-interface/Cargo.toml b/apps/system/components/ProcessManager/kata-proc-interface/Cargo.toml index 267dac4..1fc5497 100644 --- a/apps/system/components/ProcessManager/kata-proc-interface/Cargo.toml +++ b/apps/system/components/ProcessManager/kata-proc-interface/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2018" [dependencies] -cstr_core = { version = "0.2.3", default-features = false } +cstr_core = "0.2.3" kata-security-interface = { path = "../../SecurityCoordinator/kata-security-interface" } postcard = { version = "0.7", features = ["alloc"], default-features = false } serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } diff --git a/apps/system/components/ProcessManager/kata-proc-interface/src/lib.rs b/apps/system/components/ProcessManager/kata-proc-interface/src/lib.rs index ca40971..4384cca 100644 --- a/apps/system/components/ProcessManager/kata-proc-interface/src/lib.rs +++ b/apps/system/components/ProcessManager/kata-proc-interface/src/lib.rs @@ -6,6 +6,7 @@ extern crate alloc; use alloc::string::String; use alloc::vec::Vec; use core::str; +use cstr_core::CString; use kata_security_interface::SecurityRequestError; use postcard; use serde::{Deserialize, Serialize}; @@ -152,6 +153,12 @@ impl From for ProcessManagerError { } } +impl From for ProcessManagerError { + fn from(_err: cstr_core::NulError) -> ProcessManagerError { + ProcessManagerError::BundleIdInvalid + } +} + impl From for Result<(), ProcessManagerError> { fn from(err: ProcessManagerError) -> Result<(), ProcessManagerError> { if err == ProcessManagerError::Success { @@ -204,7 +211,8 @@ pub fn kata_pkg_mgmt_uninstall(bundle_id: &str) -> Result<(), ProcessManagerErro extern "C" { fn pkg_mgmt_uninstall(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; } - unsafe { pkg_mgmt_uninstall(bundle_id.as_ptr()) }.into() + let cstr = CString::new(bundle_id)?; + unsafe { pkg_mgmt_uninstall(cstr.as_ptr()) }.into() } #[inline] @@ -213,7 +221,8 @@ pub fn kata_proc_ctrl_start(bundle_id: &str) -> Result<(), ProcessManagerError> extern "C" { fn proc_ctrl_start(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; } - unsafe { proc_ctrl_start(bundle_id.as_ptr()) }.into() + let cstr = CString::new(bundle_id)?; + unsafe { proc_ctrl_start(cstr.as_ptr()) }.into() } #[inline] @@ -222,7 +231,8 @@ pub fn kata_proc_ctrl_stop(bundle_id: &str) -> Result<(), ProcessManagerError> { extern "C" { fn proc_ctrl_stop(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; } - unsafe { proc_ctrl_stop(bundle_id.as_ptr()) }.into() + let cstr = CString::new(bundle_id)?; + unsafe { proc_ctrl_stop(cstr.as_ptr()) }.into() } // TODO(sleffler): move out of interface?