Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore duplicated version number entries exist in VERSION response. #152

Merged
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
22 changes: 11 additions & 11 deletions spdmlib/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,20 +606,20 @@ mod tests {

#[test]
fn test_case0_spdm_message() {
let mut versions = gen_array_clone(SpdmVersionStruct::default(), MAX_SPDM_VERSION_COUNT);
versions[0].update = 100;
versions[0].version = SpdmVersion::SpdmVersion10;
versions[1].update = 100;
versions[1].version = SpdmVersion::SpdmVersion11;

let value = SpdmMessage {
header: SpdmMessageHeader {
version: SpdmVersion::SpdmVersion10,
request_response_code: SpdmRequestResponseCode::SpdmResponseVersion,
},
payload: SpdmMessagePayload::SpdmVersionResponse(SpdmVersionResponsePayload {
version_number_entry_count: 0x02,
versions: gen_array_clone(
SpdmVersionStruct {
update: 100,
version: SpdmVersion::SpdmVersion11,
},
MAX_SPDM_VERSION_COUNT,
),
versions,
}),
};

Expand All @@ -633,10 +633,10 @@ mod tests {
);
if let SpdmMessagePayload::SpdmVersionResponse(payload) = &spdm_message.payload {
assert_eq!(payload.version_number_entry_count, 0x02);
for i in 0..2 {
assert_eq!(payload.versions[i].update, 100);
assert_eq!(payload.versions[i].version, SpdmVersion::SpdmVersion11);
}
assert_eq!(payload.versions[0].update, 100);
assert_eq!(payload.versions[0].version, SpdmVersion::SpdmVersion10);
assert_eq!(payload.versions[1].update, 100);
assert_eq!(payload.versions[1].version, SpdmVersion::SpdmVersion11);
}
}
#[test]
Expand Down
39 changes: 22 additions & 17 deletions spdmlib/src/message/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ impl SpdmCodec for SpdmVersionResponsePayload {
},
MAX_SPDM_VERSION_COUNT,
);
let mut version_exist_map: [u8; MAX_SPDM_VERSION_COUNT] = [0; MAX_SPDM_VERSION_COUNT];
IntelCaisui marked this conversation as resolved.
Show resolved Hide resolved

let mut version_count = 0;
let rest = r.take(version_number_entry_count as usize * 2)?;

for i in 0..version_number_entry_count {
if let Some(ver) = SpdmVersionStruct::read_bytes(&rest[i as usize * 2..]) {
if version_count < MAX_SPDM_VERSION_COUNT {
jyao1 marked this conversation as resolved.
Show resolved Hide resolved
let index = ver.version as usize;
if version_exist_map[index] == 0 {
version_exist_map[index] = 1;
versions[version_count] = ver;
version_count += 1;
} else {
// the buffer is full now, stop for scaning more versions
break;
// for duplicated version, ignore it
}
} else {
// for unknown versions,
Expand Down Expand Up @@ -180,15 +182,15 @@ mod tests {
fn test_case0_spdm_version_response_payload() {
let u8_slice = &mut [0u8; 8];
let mut writer = Writer::init(u8_slice);
let mut versions = gen_array_clone(SpdmVersionStruct::default(), MAX_SPDM_VERSION_COUNT);
versions[0].update = 100;
versions[0].version = SpdmVersion::SpdmVersion10;
versions[1].update = 100;
versions[1].version = SpdmVersion::SpdmVersion11;

let value = SpdmVersionResponsePayload {
version_number_entry_count: 2u8,
versions: gen_array_clone(
SpdmVersionStruct {
update: 100u8,
version: SpdmVersion::SpdmVersion10,
},
MAX_SPDM_VERSION_COUNT,
),
versions,
};

create_spdm_context!(context);
Expand All @@ -200,13 +202,16 @@ mod tests {
SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader).unwrap();

assert_eq!(version_response.version_number_entry_count, 2u8);
for i in 0..2 {
assert_eq!(version_response.versions[i].update, 100u8);
assert_eq!(
version_response.versions[i].version,
SpdmVersion::SpdmVersion10
);
}
assert_eq!(version_response.versions[0].update, 100);
assert_eq!(
version_response.versions[0].version,
SpdmVersion::SpdmVersion10
);
assert_eq!(version_response.versions[1].update, 100);
assert_eq!(
version_response.versions[1].version,
SpdmVersion::SpdmVersion11
);
assert_eq!(0, reader.left());
}
#[test]
Expand Down
20 changes: 17 additions & 3 deletions spdmlib/src/message/version_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern crate alloc;

#[test]
fn test_case1_spdmversion_struct() {
// Validata VERSION response VersionNumberEntryCount beyond maximum allowed size.
// Validate VERSION response VersionNumberEntryCount beyond maximum allowed size.
let u8_slice = &mut [0u8; 100];

// VersionNumberEntryCount = 0xfe
Expand All @@ -19,13 +19,27 @@ fn test_case1_spdmversion_struct() {
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
assert!(res.is_none());

// Validata VERSION response VersionNumberEntryCount 0 size.
// Validate VERSION response VersionNumberEntryCount 0 size.
let u8_slice = &mut [0u8; 100];

// VersionNumberEntryCount = 0x0
u8_slice[3] = 0;
let mut reader = Reader::init(u8_slice);
create_spdm_context!(context);
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
assert!(res.is_none())
assert!(res.is_none());

// Validate VERSION response VersionNumberEntryCount beyond MAX_SPDM_VERSION_COUNT and with duplicated version entries.
let u8_slice: &mut [u8; 16] = &mut [
0x0, 0x0, 0x0, 0x6, 0x0, 0x10, 0x0, 0x10, 0x0, 0x11, 0x0, 0x12, 0x0, 0x13, 0x0, 0x13,
];
let mut reader = Reader::init(u8_slice);
create_spdm_context!(context);
let res = SpdmVersionResponsePayload::spdm_read(&mut context, &mut reader);
let version = res.unwrap();
assert_eq!(version.version_number_entry_count, 4);
assert_eq!(version.versions[0].version, SpdmVersion::SpdmVersion10);
assert_eq!(version.versions[1].version, SpdmVersion::SpdmVersion11);
assert_eq!(version.versions[2].version, SpdmVersion::SpdmVersion12);
assert_eq!(version.versions[3].version, SpdmVersion::SpdmVersion13);
}
Loading