Pair contract ratios can be messed up with dust amounts in the beginning #130
Labels
3 (High Risk)
Assets can be stolen/lost/compromised directly
bug
Something isn't working
duplicate-442
edited-by-warden
satisfactory
satisfies C4 submission criteria; eligible for awards
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/0212f9dc3b6a418803dbfacda0e340e059b8aae2/src/Pair.sol#L417-L428
Vulnerability details
Impact
The pair contract ratio can be messed up and lead to large rounding errors. This is a common cause of concern for AMM contracts, and ERC4626 Vault contracts.
The method is as follows:
This is caused due to the absence of floating point math, where in step 4 only 1 wei of LP is minted due to a rounding error.
Proof of Concept
Tools Used
Foundry // This should never revert with underflow if 1000 wei is minted during creation in the constructor
Recommended Mitigation Steps
The mitigation method is still unclear. Here are some ideas:
Uniswap handles this by burning the first 1000 wei of LP minted. This prevents this by making sure the LP always has at least 1000 wei, making the rounding error attack far less effective/impossible. This cannot be used in this case, since burning even a single wei of FRAC means losing an entire NFT in the contract.
Shift calculation origin to 1000 wei:
Instead of having the pair contract start with a balance of 0, make it start with a balance of 1000 wei, or during contract creation, create 1000 wei of unbacked LP tokens and send it to the zero address. Since these can never be recovered, being unbacked isn't an issue. In this scenario:
This will lead to errors in the order of 1000 wei which should be an insignificant amount. Also the minting logic needs to be slightly changed to recognize the new origin.
This is just a suggestion developed from rough maths. Needs to be tested thoroughly to address edge cases.
FixedPointMathLib
to calculate figures inaddQuote
andremoveQuote
. This can get rid of the rounding errors but result in a higher gas cost.The text was updated successfully, but these errors were encountered: