Skip to content

Commit 8fc7a5f

Browse files
authored
Merge pull request #2316 from microsoft/agents/issue-2315-view-hostname-shared-hex
nvx: decode the view's hostname with the shared hex parser
2 parents 840b404 + 3a00cf8 commit 8fc7a5f

4 files changed

Lines changed: 43 additions & 21 deletions

File tree

‎nvx/src/hex.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub(crate) fn hex(bytes: &[u8]) -> String {
1919
/// Parses exactly `2 * N` hexadecimal digits, in either case, into the `N` bytes that they spell.
2020
///
2121
/// A sign, whitespace, and any other byte that is not a digit, ASCII or not, refuses the text.
22-
#[cfg(any(feature = "host", all(feature = "guest", target_os = "linux")))]
2322
pub(crate) fn parse_hex<const N: usize>(text: impl AsRef<[u8]>) -> Option<[u8; N]> {
2423
let digits = text.as_ref();
2524
if digits.len() != 2 * N {

‎nvx/src/hex/tests.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ fn hexadecimal_text_is_written_in_lowercase() {
2222
assert_eq!(Hex(&identity).to_string(), hex(&identity));
2323
}
2424

25-
#[cfg(any(feature = "host", all(feature = "guest", target_os = "linux")))]
2625
#[test]
2726
fn hexadecimal_text_is_read_in_either_case() {
2827
let bytes = [0x00, 0x0a, 0xb1, 0xff];

‎nvx/src/protocol/view.rs‎

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use super::cmdline;
1212
use super::{ProtocolError, violation};
13-
use crate::hex::hex;
13+
use crate::hex::{hex, parse_hex};
1414

1515
/// The longest hostname, in bytes: Linux's `HOST_NAME_MAX`, which the guest's entry applies.
1616
pub const MAX_HOSTNAME_BYTES: usize = 64;
@@ -121,29 +121,24 @@ pub fn parse(command_line: &[u8]) -> Result<Option<View>, ProtocolError> {
121121

122122
/// Decodes a bounded, canonical hostname value, refusing a decoded NUL.
123123
fn parse_hostname(value: &[u8]) -> Result<Vec<u8>, ProtocolError> {
124+
let malformed = || {
125+
violation(format!(
126+
"the nvx_hostname kernel parameter must encode 1 to {MAX_HOSTNAME_BYTES} bytes \
127+
as lowercase hexadecimal"
128+
))
129+
};
130+
// `parse_hex` reads either case, and the token is lowercase.
124131
if value.is_empty()
125132
|| value.len() > 2 * MAX_HOSTNAME_BYTES
126-
|| !value.len().is_multiple_of(2)
127-
|| !value
128-
.iter()
129-
.all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
133+
|| value.iter().any(u8::is_ascii_uppercase)
130134
{
131-
return Err(violation(format!(
132-
"the nvx_hostname kernel parameter must encode 1 to {MAX_HOSTNAME_BYTES} bytes \
133-
as lowercase hexadecimal"
134-
)));
135+
return Err(malformed());
135136
}
136-
let digit = |byte: u8| {
137-
if byte.is_ascii_digit() {
138-
byte - b'0'
139-
} else {
140-
byte - b'a' + 10
141-
}
142-
};
143137
let hostname: Vec<u8> = value
144-
.chunks_exact(2)
145-
.map(|pair| digit(pair[0]) * 16 + digit(pair[1]))
146-
.collect();
138+
.chunks(2)
139+
.map(|pair| parse_hex::<1>(pair).map(|[byte]| byte))
140+
.collect::<Option<_>>()
141+
.ok_or_else(malformed)?;
147142
if !is_hostname(&hostname) {
148143
return Err(violation("the nvx_hostname kernel parameter encodes a NUL"));
149144
}

‎nvx/src/protocol/view/tests.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ fn hostname_values_must_be_bounded_canonical_hexadecimal_without_a_nul() {
9090
}
9191
}
9292

93+
#[test]
94+
fn malformed_hostname_values_keep_their_messages() {
95+
let encoding = format!(
96+
"the nvx_hostname kernel parameter must encode 1 to {MAX_HOSTNAME_BYTES} bytes as \
97+
lowercase hexadecimal"
98+
);
99+
let too_long = b"62".repeat(MAX_HOSTNAME_BYTES + 1);
100+
for value in [
101+
b"".as_slice(),
102+
b"0",
103+
b"62f",
104+
b"6A",
105+
b"6g",
106+
b"+6",
107+
b"6\xe9",
108+
b"\xff\xff",
109+
&too_long,
110+
] {
111+
let line = [b"nvx_hostname=".as_slice(), value, b" nvx_machine_id=image"].concat();
112+
assert_eq!(parse(&line).unwrap_err().message(), encoding, "{value:?}");
113+
}
114+
assert_eq!(
115+
parse(b"nvx_hostname=62006f nvx_machine_id=image")
116+
.unwrap_err()
117+
.message(),
118+
"the nvx_hostname kernel parameter encodes a NUL"
119+
);
120+
}
121+
93122
#[test]
94123
fn machine_id_values_are_exact_and_unquoted() {
95124
for value in [

0 commit comments

Comments
 (0)