Skip to content

Commit 9c754fc

Browse files
committed
async: harden crypto callback poll re-entry
Address review feedback: - wc_CryptoCb_Poll() now hard-fails (WC_HW_E) when the device is missing or unregistered, instead of returning WC_NO_PENDING_E and letting the async layer treat an in-flight op as done with an unfilled buffer. - Poll the callback only while the event is still WC_PENDING_E, so a completed result is not overwritten on a later poll pass. - Include WOLFSSL_ASYNC_CRYPT in the poll guards to match the wc_CryptoCb_Poll() declaration.
1 parent 37c2652 commit 9c754fc

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

wolfcrypt/src/async.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
#include <wolfssl/error-ssl.h>
2828

2929
#include <wolfssl/wolfcrypt/async.h>
30-
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_ASYNC_POLL)
30+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WOLF_CRYPTO_CB) && \
31+
defined(WOLF_CRYPTO_CB_ASYNC_POLL)
3132
#include <wolfssl/wolfcrypt/cryptocb.h>
3233
#endif
3334

@@ -598,10 +599,13 @@ int wolfAsync_EventPoll(WOLF_EVENT* event, WOLF_EVENT_FLAG flags)
598599
event->ret = wolfAsync_DoSw(asyncDev);
599600
#endif
600601

601-
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_ASYNC_POLL)
602-
/* Re-enter the crypto callback to complete the job and fill the
603-
* output buffer, like the hardware polls above. */
604-
if (asyncDev->cryptocb.devId != INVALID_DEVID) {
602+
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WOLF_CRYPTO_CB) && \
603+
defined(WOLF_CRYPTO_CB_ASYNC_POLL)
604+
/* Re-enter the crypto callback to complete the job and fill the output
605+
* buffer, like the hardware polls above. Poll only while the event is
606+
* still pending so a completed result is not overwritten. */
607+
if (asyncDev->cryptocb.devId != INVALID_DEVID &&
608+
event->ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
605609
event->ret = wc_CryptoCb_Poll(asyncDev->cryptocb.devId);
606610
}
607611
#endif
@@ -763,13 +767,17 @@ int wolfAsync_EventQueuePoll(WOLF_EVENT_QUEUE* queue, void* context_filter,
763767
event->ret = wolfAsync_DoSw(asyncDev);
764768
}
765769
#elif defined(WOLF_CRYPTO_CB) || defined(HAVE_PK_CALLBACKS)
766-
#if defined(WOLF_CRYPTO_CB) && \
770+
#if defined(WOLFSSL_ASYNC_CRYPT) && \
771+
defined(WOLF_CRYPTO_CB) && \
767772
defined(WOLF_CRYPTO_CB_ASYNC_POLL)
768773
if (asyncDev->cryptocb.devId != INVALID_DEVID) {
769-
/* Re-enter the crypto callback to complete the job
770-
* and fill the output buffer. */
771-
event->ret =
772-
wc_CryptoCb_Poll(asyncDev->cryptocb.devId);
774+
/* Re-enter the crypto callback to complete the job and
775+
* fill the output buffer. Poll only while still pending
776+
* so a completed result is not overwritten. */
777+
if (event->ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
778+
event->ret =
779+
wc_CryptoCb_Poll(asyncDev->cryptocb.devId);
780+
}
773781
}
774782
else
775783
#endif

wolfcrypt/src/cryptocb.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,10 @@ int wc_CryptoCb_GetDevIdAtIndex(int startIdx)
464464
/* Returns WC_PENDING_E while in-flight, 0 or an error when done. */
465465
int wc_CryptoCb_Poll(int devId)
466466
{
467-
int ret = WC_NO_ERR_TRACE(WC_NO_PENDING_E);
467+
/* Default hard-fails: a missing or unregistered device cannot complete
468+
* the pending job, so never report "no pending" and leave the output
469+
* buffer unfilled. */
470+
int ret = WC_NO_ERR_TRACE(WC_HW_E);
468471
CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
469472
if (dev != NULL && dev->cb != NULL) {
470473
wc_CryptoInfo info;

0 commit comments

Comments
 (0)