Skip to content
Business logic Audit finding Intermediate

In Dolomite, when opening a borrow position, the holding position in the Registry will never be updated due to the removePosition flag being set to true

When opening a borrow position in the Dolomite connector, the code incorrectly sets the removePosition flag to true, so the position is never added to the Registry's holding positions. This means the Registry does not track the borrow, which could prevent timely closing and lead to liquidations or bad debt.

iamandreiski Follow

Originally disclosed on Code4rena

Submitted by iamandreiski (View additional reports submitted by other wardens)

Impact

Whenever some of the connectors opens or closes a position, it is updated in the registry.

Usually if/when a position is closed (all asset amount withdrawn / borrow position closed in its entirety, etc.), the holding position is updated with the flag true for bool removePosition.

In Dolomite, whenever we open a new borrow position, the flag is also set to true which means that the position should be removed, and again when closing the borrow position, the flag is also set to true.

This will lead to that position never being accounted for in the Registry.

Proof of Concept

In Dolomite, whenever we want to open a borrow position, we can call the openBorrowPosition() function:

     function openBorrowPosition(uint256 marketId, uint256 _amountWei, uint256 accountId)
            public
            onlyManager
            nonReentrant
        {
            address token = dolomiteMargin.getMarketTokenAddress(marketId);

            if (!registry.isTokenTrusted(vaultId, token, address(this))) {
                revert IConnector_UntrustedToken(token);
            }
            // borrow
            borrowPositionProxy.openBorrowPosition(
                0, accountId, marketId, _amountWei, AccountBalanceHelper.BalanceCheckFlag.None
            );
            registry.updateHoldingPosition(
                vaultId, registry.calculatePositionId(address(this), DOL_POSITION_ID, ""), abi.encode(accountId), "", true
            );
        }

As we can see, when calling the registry.updateHoldingPosition the removePosition flag is set to true (the last argument in the function call).

Usually this is done when we want to either close a position or we’ve emptied the market/pool (our balance/share is 0) and we’re closing it, here is an example from the same connector Dolomite properly utilizing this in the withdraw function:

     function withdraw(uint256 marketId, uint256 _amount) public onlyManager nonReentrant {
            address token = dolomiteMargin.getMarketTokenAddress(marketId);
            depositWithdrawalProxy.withdrawWeiFromDefaultAccount(
                marketId, _amount, AccountBalanceHelper.BalanceCheckFlag.None
            );
            // Update token
            _updateTokenInRegistry(token);
            (uint256[] memory markets,,,) = dolomiteMargin.getAccountBalances(Info(address(this), 0));
            if (markets.length == 0) {
                registry.updateHoldingPosition(
                    vaultId, registry.calculatePositionId(address(this), DOL_POSITION_ID, ""), abi.encode(0), "", true
                );
            }
        }

We can also see that when closing a borrow position in the same connector, the flag is also set to true:

     function closeBorrowPosition(uint256[] memory marketIds, uint256 accountId) public onlyManager nonReentrant {
            // repay
            borrowPositionProxy.closeBorrowPosition(accountId, 0, marketIds);
            registry.updateHoldingPosition(
                vaultId, registry.calculatePositionId(address(this), DOL_POSITION_ID, ""), abi.encode(accountId), "", true
            );
        }

Due to the following circuit-breaker check, the borrow position would never be updated as part of the holdingPositions array:

      if (positionIndex == 0 && removePosition) return type(uint256).max;

The positionIndex is based on the isPositionUsed mapping:

     bytes32 holdingPositionId = keccak256(abi.encode(msg.sender, _positionId, _data));
     uint256 positionIndex = vault.isPositionUsed[holdingPositionId];

Since that mapping value of the holdingPositionId would have never been initiated, due to the circuit-breaker check above returning type(uint256).max, it would be 0, thus interrupting the function flow.

If the flag hasn’t been set to true, the function flow would continue to the end, calling the other updateHoldingPosition function, and updating the isPositionUsed mapping:

    return
                updateHoldingPosition(vault, vaultId, _positionId, _data, additionalData, positionIndex, holdingPositionId);

This position would have never been accounted for in the Registry, and if the strategy’s actions are based on Registry data, it can fail to close the position in-time, possibly leading to liquidations and bad debt.

Recommended Mitigation Steps

Set the removePosition flag on the openBorrowPosition when calling the updateHoldingPosition function to false.

gzeon (judge) increased severity to High and commented:

Both open and close borrow are impacted.

HadiEsna (NOYA) confirmed


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

The openBorrowPosition function passes true for the removePosition flag when calling updateHoldingPosition, causing the position to be skipped due to a circuit-breaker check.

Impact

The borrow position is not tracked in the Registry, which could prevent the strategy from closing it in time, potentially leading to liquidations and bad debt.

What the attacker needed

Manager role; Dolomite connector used

The fix

Set the removePosition flag to false when calling updateHoldingPosition in openBorrowPosition.

How it was found

  1. Call openBorrowPosition with a market ID, amount, and account ID.
  2. The function calls updateHoldingPosition with removePosition set to true.
  3. The circuit-breaker check returns early because positionIndex is 0 and removePosition is true.
  4. The position is never added to the holdingPositions array.

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 all open positions so it can manage them. When a new borrow position is opened, the code mistakenly tells the system to remove it instead of adding it. As a result, the system never knows about the borrow, which could cause problems later if it needs to close the position.

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 iamandreiski

View all

5 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.