`TradingLimits::update()` incorrectly only rounds up when `deltaFlowUnits` becomes 0, which will silently increase trading limits
The TradingLimits::update() function only rounds up when the delta in flow units becomes zero, but truncates when it becomes one from a value like 1.999, allowing the trading limit to be bypassed by up to double through repeated small swaps.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-10-mento-update-judging/issues/45
The protocol has acknowledged this issue.
Found by
0x73696d616f
Summary
TradingLimits::update() divides the traded funds by the decimals of the token, int256 _deltaFlowUnits = _deltaFlow / int256((10 ** uint256(decimals)));. In a token with 18 decimals, for example, swapping 1.999...e18 tokens will lead to a _deltaFlowUnitsof just1`, taking a major error. This can be exploited to swap up to twice the trading limit, if tokens are swapped 2 by 2 and the state is updated only by 1 each time. Overall, even without malicious intent, the limits will always be bypassed due to the rounding.
Root Cause
In TradingLimits:135, it only rounds up whenever deltaFlowUnits becomes 0, but the error is just as big if it becomes 1 from 2, effectively not providing enough protection.
Internal pre-conditions
None.
External pre-conditions
None.
Attack Path
- User calls
Broker::swapIn/Out()with amounts in and out that produce rounding errors (almost always).
Impact
The trading limits may be severely bypassed with malicious intent (by double the amount) or by a smaller but still significant amount organically.
PoC
TradingLimits::update() only rounds up when deltaFlowUnits becomes 0.
function update(
ITradingLimits.State memory self,
ITradingLimits.Config memory config,
int256 _deltaFlow,
uint8 decimals
) internal view returns (ITradingLimits.State memory) {
int256 _deltaFlowUnits = _deltaFlow / int256((10 ** uint256(decimals)));
require(_deltaFlowUnits <= MAX_INT48, "dFlow too large");
int48 deltaFlowUnits = int48(_deltaFlowUnits);
if (deltaFlowUnits == 0) {
deltaFlowUnits = _deltaFlow > 0 ? int48(1) : int48(-1);
}
...
Mitigation
The correct fix is:
int256 _deltaFlowUnits = (_deltaFlow - 1) / int256((10 ** uint256(decimals))) + 1;
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
Impact
The trading limits may be severely bypassed with malicious intent (by double the amount) or by a smaller but still significant amount organically.
What the attacker needed
None
The fix
The correct fix is to round up the division result by adding one after subtracting one from the numerator before dividing.
How it was found
- User calls Broker::swapIn/Out() with amounts that produce rounding errors.
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.
Smart contracts often store token amounts as whole numbers, but when you divide, the remainder is thrown away. This code only corrected for that when the result was zero, so many small trades could each be undercounted, letting someone trade more than the limit allows.
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.