From 8243ac97499a66490e4ccdf5d88e3b5d71bdd232 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 30 Apr 2021 11:16:57 +0200 Subject: [PATCH] fix a sanity memory leak with reserving memory used only for json file loading.. --- client/src/cmdhf15.c | 14 +++++----- client/src/cmdhficlass.c | 56 ++++++++++++++++++++-------------------- client/src/cmdhfmf.c | 42 +++++++++++++++--------------- client/src/cmdlfem4x50.c | 14 +++++----- client/src/cmdlfhitag.c | 2 +- client/src/cmdlft55xx.c | 14 +++++----- 6 files changed, 71 insertions(+), 71 deletions(-) diff --git a/client/src/cmdhf15.c b/client/src/cmdhf15.c index 30d2db275..0ec853b3a 100644 --- a/client/src/cmdhf15.c +++ b/client/src/cmdhf15.c @@ -1978,12 +1978,7 @@ static int CmdHF15Restore(const char *Cmd) { PrintAndLogEx(INFO, "Using block size... %d", blocksize); // 4bytes * 256 blocks. Should be enough.. - uint8_t *data = calloc(4 * 256, sizeof(uint8_t)); - if (data == NULL) { - PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); - return PM3_EMALLOC; - } - + uint8_t *data = NULL; size_t datalen = 0; int res = PM3_SUCCESS; DumpFileType_t dftype = getfiletype(filename); @@ -1997,7 +1992,12 @@ static int CmdHF15Restore(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, data, 256 * 4, &datalen, NULL); + data = calloc(4 * 256, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)data, 256 * 4, &datalen, NULL); break; } case DICTIONARY: { diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index aad535214..c36d37e5a 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -899,12 +899,7 @@ static int CmdHFiClassELoad(const char *Cmd) { CLIParserFree(ctx); size_t bytes_read = 2048; - uint8_t *dump = calloc(2048, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(ERR, "error, cannot allocate memory "); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; int res = 0; DumpFileType_t dftype = getfiletype(filename); switch (dftype) { @@ -917,7 +912,12 @@ static int CmdHFiClassELoad(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, dump, 2048, &bytes_read, NULL); + dump = calloc(2048, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(ERR, "error, cannot allocate memory "); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, 2048, &bytes_read, NULL); break; } case DICTIONARY: { @@ -1134,13 +1134,8 @@ static int CmdHFiClassDecrypt(const char *Cmd) { } size_t decryptedlen = 2048; - uint8_t *decrypted = calloc(2048, sizeof(uint8_t)); + uint8_t *decrypted = NULL; bool have_file = false; - if (decrypted == NULL) { - PrintAndLogEx(ERR, "error, cannot allocate memory "); - return PM3_EMALLOC; - } - int res = PM3_SUCCESS; // if user supplied dump file, time to load it @@ -1157,7 +1152,12 @@ static int CmdHFiClassDecrypt(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, decrypted, 2048, &decryptedlen, NULL); + decrypted = calloc(2048, sizeof(uint8_t)); + if (decrypted == NULL) { + PrintAndLogEx(ERR, "error, cannot allocate memory "); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)decrypted, 2048, &decryptedlen, NULL); break; } case DICTIONARY: { @@ -2037,12 +2037,7 @@ static int CmdHFiClassRestore(const char *Cmd) { } size_t bytes_read = 2048; - uint8_t *dump = calloc(2048, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(ERR, "error, cannot allocate memory "); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; int res = PM3_SUCCESS; DumpFileType_t dftype = getfiletype(filename); switch (dftype) { @@ -2055,7 +2050,12 @@ static int CmdHFiClassRestore(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, dump, 2048, &bytes_read, NULL); + dump = calloc(2048, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(ERR, "error, cannot allocate memory "); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, 2048, &bytes_read, NULL); break; } case DICTIONARY: { @@ -2539,12 +2539,7 @@ static int CmdHFiClassView(const char *Cmd) { CLIParserFree(ctx); size_t bytes_read = 2048; - uint8_t *dump = calloc(2048, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(ERR, "error, cannot allocate memory "); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; int res = 0; DumpFileType_t dftype = getfiletype(filename); switch (dftype) { @@ -2557,7 +2552,12 @@ static int CmdHFiClassView(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, dump, 2048, &bytes_read, NULL); + dump = calloc(2048, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(ERR, "error, cannot allocate memory "); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, 2048, &bytes_read, NULL); break; } case DICTIONARY: { diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 336d7c574..7e6858403 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -3750,12 +3750,7 @@ int CmdHF14AMfELoad(const char *Cmd) { PrintAndLogEx(INFO, "overriding number of blocks, will use %d blocks ( %u bytes )", block_cnt, block_cnt * block_width); } - uint8_t *data = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); - if (data == NULL) { - PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); - return PM3_EMALLOC; - } - + uint8_t *data = NULL; size_t datalen = 0; int res = PM3_SUCCESS; DumpFileType_t dftype = getfiletype(filename); @@ -3769,7 +3764,12 @@ int CmdHF14AMfELoad(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, data, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &datalen, NULL); + data = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)data, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &datalen, NULL); break; } case DICTIONARY: { @@ -4422,12 +4422,7 @@ static int CmdHF14AMfCLoad(const char *Cmd) { return PM3_SUCCESS; } - uint8_t *data = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); - if (data == NULL) { - PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); - return PM3_EMALLOC; - } - + uint8_t *data = NULL; size_t bytes_read = 0; int res = 0; DumpFileType_t dftype = getfiletype(filename); @@ -4441,7 +4436,12 @@ static int CmdHF14AMfCLoad(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, data, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &bytes_read, NULL); + data = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); + if (data == NULL) { + PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)data, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &bytes_read, NULL); break; } case DICTIONARY: { @@ -5921,12 +5921,7 @@ static int CmdHF14AMfView(const char *Cmd) { CLIParserFree(ctx); // reserve memory - uint8_t *dump = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; size_t bytes_read = 0; int res = 0; DumpFileType_t dftype = getfiletype(filename); @@ -5940,7 +5935,12 @@ static int CmdHF14AMfView(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, dump, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &bytes_read, NULL); + dump = calloc(MFBLOCK_SIZE * MIFARE_4K_MAXBLOCK, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, MIFARE_4K_MAXBLOCK * MFBLOCK_SIZE, &bytes_read, NULL); break; } case DICTIONARY: { diff --git a/client/src/cmdlfem4x50.c b/client/src/cmdlfem4x50.c index 46230bafd..7872ed9e5 100644 --- a/client/src/cmdlfem4x50.c +++ b/client/src/cmdlfem4x50.c @@ -130,12 +130,7 @@ static void print_info_result(uint8_t *data, bool verbose) { static int em4x50_load_file(const char *filename, uint8_t *data, size_t data_len, size_t *bytes_read) { - uint8_t *dump = calloc(DUMP_FILESIZE, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(ERR, "error, cannot allocate memory "); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; int res = PM3_SUCCESS; DumpFileType_t dftype = getfiletype(filename); switch (dftype) { @@ -148,7 +143,12 @@ static int em4x50_load_file(const char *filename, uint8_t *data, size_t data_len break; } case JSON: { - res = loadFileJSON(filename, dump, DUMP_FILESIZE, bytes_read, NULL); + dump = calloc(DUMP_FILESIZE, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(ERR, "error, cannot allocate memory "); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, DUMP_FILESIZE, bytes_read, NULL); break; } case DICTIONARY: { diff --git a/client/src/cmdlfhitag.c b/client/src/cmdlfhitag.c index 96daa6390..97a561292 100644 --- a/client/src/cmdlfhitag.c +++ b/client/src/cmdlfhitag.c @@ -257,7 +257,7 @@ static int CmdLFHitagEload(const char *Cmd) { PrintAndLogEx(ERR, "error, cannot allocate memory"); return PM3_EMALLOC; } - res = loadFileJSON(filename, dump, dumplen, &dumplen, NULL); + res = loadFileJSON(filename, (void *)dump, dumplen, &dumplen, NULL); break; } case DICTIONARY: { diff --git a/client/src/cmdlft55xx.c b/client/src/cmdlft55xx.c index d0df0dc58..1105db297 100644 --- a/client/src/cmdlft55xx.c +++ b/client/src/cmdlft55xx.c @@ -2347,12 +2347,7 @@ static int CmdT55xxRestore(const char *Cmd) { } size_t dlen = 0; - uint8_t *dump = calloc(T55x7_BLOCK_COUNT * 4, sizeof(uint8_t)); - if (dump == NULL) { - PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); - return PM3_EMALLOC; - } - + uint8_t *dump = NULL; DumpFileType_t dftype = getfiletype(filename); switch (dftype) { case BIN: { @@ -2364,7 +2359,12 @@ static int CmdT55xxRestore(const char *Cmd) { break; } case JSON: { - res = loadFileJSON(filename, dump, T55x7_BLOCK_COUNT * 4, &dlen, NULL); + dump = calloc(T55x7_BLOCK_COUNT * 4, sizeof(uint8_t)); + if (dump == NULL) { + PrintAndLogEx(WARNING, "Fail, cannot allocate memory"); + return PM3_EMALLOC; + } + res = loadFileJSON(filename, (void *)dump, T55x7_BLOCK_COUNT * 4, &dlen, NULL); break; } case DICTIONARY: {