Skip to main content
Zondax Github LinkZondax Github Link

Manual Security & Code-Quality Review — ledger-filecoin

Repository: Zondax/ledger-filecoin
Branch: audit/filecoin-main-2026-07-31 (from origin/main)
Commit: f35a2533
Date: 2026-07-31
App version: 2.3.12
Language: C (native FIL CBOR + FEVM / ERC20 / EIP-191 via zxlib EVM)
Method: Same ledger-app-audit pipeline as cosmos/ICP (recon → parallel hunt → refute → report). Focus on full APDU→parse→display→sign paths, including non-obvious valid txs (InvokeEVM, raw bytes, map params, EIP-1559 ERC20).

No application source was modified. Artifacts under untracked .audit/.


Executive summary​

ledger-filecoin has a review_pending gate for successful async reviews (CLA APDUs rejected while true). Residual risk clusters:

  1. Async error UI without the lock (High / Critical-candidate UX race) — expert-mode and blindsign error screens set IO_ASYNCH_REPLY but never set review_pending. Host can start another sign while the error modal is up (worse over BLE).
  2. Cross-INS / cross-CLA chunk sessions (High) — separate FIL vs EVM tx_initialized flags on one buffer; CLA_ETH can drive some FIL INS; multi-chunk assembly unlocked.
  3. WYSIWYS gaps (High) — ETH ERC20 clear-sign can hide native value; params trailing bytes and map param under-display; item count overflow → empty review.
  4. Crypto mostly sound — Blake2b+CID domain separation, key zeroize, ERC20 exact 68-byte gate, f0 high-byte checks, outer CBOR EOF. Residuals: unchecked blake_hash return, ETH EIP-191 first-chunk length under-subtract, raw_bytes session hygiene.

Correction (remediation pass): F07 was filed under (3) as a fee-label issue. It is actually a NULL-pointer dereference at parse time on any EIP-1559 ERC20 transfer — a live defect for ordinary users, not an adversarial one, and the highest-priority item in this report. See F07.

SeverityCount
Critical1 (F07, re-rated; error-UI race remains High / Critical-candidate)
High7
Medium8
Low1
Info3

Critical candidates​

IDWhy
F01+F02Unlocked async error UI → substitute review under user buttons (esp. BLE)
F04Clear-sign ERC20 while attaching native value (theft if user trusts token amount only)
F08Empty review after item-count overflow

Findings index​

#SevFindingLocation
F01HighExpert/blindsign error UI without review_pendingapdu_handler.c:226
F02HighETH blindsign error never sets review_pendingapdu_handler.c:402
F03HighFIL vs EVM separate chunk flags, shared bufferapdu_handler.c + zxlib EVM
F04HighERC20 clear-sign allows non-zero native valuezxlib/evm/evm_erc20.c
F05HighTrailing bytes inside params signed unshownparser_impl.c:275
F06HighMap params: pairs vs item walk under-displaysparser_impl.c:283
F07CriticalEIP-1559 ERC20 clear-sign dereferences a NULL gasPrice at parse timeparser_impl_evm_specific.c
F08Highnum_items overflow → 0 → empty validateparser_impl.c:434
F09MediumGas Limit shown as FIL with 18 decimalsparser.c:231
F10MediumInvokeEVM ERC20 hides fee cap non-expertparser_invoke_evm.c
F11MediumShared tx_initialized FIL vs raw_bytesapdu_handler.c
F12MediumIO_RESET incomplete session clearapdu_handler.c:428
F13MediumCLA_ETH can drive FIL INSapdu_handler.c:329
F14Mediumraw_bytes msg_counter / session hygieneapdu_handler.c:245
F15MediumETH path weaker than FIL (no hardened account)zxlib apdu_handler_evm.c
F17MediumETH EIP-191 first-chunk length under-subtractzxlib apdu_handler_evm.c
F16Lowcrypto_sign ignores blake_hash returncrypto.c:194
F18InfoERC20 exact 68-byte gate (trailing OK)evm_erc20.c
F19InfoSuccess-path review_pending presentapdu_handler.c:338
F20Infoprint0xToF0 high-byte zerosfil_utils.c:93

F01 — High — Async error UI without review lock​

Site: app/src/apdu_handler.c:226-232 (expert), 274-279 (raw blindsign), 303-308 (FVM EIP-191)

Sets IO_ASYNCH_REPLY + error UI, then THROW without review_pending = true. Success paths set the flag at L239/L287/L316. While the modal is up, handleApdu does not reject new APDUs.

Suggested improvement​

Set review_pending before every async UI; clear only in app_reply_error / reject paths.


F02 — High — ETH blindsign errors skip lock assignment​

Site: apdu_handler.c:402-405

review_pending is assigned after handleSignEth returns; blindsign failures THROW inside the handler, so the assignment never runs.


F03 — High — Dual chunk flags / shared buffer​

FIL (apdu_handler.c) and ETH (zxlib apdu_handler_evm.c) maintain separate tx_initialized while using the same buffering. Mid-stream interleaving is possible because multi-chunk assembly is not locked.


F04 — High — ERC20 clear-sign + native value​

Site: deps/ledger-zxlib/evm/evm_erc20.c validateERC20

No check that RLP value is empty. UI shows token transfer only. InvokeEVM path correctly requires value.len == 0.


F05 — High — Params trailing bytes​

Outer message EOF is checked (parser_impl.c:397). The params blob’s root CBOR item is not required to consume the whole params buffer — suffix garbage is signed and not listed as its own field.


F06 — High — Map params display​

numparams = mapLength (pairs) but _printParam walks single CBOR items. With N pairs (2N items), only N screens → incomplete map review.


F07 — Critical — EIP-1559 ERC20 dereferences a NULL gasPrice​

Originally filed as a display issue ("prints legacy gasPrice, often 0"). Code review during remediation showed it is worse: gasPrice for a type 0x02 transaction is not zero, it is unset.

_readEth (parser_impl_evm.c:203) zeroes eth_tx_obj, and parse_1559 (:150-171) reads max_priority_fee_per_gas / max_fee_per_gas and never touches gasPrice. Since RLP_KIND_BYTE == 0 (rlp_def.h:27), the zeroed field is {kind: RLP_KIND_BYTE, ptr: NULL, rlpLen: 0}, which rlp_readUInt256 handles as tmpBuffer[31] = *rlp->ptr (rlp.c:141) — a read through a NULL pointer.

Reached from printERC20TransferAppSpecific case 4, which passed &gasPrice for every transaction type. parser_validate_eth walks all items at parse time, so it triggers before any UI, on an ordinary EIP-1559 ERC20 transfer on FEVM (chain 314 / 314159). No attacker required.

Fixed by branching the fee screen on tx_type so gasPrice is never read for 1559. The NULL guard in rlp_readUInt256 belongs in zxlib and is still open — see the cross-app queue.


F08 — High — Item count overflow → empty review​

_getNumItems returns 0 when total > 255; parser_getNumItems accepts 0; parser_validate loops zero times and returns OK. Large param arrays can approve with no fields.


Medium / low (condensed)​

IDIssueFix sketch
F09Gas Limit as FIL 18-decInteger gas units
F10InvokeEVM hides fee cap non-expertAlways show fee cap
F11Shared FIL/raw tx_initializedPer-INS session
F12IO_RESET incomplete resetFull session clear
F13CLA_ETH → FIL INSCLA allowlists
F14raw_bytes msg_counter hygieneReset on error/INIT
F15ETH path no hardened accountMatch FIL policy
F17ETH EIP-191 length under-subtractReject body > declared
F16Unchecked blake_hashCHECK_ZXERR

What looks solid​

  • Success-path review_pending for reviews
  • Outer FIL CBOR EOF check
  • ERC20 exact 68-byte clear-sign gate (no trailing calldata on clear path)
  • InvokeEVM ERC20 requires zero native value
  • f0 rendering requires 0xFF + high zeros
  • FIL path: 44'/461' or testnet + hardened account
  • Sign: Blake2b → CID-prefixed Blake2b → ECDSA; key buffers wiped with sizeof
  • Distinct FVM vs ETH personal-message domains
  • Raw-bytes requires blindsign mode

Adversarial / full-path matrix​

ScenarioOriginal outcomeStatus
BLE CLA APDU during success reviewSAFE — rejectedunchanged
APDU during expert/blindsign error UIUNSAFE — F01/F02FIXED
Interleave FIL/ETH chunksUNSAFE — F03FIXED (re-rated, see below)
ERC20 + native value (ETH)UNSAFE — F04FIXED app-side
Params with trailing garbageUNSAFE — F05FIXED
Map-encoded paramsUNDER-DISPLAY — F06FIXED
EIP-1559 ERC20 clear-signMISLEADING FEES — F07FIXED — was a NULL deref, see below
Huge params array (item wrap)EMPTY REVIEW — F08FIXED
ERC20 + trailing calldata >68SAFE — not clear-signedunchanged
InvokeEVM ERC20 + valueSAFE — rejectedunchanged
Non-canonical f0 addressSAFE — print fails closedunchanged
Raw-bytes without blindsignSAFE — mode requiredunchanged

