Skip to content
Business logic Audit finding Intermediate

price() in AfEth.sol doesn’t take afEth held for pending withdrawals into account

The price() function in AfEth.sol calculates the afEth price using totalSupply(), which includes afEth held by the contract for pending withdrawals, making the price lower than it should be. This lets depositors receive more afEth than intended, and a first depositor can make price() return zero, causing deposit() to revert and bricking the protocol.

MiloTruck Follow with adriro and rvierdiiev

Originally disclosed on Code4rena

Submitted by MiloTruck, also found by d3e4 (1, 2), adriro, and rvierdiiev

In AfEth.sol, the price() function returns the current price of afEth:

AfEth.sol#L133-L141

    function price() public view returns (uint256) {
        if (totalSupply() == 0) return 1e18;
        AbstractStrategy vEthStrategy = AbstractStrategy(vEthAddress);
        uint256 safEthValueInEth = (ISafEth(SAF_ETH_ADDRESS).approxPrice(true) *
            safEthBalanceMinusPending()) / 1e18;
        uint256 vEthValueInEth = (vEthStrategy.price() *
            vEthStrategy.balanceOf(address(this))) / 1e18;
        return ((vEthValueInEth + safEthValueInEth) * 1e18) / totalSupply();
    }

As seen from above, the price of afEth is calculated by the TVL of both safEth and vAfEth divided by totalSupply(). However, this calculation does not take into account afEth that is transferred to the contract when requestWithdraw() is called:

AfEth.sol#L183-L187

        uint256 afEthBalance = balanceOf(address(this));
        uint256 withdrawRatio = (_amount * 1e18) /
            (totalSupply() - afEthBalance);

        _transfer(msg.sender, address(this), _amount);

When a user calls requestWithdraw() to initiate a withdrawal, his afEth is transferred to the AfEth contract as shown above. Afterwards, an amount of vAfEth proportional to his withdrawal amount is burned, and pendingSafEthWithdraws is increased.

When price() is called afterwards, safEthBalanceMinusPending() and vEthStrategy.balanceOf(address(this)) will be decreased. However, since the user’s afEth is only transferred and not burnt, totalSupply() remains the same. This causes the value returned by price() to be lower than what it should be, since totalSupply() is larger than the actual circulating supply of afEth.

This is an issue as deposit() relies on price() to determine how much afEth to mint to a depositor:

AfEth.sol#L166-L168

        uint256 amountToMint = totalValue / priceBeforeDeposit;
        if (amountToMint < _minout) revert BelowMinOut();
        _mint(msg.sender, amountToMint);

Where:

  • totalValue is the ETH value of the caller’s deposit.
  • priceBeforeDeposit is the cached value of price().

If anyone has initiated a withdrawal using requestWithdraw() but hasn’t called withdraw() to withdraw his funds, price() will be lower than what it should be. Subsequently, when deposit() is called, the depositor will receive more afEth than he should since priceBeforeDeposit is smaller.

Furthermore, a first depositor can call requestWithdraw() with all his afEth immediately after staking to make price() return 0, thereby permanently DOSing all future deposits as deposit() will always revert with a division by zero error.

Impact

When there are pending withdrawals, price() will return a value smaller than its actual value. This causes depositors to receive more afEth than intended when calling deposit(), resulting in a loss of funds for previous depositors.

Additionally, a first depositor can abuse this to force deposit() to always revert, permanently bricking the protocol forever.

Proof of Concept

Assume that the protocol is newly deployed and Alice is the only depositor.

  • This means that Alice’s afEth balance equals to totalSupply().

Alice calls requestWithdraw() with _amount as all her afEth:

  • Since _amount == totalSupply(), withdrawRatio is 1e18 (100%).
  • Therefore, all of the protocol’s vAfEth is burnt and pendingSafEthWithdraws is increased to the protocol’s safEth balance.
  • Alice’s afEth is transferred to the protocol.

Bob calls deposit() to deposit some ETH into the protocol:

  • When price() is called:

    • Since pendingSafEthWithdraws is equal to the protocol’s safEth balance, safEthBalanceMinusPending() is 0, therefore safEthValueInEth is also 0.
    • Since vEthStrategy.balanceOf(address(this)) (the protocol’s vAfEth balance) is 0, vEthValueInEth is also 0.
    • totalSupply() is non-zero.
    • Therefore, price() returns 0 as:
    ((vEthValueInEth + safEthValueInEth) * 1e18) / totalSupply() = ((0 + 0) * 1e18) / x = 0
  • As priceBeforeDeposit is 0, this line reverts with a division by zero error.

As demonstrated above, deposit() will always revert as long as Alice does not call withdraw() to burn her afEth, thereby bricking the protocol’s core functionality.

Recommended Mitigation

In price(), consider subtracting the amount of afEth held in the contract from totalSupply():

AfEth.sol#L133-L141

    function price() public view returns (uint256) {
-       if (totalSupply() == 0) return 1e18;
+       uint256 circulatingSupply = totalSupply() - balanceOf(address(this));
+       if (circulatingSupply == 0) return 1e18;
        AbstractStrategy vEthStrategy = AbstractStrategy(vEthAddress);
        uint256 safEthValueInEth = (ISafEth(SAF_ETH_ADDRESS).approxPrice(true) *
            safEthBalanceMinusPending()) / 1e18;
        uint256 vEthValueInEth = (vEthStrategy.price() *
            vEthStrategy.balanceOf(address(this))) / 1e18;
-       return ((vEthValueInEth + safEthValueInEth) * 1e18) / totalSupply();
+       return ((vEthValueInEth + safEthValueInEth) * 1e18) / circulatingSupply;
    }

elmutt (Asymmetry) confirmed and commented:

@toshiSat - I think we can solve this by burning the tokens in requestWithdraw.

Asymmetry mitigated:

For this one we made afEth just burn on requestWithdraw.

Status: Mitigation confirmed. Full details in reports from m_Rassska, d3e4, and adriro.


The original report as published on Code4rena, by MiloTruck. 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 price() function uses totalSupply() without subtracting the afEth balance held by the contract for pending withdrawals.

Impact

Depositors could receive more afEth than intended, causing a loss for previous depositors, and a first depositor could permanently brick the protocol by making deposit() always revert.

What the attacker needed

Pending withdrawals exist; attacker is first depositor for DoS

The fix

The issue was mitigated by burning afEth on requestWithdraw instead of transferring it to the contract.

How it was found

  1. Alice calls requestWithdraw() with all her afEth, transferring her afEth to the contract and increasing pendingSafEthWithdraws.
  2. Bob calls deposit() to deposit ETH.
  3. price() returns 0 because safEthBalanceMinusPending() and vEthStrategy.balanceOf(address(this)) are both 0 while totalSupply() is non-zero.
  4. deposit() reverts with a division by zero error because priceBeforeDeposit is 0.

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.

The contract holds some of its own tokens when users request withdrawals, but the price calculation still counts those tokens as if they were in circulation. This makes the price too low, so new depositors get too many tokens, and in an extreme case the price can become zero, breaking deposits entirely.

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 MiloTruck

View all

35 more disclosed reports, best first.

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.

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.