hf mfdes sim: FormatPICC, and stop esave dropping the ATS
FormatPICC is answered now. It releases every application and every file and
hands the memory back, which is what makes it different from DeleteFile and
DeleteApplication: those leave a tombstone because a real card does not give the
memory back either, and this is the one command that reclaims. Here that means
rebuilding the tables around the surviving PICC entry and putting both frontiers
back where an empty card has them.
The PICC master key, its settings and the card identity are explicitly untouched
(M134034 9.4.6), so what is left is the same card with nothing on it. The same
section says the command always requires a preceding authentication with the
PICC master key, and unlike create and delete that is not relaxed by the free
create/delete key setting, so the check is on the session rather than on the
application's key settings. Measured against the simulation: 3 applications and
6400 bytes free become 0 applications and 8000 bytes free, and a bare
`90 FC 00 00 00` with no session is refused with 91 AE.
Separately, `hf mfdes esave` wrote the ATS as zeros. desfire_em_unpack() bounded
the copy with
MIN(hdr->atslen, (uint8_t)sizeof(dump->card_info.ats))
and card_info.ats is 256 bytes, so the cast wrapped to 0 and the memcpy copied
nothing -- while ats_len was still set from the image, so the saved file carried
a correct length and eight zero bytes. Both copies here are now bounded by the
image's own array, which is the smaller of the two and the one a corrupt length
could run past.
This was invisible in simulation because SimulateIso14443aInit() falls back to
the tag type's built-in DESFire ATS when the image supplies none, and for this
card the two are identical. A reader saw a correct ATS from a card image that no
longer had one, and only a load-save round trip showed it.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7e0ce399f0
commit
480934bfd5
3 files changed
+75
-4
No files matched your search
@@ -455,12 +455,16 @@ int desfire_em_unpack(const uint8_t *img, size_t imglen, desfire_dump_t *dump) {
|
||||
|
||||
memset(dump, 0, sizeof(desfire_dump_t));
|
||||
|
||||
dump->card_info.uidlen = hdr->uidlen;
|
||||
memcpy(dump->card_info.uid, hdr->uid, MIN(hdr->uidlen, (uint8_t)sizeof(dump->card_info.uid)));
|
||||
// Bound each copy by the image's own array, which is the smaller of the two
|
||||
// and the one a corrupt length could run past. Not by the destination cast
|
||||
// to uint8_t: card_info.ats is 256 bytes, that cast wrapped to 0, and the
|
||||
// ATS was silently dropped while its length was still reported as 8.
|
||||
dump->card_info.uidlen = MIN(hdr->uidlen, sizeof(hdr->uid));
|
||||
memcpy(dump->card_info.uid, hdr->uid, dump->card_info.uidlen);
|
||||
memcpy(dump->card_info.atqa, hdr->atqa, sizeof(hdr->atqa));
|
||||
dump->card_info.sak = hdr->sak;
|
||||
dump->card_info.ats_len = hdr->atslen;
|
||||
memcpy(dump->card_info.ats, hdr->ats, MIN(hdr->atslen, (uint8_t)sizeof(dump->card_info.ats)));
|
||||
dump->card_info.ats_len = MIN(hdr->atslen, sizeof(hdr->ats));
|
||||
memcpy(dump->card_info.ats, hdr->ats, dump->card_info.ats_len);
|
||||
|
||||
dump->versionhwlen = hdr->versionhwlen;
|
||||
memcpy(dump->versionhw, hdr->versionhw, sizeof(dump->versionhw));
|
||||
|
||||
Reference in new issue
Block a user