Skip to content
Smart contracts Audit finding Intermediate

Voting algorithm does not apply maximum availab

The voting contract allocates each pool's weight by integer division, so the sum of allocated weights can be less than the voter's total available voting power. A voter splitting their vote across several pools could therefore leave a small amount of power unused, potentially skewing a close tally.

xBentley Follow

Originally disclosed on Immunefi

  • Disclosed after under an hour

31189 - [SC - High] Voting algorithm does not apply maximum availab...

Submitted on May 14th 2024 at 15:36:44 UTC by @xBentley for Boost | Alchemix

Report ID: #31189

Report type: Smart Contract

Report severity: High

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

Impacts:

  • Contract fails to deliver promised returns, but doesn't lose value

Description

Brief/Intro

When voting, the algorithm used to allocate weights to pools does not use up all available voting power for the token. This can disadvantage some voters leading to skewed voting.

Vulnerability Details

When voting, weights for each pool are allocated proportionally, https://github.com/alchemix-finance/alchemix-v2-dao/blob/f1007439ad3a32e412468c4c42f62f676822dc1f/src/Voter.sol#L432:

Solidity
 uint256 _poolWeight = (_weights[i] * totalPower) / _totalVoteWeight;

This calculation leaves out some amounts unallocated since Solidity will round down the calculation making _totalWeight to be less than totalPower. Actually the code does not check that the totalPower available has been used up. Consider this scenario:

totalPower = 500 weight1 = 10 weight2 = 50 weight3 = 75

poolWeight1 = (10 * 500)/135 = 37 poolWeight2 = (50 * 500)/135 = 185 poolWeight3 = (75 * 500)/135 = 277

total voting power used = 499.

Impact Details

Voters who will be affected by the rounding down might not be able to apply all available voting power, compared to other voters who, for example, pass in single parameters. This might lead to skewed voting results where the final tally is determined by a small difference between nays and ayes.

References

https://github.com/alchemix-finance/alchemix-v2-dao/blob/f1007439ad3a32e412468c4c42f62f676822dc1f/src/Voter.sol#L432

##Recommendation I would recommend that the weight for the last pool be allocated via Subtraction and not division, thus src/Voter.sol::Ln432:

Solidity
 if(i == _poolCnt - 1)
uint256 _poolWeight = totalPower - _totalWeight;

Proof of Concept

Add this test to src/test/Voting.t.sol:

Solidity
    function testMultiPoolVote() public {
        uint256 tokenId = createVeAlcx(admin, TOKEN_1, MAXTIME, false);

        hevm.startPrank(admin);

        hevm.warp(block.timestamp + nextEpoch);

        uint256 maxPower = veALCX.balanceOfToken(tokenId);
        console.log(maxPower);
        address[] memory pools = new address[](3);
        pools[0] = alETHPool;
        pools[1] = sushiPoolAddress;
        pools[2] = balancerPoolAddress;
        uint256[] memory weights = new uint256[](3);
        weights[0] = 1;
        weights[1] = 50;
        weights[2] = 75;
        voter.vote(tokenId, pools, weights, 0);

        uint256 weightAlETH = voter.weights(alETHPool);
        uint256 weightSushi = voter.weights(sushiPoolAddress);
        uint256 weightBalancer = voter.weights(balancerPoolAddress);
        assertGt(maxPower,weightBalancer + weightSushi + weightAlETH);
    }

Due to Solidity rounding down, the total power applied is less than available for the token.

The original report as published on Immunefi, by xBentley. 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 per-pool weight is computed with integer division and the final pool is not given the leftover remainder, so rounding losses accumulate.

Impact

Affected voters may not be able to apply all their available voting power, which could skew close results, though no funds are lost.

What the attacker needed

A veALCX position; voting across multiple pools

The fix

The last pool's weight should be assigned by subtracting the running total from the total power instead of dividing.

How it was found

  1. Create a veALCX position and advance to the next epoch.
  2. Read the token's maximum voting power.
  3. Vote across three pools with weights of 1, 50 and 75.
  4. Compare the sum of recorded pool weights against the maximum power and observe it is lower.

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 you vote, your voting power is split between pools in proportion to the weights you choose. The contract calculates each share with whole-number division, which throws away the remainder, so a little of your power is never counted. This only matters when the result is very close.

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 xBentley

View all

3 more disclosed reports, 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.