Submitted by Trust, also found by ether_sky
Users are expected to add liquidity through addLiquidity() of the Router. It receives base and quote amounts which are adjusted through _adjustAddLiquidity():
The adjustment function has DODOv2 code which determines the effective base:quote ratio and adjusts the higher of the two input values so that no input is wasted. Additionally Abrakadbra inserted the following logic at the start of the function:
The intention is that if the current balance of token X is higher than reserve, the input should be reduced so that only the delta needs to be sent. However the logic is miswritten.
The balanceOf() and getReserve() calls have been reversed. The correct logic should be:
requested amount = reserves + requested amount - balance
Instead it is:
requested amount = balance + requested amount - reserves.
The higher the current balanceOf(), the higher the final requested amount becomes. This is a critical issue because tokens can be donated to the MagicLP by an attacker, making the victim send more tokens than expected. The POC gives an example of such a sequence.
Essentially this makes addLiquidity(amountX, amountY, minShares) become: (amountX2, amountY2, minShares) where
amountX2 > amountX, amountY2 > amountY. By definition this is unauthorized use of the victims funds.
Unauthorized spending of victims tokens, leading to monetary losses.
Therefore LP::buyShares() is called with (1500, 1500) for 200 shares instead of (1000, 1000)
User has now put an extra ($500,$500) of unauthorized funds in the contract. They may or may not be aware of it when the TX is executed. Note that funds put in the LP are obviously in risk of impermanent loss alongside any other economic, systemic and security-related risks.
This is a simple example to prove the point but could be tweaked for different tokens (more or less volatile), amounts and ratios.
Change the usage of getReserves() and balanceOf() in the lines below as described:
A great extra invariant check would be to require in addLiquidity() that the results baseAdjustedInAmount, quoteAdjustedInAmount are smaller than the original input amounts respectively.
Note that the issue also occurs in the preview function:
It should also be fixed here. We view it as the same root cause, but of a lesser end impact.
0xCalibur (Abracadabra) confirmed and commented:
cccz (Judge) decreased severity to Medium and commented:
Trust (Warden) commented:
cccz (Judge) commented:
Trust (Warden) commented:
cccz (Judge) commented:
