[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.
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.
gALCXcontract is deployed- The attacker sends the
ALCXtoken to the deployedgALCXcontract directly instead of usingstakefunction so that the followingbalancevariable has value.
gALCX.sol#L73-L75 uint balance = alcx.balanceOf(address(this));
if (balance > 0) {
- Since the
ALCXtoken is given to thegALCXcontract directly,totalSupply == 0andalcx.balanceOf(address(this)) > 0becomes true.
exchangeRate += (balance * exchangeRatePrecision) / totalSupply;
- Non attackers try to call
stakefunction, butbumpExchangeRatefunction fails because of(balance * exchangeRatePrecision) / totalSupplywhen totalSupply is 0. - Owner cannot call
migrateSourcefunction sincebumpExchangeRatewill 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.
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
- Deploy the gALCX contract.
- Attacker sends ALCX tokens directly to the contract, bypassing the stake function.
- This makes alcx.balanceOf(address(this)) > 0 while totalSupply remains 0.
- Legitimate users call stake, but bumpExchangeRate reverts due to division by zero.
- 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.