Spot price and quote can be manipulated easily #50
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
duplicate-383
satisfactory
satisfies C4 submission criteria; eligible for awards
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L391
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L399
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L408
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L422
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L437
Vulnerability details
Impact
Stop price and quote can be manipulated easily
Proof of Concept
Note the implementation for get price(), buyQuote() and sellQuote()
all the price or quote is determined by the baseTokenReserves() in the pool and the fractionTokenReserves() in the pool.
basically result = a / b, if a is going up, result is going up, if b is going down, result goes down.
if a user inject ETH by self-destruct or transfer ERC20 asset (via flashloan or just transfer what they owned) into the Pair contract directly, baseTokenReserves() goes up, price is inflated.
IF a user inject fracitonal token directly, the fractionTokenReserves() goes up, the price goes down and the price is deflated.
The POC below shows that by inflating the fractionTokenReserve(), price is deflated and deflated() and user get less and less when calling sellQuote.
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/test/Pair/unit/Sell.t.sol#L66
we run the test:
the output is:
note in the worst case, when removing liqudity, the user's LP can burned and receive nothing if the user does not choose the slippage control carefully.
Tools Used
Manual Review, Solidity
Recommended Mitigation Steps
Using spot price is very subject to manipulation as we have seen a lot of flashloan oracle manipulation attack.
We recommend the project implementation a more robust oracle solution such as TWAP oracle.
The text was updated successfully, but these errors were encountered: