Skip to content
Business logic Audit finding Intermediate

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.

joestakey Follow

Originally disclosed on Code4rena

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:

  • depositEther may 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 withholdRatio set to 2 * 1e5 - ie the contract is meant to hold 20% 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 was 1e20 (100 ETH), and currentWithheldETH == 2 * 1e19 - for simplicity we assume no calls to moveWithheldETH or depositEther have been done yet.
  • The ETH balance of the minter is now 0, but currentWithheldETH is still 2 * 1e19
  • Users start depositing again.
  • At this point, the total balance is now 1e20 (100 ETH), and currentWithheldETH == 4 * e19
  • The owner calling depositEther deposits 32 ether instead of 64 ether, because currentWithheldETH == 40 ether instead of 20 ether. The owner can also call moveWithheldETH with amount == 4 * 1e19 instead of amount == 2 * 1e19.

You can add the following Foundry test in frxETHMinter.t.sol to reproduce the issue:

  • First declare address Alice = address(1); before the setUp()
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.

0xean (judge) commented:

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.


The original report as published on Code4rena, by joestakey. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Set withholdRatio to 20% and deposit 100 ETH.
  2. Call recoverEther to withdraw all ETH.
  3. Observe currentWithheldETH remains 20 ETH.
  4. Deposit another 100 ETH.
  5. Observe currentWithheldETH becomes 40 ETH instead of 20 ETH.
  6. 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.

More by joestakey

View all

10 more disclosed reports, best first.

More from Frax Ether Liquid Staking contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.