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
This commit is contained in:
Sam Leffler 2021-10-04 21:50:21 +00:00
parent 0e3ca70769
commit a91f8f7916
2 changed files with 14 additions and 4 deletions

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
cstr_core = { version = "0.2.3", default-features = false } cstr_core = "0.2.3"
kata-security-interface = { path = "../../SecurityCoordinator/kata-security-interface" } kata-security-interface = { path = "../../SecurityCoordinator/kata-security-interface" }
postcard = { version = "0.7", features = ["alloc"], default-features = false } postcard = { version = "0.7", features = ["alloc"], default-features = false }
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] }

View File

@ -6,6 +6,7 @@ extern crate alloc;
use alloc::string::String; use alloc::string::String;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::str; use core::str;
use cstr_core::CString;
use kata_security_interface::SecurityRequestError; use kata_security_interface::SecurityRequestError;
use postcard; use postcard;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -152,6 +153,12 @@ impl From<SecurityRequestError> for ProcessManagerError {
} }
} }
impl From<cstr_core::NulError> for ProcessManagerError {
fn from(_err: cstr_core::NulError) -> ProcessManagerError {
ProcessManagerError::BundleIdInvalid
}
}
impl From<ProcessManagerError> for Result<(), ProcessManagerError> { impl From<ProcessManagerError> for Result<(), ProcessManagerError> {
fn from(err: ProcessManagerError) -> Result<(), ProcessManagerError> { fn from(err: ProcessManagerError) -> Result<(), ProcessManagerError> {
if err == ProcessManagerError::Success { if err == ProcessManagerError::Success {
@ -204,7 +211,8 @@ pub fn kata_pkg_mgmt_uninstall(bundle_id: &str) -> Result<(), ProcessManagerErro
extern "C" { extern "C" {
fn pkg_mgmt_uninstall(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; 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] #[inline]
@ -213,7 +221,8 @@ pub fn kata_proc_ctrl_start(bundle_id: &str) -> Result<(), ProcessManagerError>
extern "C" { extern "C" {
fn proc_ctrl_start(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; 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] #[inline]
@ -222,7 +231,8 @@ pub fn kata_proc_ctrl_stop(bundle_id: &str) -> Result<(), ProcessManagerError> {
extern "C" { extern "C" {
fn proc_ctrl_stop(c_bundle_id: *const cstr_core::c_char) -> ProcessManagerError; 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? // TODO(sleffler): move out of interface?