Skip to content
DoS Audit finding Intermediate

If totalShares for a token falls to zero while there is pendingCredit the contract will become stuck

If the total shares of a yield token become zero while pending credit remains, several functions revert due to division by zero, locking the contract and permanently trapping the pending credit. An attacker could front-run the first deposit to cause this state, but the impact is limited to a denial of service.

AuditsAreUS Follow

Originally disclosed on Code4rena

Submitted by AuditsAreUS

AlchemistV2.sol#L1290-L1300 AlchemistV2.sol#L1268 AlchemistV2.sol#L1532 AlchemistV2.sol#L899 AlchemistV2.sol#L1625

It is possible for the contract to become stuck and unable to perform any actions if the totalShares of a yield token fall to zero while there is some pendingCredit still to be paid.

It will then be impossible to call deposit or withdraw functions, mints, burns, repay, liquidate, donate or harvest due to division by zero reverts in:

  • _distributeCredit()
  • _distributeUnlockedCredit()
  • _calculateUnrealizedDebt()
  • _convertSharesToYieldTokens()
  • donate()

Furthermore, any pendingCredit amount of tokens are still in the contract will become permanently stuck.

Proof of Concept

This case may arise under the follow steps a) deposit() is called by a user then time passes to earn some yield b) harvest() is called by the keeper which calls _distributeCredit() and increases pendingCredit c) withdraw() is called by the user to withdraw all funds

Since there is pendingCredit the following will have a non-zero balance for unlockedCredit however yieldTokenParams.totalShares is zero and thus we get a division by zero which reverts the entire transaction.

    function _distributeUnlockedCredit(address yieldToken) internal {
        YieldTokenParams storage yieldTokenParams = _yieldTokens[yieldToken];

        uint256 unlockedCredit = _calculateUnlockedCredit(yieldToken);
        if (unlockedCredit == 0) {
            return;
        }

        yieldTokenParams.accruedWeight     += unlockedCredit * FIXED_POINT_SCALAR / yieldTokenParams.totalShares;
        yieldTokenParams.distributedCredit += unlockedCredit;
    }

Each of the other listed functions will reach the same issue by attempting to divide some numerator by the totalShares which is zero.

Recommended Mitigation Steps

Consider preventing totalShares from over becoming zero once it is set. That is enforce a user to leave at least 1 unit if they are the last user to withdraw.

Another option is to transfer the first 1000 shares to a “burn” account (e.g. 0x000…01), when the first user deposits.

Alternatively, when the last user withdraws, transfer all pending credit to this user and set the required variables to zero to replicate the state before any users have deposited.

0xfoobar (Alchemix) confirmed, disagreed with severity and commented:

Disagree with severity because given the depth of distinct users using Alchemix, it is unlikely this scenario would occur.

0xleastwood (judge) commented:

This is an interesting issue. At the moment, it sits somewhere between medium and high risk, so I will need to think about this more before coming to a decision.

0xleastwood (judge) decreased severity to Medium and commented:

After further thought, I think this does not fit the criteria of high severity for the following reasons:

  • The protocol can be DoS’d on new deployments via front-running, but it does not lead to lost funds by users. It’d only require a new deployment by the Alchemist team.
  • If the protocol was to migrate to a new version of the protocol, a mass withdrawal event could lead to locked pendingCredit. However, because rewards are harvested by a keeper, I believe this to be unlikely as migrations will most certainly be coordinated by the protocol and its keepers. As such, users will be aware that they would miss out on rewards if the keeper does not harvest rewards prior to the migration.
  • Rewards are regularly harvested by the keeper, and as such, the value at risk is somewhat negligible.

For these reasons, I believe medium severity to be justified.


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

The contract does not prevent totalShares from becoming zero while pendingCredit is non-zero, leading to division by zero in multiple functions.

Impact

The contract becomes stuck and pending credit is permanently locked; the judge noted it could be triggered on new deployments via front-running but does not lead to lost funds by users, only requiring a new deployment.

What the attacker needed

PendingCredit is non-zero; totalShares becomes zero

The fix

The contract should be fixed by preventing totalShares from becoming zero, for example by requiring the last user to leave at least one unit or by burning initial shares.

How it was found

  1. A user calls deposit() and time passes to earn yield.
  2. The keeper calls harvest(), which calls _distributeCredit() and increases pendingCredit.
  3. The user calls withdraw() to withdraw all funds, leaving totalShares at zero.
  4. Subsequent calls to functions like deposit or withdraw revert due to division by zero.

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.

This is a smart contract bug where a number used as a divisor becomes zero, causing the program to crash. If all users withdraw their shares but some rewards are still owed, the contract gets stuck and no one can deposit or withdraw. An attacker could trigger this by being the first to deposit and then withdrawing, but it mainly causes a denial of service.

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 by AuditsAreUS

View all

9 more disclosed reports, best first.

More from Alchemix contest

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.