---
title: "Business logic flaws"
url: https://dontgetrekt.today/dev/business-logic
section: "Is the math and the design right?"
owasp: SC02:2026
---

# Business logic flaws

Each line of code is correct, but the design lets an attacker take value: rewards claimed twice, debt that disappears, a liquidation that pays the liquidated account.

## How the attack works

1. The protocol has an invariant that nobody wrote down, for example "total shares always match total assets".
2. One path breaks it: a transfer that does not update a reward index, a burn that skips a debt check, a self-liquidation, a deposit and a withdraw in one block.
3. The attacker repeats the path, often with a flash loan to make each step large.
4. These bugs pass audits because no single function is wrong. The bug is in how the functions work together.

## Vulnerable code

```solidity
// Rewards use the balance at claim time, not the time held.
function claim() external {
    uint256 owed = balanceOf(msg.sender) * rewardPerToken - paid[msg.sender];
    paid[msg.sender] += owed;
    reward.transfer(msg.sender, owed);
}
// Attack: claim, transfer the tokens to a 2nd address, claim again, repeat.
```

## Fixed code

```solidity
// Settle rewards for both accounts on EVERY balance change.
function _update(address from, address to, uint256 amt) internal override {
    _settle(from);
    _settle(to);
    super._update(from, to, amt);
}

function _settle(address a) internal {
    if (a == address(0)) return;
    owed[a] += balanceOf(a) * (rewardPerToken - paidPerToken[a]) / 1e18;
    paidPerToken[a] = rewardPerToken;
}
```

## Review checklist

- Write the invariants of the protocol in plain words before you write code. Put them in the docs and in the tests.
- For each state variable, list every function that changes it. Check that all of them keep the invariants.
- Check the edge cases: the first and last user, zero amounts, the same address as sender and receiver, many actions in one block.
- If you fork a protocol, list each change that you made and why it keeps the invariants of the original.

## How to test for it

- Write invariant (stateful fuzz) tests in Foundry or Echidna. Let the fuzzer call all functions in random order.
- Write a test for each attack path from the real examples below.

## Real exploits (211)

- 2023-03-13 Euler Finance ($197.0M): Euler Finance lost about $197M because the donateToReserves function, added in EIP-14, did not check the donor's account health, so an attacker could create bad debt and liquidate it for profit. [rekt.news](https://rekt.news/euler-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2023-03/Euler_exp.sol)
- 2021-09-29 Compound ($147.0M): Compound lost about $147M of COMP that was wrongly distributed by a bug in the updated Comptroller (Proposal 62), and the public drip() on the Reservoir let the loss grow. [rekt.news](https://rekt.news/compound-rekt)
- 2021-10-08 Mirror Protocol ($92.0M): Mirror Protocol lost about $90M in October 2021 because its lock contract allowed unlock_position_funds to be called repeatedly for one position, and a second ~$2M loss in May 2022 came from a stale LUNC oracle price. [rekt.news](https://rekt.news/mirror-rekt)
- 2021-09-29 Compound ($80.0M): Compound's Comptroller, after proposal 62, wrongly paid out about $80M in excess COMP rewards because of a ">" that should have been ">=" in two places. [rekt.news](https://rekt.news/overcompensated)
- 2021-04-28 Uranium Finance ($57.2M): Uranium Finance, a Uniswap V2 fork on BSC, lost about $57.2M because a constant changed from 1000 to 10000 in only some places of the swap invariant check. [rekt.news](https://rekt.news/uranium-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-04/Uranium_exp.sol)
- 2026-03-12 Aave (CoW Protocol swap integration) ($50.0M): A user lost about $50M of aEthUSDT on Aave when a collateral swap via CoW Protocol routed through a ~$74k SushiSwap pool with ~99% price impact, because the new interface had no hard price-impact or slippage cap. [rekt.news](https://rekt.news/price-impact-kills)
- 2026-03-10 Aave ($27.8M): Aave lost $27.78M of healthy wstETH E-Mode positions to wrongful liquidations when its CAPO oracle was misconfigured and priced wstETH 2.85% too low. [rekt.news](https://rekt.news/aave-rekt)
- 2021-08-03 Popsicle Finance ($20.0M): Popsicle Finance lost about $20M because fee rewards were not updated correctly when PLP tokens were transferred, so the same LP tokens could claim fees many times. [rekt.news](https://rekt.news/popsicle-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-08/Popsicle_exp.sol)
- 2021-05-16 bEarn (BearnFi) ($18.0M): bEarn's BvaultsBank on BSC lost about $18M because withdraw treated the same amount as BUSD in the bank but as ibBUSD in the strategy. [rekt.news](https://rekt.news/bearn-rekt)
- 2022-10-27 Team Finance ($15.8M): Team Finance lost about $15.8M from locked Uniswap V2 liquidity of four projects (CAW, TSUKA, KNDX, FEG) because its migrate() function from V2 to V3 was flawed. [rekt.news](https://rekt.news/teamfinance-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-10/TeamFinance_exp.sol)

## References

- [OWASP SC02:2026](https://scs.owasp.org/sctop10/SC02-BusinessLogicVulnerabilities/)
- [Foundry: Invariant testing](https://book.getfoundry.sh/forge/invariant-testing)
- [Echidna fuzzer](https://github.com/crytic/echidna)
