Skip to content
Business logic Audit finding Intermediate

Incorrect implementation of the `PositionMarginProcess.updatePositionFromBalanceMargin()` function

The function `updatePositionFromBalanceMargin()` incorrectly returns early when the position's initial margin equals the margin from balance and the amount is negative, so it fails to reduce the margin from balance. This allows users to withdraw assets without reducing their position margin, enabling them to utilise more tokens than they deposited.

KupiaSec Follow with whitehair0330

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/163

Found by

KupiaSec, whitehair0330

Summary

The updatePositionFromBalanceMargin() function does nothing when position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount < 0. However, in this case, the function should actually reduce the initialMarginInUsdFromBalance of the position.

Vulnerability Detail

In the updatePositionFromBalanceMargin() function, when amount < 0, it should reduce the value of initialMarginInUsdFromBalance for the position. However, as shown at L309, the function does nothing when position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount < 0. Consequently, if users withdraw their assets, the margin amounts of the positions are not reduced accordingly. This results in users being able to utilize more tokens than they have deposited.

Solidity
    function updatePositionFromBalanceMargin(
        Position.Props storage position,
        bool needSendEvent,
        uint256 requestId,
        int256 amount
    ) public returns (uint256 changeAmount) {
309     if (position.initialMarginInUsd == position.initialMarginInUsdFromBalance || amount == 0) {
            changeAmount = 0;
            return 0;
        }
        [...]
    }

Impact

As a result, users may be able to utilize more tokens than they have deposited.

Code Snippet

https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/main/elfi-perp-contracts/contracts/process/PositionMarginProcess.sol#L303-L338

Tool used

Manual Review

Recommendation

The PositionMarginProcess.updatePositionFromBalanceMargin() function should be fixed as follows.

Diff
-       if (position.initialMarginInUsd == position.initialMarginInUsdFromBalance || amount == 0) {
+       if ((position.initialMarginInUsd == position.initialMarginInUsdFromBalance && amount > 0) || amount == 0) {
            changeAmount = 0;
            return 0;
        }

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/0xCedar/elfi-perp-contracts/pull/43

sherlock-admin2

The Lead Senior Watson signed off on the fix.

The original report as published on Sherlock, by KupiaSec. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 early-return condition uses a logical OR instead of an AND with a check on the sign of the amount, so it skips the update when the margin values are equal and the amount is negative.

Impact

Users may be able to utilise more tokens than they have deposited.

What the attacker needed

None

The fix

The condition should be changed to only return early when the margins are equal and the amount is positive, or when the amount is zero.

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.

This is a smart contract bug where a function that should update a user's margin (the collateral backing their position) does nothing in a specific case. When a user withdraws funds, the margin should decrease, but because of a wrong condition, it stays the same, letting the user use more money than they actually have.

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 KupiaSec

View all

28 more disclosed reports, best first.

More from Elfi

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.