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.
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():
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:
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:
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.
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.
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); <======
}
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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
Attacker creates a pool or targets a newly initialised pool.
Attacker calls buyShares with carefully chosen base and quote amounts to trigger the rounding error.
The pool's _QUOTE_TARGET_ is set to a value that makes the effective ratio twice the intended I.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
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.