Skip to content

Commit c46b72e

Browse files
authored
Merge pull request #3349 from AlxCzl/fix-3346
Fix MADDecode off-by-one (#3346): mad[] sized for 39 entries but 40 written when MAD2 present
2 parents e1228ee + d4c4ab3 commit c46b72e

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

client/src/cmdhfmf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6956,7 +6956,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
69566956
}
69576957

69586958
if (aidlen == 2 || decodeholder) {
6959-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
6959+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
69606960
size_t madlen = 0;
69616961
if (MADDecode(dump, dump + (0x10 * MIFARE_1K_MAXBLOCK), mad, &madlen, swapmad, override)) {
69626962
PrintAndLogEx(ERR, "can't decode MAD");
@@ -7041,7 +7041,7 @@ static int CmdHF14AMfMAD(const char *Cmd) {
70417041
}
70427042

70437043
if (aidlen == 2 || decodeholder) {
7044-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
7044+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
70457045
size_t madlen = 0;
70467046
if (MADDecode(sector0, sector10, mad, &madlen, swapmad, override)) {
70477047
PrintAndLogEx(ERR, "can't decode MAD");
@@ -7218,7 +7218,7 @@ int CmdHFMFNDEFRead(const char *Cmd) {
72187218
return res;
72197219
}
72207220

7221-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
7221+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
72227222
size_t madlen = 0;
72237223
res = MADDecode(sector0, sector10, mad, &madlen, false, override);
72247224
if (res != PM3_SUCCESS) {
@@ -7648,7 +7648,7 @@ int CmdHFMFNDEFWrite(const char *Cmd) {
76487648
}
76497649

76507650
// decode MAD v1
7651-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
7651+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
76527652
size_t madlen = 0;
76537653
res = MADDecode(sector0, sector10, mad, &madlen, false, false);
76547654
if (res != PM3_SUCCESS) {
@@ -8528,7 +8528,7 @@ static int CmdHF14AMfView(const char *Cmd) {
85288528
PrintAndLogEx(INFO, _CYAN_("VIGIK PACS detected"));
85298529

85308530
// decode MAD v1
8531-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
8531+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
85328532
size_t madlen = 0;
85338533
res = MADDecode(dump, NULL, mad, &madlen, false, true);
85348534
if (res != PM3_SUCCESS) {

client/src/cmdhfmfp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,7 @@ static int CmdHFMFPMAD(const char *Cmd) {
26232623
}
26242624

26252625
if (aidlen == 2 || decodeholder) {
2626-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
2626+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
26272627
size_t madlen = 0;
26282628
if (MADDecode(sector0, sector16, mad, &madlen, swapmad, override)) {
26292629
PrintAndLogEx(ERR, "can't decode MAD");
@@ -2809,7 +2809,7 @@ int CmdHFMFPNDEFRead(const char *Cmd) {
28092809
}
28102810
}
28112811

2812-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
2812+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
28132813
size_t madlen = 0;
28142814
res = MADDecode(sector0, (haveMAD2 ? sector16 : NULL), mad, &madlen, false, override);
28152815
if (res != PM3_SUCCESS) {

client/src/mifare/mad.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ int convert_mad_to_arr(uint8_t *in, uint16_t ilen, uint8_t *out, uint16_t *olen,
500500
memcpy(sector16, in + (MF_MAD2_SECTOR * 4 * MFBLOCK_SIZE), sizeof(sector16));
501501
}
502502

503-
uint16_t mad[7 + 8 + 8 + 8 + 8] = {0};
503+
uint16_t mad[MAD_MAX_AID_ENTRIES] = {0};
504504
size_t madlen = 0;
505505
if (MADDecode(sector0, sector16, mad, &madlen, false, override)) {
506506
PrintAndLogEx(ERR, "can't decode MAD");

client/src/mifare/mad.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include "common.h"
2323

24+
// 16 MAD1 AIDs + 1 MAD2 marker (0x0005) + 23 MAD2 AIDs = 40
25+
#define MAD_MAX_AID_ENTRIES 40
26+
2427
int MADCheck(uint8_t *sector0, uint8_t *sector16, bool verbose, bool *haveMAD2);
2528
int MADDecode(uint8_t *sector0, uint8_t *sector16, uint16_t *mad, size_t *madlen, bool swapmad, bool override);
2629
int MAD1DecodeAndPrint(uint8_t *sector, bool swapmad, bool verbose, bool *haveMAD2);

0 commit comments

Comments
 (0)