From e6c1c4e1a7de351f95737d312cc82c433c2f222a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 15 Sep 2026 08:36:13 +0200 Subject: [PATCH] final batch of adding NULL checks --- armsrc/Standalone/hf_mattyrun.c | 5 +++++ armsrc/Standalone/lf_tharexde.c | 5 +++++ armsrc/desfire_crypto.c | 3 +++ armsrc/em4x50.c | 11 +++++++++++ armsrc/hfops.c | 6 ++++++ armsrc/hfsnoop.c | 9 +++++++++ armsrc/hitag2.c | 7 +++++++ armsrc/lfsampling.c | 5 +++++ armsrc/mifaresniff_disabled.c | 4 ++++ armsrc/sam_picopass.c | 4 ++++ armsrc/thinfilm.c | 7 +++++++ 11 files changed, 66 insertions(+) diff --git a/armsrc/Standalone/hf_mattyrun.c b/armsrc/Standalone/hf_mattyrun.c index d35345528..3ddf11e91 100644 --- a/armsrc/Standalone/hf_mattyrun.c +++ b/armsrc/Standalone/hf_mattyrun.c @@ -269,6 +269,11 @@ void RunMod(void) { sizeof(uint64_t) * (ARRAYLEN(MATTYRUN_MFC_ESSENTIAL_KEYS) + ARRAYLEN(MATTYRUN_MFC_DEFAULT_KEYS) + MIFARE_4K_MAXSECTOR * 2)); + if (mfcKeys == NULL) { + DbpString("Failed to allocate memory"); + return; + } + uint16_t mfcKeyCount = 0; // Load essential keys to dictionary buffer diff --git a/armsrc/Standalone/lf_tharexde.c b/armsrc/Standalone/lf_tharexde.c index 76b143a4b..87013593f 100644 --- a/armsrc/Standalone/lf_tharexde.c +++ b/armsrc/Standalone/lf_tharexde.c @@ -104,6 +104,11 @@ static bool get_input_data_from_file(uint32_t *tag, char *inputfile) { uint32_t size = size_in_spiffs(inputfile); uint8_t *mem = BigBuf_calloc(size); + if (mem == NULL) { + Dbprintf(_RED_("failed to allocate memory for %s"), inputfile); + BigBuf_free(); + return false; + } Dbprintf("found input file `" _YELLOW_("%s") "`", inputfile); diff --git a/armsrc/desfire_crypto.c b/armsrc/desfire_crypto.c index afe66345e..5cf71d29d 100644 --- a/armsrc/desfire_crypto.c +++ b/armsrc/desfire_crypto.c @@ -335,6 +335,9 @@ void cmac(const desfirekey_t key, uint8_t *ivect, const uint8_t *data, size_t le } uint8_t *buffer = BigBuf_calloc(padded_data_length(len, kbs)); + if (buffer == NULL) { + return; + } memcpy(buffer, data, len); diff --git a/armsrc/em4x50.c b/armsrc/em4x50.c index a2481738f..eeaf4d3d3 100644 --- a/armsrc/em4x50.c +++ b/armsrc/em4x50.c @@ -756,6 +756,17 @@ void em4x50_chk(const char *filename, bool ledcontrol) { uint32_t size = size_in_spiffs(filename); pwd_count = size / 4; uint8_t *pwds = BigBuf_calloc(size); + if (pwds == NULL) { + if (changed) { + rdv40_spiffs_lazy_unmount(); + } + BigBuf_free(); + if (ledcontrol) LEDsoff(); + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + StopTicks(); + reply_ng(CMD_LF_EM4X50_CHK, PM3_EMALLOC, (uint8_t *)&pwd, sizeof(pwd)); + return; + } rdv40_spiffs_read_as_filetype(filename, pwds, size, RDV40_SPIFFS_SAFETY_SAFE); diff --git a/armsrc/hfops.c b/armsrc/hfops.c index 1c38ae73a..9926c6eab 100644 --- a/armsrc/hfops.c +++ b/armsrc/hfops.c @@ -209,6 +209,12 @@ int HfSimulateTkm(const uint8_t *uid, uint8_t modulation, uint32_t timeout) { LEDsoff(); uint8_t *data = BigBuf_calloc(256); + if (data == NULL) { + DbpString("Failed to allocate memory"); + reply_ng(CMD_HF_TEXKOM_SIMULATE, PM3_EMALLOC, NULL, 0); + return PM3_EMALLOC; + } + uint32_t elen = HfEncodeTkm(uid, modulation, data); if (elen == 0) { DbpString("encode error"); diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index bd6c556d7..d3e68afb3 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -109,6 +109,15 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len, uint *len = BigBuf_max_traceLen(); uint8_t *mem = BigBuf_calloc(*len); + if (mem == NULL) { + Dbprintf("Failed to allocate memory"); + FpgaUpdateFrameMode(8, true, true); + LED_D_OFF(); + FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); + BigBuf_free(); + *len = 0; + return PM3_EMALLOC; + } uint32_t trigger_cnt = 0; uint16_t r = 0, interval = 0; diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index fd98999ad..7f1e9850e 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -1726,6 +1726,13 @@ void SniffHitag2(bool ledcontrol, uint8_t threshold) { auth_table_pos = 0; auth_table = (uint8_t *)BigBuf_calloc(AUTH_TABLE_LENGTH); + if (auth_table == NULL) { + DbpString("Failed to allocate memory"); + if (ledcontrol) LEDsoff(); + switch_off(); + BigBuf_free_keep_EM(); + return; + } // Only for debug, if we want to see the edge timing of the captured frames uint8_t edges[100]; diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index 2dc99efea..3bcb745ec 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -152,6 +152,11 @@ void initSampleBufferEx(uint32_t *sample_size, bool use_malloc) { } else { *sample_size = MIN(*sample_size, BigBuf_max_traceLen()); data.buffer = BigBuf_calloc(*sample_size); + if (data.buffer == NULL) { + Dbprintf("Failed to allocate memory, using whole BigBuf"); + *sample_size = BigBuf_max_traceLen(); + data.buffer = BigBuf_get_addr(); + } } } else { diff --git a/armsrc/mifaresniff_disabled.c b/armsrc/mifaresniff_disabled.c index 95f4bb61f..8a15a5b19 100644 --- a/armsrc/mifaresniff_disabled.c +++ b/armsrc/mifaresniff_disabled.c @@ -62,6 +62,10 @@ void RAMFUNC SniffMifare(uint8_t param) { // allocate the DMA buffer, used to stream samples from the FPGA uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE); + if (dmaBuf == NULL) { + if (g_dbglevel >= DBG_ERROR) Dbprintf("Failed to allocate memory"); + return; + } uint8_t *data = dmaBuf; uint8_t previous_data = 0; int dataLen, maxDataLen = 0; diff --git a/armsrc/sam_picopass.c b/armsrc/sam_picopass.c index 0294b9140..49cb9b75e 100644 --- a/armsrc/sam_picopass.c +++ b/armsrc/sam_picopass.c @@ -548,6 +548,10 @@ static int sam_set_card_detected_picopass(const picopass_hdr_t *card_select) { } uint8_t *response = BigBuf_calloc(ISO7816_MAX_FRAME); uint16_t response_len = ISO7816_MAX_FRAME; + if (response == NULL) { + res = PM3_EMALLOC; + goto out; + } // a0 12 // ad 10 diff --git a/armsrc/thinfilm.c b/armsrc/thinfilm.c index 7d6e3ce3c..9d0e7141c 100644 --- a/armsrc/thinfilm.c +++ b/armsrc/thinfilm.c @@ -46,6 +46,13 @@ void ReadThinFilm(void) { uint8_t len = 0; uint8_t *buf = BigBuf_calloc(36); + if (buf == NULL) { + if (g_dbglevel >= DBG_ERROR) DbpString("Failed to allocate memory"); + reply_ng(CMD_HF_THINFILM_READ, PM3_EMALLOC, NULL, 0); + hf_field_off(); + set_tracing(false); + return; + } // power on and listen for answer. bool status = GetIso14443aAnswerFromTag_Thinfilm(buf, 36, &len);