Underflown variable in borrowGivenDebtETHCollateral function
The borrowGivenDebtETHCollateral function initialises a uint256 excess variable to zero and then subtracts dueOut.collateral from it inside an unchecked block, causing an underflow. This means the function never correctly transfers excess ETH to the user and always reverts when maxCollateral exceeds dueOut.collateral.
Originally disclosed on Code4rena
Submitted by TerrierLover
borrowGivenDebtETHCollateral function does never properly call ETH.transfer due to underflow. If borrowGivenDebtETHCollateral function is not deprecated, it would cause unexpected behaviors for users.
Proof of Concept
Here are codes which contain a potential issue.
if (maxCollateral > dueOut.collateral) {
uint256 excess;
unchecked {
excess -= dueOut.collateral;
}
ETH.transfer(payable(msg.sender), excess);
}
excess variable is uint256, and dueOut.collateral variable is uint112 as shown below. Hence, both variables will never be less than 0.
struct Due {
uint112 debt;
uint112 collateral;
uint32 startBlock;
}
uint256 excess is initialized to 0. However, subtracting dueOut.collateral variable which is more than or equal to 0 from excess variable which is 0 will be less than 0. Hence, excess -= dueOut.collateral will be less than 0, and excess will be underflown.
Recommended Mitigation Steps
The code should properly initialize excess variable.
borrowGivenPercentETHCollateral function uses uint256 excess = maxCollateral at similar functionality.
Borrow.sol#L347
Hence, just initializing excess variable with maxCollateral can be a potential workaround to prevent the underflown.
if (maxCollateral > dueOut.collateral) {
uint256 excess = maxCollateral;
unchecked {
excess -= dueOut.collateral;
}
ETH.transfer(payable(msg.sender), excess);
}
amateur-dev (Timeswap) confirmed
Mathepreneur (Timeswap) resolved and commented:
0xleastwood (judge) commented:
Awesome find! I believe
mediumseverity is the appropriate risk as this function will always revert whenmaxCollateral > dueOut.collateral.
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 excess variable was initialised to zero instead of maxCollateral before subtracting dueOut.collateral inside an unchecked block.
Impact
The function always reverts when maxCollateral > dueOut.collateral, so users cannot receive their excess ETH; the judge rated this medium severity.
What the attacker needed
Calling borrowGivenDebtETHCollateral; maxCollateral > dueOut.collateral
The fix
The code was fixed by initialising excess with maxCollateral before the unchecked subtraction.
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 variable meant to hold the extra collateral was set to zero instead of the total collateral. When the code subtracted the actual collateral from zero, the number wrapped around to a huge value because the check for underflow was turned off. As a result, the function could not send the correct amount of ETH back to the user and simply failed.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.