Skip to content
Smart contracts Audit finding Intermediate

absolute difference is not calculated properly when a > b in MathUtils

The difference function in MathUtils.sol incorrectly computes the absolute difference when a > b, causing within1 to return wrong results. An attacker could manipulate pool calculations to make within1 fail, potentially affecting the HybridPool.

hack3r-0m Follow

Originally disclosed on Code4rena

Submitted by hack3r-0m, also found by broccoli

the difference is computed incorrectly when a > b. MathUtils.sol L22

As it only used in within1 function, scope narrows down to where difference(a, b) <= 1; is exploitable.

cases where difference(a, b) <= 1 should be true but is reported false:

  • where b = a-1 (returned value is type(uint256).max)

cases where difference(a, b) <= 1 should be false but is reported true:

  • where a = type(uint256).max and b = 0, it returns 1 but it should ideally return type(uint256).max

within1 is used at the following locations:

It is possible to decrease the denominator and increase the value of the numerator (when calculating y) using constants and input to make within1 fail

Mitigation:

Add else condition to mitigate it.

unchecked {
    if (a > b) {
        diff = a - b;
    }
    else {
        diff = b - a;
    }
}

maxsam4 (Sushi) confirmed

The original report as published on Code4rena, by hack3r-0m. 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

Impact

An attacker could cause within1 to fail by manipulating pool inputs, potentially disrupting pool operations or enabling value extraction.

What the attacker needed

Ability to interact with HybridPool

The fix

The fix is to add an else branch that computes b - a when a is not greater than b.

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 smart contract function that calculates the difference between two numbers has a bug: when the first number is larger, it returns a huge number instead of the correct difference. This can make a check that should pass fail, which might let someone manipulate the pool's calculations.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by hack3r-0m

View all

3 more disclosed reports, best first.

More from Sushi Trident contest phase 1

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.