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

Impossible to remove the operator at index 0 of operators array #58

Closed
code423n4 opened this issue Nov 15, 2021 · 3 comments
Closed

Impossible to remove the operator at index 0 of operators array #58

code423n4 opened this issue Nov 15, 2021 · 3 comments
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working duplicate This issue or pull request already exists sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

loop

Vulnerability details

Currently the operator at the 0 index can never be removed due to the require statement in removeOperator.

Impact

If this is an operator which is no longer supported, it can't be removed.

Proof of Concept

Code of removeOperator:

function removeOperator(bytes32 operator) external override onlyOwner {
        uint256 i = 0;
        while (operators[i] != operator) {
                i++;
        }
        require(i > 0, "NestedFactory::removeOperator: Cant remove non-existent operator");
        delete operators[i];
}

(https://github.com/code-423n4/2021-11-nested/blob/main/contracts/NestedFactory.sol#L79-L86)

Let's say the value bytes32(1) is at index 0 of the operators array. If we call removeOperator(bytes32(1)) the condition in the while loop will be false since bytes32(1) is equal to operators[0]. Since i does not get increased the require statement of i > 0 will fail since i is equal to 0. The error will state that a non-existant operator can't be removed, but the operator does exist and it's impossible to remove it from the array.

Recommended Mitigation Steps

Change the function code to something like this:

function removeOperator(bytes32 operator) external override onlyOwner {
        require(operators.length > 0, "NestedFactory::removeOperator: Cant remove non-existent operator");
        for (uint256 i = 0; i < operators.length; i++) {
            if (operators[i] == operator) {
                delete operators[i];
            }
        }  
    }
@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 Nov 15, 2021
code423n4 added a commit that referenced this issue Nov 15, 2021
@maximebrugel maximebrugel added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Nov 16, 2021
@maximebrugel
Copy link
Collaborator

Note

This issue also means that :

  • You can't remove the last Operator.
  • If is called with non-existant operator the while loop keeps going until gas limit is hit.

Every issues pointing this incorrect logic will be linked to this issue.

@maximebrugel
Copy link
Collaborator

Note that we should use pop to really remove the operator => #139

@alcueca
Copy link
Collaborator

alcueca commented Dec 3, 2021

Using #220 as the main instead

@alcueca alcueca closed this as completed Dec 3, 2021
@CloudEllie CloudEllie added 1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments duplicate This issue or pull request already exists and removed 2 (Med Risk) Assets not at direct risk, but function/availability of the protocol could be impacted or leak value labels Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 (Low Risk) Assets are not at risk. State handling, function incorrect as to spec, issues with comments bug Something isn't working duplicate This issue or pull request already exists sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

4 participants