top level read command dynamic memory allocation

This commit is contained in:
merlokk 2021-12-24 19:14:48 +02:00
commit b3f2a18ec4
2 changed files with 27 additions and 5 deletions

View file

@ -4615,7 +4615,12 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
PrintAndLogEx(INFO, "------------------------------- " _CYAN_("File %02x data") " -------------------------------", fnum); PrintAndLogEx(INFO, "------------------------------- " _CYAN_("File %02x data") " -------------------------------", fnum);
uint8_t resp[2048] = {0}; uint8_t *resp = calloc(DESFIRE_BUFFER_SIZE, 1);
if (resp == NULL) {
PrintAndLogEx(ERR, "Desfire calloc " _RED_("error"));
DropField();
return PM3_EMALLOC;
}
size_t resplen = 0; size_t resplen = 0;
if (filetype == RFTData) { if (filetype == RFTData) {
@ -4623,6 +4628,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadFile command " _RED_("error") ". Result: %d", res); PrintAndLogEx(ERR, "Desfire ReadFile command " _RED_("error") ". Result: %d", res);
DropField(); DropField();
free(resp);
return PM3_ESOFT; return PM3_ESOFT;
} }
@ -4640,6 +4646,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire GetValue operation " _RED_("error") ". Result: %d", res); PrintAndLogEx(ERR, "Desfire GetValue operation " _RED_("error") ". Result: %d", res);
DropField(); DropField();
free(resp);
return PM3_ESOFT; return PM3_ESOFT;
} }
PrintAndLogEx(SUCCESS, "Read file 0x%02x value: %d (0x%08x)", fnum, value, value); PrintAndLogEx(SUCCESS, "Read file 0x%02x value: %d (0x%08x)", fnum, value, value);
@ -4652,6 +4659,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadRecords (len=1) command " _RED_("error") ". Result: %d", res); PrintAndLogEx(ERR, "Desfire ReadRecords (len=1) command " _RED_("error") ". Result: %d", res);
DropField(); DropField();
free(resp);
return PM3_ESOFT; return PM3_ESOFT;
} }
reclen = resplen; reclen = resplen;
@ -4666,6 +4674,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadRecords command " _RED_("error") ". Result: %d", res); PrintAndLogEx(ERR, "Desfire ReadRecords command " _RED_("error") ". Result: %d", res);
DropField(); DropField();
free(resp);
return PM3_ESOFT; return PM3_ESOFT;
} }
} }
@ -4690,6 +4699,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(ERR, "Desfire ReadFile command " _RED_("error") ". Result: %d", res); PrintAndLogEx(ERR, "Desfire ReadFile command " _RED_("error") ". Result: %d", res);
DropField(); DropField();
free(resp);
return PM3_ESOFT; return PM3_ESOFT;
} }
@ -4717,6 +4727,7 @@ static int DesfileReadFileAndPrint(DesfireContext_t *dctx, uint8_t fnum, int fil
} }
} }
free(resp);
return PM3_SUCCESS; return PM3_SUCCESS;
} }

View file

@ -1861,20 +1861,31 @@ static int DesfireCommandEx(DesfireContext_t *dctx, uint8_t cmd, uint8_t *data,
*resplen = 0; *resplen = 0;
uint8_t respcode = 0xff; uint8_t respcode = 0xff;
uint8_t xresp[2050] = {0}; uint8_t *xresp = calloc(DESFIRE_BUFFER_SIZE, 1);
if (xresp == NULL)
return PM3_EMALLOC;
size_t xresplen = 0; size_t xresplen = 0;
int res = DesfireExchangeEx(false, dctx, cmd, data, datalen, &respcode, xresp, &xresplen, true, splitbysize); int res = DesfireExchangeEx(false, dctx, cmd, data, datalen, &respcode, xresp, &xresplen, true, splitbysize);
if (res != PM3_SUCCESS) if (res != PM3_SUCCESS) {
free(xresp);
return res; return res;
if (respcode != MFDES_S_OPERATION_OK) }
if (respcode != MFDES_S_OPERATION_OK) {
free(xresp);
return PM3_EAPDU_FAIL; return PM3_EAPDU_FAIL;
if (checklength >= 0 && xresplen != checklength) }
if (checklength >= 0 && xresplen != checklength) {
free(xresp);
return PM3_EAPDU_FAIL; return PM3_EAPDU_FAIL;
}
if (resplen) if (resplen)
*resplen = xresplen; *resplen = xresplen;
if (resp) if (resp)
memcpy(resp, xresp, (splitbysize == 0) ? xresplen : xresplen * splitbysize); memcpy(resp, xresp, (splitbysize == 0) ? xresplen : xresplen * splitbysize);
free(xresp);
return PM3_SUCCESS; return PM3_SUCCESS;
} }