Skip to content
Business logic Audit finding Intermediate

account.holdsToken is never set

The addHolding function fails to update the holdsToken map, so depositing the same token multiple times pushes duplicate entries into the holdingTokens array. This causes getHoldingAmounts to return the same balance multiple times, allowing an attacker to withdraw more tokens than they deposited and steal funds.

Marginswap

Originally disclosed on Code4rena

The addHolding function does not update the account.holdsToken map.

function addHolding(
    CrossMarginAccount storage account,
    address token,
    uint256 depositAmount
) internal {
    if (!hasHoldingToken(account, token)) {
        // SHOULD SET account.holdsToken here
        account.holdingTokens.push(token);
    }

    account.holdings[token] += depositAmount;
}

This leads to a critical vulnerability where deposits of the same token keep being pushed to the account.holdingTokens array but the sum is correctly updated in account.holdings[token]. However, because of the duplicate token in the holdingTokens array the same token is counted several times in the getHoldingAmounts function:

function getHoldingAmounts(address trader)
    external
    view
    override
    returns (
        address[] memory holdingTokens,
        uint256[] memory holdingAmounts
    )
{
    CrossMarginAccount storage account = marginAccounts[trader];
    holdingTokens = account.holdingTokens;

    holdingAmounts = new uint256[](account.holdingTokens.length);
    for (uint256 idx = 0; holdingTokens.length > idx; idx++) {
        address tokenAddress = holdingTokens[idx];
        // RETURNS SUM OF THE BALANCE FOR EACH TOKEN ENTRY
        holdingAmounts[idx] = account.holdings[tokenAddress];
    }
}

The MarginRouter.crossCloseAccount function uses these wrong amounts to withdraw all tokens:

function crossCloseAccount() external {
    (address[] memory holdingTokens, uint256[] memory holdingAmounts) =
        IMarginTrading(marginTrading()).getHoldingAmounts(msg.sender);

    // requires all debts paid off
    IMarginTrading(marginTrading()).registerLiquidation(msg.sender);

    for (uint256 i; holdingTokens.length > i; i++) {
        Fund(fund()).withdraw(
            holdingTokens[i],
            msg.sender,
            holdingAmounts[i]
        );
    }
}

An attacker can just deposit the same token X times which increases their balance by X times the actual value. This inflated balance can then be withdrawn to steal all tokens.

Recommend correctly setting the account.holdsToken map in addHolding.

The original report as published on Code4rena. 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 addHolding function does not set the holdsToken map when adding a new token, so duplicate tokens are pushed to the holdingTokens array.

Impact

An attacker could steal all tokens by inflating their balance through repeated deposits of the same token.

What the attacker needed

Ability to deposit tokens; ability to call crossCloseAccount

The fix

The holdsToken map should be correctly set in the addHolding function to prevent duplicate entries.

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 tracks a user's token deposits in two places: a list of tokens and a map of amounts. When a user deposits the same token again, the code adds it to the list again but forgets to mark it as already present. Later, when calculating the total holdings, the contract loops over the list and adds the same balance for each duplicate entry, making the user appear to have much more than they actually deposited. The attacker can then withdraw this inflated amount and steal funds.

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 from Marginswap

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.