Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mf file loading error #2155

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions client/src/fileutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,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 @@ -1102,21 +1103,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
Loading