Skip to content
Smart contracts Audit finding Intermediate

Precision loss when calculating the FLUX amount

The amount of FLUX tokens required to ragequit is calculated with a division before multiplication, causing integer truncation and a lower result than intended. An attacker could exploit this to burn fewer FLUX tokens than required, potentially freezing funds or griefing the protocol.

MTNether Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

30555 - [SC - Low] Precision loss when calculating the FLUX amount...

Precision loss when calculating the FLUX amount required to ragequit for a token

Submitted on Apr 30th 2024 at 18:42:03 UTC by @MTNether for Boost | Alchemix

Report ID: #30555

Report type: Smart Contract

Report severity: Low

Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/VotingEscrow.sol

Impacts:

  • Permanent freezing of funds
  • Temporary freezing of funds for 12 hours
  • Griefing (e.g. no profit motive for an attacker, but damage to the users or the protocol)

Description

Brief/Intro

Fewer FLUX tokens than actual are calculated and burnt in the cooling down process due to the priority of division over multiplication.

Vulnerability Details

Solidity rounds down the result of an integer division, and because of that, it is always recommended to multiply before dividing to avoid that precision loss. In the case of a prior division over multiplication, the final result may face serious precision loss as the first answer would face truncated precision and then multiplied to another integer.

The problem arises in the VotingEscrow's cooling down part. After performing the necessary checks inside the startCooldown() function, it checks the required FLUX amounts to ragequit:

Solidity
    function amountToRagequit(uint256 _tokenId) public view returns (uint256) {
        // amount of flux earned in one epoch
        uint256 oneEpochFlux = claimableFlux(_tokenId);

        // total amount of epochs in fluxMultiplier amount of years
        uint256 totalEpochs = fluxMultiplier * ((MAXTIME) / EPOCH);

        // based on one epoch, calculate total amount of flux over fluxMultiplier amount of years
        uint256 ragequitAmount = oneEpochFlux * totalEpochs;

        return ragequitAmount;
    }

It is evident that the ragequitAmount is calculated by the multiplication of claimable flux token amounts and the flux time ratio which is defined as:

Solidity
    totalEpochs = fluxMultiplier * ((MAXTIME) / EPOCH);

The variable fluxMultiplier is set to be 4, the MAXTIME, and EPOCH are 365 days, and 2 weeks respectively.

The current implementation of the aforementioned definition has a hidden division before a multiplication that rounds down the whole expression.

This is bad as the precision loss can be significant, which leads to the contract calculating and burning less ragequitAmount than the actual.

At the Proof of Concept part, we can check this behavior precisely.

Impact Details

Low ragequitAmount to ragequit inside the amountToRagequit() function is calculated leading to wrongly setting the minimum Flux amounts to ragequit and thus burning fewer tokens than actual.

References

https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/VotingEscrow.sol?utm\\_source=immunefi#L345-L356

Recommended Mitigation Steps

Consider modifying the ragequitAmount calculation to prevent such precision loss and prioritize the multiplication over division:

Diff
    function amountToRagequit(uint256 _tokenId) public view returns (uint256) {
        // amount of flux earned in one epoch
        uint256 oneEpochFlux = claimableFlux(_tokenId);

        // total amount of epochs in fluxMultiplier amount of years
-       uint256 totalEpochs = fluxMultiplier * ((MAXTIME) / EPOCH);

        // based on one epoch, calculate total amount of flux over fluxMultiplier amount of years
-       uint256 ragequitAmount = oneEpochFlux * totalEpochs;
+       uint256 ragequitAmount = (oneEpochFlux * fluxMultiplier * MAXTIME) / EPOCH;

        return ragequitAmount;
    }

Proof of Concept

You can run this code inside the Forge to see the difference between the results:

Solidity
    function test_precissionLoss() public {

        uint256 oneEpochFlux = claimableFlux;
        uint256 totalEpochs = fluxMultiplier * ((MAXTIME) / EPOCH);

        uint256 ragequitAmount_actual   = (oneEpochFlux * totalEpochs);
        uint256 ragequitAmount_accurate = (oneEpochFlux * fluxMultiplier * MAXTIME) / EPOCH;
        
        console.log("Current Implementation ", ragequitAmount_actual);
        console.log("Actual Implementation  ", ragequitAmount_accurate);
    }

The result would be: (for a sample and real claimableFlux of 156510000000 just for testing purpose)

     Current Implementation  16277040000000
     Actual Implementation   16321757142857

Thus, we can see that the actual implementation produces less ragequit amount than the precise method. This test shows a big difference between the two calculated ragequit amounts in the cooling down process.

The original report as published on Immunefi, by MTNether. Images and videos are our own copies; raw HTML is shown as text. View it on Immunefi.

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 expression `fluxMultiplier * (MAXTIME / EPOCH)` performs integer division before multiplication, truncating the result and causing precision loss.

Impact

The contract calculates and burns fewer FLUX tokens than required, which could lead to temporary or permanent freezing of funds or griefing.

What the attacker needed

None

The fix

The calculation should be changed to multiply before dividing, e.g., `(oneEpochFlux * fluxMultiplier * MAXTIME) / EPOCH`.

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.

When a smart contract does maths with whole numbers, dividing before multiplying can lose small remainders. Here, the contract divides first, so the final amount of tokens to burn is slightly too small. This means a user could pay less than they should when quitting a staking position.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by MTNether

View all

1 more disclosed report, best first.

More from Alchemix

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.