Windows: fix KDF completion lifetime

Hold a requester reference until header parsing can no longer enqueue KDFs.
This prevents stale completion signals from allowing cleanup while workers
still use request-owned state.

Add bounded tests of the production pool and caller completion protocol.
This commit is contained in:
Mounir IDRASSI
2026-10-16 18:06:08 +09:00
parent 0cb9fad329
commit 9ff43ba473
5 changed files with 415 additions and 9 deletions
+1 -1
View File
@@ -291,6 +291,7 @@ static TC_THREAD_PROC EncryptionThreadProc (void *threadArg)
InterlockedExchange (workItem->KeyDerivation.CompletionFlag, TRUE);
TC_SET_EVENT (*workItem->KeyDerivation.CompletionEvent);
// The requester retains a reference until it has finished submitting work.
if (InterlockedDecrement (workItem->KeyDerivation.OutstandingWorkItemCount) == 0)
TC_SET_EVENT (*workItem->KeyDerivation.NoOutstandingWorkItemEvent);
@@ -581,7 +582,6 @@ void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT
workItem->KeyDerivation.pAbortKeyDerivation = pAbortKeyDerivation;
InterlockedIncrement (outstandingWorkItemCount);
TC_CLEAR_EVENT (*noOutstandingWorkItemEvent);
SetWorkItemState (workItem, WorkItemReady);
TC_SET_EVENT (WorkItemReadyEvent);
+9 -1
View File
@@ -32,7 +32,15 @@ typedef enum
size_t GetCpuCount (WORD* pGroupCount);
#endif
/* derivationResult receives 0 on success; PRFs that can fail store their native nonzero result. */
/*
* The caller initializes outstandingWorkItemCount to 1 and noOutstandingWorkItemEvent
* to non-signaled, retaining its own reference while submissions are possible.
* After the last possible submission, it decrements the count and waits on the
* event if the result is nonzero, before releasing any caller-owned buffers.
* Each worker releases its reference after publishing completion. A worker that
* drops the count to zero signals the event, which must not be reset during submission.
* derivationResult receives 0 on success; PRFs that can fail store their native nonzero result.
*/
void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG *completionFlag, LONG *outstandingWorkItemCount, int pkcs5Prf, unsigned char *password, int passwordLength, unsigned char *salt, int iterationCount, int memoryCost, unsigned char *derivedKey, LONG *derivationResult, LONG volatile *pAbortKeyDerivation);
void EncryptionThreadPoolBeginReadVolumeHeaderFinalization (TC_EVENT *keyDerivationCompletedEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG* outstandingWorkItemCount, void* keyInfoBuffer, int keyInfoBufferSize, void* keyDerivationWorkItems, int keyDerivationWorkItemsSize);
void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, uint8 *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo);
+8 -7
View File
@@ -283,10 +283,11 @@ int ReadVolumeHeaderWithAbort (BOOL bBoot, unsigned char *encryptedHeader, Passw
for (i = 0; i < pkcs5PrfCount; ++i)
keyDerivationWorkItems[i].Free = TRUE;
*outstandingWorkItemCount = 0;
// Retain a requester reference until no further KDFs can be submitted.
*outstandingWorkItemCount = 1;
#ifdef DEVICE_DRIVER
KeInitializeEvent (keyDerivationCompletedEvent, SynchronizationEvent, FALSE);
KeInitializeEvent (noOutstandingWorkItemEvent, SynchronizationEvent, TRUE);
KeInitializeEvent (noOutstandingWorkItemEvent, SynchronizationEvent, FALSE);
#else
*keyDerivationCompletedEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
if (!*keyDerivationCompletedEvent)
@@ -298,7 +299,7 @@ int ReadVolumeHeaderWithAbort (BOOL bBoot, unsigned char *encryptedHeader, Passw
return ERR_OUTOFMEMORY;
}
*noOutstandingWorkItemEvent = CreateEvent (NULL, FALSE, TRUE, NULL);
*noOutstandingWorkItemEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
if (!*noOutstandingWorkItemEvent)
{
CloseHandle (*keyDerivationCompletedEvent);
@@ -736,10 +737,10 @@ ret:
#if !defined(_UEFI)
if ((selected_pkcs5_prf == 0) && (encryptionThreadCount > 1))
{
// Wait for all outstanding threads to finish or cancel
TC_WAIT_EVENT(*noOutstandingWorkItemEvent);
// Cleanup is now synchronous because we already waited for all threads to stop.
// The asynchronous finalization is no longer needed.
// Release the requester reference after the last possible submission.
// If workers remain, the last one signals completion before cleanup.
if (InterlockedDecrement (outstandingWorkItemCount) != 0)
TC_WAIT_EVENT (*noOutstandingWorkItemEvent);
#if !defined(DEVICE_DRIVER)
CloseHandle(*keyDerivationCompletedEvent);
CloseHandle(*noOutstandingWorkItemEvent);