# Unifund: cyclic-path WETH double-count drains core pool

Unifund swap facet double-counts WETH on a cyclic Uniswap V2 path, letting an attacker mint phantom group balance and drain ~4.99 WETH from the shared core.

- Payout: Real-world hack
- Severity: high
- Program: Unifund
- Analysis by: DeFiHackLabs
- Disclosed: 2026-11-10
- Write-up quality: 8/10
- Original: https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2026-11/Unifund_exp.sol
- Library page: https://bugbountylibrary.com/r/dhl%3A20261110-unifund
- Tags: area:web3 domain:web3 impact:funds-loss kind:incident platform:defihacklabs sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:flash-loan

**Target:** Unifund diamond core swap facet (swapExactTokensForTokens)
**Root cause:** The swap facet books input-side WETH spent and output-side WETH received as two separate deltas against the same per-group ledger key instead of netting them, and there is no guard against path[0] == path[last], so a cyclic WETH-A-B-WETH round trip that nets ~zero WETH is recorded as a WETH gain.
**Steps:**
1. Flash-borrow 12 WETH from Balancer V2 Vault via `flashLoan` and repay in `receiveFlashLoan`.
2. Deploy two scratch ERC20s A and B and seed rigged Uniswap V2 pools WETH/A, A/B, B/WETH so the cyclic path is routable.
3. Buy iFUND with 0.1 WETH, `createTokenList`, `addTokensToList`, `lockTokenList`, `create` a group, `join` with 1 ETH, `startFunding`.
4. Call `swapExactTokensForTokens(groupId, amountIn, amountOutMin, [WETH,A,B,WETH], deadline)` twice so each swap double-credits the group's WETH ledger key.
5. Call `finish(groupId)` then `withdraw(groupId, WETH)` to realize the phantom balance out of the core's shared pool; repay the flash loan in the same callback.
**How found:** Not stated; PoC reconstructs one of five on-chain exploit txs by forking mainnet at the exploit block and driving real contracts with typed interface calls (uint64/uint16 selectors recovered from tx and 4byte).
**Exploitation:** Attacker flash-borrows 12 WETH, deploys own path tokens and rigged pools, creates/joins/funds a group, performs two cyclic WETH-A-B-WETH swaps to inflate the group's WETH ledger, then finish()+withdraw() pays the phantom balance from the core's pooled funds belonging to other groups; flash loan repaid in the same callback.
**Escalation:** Repeated cyclic swaps double-credit each time; single reconstructed tx drained 4.628 WETH of the core's 4.994 WETH.
**Insight:** When a swap facet tracks per-group token deltas, test cyclic paths where path[0]==path[last]: if input and output legs are booked as separate deltas against one ledger key rather than netted, a ~zero round trip becomes a phantom gain payable from the shared pool.

## Proof of concept (copied exactly from the original report)

**Cyclic-path swap double-crediting group WETH**

```
function swapExactTokensForTokens(uint64 groupId, uint256 amountIn, uint256 amountOutMin, address[] calldata path, uint256 deadline) external;
    function finish(uint64 groupId) external;
    function withdraw(uint64 groupId, address token) external;
```

**Flash loan attack body and repayment**

```
function attack() external {
        address[] memory tokens = new address[](1);
        tokens[0] = address(WETH);
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = FLASH;
        VAULT.flashLoan(address(this), tokens, amounts, "");
        uint256 bal = address(this).balance;
        (bool ok,) = owner.call{value: bal}("");
        require(ok, "sweep");
    }

    function receiveFlashLoan(address[] memory, uint256[] memory, uint256[] memory, bytes memory) external {
        require(msg.sender == address(VAULT), "not vault");
        _body();
        // Repay the flash loan from the realized (phantom) balance.
        uint256 have = WETH.balanceOf(address(this));
        if (have < FLASH) WETH.deposit{value: FLASH - have}();
        WETH.transfer(address(VAULT), FLASH);
    }
```