Re-rating after code review of the matrix rows​

F07 is not a display bug — it is a NULL-pointer dereference. _readEth zeroes eth_tx_obj and parse_1559 never assigns gasPrice, so for a type 0x02 transaction that field is {kind: RLP_KIND_BYTE (== 0), ptr: NULL, rlpLen: 0}. The old ERC20 screen passed it to printRLPNumber unconditionally → rlp_readUInt256 → rlp.c:141 tmpBuffer[31] = *rlp->ptr. It fires inside parser_validate_eth, which walks every item at parse time, so it is reached before any UI on any EIP-1559 ERC20 transfer — an ordinary FEVM transaction, no attacker needed. It survived because every one of the 500 vectors in tests/testvectors/evm.json is legacy (f8…) and the two 1559 vectors in eth_tests.test.ts are ERC-721, which fails validateERC20 and takes the blind-sign hash path.

F03 is weaker than rated. Every P1_INIT / P1_ETH_FIRST already calls tx_reset(), and the !tx_initialized guard blocks finishing an ETH stream with a FIL P1_LAST; no display-vs-signed mismatch could be constructed from FIL↔ETH interleaving. The real hole was in the FIL ↔ raw_bytes pair (F11/F14), which shared tx_initialized and msg_counter: a failed raw_bytes_init left msg_counter == 1 with the hasher live but the "Filecoin Sign Bytes:\n" prefix unchecked, so a later P1_ADD took the tx_rawbytes_update branch. That prefix is the only domain separation between raw-byte signing and transaction signing — crypto_sign and _readRawBytes both produce CID(blake2b(..)) for the same _sign. Exploitation needs blind-sign enabled and control of the aliased total/current through the parser_tx_obj union, so: hard to reach, but the fix is cheap.

Because the residual FIL↔ETH exposure is not a WYSIWYS break — whichever kind closes the stream is the kind the buffer is parsed, displayed and signed as — the remediation does not track a per-payload-kind stream owner. It uses the same three-state machine as ledger-cosmos, which blocks opening a second stream while one is in flight, and closes the prefix bypass separately with the raw-bytes initialized flag. ledger-icp carries only the review-lock half of this (g_review_pending); its chunk handling still uses a bare tx_initialized bool, so F03/F11/F14 remain open there.


Fixes applied​

App-only; deps/ledger-zxlib is untouched.

IDFixFile
F01, F02, F03, F11, F13, F14Adopted the tx_state_e state machine from ledger-cosmos (24ccb719): one g_tx_state of IDLE / RECEIVING / REVIEWING replaces the old review_pending and tx_initialized flags. P1_INIT requires IDLE, continuation chunks require RECEIVING, REVIEWING is the lock and is claimed only once view_review_show() returns, and only the reply paths in actions.h hand the state back. CATCH_OTHER promotes to REVIEWING whenever a handler threw with IO_ASYNCH_REPLY set — that is the F01/F02 fix, covering the error modals here and in zxlib — and otherwise resets to IDLE except on OK / COMMAND_NOT_ALLOWED. FIL instructions are additionally gated to CLA.apdu_handler.c, common/actions.h
F14raw-bytes state carries an initialized flag set only after the prefix check passes; raw_bytes_update and _readRawBytes refuse a session without it, replacing the msg_counter == 1 sentinelparser_raw_bytes.c, parser_txdef.h
F04isClearSignableERC20 additionally requires a zero native value; a transfer carrying FIL falls back to the blind-sign hash (with the warning UI re-armed) instead of showing only the token amountevm/parser_impl_evm_specific.c
F05the root params item must end exactly at params + params_len, so a suffix cannot be signed without a screenparser_impl.c
F06map params count 2 * mapLength screens, so keys and values are both reviewedparser_impl.c
F07the ERC20 fee screen branches on tx_type: 1559 shows Max fee per gas + Max priority fee, legacy/2930 keep Gas price; gasPrice is never read for 1559evm/parser_impl_evm_specific.c
F08parser_getNumItems rejects a zero item count for fil_tx, mirroring parser_evm.c:57parser.c

Regression tests added​

