From a03b3ee42d9d0fdb561e9913b124c9a24c382d54 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Sat, 28 Nov 2026 09:07:54 -0800 Subject: [PATCH] nvx: build the EXEC payload's truncation error and length-prefixed strings with one helper each The EXEC codec spelled the error of a payload that ends early, "the exec request is truncated", at six sites: a local closure in decode_exec_header and inline literals in decode_exec, prefixed_string, and bytes_at. Its encoder also wrote each argument and environment entry with the same three lines, a u32 length and the bytes, which the decoder already reads through one prefixed_string. Name the error once in a module-level truncated(), as the MAPS codec does, and write the length-prefixed strings with push_prefixed(), the encoder's counterpart of prefixed_string. The message, its ProtocolError, the encoded bytes (including the saturating u32 conversion that the MAX_ARGUMENT_BYTES checks before it never reach), the order of the checks, and every cfg stay the same; no public item changes. The protocol tests pass unchanged at the base revision, 895d8f922be (109 passed, cargo test --all-features --lib protocol:: on Windows). New unit tests in protocol/exec/tests.rs read pushed strings back through prefixed_string, the longest included, and check that every shorter cut of one reads as the shared truncation error. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- nvx/src/protocol/exec.rs | 46 +++++++++++++++++++++------------- nvx/src/protocol/exec/tests.rs | 32 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 nvx/src/protocol/exec/tests.rs 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" + ); + } +}