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

Fix alignment issue in transport #2650

Merged
merged 3 commits into from
Apr 3, 2024
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
17 changes: 10 additions & 7 deletions library/spdm_transport_mctp_lib/libspdm_mctp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* @retval RETURN_SUCCESS The message is encoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id, size_t message_size,
libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id,
steven-bellock marked this conversation as resolved.
Show resolved Hide resolved
bool need_alignment,
size_t message_size,
void *message,
size_t *transport_message_size,
void **transport_message);
Expand All @@ -42,6 +44,7 @@ libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id, size_t
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_mctp_decode_message(uint32_t **session_id,
bool need_alignment,
size_t transport_message_size,
void *transport_message,
size_t *message_size,
Expand Down Expand Up @@ -118,7 +121,7 @@ libspdm_return_t libspdm_transport_mctp_encode_message(
/* SPDM message to APP message*/
app_message = NULL;
app_message_size = app_trans_header_size + message_size;
status = libspdm_mctp_encode_message(NULL, message_size,
status = libspdm_mctp_encode_message(NULL, false, message_size,
message,
&app_message_size,
&app_message);
Expand Down Expand Up @@ -148,7 +151,7 @@ libspdm_return_t libspdm_transport_mctp_encode_message(

/* secured message to secured MCTP message*/
status = libspdm_mctp_encode_message(
session_id, secured_message_size, secured_message,
session_id, true, secured_message_size, secured_message,
transport_message_size, transport_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_encode_message - %xu\n",
Expand All @@ -157,7 +160,7 @@ libspdm_return_t libspdm_transport_mctp_encode_message(
}
} else {
/* SPDM message to normal MCTP message*/
status = libspdm_mctp_encode_message(NULL, message_size, message,
status = libspdm_mctp_encode_message(NULL, true, message_size, message,
transport_message_size,
transport_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
Expand Down Expand Up @@ -236,7 +239,7 @@ libspdm_return_t libspdm_transport_mctp_decode_message(
secured_message_session_id = NULL;
/* Detect received message*/
status = libspdm_mctp_decode_message(
&secured_message_session_id, transport_message_size,
&secured_message_session_id, true, transport_message_size,
transport_message, &secured_message_size, (void **)&secured_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_decode_message - %xu\n", status));
Expand Down Expand Up @@ -276,7 +279,7 @@ libspdm_return_t libspdm_transport_mctp_decode_message(
}

/* APP message to SPDM message.*/
status = libspdm_mctp_decode_message(&secured_message_session_id,
status = libspdm_mctp_decode_message(&secured_message_session_id, false,
app_message_size, app_message,
message_size, message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
Expand All @@ -299,7 +302,7 @@ libspdm_return_t libspdm_transport_mctp_decode_message(
}
} else {
/* get non-secured message*/
status = libspdm_mctp_decode_message(&secured_message_session_id,
status = libspdm_mctp_decode_message(&secured_message_session_id, true,
transport_message_size,
transport_message,
message_size, message);
Expand Down
19 changes: 14 additions & 5 deletions library/spdm_transport_mctp_lib/libspdm_mctp_mctp.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright Notice:
* Copyright 2021-2022 DMTF. All rights reserved.
* Copyright 2021-2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

Expand Down Expand Up @@ -74,7 +74,9 @@ spdm_version_number_t libspdm_mctp_get_secured_spdm_version(
* @retval RETURN_SUCCESS The message is encoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id, size_t message_size,
libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id,
bool need_alignment,
size_t message_size,
void *message,
size_t *transport_message_size,
void **transport_message)
Expand All @@ -84,7 +86,11 @@ libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id, size_t
uint32_t data32;
mctp_message_header_t *mctp_message_header;

alignment = LIBSPDM_MCTP_ALIGNMENT;
if (need_alignment) {
alignment = LIBSPDM_MCTP_ALIGNMENT;
} else {
alignment = 1;
}
aligned_message_size =
(message_size + (alignment - 1)) & ~(alignment - 1);

Expand Down Expand Up @@ -132,6 +138,7 @@ libspdm_return_t libspdm_mctp_encode_message(const uint32_t *session_id, size_t
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_mctp_decode_message(uint32_t **session_id,
bool need_alignment,
size_t transport_message_size,
void *transport_message,
size_t *message_size, void **message)
Expand Down Expand Up @@ -167,8 +174,10 @@ libspdm_return_t libspdm_mctp_decode_message(uint32_t **session_id,
return LIBSPDM_STATUS_UNSUPPORTED_CAP;
}

LIBSPDM_ASSERT(((transport_message_size - sizeof(mctp_message_header_t)) &
(LIBSPDM_MCTP_ALIGNMENT - 1)) == 0);
if (need_alignment) {
LIBSPDM_ASSERT(((transport_message_size - sizeof(mctp_message_header_t)) &
(LIBSPDM_MCTP_ALIGNMENT - 1)) == 0);
}

*message_size = transport_message_size - sizeof(mctp_message_header_t);
*message = (uint8_t *)transport_message + sizeof(mctp_message_header_t);
Expand Down
19 changes: 11 additions & 8 deletions unit_test/spdm_transport_test_lib/common.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright Notice:
* Copyright 2021-2022 DMTF. All rights reserved.
* Copyright 2021-2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

Expand All @@ -21,7 +21,9 @@
* @retval RETURN_SUCCESS The message is encoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id, size_t message_size,
libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id,
bool need_alignment,
size_t message_size,
const void *message,
size_t *transport_message_size,
void **transport_message);
Expand All @@ -41,6 +43,7 @@ libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id, size_t
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_test_decode_message(uint32_t **session_id,
bool need_alignment,
size_t transport_message_size,
const void *transport_message,
size_t *message_size,
Expand Down Expand Up @@ -116,7 +119,7 @@ libspdm_return_t libspdm_transport_test_encode_message(
/* SPDM message to APP message*/
app_message = NULL;
app_message_size = transport_header_size + message_size + (LIBSPDM_TEST_ALIGNMENT - 1);
status = libspdm_test_encode_message(NULL, message_size,
status = libspdm_test_encode_message(NULL, false, message_size,
message,
&app_message_size,
&app_message);
Expand Down Expand Up @@ -145,7 +148,7 @@ libspdm_return_t libspdm_transport_test_encode_message(

/* secured message to secured Test message*/
status = libspdm_test_encode_message(
session_id, secured_message_size, secured_message,
session_id, true, secured_message_size, secured_message,
transport_message_size, transport_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_encode_message - %xu\n",
Expand All @@ -154,7 +157,7 @@ libspdm_return_t libspdm_transport_test_encode_message(
}
} else {
/* SPDM message to normal Test message*/
status = libspdm_test_encode_message(NULL, message_size, message,
status = libspdm_test_encode_message(NULL, true, message_size, message,
transport_message_size,
transport_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
Expand Down Expand Up @@ -233,7 +236,7 @@ libspdm_return_t libspdm_transport_test_decode_message(
secured_message_session_id = NULL;
/* Detect received message*/
status = libspdm_test_decode_message(
&secured_message_session_id, transport_message_size,
&secured_message_session_id, true, transport_message_size,
transport_message, &secured_message_size, (void **)&secured_message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
LIBSPDM_DEBUG((LIBSPDM_DEBUG_ERROR, "transport_decode_message - %xu\n", status));
Expand Down Expand Up @@ -273,7 +276,7 @@ libspdm_return_t libspdm_transport_test_decode_message(
}

/* APP message to SPDM message.*/
status = libspdm_test_decode_message(&secured_message_session_id,
status = libspdm_test_decode_message(&secured_message_session_id, false,
app_message_size, app_message,
message_size, message);
if (LIBSPDM_STATUS_IS_ERROR(status)) {
Expand All @@ -296,7 +299,7 @@ libspdm_return_t libspdm_transport_test_decode_message(
}
} else {
/* get non-secured message*/
status = libspdm_test_decode_message(&secured_message_session_id,
status = libspdm_test_decode_message(&secured_message_session_id, true,
transport_message_size,
transport_message,
message_size, message);
Expand Down
19 changes: 14 additions & 5 deletions unit_test/spdm_transport_test_lib/test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright Notice:
* Copyright 2021-2022 DMTF. All rights reserved.
* Copyright 2021-2024 DMTF. All rights reserved.
* License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
**/

Expand Down Expand Up @@ -70,7 +70,9 @@ spdm_version_number_t libspdm_test_get_secured_spdm_version(
* @retval RETURN_SUCCESS The message is encoded successfully.
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id, size_t message_size,
libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id,
bool need_alignment,
size_t message_size,
void *message,
size_t *transport_message_size,
void **transport_message)
Expand All @@ -80,7 +82,11 @@ libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id, size_t
uint32_t data32;
libspdm_test_message_header_t *test_message_header;

alignment = LIBSPDM_TEST_ALIGNMENT;
if (need_alignment) {
alignment = LIBSPDM_TEST_ALIGNMENT;
} else {
alignment = 1;
}
aligned_message_size =
(message_size + (alignment - 1)) & ~(alignment - 1);

Expand Down Expand Up @@ -128,6 +134,7 @@ libspdm_return_t libspdm_test_encode_message(const uint32_t *session_id, size_t
* @retval RETURN_INVALID_PARAMETER The message is NULL or the message_size is zero.
**/
libspdm_return_t libspdm_test_decode_message(uint32_t **session_id,
bool need_alignment,
size_t transport_message_size,
void *transport_message,
size_t *message_size, void **message)
Expand Down Expand Up @@ -163,8 +170,10 @@ libspdm_return_t libspdm_test_decode_message(uint32_t **session_id,
return LIBSPDM_STATUS_UNSUPPORTED_CAP;
}

LIBSPDM_ASSERT(((transport_message_size - sizeof(libspdm_test_message_header_t)) &
(LIBSPDM_TEST_ALIGNMENT - 1)) == 0);
if (need_alignment) {
LIBSPDM_ASSERT(((transport_message_size - sizeof(libspdm_test_message_header_t)) &
(LIBSPDM_TEST_ALIGNMENT - 1)) == 0);
}

*message_size = transport_message_size - sizeof(libspdm_test_message_header_t);
*message = (uint8_t *)transport_message + sizeof(libspdm_test_message_header_t);
Expand Down
4 changes: 2 additions & 2 deletions unit_test/test_spdm_requester/get_measurements.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ static libspdm_return_t libspdm_requester_get_measurements_test_send_message(
*)(session_info->secured_message_context))
->application_secret.response_data_sequence_number--;
libspdm_copy_mem(m_libspdm_local_buffer, sizeof(m_libspdm_local_buffer),
app_message, app_message_size - 3);
m_libspdm_local_buffer_size += app_message_size - 3;
app_message, app_message_size);
m_libspdm_local_buffer_size += app_message_size;
return LIBSPDM_STATUS_SUCCESS;
case 0x23:
/* m_libspdm_local_buffer_size is set by case35 and already contains the arbitrary fill
Expand Down
Loading