Skip to content
Business logic Audit finding Intermediate

MagicLpAggregator always returns lower than correct answer, leading to arbitrage loss

The MagicLpAggregator prices LP tokens by taking the minimum of two underlying oracle prices and applying it to the sum of both reserves, which undervalues the LP when the two assets have different prices. An attacker could buy LP tokens at this undervalued price, redeem them for the underlying assets, and sell those assets at their true market value for a profit.

Trust Follow

Originally disclosed on Code4rena

Submitted by Trust

MagicLpAggregator is used to price LP tokens for “closely-tied” underlying tokens. It calculates the price below:

function latestAnswer() public view override returns (int256) {
    uint256 baseAnswerNomalized = uint256(baseOracle.latestAnswer()) * (10 ** (WAD - baseOracle.decimals()));
    uint256 quoteAnswerNormalized = uint256(quoteOracle.latestAnswer()) * (10 ** (WAD - quoteOracle.decimals()));
    uint256 minAnswer = baseAnswerNomalized < quoteAnswerNormalized ? baseAnswerNomalized : quoteAnswerNormalized;
    (uint256 baseReserve, uint256 quoteReserve) = _getReserves();
    baseReserve = baseReserve * (10 ** (WAD - baseDecimals));
    quoteReserve = quoteReserve * (10 ** (WAD - quoteDecimals));
    return int256(minAnswer * (baseReserve + quoteReserve) / pair.totalSupply());
}

The code takes the minimal answer between the underlying oracles and considers all reserves to be worth that amount: return int256(minAnswer * (baseReserve + quoteReserve) / pair.totalSupply());

The issue is that any difference in price between the assets represents an easy arbitrage opportunity. Suppose we have tokens (A,B), where real oracle shows:

  • A = $0.99
  • B = $1

The Pool has 1000000 LP tokens and contains:

  • 1000000 A
  • 1000000 B

The LP value would calculate as: 0.99 * 2000000 / 1000000 = $1.98 The actual value is: (0.99 * 1000000 + 1 * 1000000) / 1000000 = $1.99

Suppose a platform trades LPs using the aggregator pricing. An attacker could:

  • Buy 100,000 LP tokens at $198000
  • Withdraw from the pool the underlying shares
  • Sell 100,000 A, 100,000 B at $199000
  • Profit $1000 from the exchange, when the difference is just$0.01 (this is very common fluctuation even with pegged assets).

The delta comes at the expense of LP holders whose position gets minimized.

Impact

Loss of value due to arbitrage of any platform using MagicLpAggregator pricing.

Recommended Mitigation Steps

Always calculate the value based on the real underlying token value multiplied by amount.

Consider creating two separate oracles for lower-bound and upper-bound results. Then a lending protocol could indeed use the lower-bound for determining collateral value.

0xm3rlin (Abracadabra) disputed and commented:

Intended behavior.

141345 (Lookout) commented:

Rounding error could accumulate in MagicLpAggregator.


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

Likely the aggregator applies the minimum of the two oracle prices to the combined reserves instead of valuing each reserve at its own oracle price.

Impact

Any platform using MagicLpAggregator pricing could suffer a loss of value through arbitrage, at the expense of LP holders, whenever the two underlying prices differ even slightly.

What the attacker needed

A platform prices LPs with MagicLpAggregator; the two underlying oracle prices differ; attacker can buy and redeem LP tokens

The fix

The value should be calculated by multiplying each underlying token's real oracle price by its own reserve amount, and the project could offer separate lower-bound and upper-bound oracles so a lending protocol can use the lower bound for collateral.

How it was found

  1. Observe that the two underlying oracles report different prices, for example 0.99 and 1.
  2. Buy LP tokens from a platform that prices them using the aggregator, paying the undervalued amount.
  3. Withdraw the underlying tokens from the pool using the LP tokens.
  4. Sell the underlying tokens at their true market price to realise a profit.

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.

An LP token represents a share of a pool holding two different tokens. The aggregator tried to price that share by taking the lower of the two token prices and applying it to the whole pool, which makes the share look cheaper than it really is. Someone could buy shares at that cheap price, cash them in for the real tokens, and sell those tokens for more than they paid.

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.