Skip to content

Commit e4b4c8c

Browse files
authored
Merge commit from fork
Memory Leak Fixes
2 parents cc13a52 + 9c34b6e commit e4b4c8c

14 files changed

Lines changed: 370 additions & 88 deletions

src/core/crypto_aos.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
977977
if (status != CRYPTO_LIB_SUCCESS)
978978
{
979979
mc_if->mc_log(status);
980+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
981+
{
982+
free(sa_ptr);
983+
}
980984
return status;
981985
}
982986

@@ -1010,6 +1014,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
10101014
#endif
10111015
status = CRYPTO_LIB_ERROR;
10121016
mc_if->mc_log(status);
1017+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1018+
{
1019+
free(sa_ptr);
1020+
}
10131021
return status;
10141022
}
10151023

@@ -1035,6 +1043,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
10351043
{
10361044
status = CRYPTO_LIB_ERR_NO_ECS_SET_FOR_ENCRYPTION_MODE;
10371045
mc_if->mc_log(status);
1046+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1047+
{
1048+
free(sa_ptr);
1049+
}
10381050
return status;
10391051
}
10401052

@@ -1060,6 +1072,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
10601072
{
10611073
status = CRYPTO_LIB_ERR_AOS_FL_LT_MAX_FRAME_SIZE;
10621074
mc_if->mc_log(status);
1075+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1076+
{
1077+
free(sa_ptr);
1078+
}
10631079
return status;
10641080
}
10651081

@@ -1084,6 +1100,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
10841100
#endif
10851101
status = CRYPTO_LIB_ERR_INVALID_FECF;
10861102
mc_if->mc_log(status);
1103+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1104+
{
1105+
free(sa_ptr);
1106+
}
10871107
return status;
10881108
}
10891109
// Valid FECF, zero out the field
@@ -1105,6 +1125,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
11051125
#endif
11061126
status = CRYPTO_LIB_ERR_TC_ENUM_USED_FOR_AOS_CONFIG;
11071127
mc_if->mc_log(status);
1128+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1129+
{
1130+
free(sa_ptr);
1131+
}
11081132
return status;
11091133
}
11101134

@@ -1117,6 +1141,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
11171141
#endif
11181142
status = CRYPTO_LIB_ERROR;
11191143
mc_if->mc_log(status);
1144+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1145+
{
1146+
free(sa_ptr);
1147+
}
11201148
return status;
11211149
}
11221150

@@ -1226,13 +1254,21 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
12261254
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
12271255
mc_if->mc_log(status);
12281256
free(p_new_dec_frame);
1257+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1258+
{
1259+
free(sa_ptr);
1260+
}
12291261
return status;
12301262
}
12311263
if (ekp->key_state != KEY_ACTIVE)
12321264
{
12331265
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
12341266
mc_if->mc_log(status);
12351267
free(p_new_dec_frame);
1268+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1269+
{
1270+
free(sa_ptr);
1271+
}
12361272
return status;
12371273
}
12381274
}
@@ -1247,13 +1283,21 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
12471283
status = CRYPTO_LIB_ERR_KEY_ID_ERROR;
12481284
mc_if->mc_log(status);
12491285
free(p_new_dec_frame);
1286+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1287+
{
1288+
free(sa_ptr);
1289+
}
12501290
return status;
12511291
}
12521292
if (akp->key_state != KEY_ACTIVE)
12531293
{
12541294
status = CRYPTO_LIB_ERR_KEY_STATE_INVALID;
12551295
mc_if->mc_log(status);
12561296
free(p_new_dec_frame);
1297+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1298+
{
1299+
free(sa_ptr);
1300+
}
12571301
return status;
12581302
}
12591303
}
@@ -1290,6 +1334,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
12901334
aad_len);
12911335
#endif
12921336
mc_if->mc_log(status);
1337+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1338+
{
1339+
free(sa_ptr);
1340+
}
12931341
return status;
12941342
}
12951343

@@ -1313,6 +1361,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
13131361
printf(KRED "Error: SA Not Operational \n" RESET);
13141362
#endif
13151363
free(p_new_dec_frame); // Add cleanup
1364+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1365+
{
1366+
free(sa_ptr);
1367+
}
13161368
return CRYPTO_LIB_ERR_SA_NOT_OPERATIONAL;
13171369
}
13181370

@@ -1389,6 +1441,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
13891441
free(p_new_dec_frame); // Add cleanup
13901442
status = CRYPTO_LIB_ERR_KEY_LENGTH_ERROR;
13911443
mc_if->mc_log(status);
1444+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1445+
{
1446+
free(sa_ptr);
1447+
}
13921448
return status;
13931449
}
13941450

@@ -1522,6 +1578,10 @@ int32_t Crypto_AOS_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, AOS_t
15221578
(uint16_t)(p_new_dec_frame[byte_idx] << 8) | p_new_dec_frame[byte_idx + 1];
15231579
}
15241580
free(p_new_dec_frame);
1581+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1582+
{
1583+
free(sa_ptr);
1584+
}
15251585

15261586
#ifdef DEBUG
15271587
printf(KYEL "----- Crypto_AOS_ProcessSecurity END -----\n" RESET);