tests_zemu/tests/eth_tests.test.ts had no EIP-1559 ERC20 coverage at all:

  • erc20_transfer_1559 (clear-sign) — the F07 crash path. Renders To / Value / Nonce / Max fee per gas / Max priority fee / Gas limit.
  • erc20_transfer_with_native_value (blind-sign) — the F04 path. Must show the blind-sign warning and Eth-Hash, never a clear-signed token amount.

Snapshots generated for all five models (sp, x, st, fl, ap).

tests_zemu/tests/apdu.test.ts is new, ported from ledger-cosmos. It drives the request state machine through raw APDU exchanges — no snapshots, one container per device — covering: continuation chunks with no flow running, a second P1_INIT against a flow in progress, get-address and a raw-bytes or ETH first chunk arriving mid-flow, the Filecoin instructions rejected under CLA_ETH, a truncated HD path leaving the device idle rather than locked, and an unrelated error aborting a half-received transaction while COMMAND_NOT_ALLOWED leaves it intact.

Verified as a real regression test: it passes on all five models with these fixes and fails against unmodified HEAD at raw-bytes under the eth class, where the unfixed app answers 0x9000 — an Ethereum-class APDU driving Filecoin raw-bytes signing (F13).

Two status words are used deliberately. A continuation chunk with no flow running keeps the pre-existing TX_NOT_INITIALIZED (0x6987) so the wire contract for that case is unchanged for integrators; only "another request owns the device" is the new COMMAND_NOT_ALLOWED (0x6986), which is also the one status that preserves in-flight state in CATCH_OTHER.

One incidental finding: handle_generic_apdu in zxlib answers the reserved E0 01 00 00 get-device-info probe before handleApdu runs, so that exact header never reaches the app's class check. Expected Ledger dashboard behaviour, noted here because it looks like a CLA-gating hole from the outside.

Unrelated defect found while running these: eth_tests.test.ts does require("js-sha3") but the package was never declared in tests_zemu/package.json. Under yarn's flat hoisting it resolved transitively; the pnpm migration (c598f8ee) made it strict, so every ETH clear-sign test had been failing with Cannot find module 'js-sha3'. Added as a devDependency.

Test status​

Full zemu suite in CI with these fixes: 259 passed, 1 failed. The single failure is Standard › Parse Bytes Params - issue #173 for nanox failing with Timeout waiting to connect inside Zemu.start — the emulator container never came up, so no assertion ever ran. Infrastructure, not code.

Local runs during the audit were noisier — up to 9 failures across Standard InvokeEVM / InvokeEVM_ERC20Transfer (ExpertModeRequired) and personal_sign_non_printable_msg (Blindsign Mode Required). Those were initially recorded here as pre-existing breakage, on the grounds that they reproduce when every source change is stashed and the app is rebuilt from unmodified HEAD.

That conclusion was wrong, and CI corrects it. Both groups pass in a clean CI run. The stash-and-rebuild check only established that the failures were not caused by this branch; it did not establish that they were deterministic. The set of affected models moved between local runs, and the local Docker daemon crashed twice during the audit. The failures track emulator instability under concurrent container load, not a defect in the app or in the tests.

No separate issue is warranted. If the suite proves flaky in CI over time, the lead is container startup under five-way concurrency, not the mode toggles.


Cryptographic notes​

ControlStatus
Domain: Blake2b + CID prefix for FIL tx/rawOK
FVM \x19Filecoin Signed Message:\n vs ETH EIP-191Distinct
Private key zeroizeOK (sizeof)
Raw digest length must be 32OK
blake_hash return in crypto_signUnchecked (F16)
ETH EIP-191 first-chunk remaining mathWeak (F17)

  1. Set review_pending on all async UI paths (including errors); full session reset on IO_RESET.
  2. Unify chunk session across FIL/ETH/raw; CLA allowlists.
  3. ERC20: require zero native value; fix 1559 fee display.
  4. Params: strict CBOR consume; fix map item counts; reject num_items overflow/0.
  5. Gas Limit label; fee cap always for InvokeEVM; hash return checks.

Appendix A — Architectural brief​

See .audit/brief.md.

Coverage​

AreaStatus
APDU / review_pending / CLAHunted
process_chunk / raw_bytes / ETH chunkHunted
FIL CBOR / params / InvokeEVMHunted
ERC20 / EIP-1559 / EIP-191Hunted
Crypto Blake2b / sign / pathHunted
Device PoC / fuzzNot run

Machine findings: .audit/findings.jsonl
Cross-app: .audit/cross-app-queue.jsonl