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.
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
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
- Alice call _decrementWeightUntilFree(alice, 2)
- userFreeWeight = 0
- gauge D is freed, totalFreed = 0, userFreed = 1
- (userFreeWeight + totalFreed) < weight, continue to free next gauge
- gauge A is freed, totalFreed = 1, userFreed = 2
- (userFreeWeight + totalFreed) < weight, continue to free next gauge
- gauge B is freed, totalFreed = 2, userFreed = 3
- All gauge is freed
Alternatively, Alice can
- Alice call _decrementWeightUntilFree(alice, 1)
- userFreeWeight = balanceOf[alice] - getUserWeight[alice] = 3 - 3 = 0
- gauge D is freed, totalFreed = 0, userFreed = 1
- (userFreeWeight + totalFreed) < weight, continue to free next gauge
- gauge A is freed, totalFreed = 1, userFreed = 2
- (userFreeWeight + totalFreed) >= weight, break
- getUserWeight[alice] -= totalFreed
- Alice call _decrementWeightUntilFree(alice, 2)
- userFreeWeight = balanceOf[alice] - getUserWeight[alice] = 3 - 1 = 2
- (userFreeWeight + totalFreed) >= weight, break
- 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.
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
- Alice calls _decrementWeightUntilFree with weight 1, having allocated 1 weight each to deprecated gauge D and live gauges A and B.
- The function frees gauge D but does not add its weight to totalFreed, so it continues to free gauge A.
- After freeing gauge A, totalFreed becomes 1, which meets the weight, so the loop stops.
- Alice then calls _decrementWeightUntilFree with weight 2, and since userFreeWeight is now 2, it returns early without freeing more.
- 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.