Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MbedTLS allocations at runtime #163

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ gmtime (
const time_t *timer
)
{
struct tm *GmTime;
STATIC struct tm GmTime;

UINT64 DayNo;
UINT64 DayRemainder;
time_t Year;
Expand All @@ -148,23 +149,18 @@ gmtime (
return NULL;
}

GmTime = malloc (sizeof (struct tm));
if (GmTime == NULL) {
return NULL;
}

ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
ZeroMem ((VOID *)&GmTime, (UINTN)sizeof (GmTime));

DayNo = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY, &Remainder);
DayRemainder = (UINT64)Remainder;

DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);
GmTime->tm_sec = (int)Remainder;
GmTime.tm_sec = (int)Remainder;
DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);
GmTime->tm_min = (int)DivS64x64Remainder (Remainder, SECSPERMIN, NULL);
GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder, SECSPERHOUR, NULL);
GmTime.tm_min = (int)DivS64x64Remainder (Remainder, SECSPERMIN, NULL);
GmTime.tm_hour = (int)DivS64x64Remainder (DayRemainder, SECSPERHOUR, NULL);
DivS64x64Remainder ((DayNo + 4), 7, &Remainder);
GmTime->tm_wday = (int)Remainder;
GmTime.tm_wday = (int)Remainder;

for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);
Expand All @@ -176,8 +172,8 @@ gmtime (
}
}

GmTime->tm_year = (int)(YearNo + (1970 - 1900));
GmTime->tm_yday = (int)DayNo;
GmTime.tm_year = (int)(YearNo + (1970 - 1900));
GmTime.tm_yday = (int)DayNo;

for (MonthNo = 12; MonthNo > 1; MonthNo--) {
if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
Expand All @@ -186,12 +182,12 @@ gmtime (
}
}

GmTime->tm_mon = (int)MonthNo - 1;
GmTime->tm_mday = (int)DayNo + 1;
GmTime.tm_mon = (int)MonthNo - 1;
GmTime.tm_mday = (int)DayNo + 1;

GmTime->tm_isdst = 0;
GmTime->tm_gmtoff = 0;
GmTime->tm_zone = NULL;
GmTime.tm_isdst = 0;
GmTime.tm_gmtoff = 0;
GmTime.tm_zone = NULL;

