Skip to content

Commit ebbab7a

Browse files
committed
src/crl.c: fix sequence in FreeCRL(): First decrement the refcount, then shut down the CRL monitor, then deallocate resources (fixes ASAN-detected read-after-free).
1 parent 6ac0f82 commit ebbab7a

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

src/crl.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,28 @@ void FreeCRL(WOLFSSL_CRL* crl, int dynamic)
316316
WOLFSSL_MSG("Couldn't lock x509 mutex");
317317
if (!doFree)
318318
return;
319-
wolfSSL_RefFree(&crl->ref);
320319
}
321320
#endif
322321

322+
#ifdef HAVE_CRL_MONITOR
323+
if (crl->tid != INVALID_THREAD_VAL) {
324+
WOLFSSL_MSG("stopping monitor thread");
325+
if (StopMonitor(crl->mfd) == 0) {
326+
if (wolfSSL_JoinThread(crl->tid) != 0)
327+
WOLFSSL_MSG("stop monitor failed in wolfSSL_JoinThread");
328+
}
329+
else {
330+
WOLFSSL_MSG("stop monitor failed");
331+
}
332+
}
333+
if (wolfSSL_CondFree(&crl->cond) != 0)
334+
WOLFSSL_MSG("wolfSSL_CondFree failed in FreeCRL");
335+
#endif
336+
337+
#ifdef OPENSSL_ALL
338+
wolfSSL_RefFree(&crl->ref);
339+
#endif
340+
323341
tmp = crl->crlList;
324342
#ifdef HAVE_CRL_MONITOR
325343
if (crl->monitors[0].path)
@@ -343,20 +361,6 @@ void FreeCRL(WOLFSSL_CRL* crl, int dynamic)
343361
tmp = next;
344362
}
345363

346-
#ifdef HAVE_CRL_MONITOR
347-
if (crl->tid != INVALID_THREAD_VAL) {
348-
WOLFSSL_MSG("stopping monitor thread");
349-
if (StopMonitor(crl->mfd) == 0) {
350-
if (wolfSSL_JoinThread(crl->tid) != 0)
351-
WOLFSSL_MSG("stop monitor failed in wolfSSL_JoinThread");
352-
}
353-
else {
354-
WOLFSSL_MSG("stop monitor failed");
355-
}
356-
}
357-
if (wolfSSL_CondFree(&crl->cond) != 0)
358-
WOLFSSL_MSG("wolfSSL_CondFree failed in FreeCRL");
359-
#endif
360364
wc_FreeRwLock(&crl->crlLock);
361365
if (dynamic) /* free self */
362366
XFREE(crl, crl->heap, DYNAMIC_TYPE_CRL);

0 commit comments

Comments
 (0)