Vaults with non-UST underlying asset vulnerable to flash loan attack on curve pool
The NonUSTStrategy contract uses a Curve pool's spot exchange rate to value assets, which can be manipulated by a flash loan. An attacker could take a flash loan, swap a large amount to skew the pool's rate, then deposit or withdraw from the vault at favourable terms and profit.
Submitted by camden, also found by cccz, cmichel, danb, defsec, harleythedog, hyh, kenzo, leastwood, palina, pauliax, pmerkleplant, Ruhum, WatchPug, and ye0lde
In short, the NonUSTStrategy is vulnerable to attacks by flash loans on curve pools.
Here’s an outline of the attack:
- Assume there is a vault with DAI underlying and a
NonUSTStrategywith a DAI / UST curve pool - Take out a flash loan of DAI
- Exchange a ton of DAI for UST
- The exchange rate from DAI to UST has gone up (!!)
- Withdraw or deposit from vault with more favorable terms than market
- Transfer back UST to DAI
- Repay flash loan
Proof of Concept
Here is my proof of concept: https://gist.github.com/CamdenClark/932d5fbeecb963d0917cb1321f754132
I can provide a full forge repo. Just ping me on discord.
Exploiting this line: https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/strategy/NonUSTStrategy.sol#L135
Tools Used
Forge
Recommended Mitigation Steps
Use an oracle
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the strategy relies on a manipulable spot price from a Curve pool instead of a secure oracle.
Impact
An attacker could profit at the expense of the vault by manipulating the pool rate, potentially draining value from the vault.
What the attacker needed
Vault with non-UST underlying; strategy uses a Curve pool; flash loan available
The fix
The strategy should use a secure oracle instead of the Curve pool's spot rate.
How it was found
- Assume a vault with DAI underlying and a NonUSTStrategy using a DAI/UST Curve pool.
- Take out a flash loan of DAI.
- Exchange a large amount of DAI for UST, which raises the DAI-to-UST exchange rate.
- Deposit or withdraw from the vault at terms more favourable than market.
- Transfer UST back to DAI.
- Repay the flash loan.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A flash loan is a loan that must be borrowed and repaid in the same transaction. Here, the vault uses the current price from a trading pool to decide how much shares are worth. An attacker can borrow a huge amount, trade it to move that price, then use the vault at the distorted price to make a profit, all in one transaction.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.