Residual ETH unreachable and unutilized in SafEth.sol
SafEth.sol does not account for residual ETH that can accumulate from accidental transfers, failed unstake outputs, or failed deposits. This ETH becomes stuck and cannot be used to benefit stakers.
RaymondFam Follow with rotcivegaf, d3e4 and 16 others
Originally disclosed on Code4rena
Submitted by RaymondFam, also found by rotcivegaf, d3e4, n33k, LeoGold, Phantasmagoria, eyexploit, neumo, yac, jasonxiale, juancito, adeolu, anodaram, mojito_auditor, aviggiano, 7siech, koxuan, chaduke, and SunSec
https://github.com/code-423n4/2023-03-asymmetry/blob/main/contracts/SafEth/SafEth.sol#L246 https://github.com/code-423n4/2023-03-asymmetry/blob/main/contracts/SafEth/SafEth.sol#L124-L127 https://github.com/code-423n4/2023-03-asymmetry/blob/main/contracts/SafEth/SafEth.sol#L138-L155
Unlike the other three contracts in scope, SafEth.sol does not have a measure in place to utilize the residual ETH, be it:
- accidentally received,
- zero ETH output from
unstake()arising from recipient non-contract existence, or - ETH sent in via
stake()fails to deposit into a derivative due to uninitialized derivatives and weights, as I have explained in a separate submission.
In the derivative contracts, the above issue is wiped clean via address(this).balance every time withdraw() is predominantly invoked in unstake() of SafEth.sol:
File: WstEth.sol#L63-L66 File: SfrxEth.sol#L84-L87 File: Reth.sol#L110-L114
(bool sent, ) = address(msg.sender).call{value: address(this).balance}(
""
);
require(sent, "Failed to send Ether");
Proof of Concept
As in SafETH.sol, ethAmountBefore and ethAmountAfter are used to determine ethAmountToWithdraw or ethAmountToRebalance respectively in untake() and rebalanceToWeights():
111: uint256 ethAmountBefore = address(this).balance;
121: uint256 ethAmountAfter = address(this).balance;
122: uint256 ethAmountToWithdraw = ethAmountAfter - ethAmountBefore;
139: uint256 ethAmountBefore = address(this).balance;
144: uint256 ethAmountAfter = address(this).balance;
145: uint256 ethAmountToRebalance = ethAmountAfter - ethAmountBefore;
Recommended Mitigation Steps
Consider having rebalanceToWeights() refactored as follows:
function rebalanceToWeights() external onlyOwner {
- uint256 ethAmountBefore = address(this).balance;
for (uint i = 0; i < derivativeCount; i++) {
if (derivatives[i].balance() > 0)
derivatives[i].withdraw(derivatives[i].balance());
}
- uint256 ethAmountAfter = address(this).balance;
- uint256 ethAmountToRebalance = ethAmountAfter - ethAmountBefore;
+ uint256 ethAmountToRebalance = address(this).balance;
for (uint i = 0; i < derivativeCount; i++) {
if (weights[i] == 0 || ethAmountToRebalance == 0) continue;
uint256 ethAmount = (ethAmountToRebalance * weights[i]) /
totalWeight;
// Price will change due to slippage
derivatives[i].deposit{value: ethAmount}();
}
emit Rebalanced();
}
This will at least have the residual ETH harnessed and distributed to all existing stakers whenever rebalanceToWeights() is called.
toshiSat (Asymmetry) disputed via duplicate issue #455
Use entire balance for rebalance.
Status: Partially mitigated. Full details in reports from d3e4 and adriro - and also shared below in the Mitigation Review section.
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 contract calculates the amount to rebalance or withdraw by measuring the balance change during the transaction, ignoring any pre-existing ETH balance.
Impact
Residual ETH remains stuck in the contract and cannot be distributed to stakers, reducing their potential rewards.
What the attacker needed
ETH sent to the contract outside normal functions; or a failed deposit during stake(); or a failed unstake() transfer
The fix
The contract should use its entire ETH balance when rebalancing, rather than only the amount received during the transaction.
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.
In a smart contract, ETH can get stuck if the contract does not have a way to spend it. Here, the contract only uses ETH that it receives during a transaction, so any extra ETH sent directly or left over from failed operations stays locked forever.
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.