recoverEther not updating currentWithheldETH breaks calculation of withheld amount for further deposits
The recoverEther function does not update currentWithheldETH, so after an emergency recovery the withheld amount is offset and no longer matches the withholdRatio. This can cause depositEther to deposit less ETH than expected and moveWithheldETH to move more ETH than intended.
Submitted by joestakey, also found by Chom
The emergency exit function recoverEther allows the owner to retrieve the ETH in case an issue were to happen.
The problem is that this function does not update currentWithheldETH.
This means upon deposit starting again after the emergency recovery, currentWithheldETH will have an offset and will not match the withholdRatio.
Direct consequences:
depositEthermay not deposit the expected amount of ETH into the ETH 2.0 staking contract.- The amount of ETH moved to an external yield protocol using
moveWithheldETH()will be higher than what it should be.
Proof Of Concept
- frxETHMinter has a
withholdRatioset to2 * 1e5- ie the contract is meant to hold20%of the ETH deposited. - Users deposit ETH into the contract.
- An issue happens and the owner calls
recoverEther(address(this).balance). Before the call, the total balance was1e20(100 ETH), andcurrentWithheldETH == 2 * 1e19- for simplicity we assume no calls tomoveWithheldETHordepositEtherhave been done yet. - The ETH balance of the minter is now
0, butcurrentWithheldETHis still2 * 1e19 - Users start depositing again.
- At this point, the total balance is now
1e20(100 ETH), andcurrentWithheldETH == 4 * e19 - The owner calling
depositEtherdeposits32 etherinstead of64 ether, becausecurrentWithheldETH == 40 etherinstead of20 ether. The owner can also callmoveWithheldETHwithamount == 4 * 1e19instead ofamount == 2 * 1e19.
You can add the following Foundry test in frxETHMinter.t.sol to reproduce the issue:
- First declare
address Alice = address(1);before thesetUp()
function testIssueRecoverEther() public {
vm.startPrank(FRAX_COMPTROLLER);
// Note the starting ETH balance of the comptroller
uint256 starting_eth = FRAX_COMPTROLLER.balance;
// Give Alice 200 eth
vm.deal(Alice, 200 ether);
// Set the withhold ratio to 20% (2 * 1e5)
minter.setWithholdRatio(200000);
vm.stopPrank();
vm.startPrank(Alice);
//deposit 100 ETH
minter.submit{ value: 100 ether }();
vm.stopPrank();
vm.startPrank(FRAX_COMPTROLLER);
// Recover all
minter.recoverEther(100 ether);
// Make sure the FRAX_COMPTROLLER got 100 ether back
assertEq(FRAX_COMPTROLLER.balance, starting_eth + (100 ether));
//check `currentWithheldETH`: it has not been reset and is still 20 ETH
assertEq(minter.currentWithheldETH(), 20 ether);
vm.stopPrank();
vm.startPrank(Alice);
//deposit 100 ETH
minter.submit{ value: 100 ether }();
//check `currentWithheldETH`: because of the offset, it is now 40 ETH, ie 40% of the total ETH in the minter
assertEq(minter.currentWithheldETH(), 40 ether);
assertEq(address(minter).balance, 100 ether);
vm.stopPrank();
vm.startPrank(FRAX_COMPTROLLER);
//Owner can call moveWithheldETH, transferring more than 40% of the balance, while the withheld amount should be 20%
minter.moveWithheldETH(payable(address(Alice)), 40 ether);
assertEq(address(minter).balance, 60 ether);
vm.stopPrank();
}
Tools Used
Foundry
Recommended Mitigation Steps
Update currentWithheldETH in recoverEther :
+ currentWithheldETH = currentWithheldETH >= amount ? currentWithheldETH - amount : 0 ;
192: (bool success,) = address(owner).call{ value: amount }("");
193: require(success, "Invalid transfer");
194:
195: emit EmergencyEtherRecovered(amount);
FortisFortuna (Frax) commented:
@denett withholdRatio is is not an iron rule and can be updated by the owner at will. recoverEther will likely only be used when we are migrating to a new minting contract, so the accounting in that case is no longer important.
Issue 346 has some great suggestions in it on ensuring user safety in an emergency scenario and think that both of these issues do highlight a valid concern that ultimately could affect the protocol in an emergency scenario.
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
Impact
After an emergency recovery, depositEther may deposit less ETH than expected and moveWithheldETH may move more ETH than intended, potentially affecting protocol operations.
What the attacker needed
Owner calls recoverEther; withholdRatio set; users deposit after recovery
The fix
Update currentWithheldETH in recoverEther to subtract the withdrawn amount, ensuring it does not go below zero.
How it was found
- Set withholdRatio to 20% and deposit 100 ETH.
- Call recoverEther to withdraw all ETH.
- Observe currentWithheldETH remains 20 ETH.
- Deposit another 100 ETH.
- Observe currentWithheldETH becomes 40 ETH instead of 20 ETH.
- Call moveWithheldETH with 40 ETH, moving more than intended.
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 smart contract holds some ETH aside as a safety buffer, tracked by a variable. When the owner uses an emergency function to take all ETH out, that variable is not reset, so later deposits think more ETH is already set aside than actually is. This can make the contract send too little to staking or move too much out.
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.