return GmTime;
return &GmTime;
}
9 changes: 5 additions & 4 deletions CryptoPkg/Library/BaseCryptLibMbedTls/Hmac/CryptHmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ HmacMdNew (
{
VOID *HmacMdCtx;

HmacMdCtx = AllocateZeroPool (sizeof (mbedtls_md_context_t));
HmacMdCtx = calloc (sizeof (mbedtls_md_context_t), 1);
if (HmacMdCtx == NULL) {
return NULL;
}

// XXX: No mbedtls_md_init()? mbedtls_md_free() shouldn't be called in this
// case. `HmacMdFree (HmacMdNew ())` can cause problems.

return HmacMdCtx;
}

Expand All @@ -44,9 +47,7 @@ HmacMdFree (
)
{
mbedtls_md_free (HmacMdCtx);
if (HmacMdCtx != NULL) {
FreePool (HmacMdCtx);
}
free (HmacMdCtx);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions CryptoPkg/Library/BaseCryptLibMbedTls/Pem/CryptPem.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ RsaGetPrivateKeyFromPem (

NewPemData = NULL;
if (PemData[PemSize - 1] != 0) {
NewPemData = AllocateZeroPool (PemSize + 1);
NewPemData = calloc (PemSize + 1, 1);
if (NewPemData == NULL) {
return FALSE;
}
Expand All @@ -73,10 +73,8 @@ RsaGetPrivateKeyFromPem (

Ret = mbedtls_pk_parse_key (&Pk, PemData, PemSize, (CONST UINT8 *)Password, PasswordLen, NULL, NULL);

if (NewPemData != NULL) {
FreePool (NewPemData);
NewPemData = NULL;
}
free (NewPemData);
NewPemData = NULL;

if (Ret != 0) {
mbedtls_pk_free (&Pk);
Expand Down
6 changes: 2 additions & 4 deletions CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptRsaBasic.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ RsaNew (
{
VOID *RsaContext;

RsaContext = AllocateZeroPool (sizeof (mbedtls_rsa_context));
RsaContext = calloc (sizeof (mbedtls_rsa_context), 1);
if (RsaContext == NULL) {
return RsaContext;
}
Expand All @@ -59,9 +59,7 @@ RsaFree (
)
{
mbedtls_rsa_free (RsaContext);
if (RsaContext != NULL) {
FreePool (RsaContext);
}
free (RsaContext);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions CryptoPkg/Library/BaseCryptLibMbedTls/Pk/CryptX509.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ X509ConstructCertificate (
return FALSE;
}

MbedTlsCert = AllocateZeroPool (sizeof (mbedtls_x509_crt));
MbedTlsCert = calloc (sizeof (mbedtls_x509_crt), 1);
if (MbedTlsCert == NULL) {
return FALSE;
}
Expand All @@ -95,7 +95,7 @@ X509ConstructCertificate (
return TRUE;
} else {
mbedtls_x509_crt_free (MbedTlsCert);
FreePool (MbedTlsCert);
free (MbedTlsCert);
return FALSE;
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ X509ConstructCertificateStackV (
Ret = 0;
Crt = NULL;
if (*X509Stack == NULL) {
Crt = AllocateZeroPool (sizeof (mbedtls_x509_crt));
Crt = calloc (sizeof (mbedtls_x509_crt), 1);
if (Crt == NULL) {
return FALSE;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ X509ConstructCertificateStackV (
} else {
if (Crt != NULL) {
mbedtls_x509_crt_free (Crt);
FreePool (Crt);
free (Crt);
*X509Stack = NULL;
}

Expand Down Expand Up @@ -230,7 +230,7 @@ X509Free (
{
if (X509Cert != NULL) {
mbedtls_x509_crt_free (X509Cert);
FreePool (X509Cert);
free (X509Cert);
}
}

Expand Down
34 changes: 15 additions & 19 deletions CryptoPkg/Library/BaseCryptLibMbedTls/SysCall/TimerWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ gmtime (
const time_t *timer
)
{
struct tm *GmTime;
STATIC struct tm GmTime;

UINT16 DayNo;
UINT16 DayRemainder;
time_t Year;
Expand All @@ -125,20 +126,15 @@ gmtime (
return NULL;
}

GmTime = AllocateZeroPool (sizeof (struct tm));
if (GmTime == NULL) {
return NULL;
}

ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));
ZeroMem ((VOID *)&GmTime, (UINTN)sizeof (GmTime));

DayNo = (UINT16)(*timer / SECSPERDAY);
DayRemainder = (UINT16)(*timer % SECSPERDAY);

GmTime->tm_sec = (int)(DayRemainder % SECSPERMIN);
GmTime->tm_min = (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN);
GmTime->tm_hour = (int)(DayRemainder / SECSPERHOUR);
GmTime->tm_wday = (int)((DayNo + 4) % 7);
GmTime.tm_sec = (int)(DayRemainder % SECSPERMIN);
GmTime.tm_min = (int)((DayRemainder % SECSPERHOUR) / SECSPERMIN);
GmTime.tm_hour = (int)(DayRemainder / SECSPERHOUR);
GmTime.tm_wday = (int)((DayNo + 4) % 7);

for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {
TotalDays = (UINT16)(IsLeap (Year) ? 366 : 365);
Expand All @@ -150,8 +146,8 @@ gmtime (
}
}

GmTime->tm_year = (int)(YearNo + (1970 - 1900));
GmTime->tm_yday = (int)DayNo;
GmTime.tm_year = (int)(YearNo + (1970 - 1900));
GmTime.tm_yday = (int)DayNo;

for (MonthNo = 12; MonthNo > 1; MonthNo--) {
if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {
Expand All @@ -160,14 +156,14 @@ gmtime (
}
}

GmTime->tm_mon = (int)MonthNo - 1;
GmTime->tm_mday = (int)DayNo + 1;
GmTime.tm_mon = (int)MonthNo - 1;
GmTime.tm_mday = (int)DayNo + 1;

GmTime->tm_isdst = 0;
GmTime->tm_gmtoff = 0;
GmTime->tm_zone = NULL;
GmTime.tm_isdst = 0;
GmTime.tm_gmtoff = 0;
GmTime.tm_zone = NULL;

return GmTime;
return &GmTime;
}

/**_time64 function. **/
Expand Down