Reproduced Exploit

Pancake BinPool: a donated active bin rounds the next LP's shares to zero and keeps their tokens

The first LP mints 1 share with 1 X and 1 Y, then donates 1000 of each into the same bin. A joiner adds 100 X and 0 Y. Composition fees are charged because the ratio does not match the bin. mulDivRoundDown then divides 99 X of user liquidity by about 2003 X of bin liquidity with supply 1, and the q…

Aug 2024•Ethereum•arithmetic•3 min read

Loss

Joiner's 100 X is absorbed by a 1-share bin. The first LP withdraws 1101 X

Chain

Ethereum

Category

arithmetic

Date

Aug 2024

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: 46983-loss-of-input-tokens-due-to-incorrect-rounding-ottersec-none. Standalone Foundry PoC and full write-up: 46983-loss-of-input-tokens-due-to-incorrect-rounding-ottersec-none_exp in the evm-hack-registry mirror.


Vulnerability classes: vuln/arithmetic/rounding-direction · vuln/arithmetic/precision-loss

Key info#

FieldValue
LossJoiner's 100 X is absorbed by a 1-share bin. The first LP withdraws 1101 X
Vulnerable contractBinPool.mulDivRoundDown inside _updateBin
Attacker EOA0x1111111111111111111111111111111111111111
Attack contractExploit
Attack txExploit.run()
Chain / block / dateEthereum model · block 0 · 2024-08
Compilersolc 0.8.27, cancun, via IR
Bug classActive-bin share mint rounds down against reserves inflated by a donation

TL;DR#

The first LP mints 1 share with 1 X and 1 Y, then donates 1000 of each into the same bin. A joiner adds 100 X and 0 Y. Composition fees are charged because the ratio does not match the bin. mulDivRoundDown then divides 99 X of user liquidity by about 2003 X of bin liquidity with supply 1, and the quotient is 0. The 100 X stays in the reserves. The single share withdraws 1101 X. A mint that returns before transferFrom when the quotient is 0 leaves the joiner's 100 X untouched.

Background#

OtterSec's review of Pancake Infinity BinPool._updateBin, reported by Nicholas R. Putra. Adding liquidity to the active bin at a different ratio charges composition fees and then recomputes shares with userLiquidity.mulDivRoundDown(supply, binLiquidity). The patch in pancake commit adf9b04 on 2024-08-26 requires a minimum share before a donation can inflate the bin.

The vulnerable code#

SOLIDITY
function mulDivRoundDown(uint256 a, uint256 b, uint256 c) internal pure returns (uint256) {
    // @> VULN: division rounds down, so 99 tokens of liquidity over donated reserves mints 0 shares
    return (a * b) / c;
}

_updateBin calls this after getCompositionFeesAmount when the active-bin fee is nonzero. a is user liquidity, b is share supply, c is bin liquidity including the donation. The deposit is added to reserves whether or not shares is 0.

Root cause#

Share supply stays at 1 while a donation increases reserves without minting shares. The round-down division of a modest deposit by that inflated liquidity is 0, but the tokens have already been pulled.

Preconditions#

  • One LP holds the only share. Reserves are 1001 X and 1001 Y after a 1 plus 1000 donation on each side.
  • The joiner deposits 100 X and 0 Y, which does not match the bin ratio, so the composition-fee branch runs.

Attack walkthrough#

  1. The joiner's mint pulls 100 X.
  2. The composition fee is ax / 100, which is 1 X. User liquidity is 99 X.
  3. (a * b) / c with supply 1 floors to 0 shares.
  4. lastShares is 0 and totalSupply stays 1.
  5. withdraw pays the whole bin to the first LP: 1101 X.
  6. The control computes the same zero quotient and returns before taking the tokens. The joiner still holds 100 X and the reserves stay at the donated 1001 X.

Diagrams#

flowchart TD A["First LP holds 1 share after donating 1000 X and 1000 Y"] --> B["Joiner adds 100 X and 0 Y"] B --> C["Composition fee is 1 X and user liquidity is 99 X"] C --> D["VULN divides 99 X by about 2003 X and rounds to 0 shares"] D --> E["The 100 X stays in the bin"] E --> F["The single share withdraws 1101 X"]

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.