Skip to content
DoS Audit finding Intermediate

[gALCX.sol] Attacker can make the contract unusable when totalSupply is 0

The gALCX contract's bumpExchangeRate function divides by totalSupply without checking for zero, so if an attacker sends ALCX tokens directly to the contract while totalSupply is zero, the function reverts. This makes stake, unstake, and migrateSource unusable, effectively denying service to legitimate users.

TerrierLover Follow

Originally disclosed on Code4rena

Submitted by TerrierLover

An attacker can make the contract unusable when totalSupply is 0. Specifically, bumpExchangeRate function does not work correctly which results in making stake, unstake and migrateSource functions that do not work as expected.

Proof of Concept

Here are steps on how the gALCX contract can be unusable.

  1. gALCX contract is deployed
  2. The attacker sends the ALCX token to the deployed gALCX contract directly instead of using stake function so that the following balance variable has value.

gALCX.sol#L73-L75 uint balance = alcx.balanceOf(address(this));

if (balance > 0) {
  1. Since the ALCX token is given to the gALCX contract directly, totalSupply == 0 and alcx.balanceOf(address(this)) > 0 becomes true.

gALCX.sol#L76

exchangeRate += (balance * exchangeRatePrecision) / totalSupply;
  1. Non attackers try to call stake function, but bumpExchangeRate function fails because of (balance * exchangeRatePrecision) / totalSupply when totalSupply is 0.
  2. Owner cannot call migrateSource function since bumpExchangeRate will be in the same situation mentioned in the step4 above

Recommended Mitigation Steps

Add handling when totalSupply is 0 but alcx.balanceOf(address(this)) is more than 0.

0xfoobar (Alchemix) acknowledged and commented:

Given that the gALCX deployment has 412 unique tokenholders on mainnet, this series of events is extraordinarily unlikely to occur. But we will keep it in mind for future deployments.

0xleastwood (judge) commented:

Nice find! Early stakers can DoS new contract deployments, making it impossible for other users to participate in the protocol. As this does not lead to lost funds and is recoverable through redeployment, I believe medium severity to be justified by the warden.


The original report as published on Code4rena, by TerrierLover. 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 bumpExchangeRate function does not handle the case where totalSupply is zero but the contract's token balance is greater than zero, causing a division by zero.

Impact

The contract becomes unusable for staking, unstaking, and migration, causing a denial of service, though no funds are lost and the issue is recoverable by redeployment.

What the attacker needed

Attacker can send tokens directly to the contract; totalSupply is zero (e.g., new deployment)

The fix

The contract should handle the case where totalSupply is zero but the contract's token balance is greater than zero, for example by skipping the exchange rate update or setting a default rate.

How it was found

  1. Deploy the gALCX contract.
  2. Attacker sends ALCX tokens directly to the contract, bypassing the stake function.
  3. This makes alcx.balanceOf(address(this)) > 0 while totalSupply remains 0.
  4. Legitimate users call stake, but bumpExchangeRate reverts due to division by zero.
  5. Owner cannot call migrateSource because bumpExchangeRate also fails.

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 has a function that calculates an exchange rate by dividing by the total number of tokens. If that total is zero, the division fails and the function stops working. An attacker can force the total to be zero while still sending tokens to the contract, which breaks the contract for everyone else.

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 TerrierLover

View all

2 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.