Skip to content
Smart contracts Audit finding Intermediate

implicit underflows

The smart contract code performs arithmetic on unsigned integers that can underflow, and then casts the result to a signed integer, producing an unexpected large positive value instead of a negative one. An attacker could potentially trigger this underflow in the safetyCheck function, which runs on every deposit, to bypass a safety check.

gpersoon Follow

Originally disclosed on Code4rena

Submitted by gpersoon, also found by cmichel

There are a few underflows that are converted via a typecast afterwards to the expected value. If solidity 0.8.x would be used, then the code would revert.

  • int256(a-b) where a and b are uint: For example, if a=1 and b=2, then the intermediate result would be uint(-1) == 2**256-1
  • int256(-x) where x is a uint. For example, if x=1, then the intermediate result would be uint(-1) == 2**256-1

It’s better not to have underflows by using the appropriate typecasts. This is especially relevant when moving to solidity 0.8.x.

From Exposure.sol L178:

function sortVaultsByDelta(..)
..
    for (uint256 i = 0; i < N_COINS; i++) {
        // Get difference between vault current assets and vault target
        int256 delta = int256(unifiedAssets[i] - unifiedTotalAssets.mul(targetPercents[i]).div(PERCENTAGE_DECIMAL_FACTOR)); // underflow in intermediate result

From PnL.sol L112:

 function decreaseGTokenLastAmount(bool pwrd, uint256 dollarAmount, uint256 bonus)...
..
 emit LogNewGtokenChange(pwrd, int256(-dollarAmount)); // underflow in intermediate result

From Buoy3Pool.sol L87:

function safetyCheck() external view override returns (bool) {
    ...
        _ratio = abs(int256(_ratio - lastRatio[i])); // underflow in intermediate result

Recommend replacing int256(a-b) with int256(a)-int256(b), and replacing int256(-x) with -int256(x)

kristian-gro (Gro) confirmed but disagreed with severity:

Confirmed and We’ve mitigated this issue in our release version.

ghoul-sol (Judge) commented:

Majority of overflow listed above seems low risk with one exception of safetyCheck. Underflow is a real risk here.safetyCheck is run every time a deposit is made. Ratios can change and the change does not need to be substantial for it to overflow. For that reason it’s a high risk.

The original report as published on Code4rena, by gpersoon. 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 code subtracts unsigned integers before casting to a signed integer, so an underflow wraps to a large positive value instead of producing a negative difference.

Impact

The underflow in safetyCheck could allow a deposit to bypass the safety check, which the judge rated as high risk, while other instances were considered low risk.

What the attacker needed

Deposit into the contract

The fix

The issue was mitigated in the release version, and the recommendation is to replace int256(a-b) with int256(a)-int256(b) and int256(-x) with -int256(x).

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.

In some programming languages, numbers have a fixed size. If you subtract a larger number from a smaller one, the result wraps around to a very large number instead of going negative. Here, the code does this accidentally and then converts the wrapped number to a signed type, so it looks like a huge positive value. This can fool a safety check that expects a small difference.

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 gpersoon

View all

47 more disclosed reports, best first.

More from Gro Protocol

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.