Skip to content
Business logic Audit finding Intermediate

Registry.sol#updateHoldingPosition remove position logic is incorrect: should use ownerConnector instead of calculatorConnector when calculating holdingPositionId

The updateHoldingPosition function in Registry.sol calculates the holdingPositionId using the calculatorConnector instead of the ownerConnector when removing a position that is not the last in the array. This causes the position index mapping to become incorrect, leading to wrong TVL calculations and potentially zero TVL when positions are removed.

pkqs90 Follow with Autosaida, josephdara and 4 others

Originally disclosed on Code4rena

Submitted by pkqs90, also found by Autosaida, josephdara, den-sosnowsky, eta, SeveritySquad, and 0xAlix2

Impact

When removing a holdingPosition that is not at the back of holdingPositions array, the holdingPositionId is incorrectly calculated. This will mess up the position index, and ultimately mess up TVL calculation.

Description

When removing a position in updateHoldingPosition, if the index of the position is not the last, it will do a swap between the current index and the last index, and remove the last position in holdingPositions array. However, the issue is when updating vault.isPositionUsed[holdingPositionId] index, the holdingPositionId is not correct.

It should be using vault.holdingPositions[positionIndex].ownerConnector instead of vault.holdingPositions[positionIndex].calculatorConnector.

This would be an issue for most of the token-holding positions because the calculateorConnector is the AccountingManager while the ownerConnector is the connector’s own address.

    function updateHoldingPosition(
        uint256 vaultId,
        bytes32 _positionId,
        bytes calldata _data,
        bytes calldata additionalData,
        bool removePosition
    ) public vaultExists(vaultId) returns (uint256) {
        Vault storage vault = vaults[vaultId];
        if (!vault.connectors[msg.sender].enabled) revert UnauthorizedAccess();
        if (!vault.trustedPositionsBP[_positionId].isEnabled) revert InvalidPosition(_positionId);
        bytes32 holdingPositionId = keccak256(abi.encode(msg.sender, _positionId, _data));
        uint256 positionIndex = vault.isPositionUsed[holdingPositionId];
        if (positionIndex == 0 && removePosition) return type(uint256).max;
        if (removePosition) {
            if (positionIndex < vault.holdingPositions.length - 1) {
                vault.holdingPositions[positionIndex] = vault.holdingPositions[vault.holdingPositions.length - 1];
                vault.isPositionUsed[keccak256(
                    abi.encode(
>                       vault.holdingPositions[positionIndex].calculatorConnector,
                        vault.holdingPositions[positionIndex].positionId,
                        vault.holdingPositions[positionIndex].data
                    )
                )] = positionIndex;
            }
            vault.holdingPositions.pop();
            vault.isPositionUsed[holdingPositionId] = 0;
            emit HoldingPositionUpdated(vaultId, _positionId, _data, additionalData, removePosition, positionIndex);
            return type(uint256).max;
        }
        return
            updateHoldingPosition(vault, vaultId, _positionId, _data, additionalData, positionIndex, holdingPositionId);
    }

Proof of Concept

We will give an example of how this bug will mess up the position index and ultimately lead to incorrect TVL.

Add the following test code in BaseConnector.t.sol. It does the following:

  1. Create 3 connectors, with the order [1, 2, 3] (with a dummy connector up front), and deal 10e6 USDC to each of them.
  2. Check that current TVL is 30e6.
  3. Move all USDC from connector 1 to 3. This should remove connector 1’s position. The current position array is [3, 2].
  4. Move all USDC from connector 3 to 2. This should remove connector 3’s position. However, since the positionIndex is not correctly updated in step 3, the connector 3’s positionIndex is still 3. This in updateHoldingPosition() will result in popping the position at the back (which is actually position 2).
  5. Check that current TVL is 0: because connector 2 is holding all the USDC but it has been popped out in step 4.
    function testRemoveConnectorBug() public {
        vm.startPrank(owner);
        BaseConnector connector3;
        connector3 = new BaseConnector(BaseConnectorCP(registry, 0, swapHandler, noyaOracle));
        addConnectorToRegistry(vaultId, address(connector3));

        // Step 1: Deal 10e6 USDC tokens to all 3 connectors.
        uint256 amount = 10 * 1e6;
        _dealWhale(baseToken, address(connector), USDC_Whale, amount);
        _dealWhale(baseToken, address(connector2), USDC_Whale, amount);
        _dealWhale(baseToken, address(connector3), USDC_Whale, amount);
        vm.startPrank(owner);
        connector.updateTokenInRegistry(USDC);
        connector2.updateTokenInRegistry(USDC);
        connector3.updateTokenInRegistry(USDC);

        // Make sure the TVL is 30e6.
        uint tvl = accountingManager.TVL();
        assertEq(tvl, 30e6);

        // Step 2: Move all USDC from connector 1 to connector 3. TVL is still 30e6.
        address[] memory tokens = new address[](1);
        tokens[0] = USDC;
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;
        {
            connector.transferPositionToAnotherConnector(tokens, amounts, "", address(connector3));
            uint tvl = accountingManager.TVL();
            assertEq(tvl, 30e6);
        }

        // Step 3: Move all USDC from connector 3 to connector 2. TVL is now ZERO.
        {
            amounts[0] = 2 * amount;
            connector3.transferPositionToAnotherConnector(tokens, amounts, "", address(connector2));
            uint tvl = accountingManager.TVL();
            assertEq(tvl, 0);
        }
    }

Tools Used

Foundry

Recommended Mitigation Steps

Use vault.holdingPositions[positionIndex].ownerConnector to calculate holdingPositionId.

HadiEsna (NOYA) confirmed and commented:

Fix in commit aef35f768753277552c65ffecf199ea73d0cd058.


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

Root cause

Likely the code uses calculatorConnector instead of ownerConnector when recomputing the holdingPositionId for the swapped position, so the isPositionUsed mapping is updated with an incorrect key.

Impact

The position index mapping becomes corrupted, leading to incorrect TVL calculation and potentially zero TVL when positions are removed.

What the attacker needed

Vault with multiple holding positions; connector enabled to call updateHoldingPosition

The fix

The code was fixed to use ownerConnector instead of calculatorConnector when calculating the holdingPositionId.

How it was found

  1. Create three connectors and give each 10e6 USDC, then check TVL is 30e6.
  2. Move all USDC from connector 1 to connector 3, which removes connector 1's position; TVL remains 30e6.
  3. Move all USDC from connector 3 to connector 2, which should remove connector 3's position but instead pops connector 2 due to the incorrect index.
  4. Check TVL, which is now 0 because connector 2 holding all USDC was removed.

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.

A smart contract keeps a list of positions and a lookup table that maps each position to its index in the list. When removing an item from the middle, the code swaps it with the last item and updates the lookup table, but it uses the wrong identifier for the swapped item, so the table points to the wrong place. Later removals then delete the wrong position, making the total value locked (TVL) calculation wrong.

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 pkqs90

View all

25 more disclosed reports, best first.

More from NOYA

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.