Summary
In the withdrawInterest function, if totalInterest is less than amount, subtracting amount from totalInterest will cause an underflow and revert the transaction due to Solidity 0.8.x's checked arithmetic, even if totalInterestFromLiquidation has sufficient balance to cover the withdrawal.
Root Cause
The function checks the combined balance but only subtracts from totalInterest:
require(amount <= (totalInterest + totalInterestFromLiquidation), "Treasury don't have enough interest");
totalInterest -= amount;If totalInterest < amount, this subtraction underflows and reverts.
Internal pre-conditions
none
External pre-conditions
none
Impact
DOS, underflow and revert.
PoC
- Set
totalInterestto 50 andtotalInterestFromLiquidationto 100. - Attempt to withdraw an
amountof 75. - The
requirepasses because 75 ≤ 150. - Subtracting 75 from
totalInterest(50) causes an underflow and reverts.
Mitigation
Implement logic to deduct the amount from totalInterest and totalInterestFromLiquidation proportionally or sequentially:
if (amount <= totalInterest) {
totalInterest -= amount;
} else {
uint256 remaining = amount - totalInterest;
totalInterest = 0;
totalInterestFromLiquidation -= remaining;
}