# `TradingLimits::update()` incorrectly only rounds up when `deltaFlowUnits` becomes 0, which will silently increase trading limits

TradingLimits.update() truncates deltaFlow to whole units, so sub-unit trades round down and let swaps exceed the configured trading limit by up to 2x.

- Payout: Audit contest
- Severity: medium
- Program: Mento Update
- Found by: 0x73696d616f
- Disclosed: 2024-10-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-mento-update-judging/issues/45
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-mento-update%3AM-4
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** TradingLimits library / Broker swap flow (Solidity)
**Root cause:** update() divides _deltaFlow by 10**decimals with integer truncation and only rounds up when the result is exactly 0, so any fractional remainder (e.g. 1.999e18 -> 1) is silently dropped from the tracked flow.
**Steps:**
1. Call `Broker::swapIn/Out()` with amounts whose _deltaFlow is not an exact multiple of 10**decimals (e.g. 1.999e18 on an 18-decimal token).
2. Observe `_deltaFlowUnits = _deltaFlow / 10**decimals` truncates to 1 instead of rounding to 2.
3. Repeat swaps of ~2 units each; state increments by 1 per swap while ~2 units of flow occur.
4. Accumulate up to ~2x the configured trading limit before the limit check trips.
**How found:** Code review of the rounding logic in TradingLimits.update(); noticed the round-up guard only triggers on a zero result, then reasoned that a result of 1 from 1.999 carries the same magnitude of error.
**Exploitation:** Attacker repeatedly swaps amounts just under 2 whole units so each update records only 1 unit of flow, allowing cumulative trading volume roughly double the intended limit before the limit is enforced.
**Insight:** When auditing fixed-point accounting, check that rounding direction is applied consistently for all fractional remainders, not just the zero case; a guard `if (x==0) x=1` still truncates 1.999->1 and can be gamed by sizing trades just under the next integer unit.

## Proof of concept (copied exactly from the original report)

**TradingLimits::update() rounding code**

```
int256 _deltaFlowUnits = _deltaFlow / int256((10 ** uint256(decimals)));`. In a token with 18 decimals, for example, swapping 1.999...e18 tokens will lead to a `_deltaFlowUnits` of just `1`, 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

1. 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);
  }
```

**Proposed rounding fix**

```
int256 _deltaFlowUnits = (_deltaFlow - 1) / int256((10 ** uint256(decimals))) + 1;
```

