Skip to content
Business logic Audit finding Intermediate

Miscalculation in addLiquidity of Router results in unauthorized spending of tokens

A miscalculation in the addLiquidity function of the Router causes the requested token amounts to be incorrectly adjusted based on the pool's balance and reserves, leading to the victim spending more tokens than intended. An attacker can donate tokens to the pool to manipulate the calculation and force the victim to transfer extra funds.

Trust Follow with ether_sky

Originally disclosed on Code4rena

Submitted by Trust, also found by ether_sky

Description

Users are expected to add liquidity through addLiquidity() of the Router. It receives base and quote amounts which are adjusted through _adjustAddLiquidity():

function addLiquidity(
    address lp,
    address to,
    uint256 baseInAmount,
    uint256 quoteInAmount,
    uint256 minimumShares,
    uint256 deadline
) external ensureDeadline(deadline) returns (uint256 baseAdjustedInAmount, uint256 quoteAdjustedInAmount, uint256 shares) {
    (baseAdjustedInAmount, quoteAdjustedInAmount) = _adjustAddLiquidity(lp, baseInAmount, quoteInAmount);
    IMagicLP(lp)._BASE_TOKEN_().safeTransferFrom(msg.sender, lp, baseAdjustedInAmount);
    IMagicLP(lp)._QUOTE_TOKEN_().safeTransferFrom(msg.sender, lp, quoteAdjustedInAmount);
    shares = _addLiquidity(lp, to, minimumShares);
}

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:

(uint256 baseReserve, uint256 quoteReserve) = IMagicLP(lp).getReserves();
uint256 baseBalance = IMagicLP(lp)._BASE_TOKEN_().balanceOf(address(lp)) + baseInAmount;
uint256 quoteBalance = IMagicLP(lp)._QUOTE_TOKEN_().balanceOf(address(lp)) + quoteInAmount;
baseInAmount = baseBalance - baseReserve;
quoteInAmount = quoteBalance - quoteReserve;

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 victim’s funds.

Impact

Unauthorized spending of victim’s tokens, leading to monetary losses.

Proof of Concept

  1. MagicLP has (500 USDC, 500 DAI) reserves and balances, 100 shares
  2. Victim calls addLiquidity for (1000 USDC, 1000 DAI) , min 200 shares
  3. Attacker frontrans and donates (1000 USDC, 500 DAI) to the LP
  4. addLiquidity() executes:
  5. baseInAmount = 1500 + 1000 - 500 = 2000 USDC
  6. quoteInAmount = 1000 + 1000 - 500 = 1500 DAI
  7. baseIncreaseRatio = 2000 / 500 = 4
  8. quoteIncreaseRatio = 1500 / 500 = 3
  9. quoteAdjustedInAmount = 1500 DAI
  10. baseAdjustedInAmount = 500 * 3 = 1500 USDC

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.

Recommended Mitigation Steps

Change the usage of getReserves() and balanceOf() in the lines below as described:

(uint256 baseReserve, uint256 quoteReserve) = IMagicLP(lp).getReserves();
uint256 baseBalance = IMagicLP(lp)._BASE_TOKEN_().balanceOf(address(lp)) + baseInAmount;
uint256 quoteBalance = IMagicLP(lp)._QUOTE_TOKEN_().balanceOf(address(lp)) + quoteInAmount;

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:

function previewAddLiquidity(
    address lp,
    uint256 baseInAmount,
    uint256 quoteInAmount
) external view returns (uint256 baseAdjustedInAmount, uint256 quoteAdjustedInAmount, uint256 shares) {
    (uint256 baseReserve, uint256 quoteReserve) = IMagicLP(lp).getReserves();
    uint256 baseBalance = IMagicLP(lp)._BASE_TOKEN_().balanceOf(address(lp)) + baseInAmount;
    uint256 quoteBalance = IMagicLP(lp)._QUOTE_TOKEN_().balanceOf(address(lp)) + quoteInAmount;
    baseInAmount = baseBalance - baseReserve;
    quoteInAmount = quoteBalance - quoteReserve;

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:

Fix here:

https://github.com/Abracadabra-money/abracadabra-money-contracts/pull/141

cccz (Judge) decreased severity to Medium and commented:

This is more of a griefing attack, and it seems the victim’s 200 share would be worth 2000 USDC, 1666 DAI?

Trust (Warden) commented:

Unauthorized spending of tokens is breaking of a core invariant (do not use money user did not intend to spend), which by itself achieves High impact. The submission also explains the risks affecting the unapproved funds.

Note that sponsor has confirmed the issue at High severity and views it as such.

cccz (Judge) commented:

Hey @trust1995, in POC, if I understand correctly, while the victim spends more tokens, the shares the victim receives will be worth more due to the donations of the griefer (3000 -> 3666), and there doesn’t seem to be any locks here, so I don’t think this as a higher impact than DOS.

Trust (Warden) commented:

I can confirm that the user spends more tokens and receives more shares. In my opinion that suffices for H severity as it is not blocking a victim interaction, it is taking unapproved funds (until the user figures out how to redeem them back). Again, touching user’s assets without their permission is very severe.

cccz (Judge) commented:

In terms of this attack scenario

  1. The attacker needs to donate funds
  2. the victim spends more funds than expected
  3. The victim receives a portion of the attacker’s donated funds
  4. the user who added the liquidity should know how to get them back
  5. victims can get more money back immediately

So I don’t think this is high severity.


The original report as published on Code4rena, by Trust. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

The balanceOf() and getReserves() calls were reversed in the adjustment logic, so the requested amount is calculated as balance + requested - reserves instead of reserves + requested - balance.

Impact

The victim spends more tokens than intended, but they receive proportionally more shares that are worth more due to the attacker's donation, so the net financial loss may be limited; the judge noted the victim's shares would be worth more.

What the attacker needed

Attacker can front-run the victim's transaction; attacker donates tokens to the pool; victim calls addLiquidity

The fix

The issue was fixed by correcting the order of getReserves() and balanceOf() in the calculation and adding an invariant check that adjusted amounts are not greater than the original input amounts.

How it was found

  1. MagicLP has (500 USDC, 500 DAI) reserves and balances, 100 shares
  2. Victim calls addLiquidity for (1000 USDC, 1000 DAI), min 200 shares
  3. Attacker frontruns and donates (1000 USDC, 500 DAI) to the LP
  4. addLiquidity() executes and calculates baseInAmount = 1500 + 1000 - 500 = 2000 USDC and quoteInAmount = 1000 + 1000 - 500 = 1500 DAI
  5. baseIncreaseRatio = 2000 / 500 = 4 and quoteIncreaseRatio = 1500 / 500 = 3
  6. quoteAdjustedInAmount = 1500 DAI and baseAdjustedInAmount = 500 * 3 = 1500 USDC, so LP::buyShares() is called with (1500, 1500) for 200 shares instead of (1000, 1000)

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.

When you add money to a liquidity pool, the code is supposed to adjust your deposit so you don't put in more than needed. But a mistake in the calculation means that if someone else has already put extra money into the pool, you end up sending more of your own money than you wanted. An attacker can exploit this by donating to the pool first, causing you to spend more.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Trust

View all

77 more disclosed reports, best first.

More from Abracadabra Mimswap

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.