#1155085 libnfc: unbounded card copies in pn53x_decode_target_data()

Package:
src:libnfc
Source:
src:libnfc
Submitter:
Aditya Gupta
Date:
2026-11-23 23:35:03 UTC
Severity:
normal
Tags:
#1155085#5
Date:
2026-11-23 23:32:28 UTC
From:
To:
Hi,

Two unbounded card-controlled copies in libnfc's
pn53x_decode_target_data() (libnfc/chips/pn53x.c) affect the packaged
libnfc 1.8.0-3.1 (and 1.8.0-4 in sid), as well as upstream master
(3fa0751a). Reported upstream at [0].

Summary:

- NMT_BARCODE (libnfc/chips/pn53x.c:625-628): copies szRawData bytes
from the card's response frame into the 32-byte pnti->nti.abtData
field and stores szRawData in szDataLen with no bounds check.
- NMT_ISO14443BI (libnfc/chips/pn53x.c:556-574): copies szRawData - 8
bytes into the 33-byte pnti->nii.abtAtr field and stores szRawData - 8
in szAtrLen, gated only by three card-supplied header bytes (0x07,
btVerLog & 0x80, btConfig & 0x40).

Reachability in Debian:

Both branches are reached through the exported
nfc_initiator_select_passive_target() and
nfc_initiator_list_passive_targets() APIs (even though
pn53x_decode_target_data itself is filtered out of libnfc.so.6 exports
by Makefile.am). In libnfc-bin, stock nfc-list with no arguments polls
both NMT_ISO14443BI (mask & 0x10) and NMT_BARCODE (mask & 0x200) via
its default mask 0x3ff (utils/nfc-list.c:90). nfc-barcode hardcodes
NMT_BARCODE (utils/nfc-barcode.c:183). No authentication or operator
action is required beyond presenting a crafted card/emulator during a
scan.

Impact on the shipped path:

On all in-tree call sites, the destination is the full 283-byte
nfc_target_info union and the PN53x staging buffer is capped at 264
bytes, so the decoder's memcpy stays inside the union rather than
escaping the stack object. However, the card overwrites adjacent union
members (such as nai.abtUid and nai.szAtsLen) and leaves an unchecked
szDataLen / szAtrLen in the struct that downstream printers trust. In
particular, snprint_nfc_barcode_info() (libnfc/target-subr.c:509,
called by nfc-list) and utils/nfc-barcode.c:189 iterate with a uint8_t
loop counter ("for (uint8_t i = 0; i < szDataLen; i++)"); at szDataLen
== 256 (the max 128-bit-aligned barcode frame inside
abtTargetsData[264]), the counter wraps infinitely while off advances
past the 4096-byte buffer in str_nfc_target() (libnfc/nfc.c:1427).

Verification:

Tested against upstream master (3fa0751a) built with ASan using a
scripted driver registered via nfc_register_driver() through
nfc_initiator_select_passive_target(): an 80-byte barcode frame sets
szDataLen=80 and spills 48 bytes into adjacent union members; a
48-byte B-prime REPGEN frame sets szAtrLen=40 and spills 7 bytes past
abtAtr. Clean controls (16-byte barcode, 12-byte B-prime) decode
cleanly. The szDataLen == 256 wraparound in snprint_nfc_barcode_info()
is from code inspection.

Suggested fix:

In pn53x_decode_target_data(), reject szRawData >
sizeof(pnti->nti.abtData) and (szRawData < 8 || szRawData - 8 >
sizeof(pnti->nii.abtAtr)) with NFC_ECHIP. In libnfc/target-subr.c,
clamp the print loops to the field size and widen the loop counter
from uint8_t to size_t.

References:
[0] https://github.com/nfc-tools/libnfc/issues/770

Thanks,
Aditya