diff --git a/armsrc/Standalone/hf_iceclass.c b/armsrc/Standalone/hf_iceclass.c index 8a6345d79..840fd36f0 100644 --- a/armsrc/Standalone/hf_iceclass.c +++ b/armsrc/Standalone/hf_iceclass.c @@ -240,6 +240,10 @@ static int reader_attack_mode(void) { BigBuf_free(); uint16_t mac_response_len = 0; uint8_t *mac_responses = BigBuf_calloc(MAC_RESPONSES_SIZE); + if (mac_responses == NULL) { + Dbprintf("Failed to allocate memory"); + return PM3_EMALLOC; + } iclass_simulate(ICLASS_SIM_MODE_READER_ATTACK, NUM_CSNS, false, false, csns, mac_responses, &mac_response_len); @@ -252,7 +256,7 @@ static int reader_attack_mode(void) { size_t dumplen = NUM_CSNS * 24; uint8_t *dump = BigBuf_calloc(dumplen); - if (dump == false) { + if (dump == NULL) { Dbprintf("Failed to allocate memory"); return PM3_EMALLOC; } @@ -306,6 +310,10 @@ static int reader_dump_mode(void) { BigBuf_free(); uint8_t *card_data = BigBuf_malloc(ICLASS_16KS_SIZE); + if (card_data == NULL) { + Dbprintf("Failed to allocate memory"); + return PM3_EMALLOC; + } // Don't use calloc since we set allocated memory to 0xFF's memset(card_data, 0xFF, ICLASS_16KS_SIZE); @@ -444,6 +452,10 @@ static int dump_sim_mode(void) { BigBuf_free(); uint8_t *card_data = BigBuf_malloc(ICLASS_16KS_SIZE); + if (card_data == NULL) { + Dbprintf("Failed to allocate memory"); + return PM3_EMALLOC; + } // Don't use calloc since we set allocated memory to 0xFF's memset(card_data, 0xFF, ICLASS_16KS_SIZE); diff --git a/armsrc/hitag2_crack.c b/armsrc/hitag2_crack.c index 1c9af611e..c2007576a 100644 --- a/armsrc/hitag2_crack.c +++ b/armsrc/hitag2_crack.c @@ -344,6 +344,11 @@ void ht2_crack2(uint8_t *nrar_hex) { uint8_t *e_response = BigBuf_calloc(32); lf_hitag_crack2_t *c2 = (lf_hitag_crack2_t *)BigBuf_calloc(sizeof(lf_hitag_crack2_t)); + if (e_response == NULL || c2 == NULL) { + BigBuf_free(); + reply_ng(CMD_LF_HITAG2_CRACK_2, PM3_EMALLOC, NULL, 0); + return; + } g_logging = false; LEDsoff(); @@ -450,6 +455,11 @@ void ht2_crack2(uint8_t *nrar_hex) { Dbprintf("Recovered " _YELLOW_("%4i") " bits of keystream", kslen); lf_hitag_crack_response_t *packet = (lf_hitag_crack_response_t *)BigBuf_calloc(sizeof(lf_hitag_crack_response_t)); + if (packet == NULL) { + BigBuf_free(); + reply_ng(CMD_LF_HITAG2_CRACK_2, PM3_EMALLOC, NULL, 0); + return; + } packet->status = 1; binarray2hex(c2->keybits, kslen, packet->data); diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index 1bc63df48..44572bac9 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -196,6 +196,10 @@ static void CodeIso15693AsReaderEOF(void) { static int get_uid_slix(uint32_t start_time, uint32_t *eof_time, uint8_t *uid) { uint8_t *answer = BigBuf_calloc(ISO15693_MAX_RESPONSE_LENGTH); + if (answer == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Failed to allocate response buffer. Exiting"); + return PM3_EMALLOC; + } start_time = *eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; @@ -1649,6 +1653,11 @@ void AcquireRawAdcSamplesIso15693(void) { LED_A_ON(); uint8_t *dest = BigBuf_calloc(4096); + if (dest == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Failed to allocate sample buffer. Exiting"); + LEDsoff(); + return; + } // switch field on FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER); @@ -2097,6 +2106,13 @@ void ReaderIso15693(iso15_card_select_t *p_card) { set_tracing(true); uint8_t *answer = BigBuf_calloc(ISO15693_MAX_RESPONSE_LENGTH); + if (answer == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Failed to allocate response buffer. Exiting"); + reply_ng(CMD_HF_ISO15693_READER, PM3_EMALLOC, NULL, 0); + switch_off(); + return; + } + memset(answer, 0x00, ISO15693_MAX_RESPONSE_LENGTH); // FIRST WE RUN AN INVENTORY TO GET THE TAG UID diff --git a/armsrc/sam_common.c b/armsrc/sam_common.c index db124f48f..f56351fd3 100644 --- a/armsrc/sam_common.c +++ b/armsrc/sam_common.c @@ -445,6 +445,10 @@ int sam_get_version(bool info) { uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME); uint16_t response_len = ISO7816_MAX_FRAME; + if (response == NULL) { + res = PM3_EMALLOC; + goto out; + } uint8_t payload[] = { 0xa0, // <- SAM command @@ -543,6 +547,10 @@ int sam_get_serial_number(void) { uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME); uint16_t response_len = ISO7816_MAX_FRAME; + if (response == NULL) { + res = PM3_EMALLOC; + goto out; + } uint8_t payload[] = { 0xa0, // <- SAM command @@ -658,6 +666,11 @@ void sam_append_asn1_node(const uint8_t *root, const uint8_t *node, uint8_t type void sam_send_ack(void) { uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME); uint16_t response_len = ISO7816_MAX_FRAME; + if (response == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString(_RED_("SAM: failed to allocate buffer")); + BigBuf_free(); + return; + } uint8_t payload[] = { 0xa0, 0 }; uint16_t payload_len = sizeof(payload);