Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERC20Gauges.sol: infinite loop if one of the user's gauges has a zero weight #260

Closed
code423n4 opened this issue Jun 24, 2023 · 3 comments
Closed
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-735 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards

Comments

@code423n4
Copy link
Contributor

code423n4 commented Jun 24, 2023

Lines of code

https://github.com/code-423n4/2023-05-maia/blob/main/src/erc-20/ERC20Gauges.sol#L548

Vulnerability details

Impact

When a user transfers or burns his tokens, the algorithm is freeing weights from the gauges that belong to him. This is done inside a for loop that iterates over the list of user's gauges _userGauges[user].values(), if the gauge has a non-zero weight we remove it, unfortunately loop counter is placed inside the if block if (userGaugeWeight != 0) making it impossible to proceed if gauge weight is zero, therefore transfer/burn will fail out of gas.

I'm putting this bug in medium because the only way for this to happen is when the user increments his gauge with a zero value.

Proof of Concept

Test case in ERC20GaugesTest.t.sol

    function testIncrementGauges() public {
        token.mint(address(this), 100e18);
        token.setMaxDelegates(1);
        token.delegate(address(this));

        token.setMaxGauges(2);
        token.addGauge(gauge1);
        token.addGauge(gauge2);

        token.incrementGauge(gauge1, 0);
        token.incrementGauge(gauge2, 100e18);

        address to = hevm.addr(100);
        token.transfer(to, 100e18);
    }

Tools Used

Forge

Recommended Mitigation Steps

Put i++ outside of the if block

        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++;
            }
        }

Assessed type

DoS

@code423n4 code423n4 added 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working labels Jun 24, 2023
code423n4 added a commit that referenced this issue Jun 24, 2023
@code423n4 code423n4 changed the title ERC20Gauges.sol: infinite loop if one of the user's gause has zero weight ERC20Gauges.sol: infinite loop if one of the user's gauges has a zero weight Jun 24, 2023
@c4-judge
Copy link
Contributor

trust1995 marked the issue as primary issue

@c4-judge c4-judge added the primary issue Highest quality submission among a set of duplicates label Jul 10, 2023
@c4-judge
Copy link
Contributor

trust1995 marked the issue as satisfactory

@c4-judge c4-judge added the satisfactory satisfies C4 submission criteria; eligible for awards label Jul 10, 2023
@c4-judge c4-judge added duplicate-717 and removed primary issue Highest quality submission among a set of duplicates labels Jul 11, 2023
@c4-judge
Copy link
Contributor

trust1995 marked the issue as duplicate of #717

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value bug Something isn't working duplicate-735 edited-by-warden satisfactory satisfies C4 submission criteria; eligible for awards
Projects
None yet
Development

No branches or pull requests

2 participants