Skip to content

Commit

Permalink
Add a wire protocol version check
Browse files Browse the repository at this point in the history
As per the discussion in parallaxsecond/parsec#105, it was decided that
the version check should happen in the front-end handler. Because there
is currently only one version supported (0.1), and it might stay like
this for a long time, this commit adds a simple check directly in the
request/response parser.
If multiple versions are supported, this would not suffice and a better
solution should be adopted but this is probably fine for now.

Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
  • Loading branch information
hug-dev committed Feb 21, 2020
1 parent 8ceba4f commit 26c6e57
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parsec-interface"
version = "0.7.1"
version = "0.8.1"
authors = ["Paul Howard <paul.howard@arm.com>",
"Ionut Mihalcea <ionut.mihalcea@arm.com>",
"Hugues de Valon <hugues.devalon@arm.com>"]
Expand Down
10 changes: 5 additions & 5 deletions src/requests/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ mod tests {
let resp_hdr: ResponseHeader = req_hdr.into();

let mut resp_hdr_exp = ResponseHeader::new();
resp_hdr_exp.version_maj = 0xde;
resp_hdr_exp.version_min = 0xf0;
resp_hdr_exp.version_maj = 0x00;
resp_hdr_exp.version_min = 0x01;
resp_hdr_exp.provider = ProviderID::CoreProvider;
resp_hdr_exp.session = 0x11_22_33_44_55_66_77_88;
resp_hdr_exp.content_type = BodyType::Protobuf;
Expand All @@ -217,8 +217,8 @@ mod tests {
let body = RequestBody::from_bytes(vec![0x70, 0x80, 0x90]);
let auth = RequestAuth::from_bytes(vec![0xa0, 0xb0, 0xc0]);
let header = RequestHeader {
version_maj: 0xde,
version_min: 0xf0,
version_maj: 0x00,
version_min: 0x01,
provider: ProviderID::CoreProvider,
session: 0x11_22_33_44_55_66_77_88,
content_type: BodyType::Protobuf,
Expand All @@ -231,7 +231,7 @@ mod tests {

fn get_request_bytes() -> Vec<u8> {
vec![
0x10, 0xA7, 0xC0, 0x5E, 0x16, 0x00, 0xde, 0xf0, 0x00, 0x88, 0x77, 0x66, 0x55, 0x44,
0x10, 0xA7, 0xC0, 0x5E, 0x16, 0x00, 0x00, 0x01, 0x00, 0x88, 0x77, 0x66, 0x55, 0x44,
0x33, 0x22, 0x11, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
0x70, 0x80, 0x90, 0xa0, 0xb0, 0xc0,
]
Expand Down
8 changes: 7 additions & 1 deletion src/requests/request/request_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl RawRequestHeader {
/// sent across
/// - if the parsed bytes cannot be unmarshalled into the contained fields,
/// `ResponseStatus::InvalidEncoding` is returned.
/// - if the wire protocol version used is different than 0.1
pub fn read_from_stream<R: Read>(mut stream: &mut R) -> Result<RawRequestHeader> {
let magic_number = get_from_stream!(stream, u32);
let hdr_size = get_from_stream!(stream, u16);
Expand All @@ -96,7 +97,12 @@ impl RawRequestHeader {
let mut bytes = vec![0_u8; usize::try_from(hdr_size)?];
stream.read_exact(&mut bytes)?;

Ok(bincode::deserialize(&bytes)?)
let raw_request: RawRequestHeader = bincode::deserialize(&bytes)?;
if raw_request.version_maj != 0 || raw_request.version_min != 1 {
Err(ResponseStatus::WireProtocolVersionNotSupported)
} else {
Ok(raw_request)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/requests/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ mod tests {
fn get_response() -> Response {
let body = ResponseBody::from_bytes(vec![0x70, 0x80, 0x90]);
let header = ResponseHeader {
version_maj: 0xde,
version_min: 0xf0,
version_maj: 0x00,
version_min: 0x01,
provider: ProviderID::CoreProvider,
session: 0x11_22_33_44_55_66_77_88,
content_type: BodyType::Protobuf,
Expand All @@ -193,7 +193,7 @@ mod tests {

fn get_response_bytes() -> Vec<u8> {
vec![
0x10, 0xA7, 0xC0, 0x5E, 0x14, 0x00, 0xde, 0xf0, 0x00, 0x88, 0x77, 0x66, 0x55, 0x44,
0x10, 0xA7, 0xC0, 0x5E, 0x14, 0x00, 0x00, 0x01, 0x00, 0x88, 0x77, 0x66, 0x55, 0x44,
0x33, 0x22, 0x11, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x70, 0x80,
0x90,
]
Expand Down
10 changes: 8 additions & 2 deletions src/requests/response/response_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl RawResponseHeader {
/// sent across
/// - if the parsed bytes cannot be unmarshalled into the contained fields,
/// an error of kind `ErrorKind::InvalidData` is returned
/// - if the wire protocol version used is different than 0.1
pub fn read_from_stream(mut stream: &mut impl Read) -> Result<RawResponseHeader> {
let magic_number = get_from_stream!(stream, u32);
let hdr_size = get_from_stream!(stream, u16);
Expand All @@ -89,7 +90,12 @@ impl RawResponseHeader {
let mut bytes = vec![0_u8; usize::try_from(hdr_size)?];
stream.read_exact(&mut bytes)?;

Ok(bincode::deserialize(&bytes)?)
let raw_response: RawResponseHeader = bincode::deserialize(&bytes)?;
if raw_response.version_maj != 0 || raw_response.version_min != 1 {
Err(ResponseStatus::WireProtocolVersionNotSupported)
} else {
Ok(raw_response)
}
}
}

Expand All @@ -113,7 +119,7 @@ impl ResponseHeader {
pub(crate) fn new() -> ResponseHeader {
ResponseHeader {
version_maj: 0,
version_min: 0,
version_min: 1,
provider: ProviderID::CoreProvider,
session: 0,
content_type: BodyType::Protobuf,
Expand Down
4 changes: 2 additions & 2 deletions src/requests/response_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ResponseStatus {
WrongProviderID = 1,
ContentTypeNotSupported = 2,
AcceptTypeNotSupported = 3,
VersionTooBig = 4,
WireProtocolVersionNotSupported = 4,
ProviderNotRegistered = 5,
ProviderDoesNotExist = 6,
DeserializingBodyFailed = 7,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl fmt::Display for ResponseStatus {
ResponseStatus::AcceptTypeNotSupported => {
write!(f, "requested accept type is not supported by the backend")
}
ResponseStatus::VersionTooBig => {
ResponseStatus::WireProtocolVersionNotSupported => {
write!(f, "requested version is not supported by the backend")
}
ResponseStatus::ProviderNotRegistered => {
Expand Down

0 comments on commit 26c6e57

Please sign in to comment.