diff --git a/nvx/src/protocol/exec.rs b/nvx/src/protocol/exec.rs index 20c71e0e0..91a630a3e 100644 --- a/nvx/src/protocol/exec.rs +++ b/nvx/src/protocol/exec.rs @@ -131,9 +131,7 @@ pub(crate) fn encode_exec_payload(workload: &Workload<'_>) -> Result, Pr "exec arguments must be 1 to {MAX_ARGUMENT_BYTES} bytes without NUL characters" ))); } - let length = u32::try_from(bytes.len()).unwrap_or(u32::MAX); - payload.extend_from_slice(&length.to_le_bytes()); - payload.extend_from_slice(bytes); + push_prefixed(&mut payload, bytes); } payload.extend_from_slice(cwd.unwrap_or_default()); let mut names = BTreeSet::new(); @@ -156,9 +154,7 @@ pub(crate) fn encode_exec_payload(workload: &Workload<'_>) -> Result, Pr "exec environment names must be unique, but {name:?} repeats" ))); } - let length = u32::try_from(bytes.len()).unwrap_or(u32::MAX); - payload.extend_from_slice(&length.to_le_bytes()); - payload.extend_from_slice(bytes); + push_prefixed(&mut payload, bytes); } if APP_HEADER_LEN + payload.len() > OUTER_MAX_PAYLOAD { return Err(violation("exec request exceeds the control protocol limit")); @@ -166,6 +162,16 @@ pub(crate) fn encode_exec_payload(workload: &Workload<'_>) -> Result, Pr Ok(payload) } +/// Appends `bytes`, an argument or environment entry, to `payload` after their `u32` length, as +/// [`prefixed_string`] reads them. Its callers bound `bytes` by [`MAX_ARGUMENT_BYTES`], so the +/// length fits. +#[cfg(feature = "cli")] +fn push_prefixed(payload: &mut Vec, bytes: &[u8]) { + let length = u32::try_from(bytes.len()).unwrap_or(u32::MAX); + payload.extend_from_slice(&length.to_le_bytes()); + payload.extend_from_slice(bytes); +} + /// An `EXEC` payload as the guest agent reads it. #[cfg(feature = "testing")] #[derive(Debug, Clone, PartialEq, Eq)] @@ -269,7 +275,6 @@ struct ExecHeader { all(feature = "guest", target_os = "linux", target_arch = "x86_64") ))] fn decode_exec_header(payload: &[u8], max_cwd: usize) -> Result { - let truncated = || violation("the exec request is truncated"); if payload.len() < header::LEGACY_LEN { return Err(truncated()); } @@ -374,9 +379,7 @@ fn decode_exec( } else { let what = "working directory"; let cwd = S::convert(bytes_at(payload, offset, cwd_len, what)?.to_vec(), what)?; - offset = offset - .checked_add(cwd_len) - .ok_or_else(|| violation("the exec request is truncated"))?; + offset = offset.checked_add(cwd_len).ok_or_else(truncated)?; if cwd.as_ref().first() != Some(&b'/') { return Err(violation( "exec working directory must be an absolute guest path", @@ -537,20 +540,17 @@ fn prefixed_string( offset: &mut usize, what: &str, ) -> Result { - let length = u32_at(payload, *offset) - .ok_or_else(|| violation("the exec request is truncated"))? as usize; + let length = u32_at(payload, *offset).ok_or_else(truncated)? as usize; *offset = offset .checked_add(LENGTH_PREFIX_LEN) - .ok_or_else(|| violation("the exec request is truncated"))?; + .ok_or_else(truncated)?; if length == 0 || length > MAX_ARGUMENT_BYTES { return Err(violation(format!( "exec {what} must be 1 to {MAX_ARGUMENT_BYTES} bytes" ))); } let bytes = bytes_at(payload, *offset, length, what)?.to_vec(); - *offset = offset - .checked_add(length) - .ok_or_else(|| violation("the exec request is truncated"))?; + *offset = offset.checked_add(length).ok_or_else(truncated)?; S::convert(bytes, what) } @@ -568,7 +568,7 @@ fn bytes_at<'a>( let bytes = offset .checked_add(length) .and_then(|end| payload.get(offset..end)) - .ok_or_else(|| violation("the exec request is truncated"))?; + .ok_or_else(truncated)?; if bytes.contains(&0) { return Err(violation(format!( "exec {what} must not contain NUL characters" @@ -576,3 +576,15 @@ fn bytes_at<'a>( } Ok(bytes) } + +/// Returns the error of an `EXEC` payload that ends before its header or one of its strings does. +#[cfg(any( + feature = "testing", + all(feature = "guest", target_os = "linux", target_arch = "x86_64") +))] +fn truncated() -> ProtocolError { + violation("the exec request is truncated") +} + +#[cfg(all(test, feature = "cli", feature = "testing"))] +mod tests; diff --git a/nvx/src/protocol/exec/tests.rs b/nvx/src/protocol/exec/tests.rs new file mode 100644 index 000000000..8af98c031 --- /dev/null +++ b/nvx/src/protocol/exec/tests.rs @@ -0,0 +1,32 @@ +use super::{LENGTH_PREFIX_LEN, MAX_ARGUMENT_BYTES, prefixed_string, push_prefixed, truncated}; + +#[test] +fn prefixed_strings_read_back_as_they_were_pushed() { + let longest = "x".repeat(MAX_ARGUMENT_BYTES); + let mut payload = Vec::new(); + push_prefixed(&mut payload, b"/bin/sh"); + push_prefixed(&mut payload, longest.as_bytes()); + assert_eq!(payload[..LENGTH_PREFIX_LEN], 7_u32.to_le_bytes()); + + let mut offset = 0; + let program = prefixed_string::(&payload, &mut offset, "arguments").unwrap(); + assert_eq!(program, "/bin/sh"); + assert_eq!(offset, LENGTH_PREFIX_LEN + program.len()); + let argument = prefixed_string::(&payload, &mut offset, "arguments").unwrap(); + assert_eq!(argument, longest); + assert_eq!(offset, payload.len()); +} + +#[test] +fn a_prefixed_string_cut_short_is_truncated() { + let mut payload = Vec::new(); + push_prefixed(&mut payload, b"/bin/sh"); + for end in 0..payload.len() { + let mut offset = 0; + assert_eq!( + prefixed_string::(&payload[..end], &mut offset, "arguments"), + Err(truncated()), + "a payload cut at {end} bytes" + ); + } +}