Skip to content
Business logic Audit finding Advanced

Attacker can amplify a rounding error in MagicLP to break the I invariant and cause malicious pricing

A rounding error in the initial LP deposit of MagicLP pools allows an attacker to set the pool's target ratio to twice the intended value, breaking the invariant and causing incorrect pricing. An attacker could then trade against the pool to profit at the expense of other users.

Trust Follow

Originally disclosed on Code4rena

Submitted by Trust

One of the two key parameters in MagicLP pools is I, which is defined to be the ideal ratio between the two reserves. It is set during MagicLP initialization: _I_ = i;

It is used when performing the initial LP deposit, in buyShares():

if (totalSupply() == 0) {
    // case 1. initial supply
    if (quoteBalance == 0) {
        revert ErrZeroQuoteAmount();
    }
    shares = quoteBalance < DecimalMath.mulFloor(baseBalance, _I_) ? DecimalMath.divFloor(quoteBalance, _I_) : baseBalance;
    _BASE_TARGET_ = shares.toUint112();
    _QUOTE_TARGET_ = DecimalMath.mulFloor(shares, _I_).toUint112();
    if (_QUOTE_TARGET_ == 0) {
        revert ErrZeroQuoteTarget();
    }
    if (shares <= 2001) {
        revert ErrMintAmountNotEnough();
    }
    _mint(address(0), 1001);
    shares -= 1001;

The QUOTE_TARGET is determined by multiplying the BASE_TARGET with I.

The flaw is in the check below: shares = quoteBalance < DecimalMath.mulFloor(baseBalance, _I_) ? DecimalMath.divFloor(quoteBalance, _I_) : baseBalance; Essentially there needs to be enough quoteBalance at the I ratio to mint baseBalance shares, if there’s not enough then shares are instead determined by dividing the quoteBalance with I. An attacker can abuse the mulFloor() to create a major inconsistency. Suppose quoteBalance = 1, baseBalance = 19999, I = 1e14. Then we have: 1 < 19999 * 1e14 / 1e18 => 1 < 1 => False Therefore shares = 19999 . It sets the targets:

_BASE_TARGET_ = 19999
_QUOTE_TARGET_ = 19999 * 1e14 / 1e18 = 1

The result is the ratio 1:19999, when the intended ratio from I is 1:1000.

Essentially a small rounding error is magnified. The rounding direction should instead be: quoteBalance < DecimalMath.mulCeil(baseBalance, _I_) This would ensure that DecimalMath.divFloor(quoteBalance, _I_) is executed. At this point, when calculating QUOTE_TARGET there will not be a precision error as above (it performs the opposing actions to the divFloor).

An attacker can abuse it by making users perform trades under the assumption I is the effective ratio, however the ratio is actually 2I. The pool’s pricing mechanics will be wrong. Note that users will legitimately trust any MagicLP pool created by the Factory as it is supposed to enforce that ratio.

The attack can be performed on another entity’s pool right after the init() call, or on a self-created pool. The initial cost for the attack is very small due to the small numbers involved.

Impact

Attacker can initialize a Pool with malicious pricing mechanics that break the assumed invariants of the pool, leading to incorrect pool interactions.

POC

Step by step of the initial buyShares() was provided above. _QUOTE_TARGET_ is used by the pricer:

function getPMMState() public view returns (PMMPricing.PMMState memory state) {
    state.i = _I_;
    state.K = _K_;
    state.B = _BASE_RESERVE_;
    state.Q = _QUOTE_RESERVE_;
    state.B0 = _BASE_TARGET_; // will be calculated in adjustedTarget
    state.Q0 = _QUOTE_TARGET_;
    state.R = PMMPricing.RState(_RState_);
    PMMPricing.adjustedTarget(state);
}
...
function sellBaseToken(PMMState memory state, uint256 payBaseAmount) internal pure returns (uint256 receiveQuoteAmount, RState newR) {
    if (state.R == RState.ONE) {
        // case 1: R=1
        // R falls below one
        receiveQuoteAmount = _ROneSellBaseToken(state, payBaseAmount);
        newR = RState.BELOW_ONE;
    } else if (state.R == RState.ABOVE_ONE) {
        uint256 backToOnePayBase = state.B0 - state.B;
        uint256 backToOneReceiveQuote = state.Q -
		// @@@@@@@@@@   USED HERE
		state.Q0;

Note that deposits/withdrawals will continue to apply the bad ratio:

} else if (baseReserve > 0 && quoteReserve > 0) {
    // case 2. normal case
    uint256 baseInputRatio = DecimalMath.divFloor(baseInput, baseReserve);
    uint256 quoteInputRatio = DecimalMath.divFloor(quoteInput, quoteReserve);
    uint256 mintRatio = quoteInputRatio < baseInputRatio ? quoteInputRatio : baseInputRatio;
    shares = DecimalMath.mulFloor(totalSupply(), mintRatio);
    _BASE_TARGET_ = (uint256(_BASE_TARGET_) + DecimalMath.mulFloor(uint256(_BASE_TARGET_), mintRatio)).toUint112();
    _QUOTE_TARGET_ = (uint256(_QUOTE_TARGET_) + DecimalMath.mulFloor(uint256(_QUOTE_TARGET_), mintRatio)).toUint112();

Recommended Mitigation Steps

Use DecimalMaths.mulCeil() to protect against the rounding error.

0xCalibur (Abracadabra) acknowledged, but disagreed with severity and commented:

Based on our previous audit, it was discussed that using mulCeil here would not be the right answer since that would just create imprecision in the ratio in the other direction.

It would be good if the submitter could provide a PoC showing a veritable exploit case for this one.

Acknowledged, but will not fix it on contract level but filtering pool quality and legitimacy on our main frontend.

trust1995 commented:

Hi,

The impact demonstrated is doubling the ratio of the pool, which is a core invariant of the MIMswap platform. It means pricing will be incorrect, which is the core functionality of an AMM. A user who will make a trade assuming they will follow the price set out by the I parameter will make incorrect trades, losing their funds inappropriately. I believe the direct risk of loss of funds by innocent traders who are not making a mistake, warrants the severity of High.

cccz (Judge) commented:

  1. The attacker can compromise QUOTETARGET_ in buyShares() after the pool is created.

    function createPool(
        address baseToken,
        address quoteToken,
        uint256 lpFeeRate,
        uint256 i,
        uint256 k,
        address to,
        uint256 baseInAmount,
        uint256 quoteInAmount
    ) external returns (address clone, uint256 shares) {
        _validateDecimals(IERC20Metadata(baseToken).decimals(), IERC20Metadata(quoteToken).decimals());
    
        clone = IFactory(factory).create(baseToken, quoteToken, lpFeeRate, i, k);
    
        baseToken.safeTransferFrom(msg.sender, clone, baseInAmount);
        quoteToken.safeTransferFrom(msg.sender, clone, quoteInAmount);
        (shares, , ) = IMagicLP(clone).buyShares(to);  <========
    }
    
  2. The victim calls sellBase, and the call chain is as follows. In adjustedTarget, state.Q0 will not be adjusted since state.R == RState.ONE.

    function sellBase(address to) external nonReentrant returns (uint256 receiveQuoteAmount) {
        uint256 baseBalance = _BASE_TOKEN_.balanceOf(address(this));
        uint256 baseInput = baseBalance - uint256(_BASE_RESERVE_);
        uint256 mtFee;
        uint256 newBaseTarget;
        PMMPricing.RState newRState;
        (receiveQuoteAmount, mtFee, newRState, newBaseTarget) = querySellBase(tx.origin, baseInput); <==============
    ...
    function querySellBase(
        address trader,
        uint256 payBaseAmount
    ) public view returns (uint256 receiveQuoteAmount, uint256 mtFee, PMMPricing.RState newRState, uint256 newBaseTarget) {
        PMMPricing.PMMState memory state = getPMMState(); <========
        (receiveQuoteAmount, newRState) = PMMPricing.sellBaseToken(state, payBaseAmount); <======
    ...
    function getPMMState() public view returns (PMMPricing.PMMState memory state) {
        state.i = _I_;
        state.K = _K_;
        state.B = _BASE_RESERVE_;
        state.Q = _QUOTE_RESERVE_;
        state.B0 = _BASE_TARGET_; // will be calculated in adjustedTarget
        state.Q0 = _QUOTE_TARGET_;
        state.R = PMMPricing.RState(_RState_);
        PMMPricing.adjustedTarget(state); <======
    }
    ...
    function adjustedTarget(PMMState memory state) internal pure {
        if (state.R == RState.BELOW_ONE) {
            state.Q0 = Math._SolveQuadraticFunctionForTarget(state.Q, state.B - state.B0, state.i, state.K);
        } else if (state.R == RState.ABOVE_ONE) {
            state.B0 = Math._SolveQuadraticFunctionForTarget(
                state.B,
                state.Q - state.Q0,
                DecimalMath.reciprocalFloor(state.i),
                state.K
            );
        }
    }
    
  3. sellBaseToken calls ROneSellBaseToken, and _ROneSellBaseToken calls _SolveQuadraticFunctionForTrade to use compromised _QUOTETARGET_ for calculation. It’ll compromise the victim.

    function sellBaseToken(PMMState memory state, uint256 payBaseAmount) internal pure returns (uint256 receiveQuoteAmount, RState newR) {
        if (state.R == RState.ONE) {
            // case 1: R=1
            // R falls below one
            receiveQuoteAmount = _ROneSellBaseToken(state, payBaseAmount); <======
            newR = RState.BELOW_ONE;
    ...
    function _ROneSellBaseToken(
        PMMState memory state,
        uint256 payBaseAmount
    )
        internal
        pure
        returns (
            uint256 // receiveQuoteToken
        )
    {
        // in theory Q2 <= targetQuoteTokenAmount
        // however when amount is close to 0, precision problems may cause Q2 > targetQuoteTokenAmount
        return Math._SolveQuadraticFunctionForTrade(state.Q0, state.Q0, payBaseAmount, state.i, state.K); <======
    }
    

Therefore, High Severity is warranted.

Note: For full discussion, see here.


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 use of floor rounding in the comparison that decides the initial share amount, which can magnify a small rounding error and set the target ratio incorrectly.

Impact

An attacker could cause incorrect pricing in a MagicLP pool, leading to financial losses for traders who trust the pool's advertised ratio.

What the attacker needed

Attacker can create a pool or access a newly initialised one; Victim trades on the pool

The fix

The rounding direction should be changed to use mulCeil instead of mulFloor in the comparison, ensuring that the division path is taken and preventing the precision error.

How it was found

  1. Attacker creates a pool or targets a newly initialised pool.
  2. Attacker calls buyShares with carefully chosen base and quote amounts to trigger the rounding error.
  3. The pool's _QUOTE_TARGET_ is set to a value that makes the effective ratio twice the intended I.
  4. Victim trades on the pool assuming the correct ratio, receiving incorrect pricing and losing funds.

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.

A MagicLP pool is a type of automated market maker (AMM) that uses a parameter called I to set the ideal ratio between two token reserves. Due to a rounding mistake when the pool is first funded, an attacker can make the pool's actual ratio twice as large as intended. This means the pool will trade at the wrong prices, and an attacker can exploit that to take money from other users.

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.