Reproduced Exploit

Term Structure: assigning bad debt overwrites a 900 shortfall and 10 of debt sweeps 910 collateral

_redeemFromMarket stores ftReserve - totalRedeem with =. Two orders share one collateral. The first shortfall is 900. The second is 10 and replaces the 900. dealBadDebt pays amount collateralBalance / badDebtAmt. Calling it for the remaining 10 pays 10 910 / 10, which is the entire 910 collateral.…

Feb 2025•Ethereum•logic•3 min read

Loss

910 collateral paid out against 10 of recorded bad debt. The first 900 shortfall is erased

Chain

Ethereum

Category

logic

Date

Feb 2025

Source

AuditVault

EVM Playground

Source-level debugger — step opcodes and Solidity in sync

evm-hack-analyzer

The attack is replayed in an in-browser EVM preloaded with the exact dumped fork state. The execution tree shows every call; step by Solidity line or by opcode across all depths — source, Stack, Memory, Storage, Balances (native / ERC-20 / NFT), Transient storage and Return value stay in sync. Click a tree node, opcode, or source line to jump. Query opens a search over the trace: previous and next writes, balance changes and calls, breakpoints, and a diff of everything that changed between two steps. No backend, no live RPC.

Loading fork state…

Source & credit. Reproduction of a public audit finding curated by AuditVault — the original finding: 54897-baddebtmappingcollateral-can-be-unintentionally-rewrite-cant. Standalone Foundry PoC and full write-up: 54897-baddebtmappingcollateral-can-be-unintentionally-rewrite-cant_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/logic/state-update · vuln/logic/incorrect-state-transition

Key info#

FieldValue
Loss910 collateral paid out against 10 of recorded bad debt. The first 900 shortfall is erased
Vulnerable contractTermVault.redeemFromMarket
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2025-02
Compilersolc 0.8.27, cancun, optimizer 200
Bug class= replaces _badDebtMapping[collateral] instead of +=

TL;DR#

_redeemFromMarket stores ftReserve - totalRedeem with =. Two orders share one collateral. The first shortfall is 900. The second is 10 and replaces the 900. dealBadDebt pays amount * collateralBalance / badDebtAmt. Calling it for the remaining 10 pays 10 * 910 / 10, which is the entire 910 collateral. Lenders of the first shortfall have nothing left.

Background#

Cantina's Term Structure competition, February 2025, reported by BengalCatBalu. Different markets can share a collateral token. A withdrawal fee often makes totalRedeem < ftReserve, so the bad-debt branch runs often. Term Structure fixed the write in PR 4 and PR 5. Cantina verified the fix.

The vulnerable code#

SOLIDITY
if (totalRedeem < ftReserve) {
    address coll = address(collateral);
    badDebtMapping[coll] = ftReserve - totalRedeem; // @> VULN: replaces the previous shortfall instead of adding to it
}
DIFF
- badDebtMapping[coll] = ftReserve - totalRedeem;
+ badDebtMapping[coll] += ftReserve - totalRedeem;

Root cause#

The mapping is keyed only by collateral, and the update is a replacement. A second order on the same collateral drops the earlier shortfall out of the denominator that dealBadDebt uses to split the seized balance.

Preconditions#

  • Two redeems leave a shortfall on the same collateral. Here, 1000 redeemed for 100, then 100 redeemed for 90.
  • The vault holds collateral for both shortfalls, 910.
  • dealBadDebt is called for the overwritten amount, 10.

Attack walkthrough#

  1. Vault holds 910 collateral.
  2. First redeem records 900.
  3. Second redeem writes 10 over the 900.
  4. dealBadDebt(attacker, collateral, 10) transfers 910 collateral.

Diagrams#

flowchart TD A["Vault holds 910 collateral"] --> B["First shortfall stores 900"] B --> C["VULN: second shortfall assigns 10"] C --> D["dealBadDebt reads the denominator 10"] D --> E["10 times 910 over 10 pays all collateral"] E --> F["The 900 shortfall has no collateral left"]

Impact#

The caller receives 910 collateral. The vault balance is 0 and the mapping is 0. The 900 of bad debt from the first order is not paid. With += the same 10 call would receive 10 collateral, and 900 would remain reserved for the first shortfall. The control assertion is 10 * 910 / 910 = 10.

Remediation#

Use += when storing the shortfall, which is the change Term Structure shipped.

How to reproduce#

BASH
cd 54897-baddebtmappingcollateral-can-be-unintentionally-rewrite-cant_exp
forge test -vv

test_overwrite_lets_10_of_debt_sweep_910_collateral asserts the attacker received 910 ether and the vault is empty. test_control_plus_equals_pays_only_the_second_slice asserts the fair payout is 10 ether.

Sources#


Sources & further analysis#

Reproductions & code

Alerts & third-party analyses

  • Web3Sec X hacked database: search.
  • Rekt leaderboard: search.
  • Solodit incident search: search.

These dashboards index community alerts tweets, post-mortems, and independent write-ups. Reach them through the protocol name above to cross-check this reproduction against other analyses.