Skip to content
DoS Audit finding Intermediate

Potential stake() DoS if sole safETH holder (ie: first depositor) unstakes totalSupply - 1

The stake() function can revert due to a division by zero if the total supply of safETH is 1 wei and the underlying value is zero, which can be achieved by the sole holder unstaking all but 1 wei and manipulating the derivative price. This could prevent new stakes, effectively causing a denial of service.

whoismatthewmc1 Follow with m_Rassska

Originally disclosed on Code4rena

Submitted by whoismatthewmc1, also found by m_Rassska

https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L68-L81 https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L98

Impact

Potential inability to stake (ie: DoS) if sole safETH user (ie: this would also make them the sole safETH holder) unstakes totalSupply - 1.

Proof of Concept

The goal of this POC is to prove that this line can revert https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L98

        uint256 mintAmount = (totalStakeValueEth * 10 ** 18) / preDepositPrice;

This can occur if the attacker can cause preDepositPrice = 0.

A user who is the first staker will be the sole holder of 100% of totalSupply of safETH.

They can then unstake (and therefore burn) totalSupply - 1 leaving a total of 1 wei of safETH in circulation.

In earlier lines in stake() https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L77-L81, we see

        uint256 totalSupply = totalSupply();
        uint256 preDepositPrice; // Price of safETH in regards to ETH
        if (totalSupply == 0)
            preDepositPrice = 10 ** 18; // initializes with a price of 1
        else preDepositPrice = (10 ** 18 * underlyingValue) / totalSupply;

With totalSupply = 1, we see that the above code block will execute the else code path, and that if underlyingValue = 0, then preDepositPrice = 0.

underlyingValue is set in earlier lines: https://github.com/code-423n4/2023-03-asymmetry/blob/44b5cd94ebedc187a08884a7f685e950e987261c/contracts/SafEth/SafEth.sol#L68-L75

        uint256 underlyingValue = 0;

        // Getting underlying value in terms of ETH for each derivative
        for (uint i = 0; i < derivativeCount; i++)
            underlyingValue +=
                (derivatives[i].ethPerDerivative(derivatives[i].balance()) *
                    derivatives[i].balance()) /
                10 ** 18;

For a simple case, assume there is 1 derivative with 100% weight. Let’s use rETH for this example since the derivative can get its ethPerDerivative price from an AMM. In this case:

  • Assume the ethPerDerivative() value has been manipulated in the underlying AMM pool such that 1 derivative ETH is worth less than 1 ETH. eg: 1 rETH = 9.99…9e17 ETH
  • In this case, also assume that since there is 1 wei of safETH circulating, there should be 1 wei of ETH staked through the protocol, and therefore derivatives[i].balance() = 1 wei.

This case will result in underlyingValue += (9.99...9e17 * 1) / 10 ** 18 = 0.

We can see that it is therefore possible to cause a divide by 0 revert and malfunction of the stake() function.

Recommended Mitigation Steps

Assuming the deployment process will set up at least 1 derivative with a weight, simply adding a stake() operation of 0.5 ETH as the first depositor as part of the deployment process avoids the case where safETH totalSupply drops to 1 wei.

Otherwise, within unstake() it is also possible to require that totalSupply does not fall between 0 and minimumSupply where minimumSupply is, for example, the configured minAmount.

toshiSat (Asymmetry) confirmed, but disagreed with severity and commented:

Seems like a pretty big edge case and it would leave the contract with basically no funds which doesn’t seem like a High severity to me.

Picodes (judge) decreased severity to Medium and commented:

Indeed, the described scenario isn’t of high severity although the finding is valid. Basically, the first or last SafETH user could force the owner to redeploy, so downgrading to Medium.

Asymmetry commented:

Out of scope for mitigation review. We will be manually holding safETH to prevent this, if not redeploy.


The original report as published on Code4rena, by whoismatthewmc1. 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

Root cause

Likely the stake() function calculates preDepositPrice as (10**18 * underlyingValue) / totalSupply without checking for zero totalSupply or zero underlyingValue, leading to a division by zero when totalSupply is 1 and underlyingValue is 0.

Impact

The stake() function could revert, preventing new stakes and potentially forcing the owner to redeploy the contract, though the contract would have almost no funds.

What the attacker needed

Attacker is the first depositor; Attacker can manipulate derivative price

The fix

The deployment process should include an initial stake of 0.5 ETH to prevent totalSupply from dropping to 1 wei, or unstake() should enforce a minimum totalSupply.

How it was found

  1. The attacker becomes the first staker, holding 100% of safETH totalSupply.
  2. The attacker unstakes all but 1 wei of safETH, leaving totalSupply = 1 wei.
  3. The attacker manipulates the derivative's ethPerDerivative price in the underlying AMM pool so that 1 derivative ETH is worth less than 1 ETH.
  4. With 1 wei of safETH and 1 wei of derivative balance, underlyingValue becomes 0.
  5. When stake() is called, preDepositPrice is computed as 0, causing a division by zero revert.

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 staking contract, the price per share is calculated by dividing the total value of assets by the total number of shares. If there is only one share (1 wei) and the total value is zero, the division by zero causes the transaction to fail. An attacker who is the only holder can unstake almost all shares and manipulate the asset price to make the value zero, blocking new stakes.

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.

More from Asymmetry Finance afETH Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.