Skip to content
DoS Audit finding Intermediate

totalUnderlyingMinusSponsored() may revert on underflow and malfunction the contract

The function totalUnderlyingMinusSponsored() subtracts totalSponsored from totalUnderlying without checking for underflow. If totalSponsored exceeds totalUnderlying, the subtraction reverts, causing any function that calls it, such as deposit(), to fail.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug

https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/Vault.sol#L290-L293

function totalUnderlyingMinusSponsored() public view returns (uint256) {
    // TODO no invested amount yet
    return totalUnderlying() - totalSponsored;
}

As a function that many other functions depended on, totalUnderlyingMinusSponsored() can revert on underflow when sponsorAmount > totalUnderlying() which is possible and has been considered elsewhere in this contract:

https://github.com/code-423n4/2022-01-sandclock/blob/a90ad3824955327597be00bb0bd183a9c228a4fb/sandclock/contracts/Vault.sol#L390-L392

if (_force && sponsorAmount > totalUnderlying()) {
    sponsorToTransfer = totalUnderlying();
}

Proof of Concept

  • Underlying token = USDT
  • Swap Fee = 0.04%
  • Sponsor call sponsor() and send 10,000 USDT
  • totalSponsored = 10,000
  • NonUSTStrategy.sol#doHardWork() swapped USDT for UST
  • pendingDeposits = 9,996
  • totalUnderlying() = 9,996
  • Alice tries to call deposit(), the tx will revet due to underflow in totalUnderlyingMinusSponsored().

Recommendation

Change to:

function totalUnderlyingMinusSponsored() public view returns (uint256) {
    uint256 _totalUnderlying = totalUnderlying();
    if (totalSponsored > _totalUnderlying) {
        return 0;
    }
    return _totalUnderlying - totalSponsored;
}

naps62 (Sandclock) confirmed

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

The function performs subtraction without checking if the result would be negative, leading to an underflow revert.

Impact

The contract may malfunction because deposit() and other functions that rely on totalUnderlyingMinusSponsored() will revert, preventing users from depositing.

What the attacker needed

SponsorAmount > totalUnderlying()

How it was found

  1. Sponsor calls sponsor() and sends 10,000 USDT.
  2. totalSponsored becomes 10,000.
  3. NonUSTStrategy.sol#doHardWork() swaps USDT for UST.
  4. pendingDeposits becomes 9,996 and totalUnderlying() becomes 9,996.
  5. Alice tries to call deposit(), but the transaction reverts due to underflow in totalUnderlyingMinusSponsored().

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 calculates a value by subtracting one number from another. If the number being subtracted is larger, the calculation fails and stops the whole transaction. This can happen when a sponsor deposits funds and then the total underlying amount drops below the sponsored amount, blocking other users from depositing.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Sandclock contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.