Skip to content

Commit e0c7997

Browse files
committed
Fix use-after-free possibility in GetCRLInfo
1 parent 753a477 commit e0c7997

3 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/crl.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,22 @@ int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert)
687687
#ifdef HAVE_CRL_UPDATE_CB
688688
static void SetCrlInfo(CRL_Entry* entry, CrlInfo *info)
689689
{
690-
info->issuerHash = (byte *)entry->issuerHash;
691-
info->issuerHashLen = CRL_DIGEST_SIZE;
692-
info->lastDate = (byte *)entry->lastDate;
693-
info->lastDateMaxLen = MAX_DATE_SIZE;
690+
/* Ensure the copy below stays within bounds. */
691+
wc_static_assert(sizeof(info->issuerHashData) == sizeof(entry->issuerHash));
692+
693+
/* Copy into info's own buffers so the pointers stay valid for the
694+
* lifetime of the CrlInfo, not just that of the source entry. */
695+
info->issuerHashLen = sizeof(info->issuerHashData);
696+
XMEMCPY(info->issuerHashData, entry->issuerHash,
697+
sizeof(info->issuerHashData));
698+
info->issuerHash = info->issuerHashData;
699+
info->lastDateMaxLen = sizeof(info->lastDateData);
700+
XMEMCPY(info->lastDateData, entry->lastDate, sizeof(info->lastDateData));
701+
info->lastDate = info->lastDateData;
694702
info->lastDateFormat = entry->lastDateFormat;
695-
info->nextDate = (byte *)entry->nextDate;
696-
info->nextDateMaxLen = MAX_DATE_SIZE;
703+
info->nextDateMaxLen = sizeof(info->nextDateData);
704+
XMEMCPY(info->nextDateData, entry->nextDate, sizeof(info->nextDateData));
705+
info->nextDate = info->nextDateData;
697706
info->nextDateFormat = entry->nextDateFormat;
698707
info->crlNumberSet = entry->crlNumberSet;
699708
if (info->crlNumberSet)
@@ -702,13 +711,19 @@ static void SetCrlInfo(CRL_Entry* entry, CrlInfo *info)
702711

703712
static void SetCrlInfoFromDecoded(DecodedCRL* entry, CrlInfo *info)
704713
{
705-
info->issuerHash = (byte *)entry->issuerHash;
706-
info->issuerHashLen = SIGNER_DIGEST_SIZE;
707-
info->lastDate = (byte *)entry->lastDate;
708-
info->lastDateMaxLen = MAX_DATE_SIZE;
714+
/* Copy into info's own buffers so the pointers stay valid after the
715+
* decoded CRL is freed by the caller. */
716+
info->issuerHashLen = sizeof(info->issuerHashData);
717+
XMEMCPY(info->issuerHashData, entry->issuerHash,
718+
sizeof(info->issuerHashData));
719+
info->issuerHash = info->issuerHashData;
720+
info->lastDateMaxLen = sizeof(info->lastDateData);
721+
XMEMCPY(info->lastDateData, entry->lastDate, sizeof(info->lastDateData));
722+
info->lastDate = info->lastDateData;
709723
info->lastDateFormat = entry->lastDateFormat;
710-
info->nextDate = (byte *)entry->nextDate;
711-
info->nextDateMaxLen = MAX_DATE_SIZE;
724+
info->nextDateMaxLen = sizeof(info->nextDateData);
725+
XMEMCPY(info->nextDateData, entry->nextDate, sizeof(info->nextDateData));
726+
info->nextDate = info->nextDateData;
712727
info->nextDateFormat = entry->nextDateFormat;
713728
info->crlNumberSet = entry->crlNumberSet;
714729
if (info->crlNumberSet)

tests/api.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26119,6 +26119,15 @@ static int test_wolfSSL_CTX_LoadCRL_largeCRLnum(void)
2611926119
WOLFSSL_SUCCESS);
2612026120
AssertIntEQ(XMEMCMP(
2612126121
crlInfo.crlNumber, exp_crlnum, XSTRLEN(exp_crlnum)), 0);
26122+
/* The pointer fields must reference storage inside crlInfo so they stay
26123+
* valid after the call returns; before the fix they pointed into the
26124+
* freed decoded CRL. */
26125+
AssertTrue((byte*)crlInfo.issuerHash >= (byte*)&crlInfo &&
26126+
(byte*)crlInfo.issuerHash < (byte*)(&crlInfo + 1));
26127+
AssertTrue((byte*)crlInfo.lastDate >= (byte*)&crlInfo &&
26128+
(byte*)crlInfo.lastDate < (byte*)(&crlInfo + 1));
26129+
AssertTrue((byte*)crlInfo.nextDate >= (byte*)&crlInfo &&
26130+
(byte*)crlInfo.nextDate < (byte*)(&crlInfo + 1));
2612226131
ExpectIntEQ(wolfSSL_CertManagerGetCRLInfo(
2612326132
cm, &crlInfo, crlLrgCrlNumBuff, -1, WOLFSSL_FILETYPE_PEM),
2612426133
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

wolfssl/ssl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,9 @@ typedef struct CrlInfo {
40214021
word32 nextDateMaxLen;
40224022
byte nextDateFormat;
40234023
byte crlNumberSet:1;
4024+
byte issuerHashData[SIGNER_DIGEST_SIZE];
4025+
byte lastDateData[MAX_DATE_SIZE];
4026+
byte nextDateData[MAX_DATE_SIZE];
40244027
} CrlInfo;
40254028

40264029
typedef void (*CbUpdateCRL)(CrlInfo* old, CrlInfo* cnew);

0 commit comments

Comments
 (0)