Skip to content

Potential Infinite Loop in RNG Generation When Server Is Unresponsive #130

@Jpgig

Description

@Jpgig

Description
The wh_Client_RngGenerate function contains a blocking wait for server responses that could result in an infinite loop if the server becomes unresponsive or malfunctions.

Current Behavior
In the current implementation, when waiting for a response from the server, the code enters a do-while loop that continues as long as wh_Client_RecvResponse() returns WH_ERROR_NOTREADY:
Code snippet:
/* Receive response /
if (ret == 0) {
do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, (uint8_t
)packet);
} while (ret == WH_ERROR_NOTREADY);
}
If the server stops responding or fails to provide a valid response, this loop will continue indefinitely, causing the client application to hang.
Expected Behavior
The function should implement a timeout mechanism to prevent infinite waiting. After a reasonable number of retries or a specific time period, it should exit the loop with an appropriate error code.
Few Solutions for reference:

  1. Retry count-based timeout:
    int retry_count = 0;
    const int max_retries = WOLFHSM_MAX_RESPONSE_RETRIES;

do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, (uint8_t*)packet);
if (ret == WH_ERROR_NOTREADY && ++retry_count >= max_retries) {
ret = WH_ERROR_TIMEOUT;
break;
}
} while (ret == WH_ERROR_NOTREADY);

  1. Time-based timeout:
    uint32_t start_time = wh_GetCurrentTimeMs();
    uint32_t timeout_ms = WOLFHSM_RESPONSE_TIMEOUT_MS;

do {
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &res_len, (uint8_t*)packet);
if (ret == WH_ERROR_NOTREADY &&
(wh_GetCurrentTimeMs() - start_time) > timeout_ms) {
ret = WH_ERROR_TIMEOUT;
break;
}
} while (ret == WH_ERROR_NOTREADY);

Additional Context
This issue affects not only the RNG generation but potentially other client functions that use the same pattern for waiting on server responses. A comprehensive review of all client-server communication patterns should be conducted.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions