Skip to content

Commit

Permalink
Merge pull request #2155 from henrygab/mf_dump_fix
Browse files Browse the repository at this point in the history
Fix mf file loading error
  • Loading branch information
iceman1001 authored Oct 30, 2023
2 parents 5b74865 + f2ed7d1 commit b50b405
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions client/src/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, siz

// Proxmark3 settings file. No
if (!strcmp(ctype, "settings")) {
PrintAndLogEx(ERR, "ERROR: json " _YELLOW_("%s") " appears to be Proxmark3 settings file ... not a valid dump file.", preferredName);
goto out;
}

Expand All @@ -1144,21 +1145,26 @@ int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, siz
// depricated mfcard
if (!strcmp(ctype, "mfcard") || !strcmp(ctype, "mfc v2")) {
size_t sptr = 0;
for (int i = 0; i < maxdatalen; i++) {

// load blocks (i) from 0..N, but check sptr against total data length, not `i`
for (int i = 0; sptr < maxdatalen; i++) {
if (sptr + MFBLOCK_SIZE > maxdatalen) {
PrintAndLogEx(ERR, "loadFileJSONex: maxdatalen=%4d (%04x) block (i)=%4d (%04x) sptr=%4d (%04x) -- exceeded maxdatalen", maxdatalen, maxdatalen, i, i, sptr, sptr);
retval = PM3_EMALLOC;
goto out;
}

snprintf(blocks, sizeof(blocks), "$.blocks.%d", i);
uint8_t block[MFBLOCK_SIZE];
uint8_t block[MFBLOCK_SIZE] = {0}; // ensure zero-filled when partial block of data read
JsonLoadBufAsHex(root, blocks, block, MFBLOCK_SIZE, &len);
if (!len)
if (!len) {
PrintAndLogEx(WARNING, "WARNING: json %s block %d has zero-length data ... file parsing stopped", ctype, i);
break;
} else if (len != MFBLOCK_SIZE) {
PrintAndLogEx(WARNING, "WARNING: json %s block %d only has %d bytes, expected %d (will fill with zero data)", ctype, i, len, MFBLOCK_SIZE);
}

memcpy(&udata.bytes[sptr], block, MFBLOCK_SIZE);
sptr += len;
sptr += MFBLOCK_SIZE; // always increment pointer by the full block size, even if only partial data read from dump file
}

*datalen = sptr;
Expand Down

0 comments on commit b50b405

Please sign in to comment.