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.
Submitted by WatchPug
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:
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 intotalUnderlyingMinusSponsored().
Recommendation
Change to:
function totalUnderlyingMinusSponsored() public view returns (uint256) {
uint256 _totalUnderlying = totalUnderlying();
if (totalSponsored > _totalUnderlying) {
return 0;
}
return _totalUnderlying - totalSponsored;
}
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
- Sponsor calls sponsor() and sends 10,000 USDT.
- totalSponsored becomes 10,000.
- NonUSTStrategy.sol#doHardWork() swaps USDT for UST.
- pendingDeposits becomes 9,996 and totalUnderlying() becomes 9,996.
- 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.