Skip to content
Business logic Audit finding Intermediate

Incorrect accounting of free weight in _decrementWeightUntilFree

The function _decrementWeightUntilFree incorrectly calculates free weight by excluding deprecated gauges from the total freed weight, causing it to free more user gauge weight than necessary. An attacker could exploit this to free excessive weight, potentially disrupting gauge allocations.

gzeon Follow

Originally disclosed on Code4rena

Submitted by gzeon

In _decrementWeightUntilFree, the free weight is calculated by balanceOf[user] - getUserWeight[user] plus weight freed from non-deprecated gauges. The non-deprecated criteria is unnecessary and lead to incorrect accounting of free weight.

Proof of Concept

ERC20Gauges.sol#L547-L583

    function _decrementWeightUntilFree(address user, uint256 weight) internal {
        uint256 userFreeWeight = balanceOf[user] - getUserWeight[user];

        // early return if already free
        if (userFreeWeight >= weight) return;

        uint32 currentCycle = _getGaugeCycleEnd();

        // cache totals for batch updates
        uint112 userFreed;
        uint112 totalFreed;

        // Loop through all user gauges, live and deprecated
        address[] memory gaugeList = _userGauges[user].values();

        // Free gauges until through entire list or under weight
        uint256 size = gaugeList.length;
        for (uint256 i = 0; i < size && (userFreeWeight + totalFreed) < weight; ) {
            address gauge = gaugeList[i];
            uint112 userGaugeWeight = getUserGaugeWeight[user][gauge];
            if (userGaugeWeight != 0) {
                // If the gauge is live (not deprecated), include its weight in the total to remove
                if (!_deprecatedGauges.contains(gauge)) {
                    totalFreed += userGaugeWeight;
                }
                userFreed += userGaugeWeight;
                _decrementGaugeWeight(user, gauge, userGaugeWeight, currentCycle);

                unchecked {
                    i++;
                }
            }
        }

        getUserWeight[user] -= userFreed;
        _writeGaugeWeight(_totalWeight, _subtract, totalFreed, currentCycle);
    }

Consider Alice allocated 3 weight to gauge D, gauge A and gauge B equally where gauge D is depricated

  1. Alice call _decrementWeightUntilFree(alice, 2)
  2. userFreeWeight = 0
  3. gauge D is freed, totalFreed = 0, userFreed = 1
  4. (userFreeWeight + totalFreed) < weight, continue to free next gauge
  5. gauge A is freed, totalFreed = 1, userFreed = 2
  6. (userFreeWeight + totalFreed) < weight, continue to free next gauge
  7. gauge B is freed, totalFreed = 2, userFreed = 3
  8. All gauge is freed

Alternatively, Alice can

  1. Alice call _decrementWeightUntilFree(alice, 1)
  2. userFreeWeight = balanceOf[alice] - getUserWeight[alice] = 3 - 3 = 0
  3. gauge D is freed, totalFreed = 0, userFreed = 1
  4. (userFreeWeight + totalFreed) < weight, continue to free next gauge
  5. gauge A is freed, totalFreed = 1, userFreed = 2
  6. (userFreeWeight + totalFreed) >= weight, break
  7. getUserWeight[alice] -= totalFreed
  8. Alice call _decrementWeightUntilFree(alice, 2)
  9. userFreeWeight = balanceOf[alice] - getUserWeight[alice] = 3 - 1 = 2
  10. (userFreeWeight + totalFreed) >= weight, break
  11. Only 2 gauge is freed

Recommended Mitigation Steps

No need to treat deprecated gauge separately.

Joeysantoro (xTRIBE) confirmed and commented:

This appears correct. Would be for a Tribe dev to validate with a test that certain paths could brick create this incorrect accounting.


The original report as published on Code4rena, by gzeon. 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

Impact

This could allow a user to free more gauge weight than intended, potentially disrupting the allocation system.

What the attacker needed

User has allocated weight to gauges; at least one gauge is deprecated

The fix

The code should be fixed by removing the special treatment of deprecated gauges and including their weight in the total freed weight.

How it was found

  1. Alice calls _decrementWeightUntilFree with weight 1, having allocated 1 weight each to deprecated gauge D and live gauges A and B.
  2. The function frees gauge D but does not add its weight to totalFreed, so it continues to free gauge A.
  3. After freeing gauge A, totalFreed becomes 1, which meets the weight, so the loop stops.
  4. Alice then calls _decrementWeightUntilFree with weight 2, and since userFreeWeight is now 2, it returns early without freeing more.
  5. As a result, only two gauges are freed instead of the correct amount.

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.

This report is about a smart contract function that manages how much weight a user has allocated to different gauges. The function is supposed to free up just enough weight when needed, but it mistakenly ignores some gauges when counting, so it frees too much. This could let someone mess with the system by freeing more weight than they should.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by gzeon

View all

46 more disclosed reports, best first.

More from xTRIBE contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.