Skip to content
Business logic Audit finding Advanced

NestedFactory does not track operators properly

The NestedFactory contract's operator cache could fall out of sync with the factory's own list of operators, because adding or removing an operator did not trigger a cache rebuild and the cache check could not detect removed operators. As a result, a removed operator could still be used in core operations such as create() and swapTokenForTokens(), and a newly added one could fail until the cache was rebuilt manually.

IllIllI Follow

Originally disclosed on Code4rena

Submitted by IllIllI, also found by 0x1f8b, hyh, and pauliax

NestedFactory.sol#L99-L108 MixinOperatorResolver.sol#L30-L47 NestedFactory.sol#L110-L122 MixinOperatorResolver.sol#L49-L55

NestedFactory extends the MixinOperatorResolver contract which comes from the synthetix/MixinResolver.sol code base where the expectation is that isResolverCached() returns false until rebuildCache() is called and the cache is fully up to date. Due to a medium issue identified in a prior contest, the OperatorResolver.importOperators() step was made to be atomically combined with the NestedFactory.rebuildCache() step. However, the atomicity was not applied everywhere and the ability to add/remove operators from the NestedFactory also had other cache-inconsistency issues. There are four separate instances of operator tracking problems in this submission.

Impact

As with the prior issue, many core operations (such as NestedFactory.create() and NestedFactory.swapTokenForTokens()) are dependant on the assumption that the operatorCache cache is synced prior to these functions being executed, but this may not necessarily be the case. Unlike the prior issue which was about updates to the resolver not getting reflected in the cache, this issue is about changes to the factory not updating the cache.

Proof of Concept

1. removeOperator() does not call rebuildCache()

  1. NestedFactory.removeOperator() is called to remove an operator
  2. A user calls NestedFactory(MixinOperatorResolver).create() using that operator and succeedes
  3. NestedFactory.rebuildCache() is called to rebuild cache

This flow is not aware that the cache is not in sync

/// @inheritdoc INestedFactory
function addOperator(bytes32 operator) external override onlyOwner {
  require(operator != bytes32(""), "NF: INVALID_OPERATOR_NAME");
  bytes32[] memory operatorsCache = operators;
  for (uint256 i = 0; i < operatorsCache.length; i++) {
      require(operatorsCache[i] != operator, "NF: EXISTENT_OPERATOR");
  }
  operators.push(operator);
  emit OperatorAdded(operator);
}

NestedFactory.sol#L99-L108

2. Using both removeOperator() and rebuildCache() does not prevent create() from using the operator

Even if removeOperator() calls rebuildCache() the function will still not work because resolverOperatorsRequired() only keeps track of remaining operators, and rebuildCache() currently has no way of knowing that an entry was removed from that array and that a corresponding entry from operatorCache needs to be removed too.

/// @notice Rebuild the operatorCache
function rebuildCache() external {
    bytes32[] memory requiredOperators = resolverOperatorsRequired();
    bytes32 name;
    IOperatorResolver.Operator memory destination;
    // The resolver must call this function whenever it updates its state
    for (uint256 i = 0; i < requiredOperators.length; i++) {
        name = requiredOperators[i];
        // Note: can only be invoked once the resolver has all the targets needed added
        destination = resolver.getOperator(name);
        if (destination.implementation != address(0)) {
            operatorCache[name] = destination;
        } else {
            delete operatorCache[name];
        }
        emit CacheUpdated(name, destination);
    }
}

MixinOperatorResolver.sol#L30-L47

3. addOperator() does not call rebuildCache()

  1. NestedFactory.addOperator() is called to add an operator
  2. A user calls NestedFactory(MixinOperatorResolver).create() using that operator and fails because the operator wasn’t in the resolverOperatorsRequired() during the last call to rebuildCaches(), so the operator isn’t in operatorCache
  3. NestedFactory.rebuildCache() is called to rebuild cache

This flow is not aware that the cache is not in sync

/// @inheritdoc INestedFactory
function removeOperator(bytes32 operator) external override onlyOwner {
  uint256 operatorsLength = operators.length;
  for (uint256 i = 0; i < operatorsLength; i++) {
      if (operators[i] == operator) {
          operators[i] = operators[operatorsLength - 1];
          operators.pop();
          emit OperatorRemoved(operator);
          return;
      }
  }
  revert("NF: NON_EXISTENT_OPERATOR");
}

NestedFactory.sol#L110-L122

4. isResolverCached() does not reflect the actual updated-or-not state

This function, like removeOperator() is not able to tell that there is an operator that needs to be removed from resolverCache, causing the owner not to know a call to rebuildCache() is required to ‘remove’ the operator

/// @notice Check the state of operatorCache
function isResolverCached() external view returns (bool) {
  bytes32[] memory requiredOperators = resolverOperatorsRequired();
  bytes32 name;
  IOperatorResolver.Operator memory cacheTmp;
  IOperatorResolver.Operator memory actualValue;
  for (uint256 i = 0; i < requiredOperators.length; i++) {

MixinOperatorResolver.sol#L49-L55

Recommended Mitigation Steps

Add calls to rebuildCache() in addOperator() and removeOperator(), have INestedFactory also track operators that have been removed with a new array, and have isResolverCached() also check whether this new array is empty or not.

maximebrugel (Nested Finance) confirmed and commented:

With this fix => #18 No need to add an array of removed operators, because we are now removing the operators from the cache at the same time. Only need to call rebuildCache when adding and removing operators.

PR: Med/High Risk Fixes


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

Core operations such as create() and swapTokenForTokens() could run with a stale operator cache, so a removed operator may still be usable and a newly added one may be unusable until the cache is rebuilt.

What the attacker needed

Owner adds or removes an operator; cache not rebuilt afterwards

The fix

The team said they would call rebuildCache when adding and removing operators, removing entries from the cache at the same time, and a fix PR was linked.

How it was found

  1. The owner calls removeOperator() to remove an operator.
  2. A user calls create() with that operator and the call still succeeds.
  3. The owner calls rebuildCache() to bring the cache back in sync.
  4. Separately, the owner calls addOperator() to add an operator.
  5. A user calls create() with the new operator and it fails because the cache was not rebuilt.
  6. The owner calls rebuildCache() to fix the cache.

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.

The contract keeps a shortcut list (a cache) of approved helper contracts called operators, so it does not have to look them up every time. When the owner added or removed an operator, that shortcut list was not updated, and the check that is supposed to notice this could not see removals. So a helper the owner had removed could still be used, and a newly added one would not work until someone manually refreshed the list.

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 IllIllI

View all

119 more disclosed reports, best first.

More from Nested Finance 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.