src/core/crypto_config.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,19 +751,24 @@ int32_t Crypto_Config_Cam(uint8_t cam_enabled, char *cookie_file_path, char *key
751751
char *access_manager_uri, char *username, char *cam_home)
752752
{
753753
int32_t status = CRYPTO_LIB_SUCCESS;
754-
cam_config = (CamConfig_t *)calloc(1, CAM_CONFIG_SIZE);
755754

755+
cam_config = (CamConfig_t *)calloc(1, CAM_CONFIG_SIZE);
756756
if (cam_config == NULL)
757757
{
758758
return CRYPTO_LIB_ERROR;
759759
}
760760

761761
if (Crypto_is_safe_username(username) != CRYPTO_LIB_SUCCESS)
762762
{
763+
free(cam_config);
764+
cam_config = NULL;
763765
return CAM_CONFIG_NOT_SUPPORTED_ERROR;
764766
}
767+
765768
if (Crypto_is_safe_path(keytab_file_path) != CRYPTO_LIB_SUCCESS)
766769
{
770+
free(cam_config);
771+
cam_config = NULL;
767772
return CAM_CONFIG_NOT_SUPPORTED_ERROR;
768773
}
769774

src/core/crypto_tc.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ int32_t Crypto_TC_Encrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_ptr
488488
{
489489
Crypto_TC_Safe_Free_Ptr(*aad);
490490
status = CRYPTO_LIB_ERR_KEY_LENGTH_ERROR;
491+
free(p_new_enc_frame);
491492
mc_if->mc_log(status);
492493
return status;
493494
}
@@ -577,6 +578,7 @@ int32_t Crypto_TC_Encrypt(uint8_t sa_service_type, SecurityAssociation_t *sa_ptr
577578
if (status != CRYPTO_LIB_SUCCESS)
578579
{
579580
Crypto_TC_Safe_Free_Ptr(*aad);
581+
free(p_new_enc_frame);
580582
mc_if->mc_log(status);
581583
return status; // Cryptography IF call failed, return.
582584
}
@@ -1158,6 +1160,7 @@ int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t *p_in_frame, const uint16_t in
11581160
if (status != CRYPTO_LIB_SUCCESS)
11591161
{
11601162
mc_if->mc_log(status);
1163+
free(p_new_enc_frame);
11611164
return status;
11621165
}
11631166

@@ -1272,6 +1275,10 @@ int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t *p_in_frame, const uint16_t in
12721275
if (status != CRYPTO_LIB_SUCCESS)
12731276
{
12741277
mc_if->mc_log(status);
1278+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1279+
{
1280+
free(sa_ptr);
1281+
}
12751282
return status;
12761283
}
12771284

@@ -2217,18 +2224,14 @@ static int32_t validate_sa_index(SecurityAssociation_t *sa)
22172224
int32_t returnval = 0;
22182225
SecurityAssociation_t *temp_sa;
22192226

2220-
if (crypto_config.sa_type == SA_TYPE_MARIADB)
2221-
{
2222-
mariadb_table_name = MARIADB_TC_TABLE_NAME;
2223-
}
2224-
sa_if->sa_get_from_spi(sa->spi, &temp_sa);
2225-
22262227
// Do not validate sa index on KMC
22272228
if (crypto_config.sa_type == SA_TYPE_MARIADB)
22282229
{
22292230
return returnval;
22302231
}
22312232

2233+
sa_if->sa_get_from_spi(sa->spi, &temp_sa);
2234+
22322235
int sa_index = -1;
22332236
sa_index = (int)(sa - temp_sa); // Based on array memory location
22342237
#ifdef DEBUG

src/core/crypto_tm.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,10 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
17961796
{
17971797
status = CRYPTO_LIB_ERR_TM_FRAME_LENGTH_UNDERFLOW;
17981798
mc_if->mc_log(status);
1799+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1800+
{
1801+
free(sa_ptr);
1802+
}
17991803
return status;
18001804
}
18011805

@@ -1804,6 +1808,10 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
18041808
{
18051809
status = CRYPTO_LIB_ERR_TM_FRAME_LENGTH_UNDERFLOW;
18061810
mc_if->mc_log(status);
1811+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1812+
{
1813+
free(sa_ptr);
1814+
}
18071815
return status;
18081816
}
18091817

@@ -1861,6 +1869,11 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
18611869
{
18621870
status = CRYPTO_LIB_ERR_TM_FRAME_LENGTH_UNDERFLOW;
18631871
mc_if->mc_log(status);
1872+
free(p_new_dec_frame);
1873+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1874+
{
1875+
free(sa_ptr);
1876+
}
18641877
return status;
18651878
}
18661879

@@ -1873,6 +1886,10 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
18731886
if (status != CRYPTO_LIB_SUCCESS)
18741887
{
18751888
free(p_new_dec_frame);
1889+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1890+
{
1891+
free(sa_ptr);
1892+
}
18761893
return status;
18771894
}
18781895
}
@@ -1892,6 +1909,10 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
18921909
printf(KRED "Error: SA Not Operational \n" RESET);
18931910
#endif
18941911
free(p_new_dec_frame);
1912+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1913+
{
1914+
free(sa_ptr);
1915+
}
18951916
return CRYPTO_LIB_ERR_SA_NOT_OPERATIONAL;
18961917
}
18971918

@@ -1900,6 +1921,10 @@ int32_t Crypto_TM_ProcessSecurity(uint8_t *p_ingest, uint16_t len_ingest, TM_t *
19001921
p_decrypted_length);
19011922
}
19021923

1924+
if (crypto_config.sa_type == SA_TYPE_MARIADB)
1925+
{
1926+
free(sa_ptr);
1927+
}
19031928
return status;
19041929
}
19051930

0 commit comments

Comments
 (0)