# Reentrancy in creating Creds allows an attacker to steal all Ether from the Cred contract

Reentrancy via ETH refund in Phi Cred creation lets attacker overwrite cred's bonding curve before credIdCounter increments, then sell shares at inflated price to drain contract.

- Payout: Audit contest
- Severity: high
- Program: Phi
- Found by: CAUsr
- Disclosed: 2024-10-07
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2024-08-phi-findings/issues/25
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-phi%3AH-06
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:reentrancy

**Target:** Cred.sol createCred/buyShareCred/sellShareCred share trading contract
**Root cause:** createCred writes creds[credIdCounter] then calls buyShareCred which refunds excess ETH to sender before credIdCounter is incremented; no reentrancy guard, so a reentrant createCred overwrites the same cred slot including its bondingCurve.
**Steps:**
1. Whitelist two bonding curves: a cheap FixedPriceBondingCurve and the expensive default BondingCurve.
2. Prepare two signed createCred payloads (cheap curve and expensive curve) with attacker helper contract as creator.
3. Attacker calls createCred with cheap curve and excess ETH (`1.5 ether`), triggering refund to receive().
4. In receive(), call `buyShareCred{value:0.1 ether}(credId, 900, 0)` to buy shares cheaply, triggering another refund.
5. In next receive(), call createCred again with expensive curve payload; credIdCounter unchanged so creds[credId] is overwritten with expensive bondingCurve.
6. In next receive(), compute sellAmount fitting contract balance and call `sellShareCred(credId, sellAmount, 0)` to sell at expensive curve price.
7. Withdraw proceeds; nested calls unwind paying fees, attacker nets >40 ETH from 2 ETH initial.
8. Repeat attack since no lock (lastTradeTimestamp unset) and counter resets.
**How found:** Code reading of _createCredInternal: noticed creds[credIdCounter] is populated and buyShareCred (which refunds excess ETH) is called before credIdCounter += 1, and that no reentrancy guard exists on createCred/buyShareCred/sellShareCred; hypothesized overwriting bondingCurve mid-call.
**Exploitation:** Attacker deploys helper contract, funds with small capital (2 ETH in PoC), and in a single transaction chains createCred -> buyShareCred -> createCred(overwrite curve) -> sellShareCred using ETH refund callbacks as reentry points. Cheap curve purchases shares at low price; overwritten expensive curve prices the same shares high, so selling drains >45 ETH of the Cred contract's 50 ETH balance.
**Escalation:** Attack repeatable instantly and numerously; requires only >1 whitelisted bonding curve with ~10% price difference to be profitable.
**Insight:** In share/curve contracts, check ordering of state writes vs external ETH refunds: any refund callback before an ID counter increment lets a reentrant call overwrite the same storage slot, so swapping a pricing parameter (bonding curve) mid-transaction turns cheap buys into expensive sells.

