Source: libfreefare Version: 0.4.0-2.4 Severity: important Tags: security upstream Forwarded: https://github.com/nfc-tools/libfreefare/issues/157 X-Debbugs-Cc: [email protected]
Hi, Two card-controlled buffer overflows in libfreefare's DESFire read path (libfreefare/mifare_desfire.c) affect the packaged libfreefare 0.4.0-2.4 (also 0.4.0-2.3 in trixie and 0.4.0-2.1 in bookworm) and upstream master (c2b0cfa4). Reported upstream at [0]. Summary: 1. Unbounded heap overflow + unchecked caller copy in read_data() (mifare_desfire.c:1730-1752 on master; :1549 in 0.4.0): read_buffer is allocated from the caller's requested length (length * record_size), then 59-byte card frames are appended inside a "while (0xAF == res[__res_n - 1])" loop with no capacity check or frame cap. A card can return 0xAF indefinitely to overwrite arbitrary heap memory before crypto verification runs, and line 1752 then copies the full aggregate (sr - 1) into the caller's data pointer without checking length. (In 0.4.0, the loop writes directly into the caller's data buffer with the same missing bound.) 2. Stack buffer overflow in mifare_desfire_get_file_settings() (mifare_desfire.c:1376-1377): copies sn - 1 card-supplied bytes into a packed 17-byte stack struct (raw_settings). Because the receive buffer is 26 bytes, a card returning a 25-byte body writes 8 bytes past the struct on the stack. This runs automatically on the first uncached read of any file via madame_soleil_get_read_communication_settings() before any file data is returned. Reachability & hardened-build behavior: Both bugs are triggered by a hostile or emulated DESFire card during an ordinary read, with no authentication or session key required. Shipped binaries in libfreefare-bin (mifare-desfire-read-ndef, mifare-desfire-access, mifare-desfire-write-ndef) reach both paths. With Debian's hardening=+all build flags: - Bug 2 (raw_settings) is caught by _FORTIFY_SOURCE (__memcpy_chk -> "*** buffer overflow detected ***", exit 134). - Bug 1 (read_buffer / caller data) is NOT caught by _FORTIFY_SOURCE because neither destination has a compile-time size in scope; in mifare-desfire-read-ndef, a single 59-byte frame overwrites 39 bytes past its 20-byte stack array (lendata[20]). Verification: Reproduced with ASan against upstream master (c2b0cfa4) using a scripted transport harness (full steps and hex payloads in [0]): - GetFileSettings (27-byte response): stack-buffer-overflow WRITE of size 25 at mifare_desfire.c:1377 (3/3 runs). - ReadData (1-byte request, single 59-byte frame): heap-buffer-overflow WRITE of size 59 into a 4-byte allocation at mifare_desfire.c:1739 (3/3 runs). - Valid controls (7-byte settings, 1-byte read) pass cleanly. Suggested fix: 1. Track read_buffer capacity and reject frames exceeding read_buffer_size - bytes_received before the memcpy in read_data(), cap the 0xAF continuation loop, and reject (size_t)(sr - 1) > length * record_size before copying into data. 2. In mifare_desfire_get_file_settings(), validate sn >= 1 && (size_t)(sn - 1) <= sizeof(raw_settings) and check the expected body length for file_type (7/17/13 bytes) before copying or caching. References: [0] https://github.com/nfc-tools/libfreefare/issues/157 Thanks, Aditya

