Submitted by 0xNineDec.
It is possible to manipulate the price of BathV2 tokens before a migration and consequently the migrator will receive underpriced V2 tokens, incurring losses.
The migration process of V2Migrator.migrate() hands in the users BathV1 balance in exchange of its underlying. Then, proceeds to mint the counterpart of BathV2 tokens providing the same amount of recovered underlying tokens:
This process can be abused from external actors when the V2 pool has low liquidity to manipulate the price of the V2 tokens considerably changing the amount of V2 tokens received by the migrating user.
The amount of V2 tokens (CTokens) that are minted are calculated after accruing the respective interests in CToken.mintFresh():
An attacker is able to manipulate the exchangeRate so it harms the subsequent minters:
Essentially, more tokens will be minted if the rate is decreased, as it is dividing the actualMintAmount, which could be done by increasing the totalSupply. The opposite effect can be done by increasing the totalCash or the totalBorrows, for example.
This scenario can be abused by attackers willing to harm other users that are migrating from one type of token to another, knowing that the liquidity of the pool is low.
The following script shows how an attacker is able to manipulate the price of the V2 token by borrowing in the same market. The output shows both scenarios, when the price is manipulated and when it is under normal conditions.
It is simulating the amount of underlying tokens that the victim would receive in either cases. The difference between the scenario A and B (without repayment and repaying the borrow) is: WITHOUT_REPAYING - WITH_REPAYMENT = 19899995499999977864007 - 19899995504034745960181 = -4034768096174, considering that the TestCoin has 8 decimals and is a valuable token (like WBTC), the loss of migrating will mean 40347 of tokens, even if the price is 1 USD per token, the loss is considerable.
Simulate the amount of underlying tokens users would get if they redeem the whole BathV2 amount immediately after migrating and allow users to set up a slippage on the amount of underlying tokens they would get (comparing against the simulated amount).
daoio (Rubicon) acknowledged
