Overflow potential in processEpoch()
The processEpoch function in PublicVault.sol uses an unchecked block to calculate withdrawReserve, assuming no overflow. Because arbitrary ERC20 tokens are allowed, a token with a very large totalAssets could cause an overflow, leading to incorrect reserve values.
Submitted by obront
In PublicVault.sol#processEpoch(), we update the withdraw reserves based on how totalAssets() (the real amount of the underlying asset held by the vault) compares to the expected value in the withdraw proxy:
unchecked {
if (totalAssets() > expected) {
s.withdrawReserve = (totalAssets() - expected)
.mulWadDown(s.liquidationWithdrawRatio)
.safeCastTo88();
} else {
s.withdrawReserve = 0;
}
}
In the event that the totalAssets() is greater than expected, we take the surplus in assets multiply it by the withdraw ratio, and assign this value to s.withdrawReserve.
However, because this logic is wrapped in an unchecked block, it must have confidence that this calculation does not overflow. Because the protocol allows arbitrary ERC20s to be used, it can’t have confidence in the size of totalAssets(), which opens up the possibility for an overflow in this function.
Proof of Concept
mulWadDown is calculated by first multiplying the two values, and then dividing by 1e18. This is intended to prevent rounding errors with the division, but also means that an overflow is possible when the two values have been multiplied, before any division has taken place.
This unchecked block is safe from overflows if:
(totalAssets() - expected) * s.liquidationWithdrawRatio < 1e88(because s.withdrawRatio is 88 bytes)liquidationWithdrawRatiois represented as a ratio of WAD, so it will be in the1e17-1e18range, let’s assume1e17to be safe- therefore we require
totalAssets() - expected < 1e61
Although this is unlikely with most tokens, and certainly would have been safe in the previous iteration of the protocol that only used WETH, when allowing arbitrary ERC20 tokens, this is a risk.
Recommended Mitigation Steps
Remove the unchecked block around this calculation, or add an explicitly clause to handle the situation where totalAssets() gets too large for the current logic.
SantiagoGregory (Astaria) confirmed
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 use of an unchecked arithmetic block for a calculation involving totalAssets(), which can be arbitrarily large when any ERC20 token is supported.
Impact
An overflow could occur if totalAssets() - expected exceeds about 1e61, leading to an incorrect withdrawReserve value; the report notes this is unlikely with most tokens.
What the attacker needed
Arbitrary ERC20 tokens allowed; token with extremely large total supply
The fix
Remove the unchecked block around the calculation or add an explicit check to handle very large totalAssets().
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.
Smart contracts often use a block called 'unchecked' to save gas by skipping overflow checks. If a number gets too big, it can wrap around like a car odometer, giving a wrong result. Here, a token with an extremely large supply could make the vault's asset total so big that the calculation overflows, messing up the withdraw reserves.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.