Withdrawing ETH collateral with max uint256 amount value reverts transaction
Withdrawing ETH collateral using the maximum uint256 amount causes the transaction to revert because the code tries to call decimals() on the zero address, which is not a valid token. An attacker could not exploit this for gain, but it prevents legitimate withdrawals of the full ETH balance.
Originally disclosed on Code4rena
Submitted by berndartmueller, also found by WatchPug
Withdrawing ETH collateral via the withdrawCollateral function using type(uint256).max for the _amount parameter reverts the transaction due to _asset being the zero-address and IERC20Detailed(_asset).decimals() not working for native ETH.
Proof of Concept
if (_amount == type(uint256).max) {
uint256 decimal = IERC20Detailed(_asset).decimals(); // @audit-info does not work for native ETH. Transaction reverts
_amount = _amountToWithdraw.mul(this.pricePerShare()).div(10**decimal);
}
Recommended mitigation steps
Check _asset and use hard coded decimal value (18) for native ETH.
sforman2000 (Sturdy) confirmed
Fix the issue of transaction fails due to calculate ETH’s decimals sturdyfi/code4rena-may-2022#7
Good find! Stated in
_assetdescription that null address is interpreted as ETH, which isn’t a token, and therefore reverts when attempting to fetch its decimals.
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 code assumes the asset is an ERC20 token and calls decimals() on it, but for native ETH the asset address is zero, which has no such function.
Impact
Users cannot withdraw their full ETH collateral in a single transaction using the maximum amount value; the transaction reverts.
What the attacker needed
User has ETH collateral deposited; User attempts to withdraw full amount
The fix
The code should check if the asset is native ETH and use a hardcoded decimal value of 18 instead of calling decimals().
How it was found
- Call withdrawCollateral with _amount set to type(uint256).max for native ETH.
- The function checks if _amount equals max and then calls decimals() on the zero address.
- The call reverts because the zero address does not implement decimals().
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.
When you withdraw all your ETH from a smart contract, the code tries to look up how many decimal places ETH has, but ETH is not a token contract, so the lookup fails and the whole withdrawal is cancelled. This means you cannot withdraw your full ETH balance in one go.
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.