Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions nvx/src/protocol/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ pub(crate) fn encode_exec_payload(workload: &Workload<'_>) -> Result<Vec<u8>, 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();
Expand All @@ -156,16 +154,24 @@ pub(crate) fn encode_exec_payload(workload: &Workload<'_>) -> Result<Vec<u8>, 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"));
}
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<u8>, 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)]
Expand Down Expand Up @@ -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<ExecHeader, ProtocolError> {
let truncated = || violation("the exec request is truncated");
if payload.len() < header::LEGACY_LEN {
return Err(truncated());
}
Expand Down Expand Up @@ -374,9 +379,7 @@ fn decode_exec<S: ExecString>(
} 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",
Expand Down Expand Up @@ -537,20 +540,17 @@ fn prefixed_string<S: ExecString>(
offset: &mut usize,
what: &str,
) -> Result<S, ProtocolError> {
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)
}

Expand All @@ -568,11 +568,23 @@ 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"
)));
}
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;
32 changes: 32 additions & 0 deletions nvx/src/protocol/exec/tests.rs
Original file line number Diff line number Diff line change
@@ -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::<String>(&payload, &mut offset, "arguments").unwrap();
assert_eq!(program, "/bin/sh");
assert_eq!(offset, LENGTH_PREFIX_LEN + program.len());
let argument = prefixed_string::<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::<String>(&payload[..end], &mut offset, "arguments"),
Err(truncated()),
"a payload cut at {end} bytes"
);
}
}
Loading