Skip to content
Business logic Audit finding Intermediate

withdrawTo Does Not Sync Before Checking A Position’s Margin Requirements

The withdrawTo function checks margin requirements using an oracle price that may be outdated because it does not sync the account first. An attacker could withdraw collateral while their position is undercollateralised, potentially leaving bad debt.

leastwood Follow

Originally disclosed on Code4rena

Submitted by leastwood

Impact

The maintenanceInvariant modifier in Collateral aims to check if a user meets the margin requirements to withdraw collateral by checking its current and next maintenance. maintenanceInvariant inevitably calls AccountPosition.maintenance which uses the oracle’s price to calculate the margin requirements for a given position. Hence, if the oracle has not synced in a long time, maintenanceInvariant may end up utilising an outdated price for a withdrawal. This may allow a user to withdraw collateral on an undercollaterized position.

Proof of Concept

https://github.com/code-423n4/2021-12-perennial/blob/main/protocol/contracts/collateral/Collateral.sol#L67-L76

function withdrawTo(address account, IProduct product, UFixed18 amount)
notPaused
collateralInvariant(msg.sender, product)
maintenanceInvariant(msg.sender, product)
external {
    _products[product].debitAccount(msg.sender, amount);
    token.push(account, amount);

    emit Withdrawal(msg.sender, product, amount);
}

https://github.com/code-423n4/2021-12-perennial/blob/main/protocol/contracts/collateral/Collateral.sol#L233-L241

modifier maintenanceInvariant(address account, IProduct product) {
    _;

    UFixed18 maintenance = product.maintenance(account);
    UFixed18 maintenanceNext = product.maintenanceNext(account);

    if (UFixed18Lib.max(maintenance, maintenanceNext).gt(collateral(account, product)))
        revert CollateralInsufficientCollateralError();
}

https://github.com/code-423n4/2021-12-perennial/blob/main/protocol/contracts/product/types/position/AccountPosition.sol#L71-L75

function maintenanceInternal(Position memory position, IProductProvider provider) private view returns (UFixed18) {
    Fixed18 oraclePrice = provider.priceAtVersion(provider.currentVersion());
    UFixed18 notionalMax = Fixed18Lib.from(position.max()).mul(oraclePrice).abs();
    return notionalMax.mul(provider.maintenance());
}

Tools Used

Manual code review.

Recommended Mitigation Steps

Consider adding settleForAccount(msg.sender) to the Collateral.withdrawTo function to ensure the most up to date oracle price is used when assessing an account’s margin requirements.

kbrizzle (Perennial) confirmed:

Great catch 🙏

Alex the Entreprenerd (judge) commented:

With most onChain protocols where there is potential for undercollateralized positions and liquidations, it is very important to accrue a user position before making any changes to their balance.

The warden identified a potential way for a user to withdraw funds while their account is below margin requirements.

Because this impacts the core functionality functionality of the protocol (accounting), I’m raising the severity to high

Mitigation seems to be straightforward

The original report as published on Code4rena, by leastwood. 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 withdrawTo function does not call settleForAccount to update the oracle price before checking margin requirements, so it uses a stale price.

Impact

A user could withdraw collateral while their position is undercollateralised, potentially leaving the protocol with bad debt.

What the attacker needed

Oracle price is outdated; user has an undercollateralised position

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 lets users withdraw collateral, but before allowing it, it checks that the user has enough collateral based on the current price of the asset. However, the price used might be old because the contract does not update the price before checking. So a user could take out collateral even though their position is actually unsafe, which could cause losses for the system.

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 leastwood

View all

70 more disclosed reports, best first.

More from Perennial 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.