Precision Loss in FluxTokensolgetClaimableFlux
The getClaimableFlux function in FluxToken.sol performs division before multiplication, causing precision loss and users receiving less Flux than they should. An attacker could exploit this to permanently freeze unclaimed royalties.
Originally disclosed on Immunefi
- Disclosed after under an hour
31390 - [SC - High] Precision Loss in FluxTokensolgetClaimableFlux
Submitted on May 17th 2024 at 23:44:47 UTC by @gladiator111 for Boost | Alchemix
Report ID: #31390
Report type: Smart Contract
Report severity: High
Target: https://github.com/alchemix-finance/alchemix-v2-dao/blob/main/src/FluxToken.sol
Impacts:
- Permanent freezing of unclaimed royalties
Description
Brief/Intro
Precision Loss is there in FluxToken.sol::getClaimableFlux resulting in getting less Flux Token
Vulnerability Details
Note - Please adjust the Severity Level or impact as you seem proper. I have selected the most close impact from the impact list.
In the function FluxToken.sol::getClaimableFlux
// @audit Precision loss
function getClaimableFlux(uint256 _amount, address _nft) public view returns (uint256 claimableFlux) {
uint256 bpt = calculateBPT(_amount); // .4 times amount
uint256 veMul = IVotingEscrow(veALCX).MULTIPLIER(); // 2
uint256 veMax = IVotingEscrow(veALCX).MAXTIME(); // 365 days
uint256 fluxPerVe = IVotingEscrow(veALCX).fluxPerVeALCX(); // 5000 or 50%
uint256 fluxMul = IVotingEscrow(veALCX).fluxMultiplier(); //4 or 4x
// Amount of flux earned in 1 yr from _amount assuming it was deposited for maxtime
claimableFlux = (((bpt * veMul) / veMax) * veMax * (fluxPerVe + BPS)) / BPS / fluxMul;
//((( 40*amount * 2) / 31536000) * 31536000 * ( 5000 + 10000 ))/ 10000 / 4
// Claimable flux for alchemechNFT is different than patronNFT
if (_nft == alchemechNFT) {
claimableFlux = (claimableFlux * alchemechMultiplier) / BPS; // flux * 5 /10000 or 0.05% flux
}
}
The calculation of claimable flux leads to precision loss because of division before multiply
claimableFlux = (((bpt * veMul) / veMax) * veMax * (fluxPerVe + BPS)) / BPS / fluxMul;
Notice the veMax is divided before multiplying which leads to precision loss.
Impact Details
User will get less Flux because of precision Loss.
Suggestion / Recommendation
- claimableFlux = (((bpt * veMul) / veMax) * veMax * (fluxPerVe + BPS)) / BPS / fluxMul;
+ claimableFlux = (((bpt * veMul)) * veMax * (fluxPerVe + BPS)) / BPS / fluxMul / veMax;
References
Proof of Concept
Add the following to the FluxToken.t.sol and run using
forge test --match-test testGetClaimableFluxPrecisionLoss -vvvv --fork-url $FORK_URL
function testGetClaimableFluxPrecisionLoss() public {
uint256 claimableFlux = flux.getClaimableFlux(350000, patronNFT);
uint256 correctClaimableFlux = CorrectedClaimableFlux(350000, patronNFT);
uint256 claimableFluxWithDifferentAmount = flux.getClaimableFlux(1e18, patronNFT);
uint256 correctClaimableFluxWithDifferentAmount = CorrectedClaimableFlux(1e18, patronNFT);
console.log(claimableFlux);
console.log(correctClaimableFlux);
console.log(claimableFluxWithDifferentAmount);
console.log(correctClaimableFluxWithDifferentAmount);
assert(claimableFlux != correctClaimableFlux);
assert(claimableFluxWithDifferentAmount != correctClaimableFluxWithDifferentAmount);
}
function CorrectedClaimableFlux(uint256 _amount, address _nft) public view returns (uint256 claimableFlux) {
// Values are Hardcoded for simplification
uint256 bpt = 40*_amount;/*calculateBPT(_amount);*/ // .4 times amount
uint256 veMul = 2; /*IVotingEscrow(veALCX).MULTIPLIER();*/ // This equals 2
uint256 veMax = 365 days; /*IVotingEscrow(veALCX).MAXTIME(); */ // This equals 365 days
uint256 fluxPerVe = 5000;/*IVotingEscrow(veALCX).fluxPerVeALCX();*/ // This equals 5000 or 50%
uint256 fluxMul = 4;/*IVotingEscrow(veALCX).fluxMultiplier();*/ // This equals 4 or 4x
// Amount of flux earned in 1 yr from _amount assuming it was deposited for maxtime
// Precision Loss fixed here
claimableFlux = (((bpt * veMul)) * veMax * (fluxPerVe + BPS)) / BPS / fluxMul / veMax;
//((( 40*amount * 2) / 31536000) * 31536000 * ( 5000 + 10000 ))/ 10000 / 4
// Claimable flux for alchemechNFT is different than patronNFT
if (_nft == alchemechNFT) {
claimableFlux = (claimableFlux * 5) / BPS; // flux * 5 /10000 or 0.05% flux
}
}
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 calculation divides by veMax before multiplying by veMax, causing integer truncation and precision loss.
Impact
Users will receive less Flux than expected, which could lead to permanent freezing of unclaimed royalties.
What the attacker needed
None
The fix
The calculation should be reordered to multiply before dividing, as shown in the suggestion.
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 calculates a reward, it should multiply before dividing to avoid losing small amounts due to rounding. Here, the code divides first, so the final number is smaller than it should be, meaning users get less of the Flux token.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.