Skip to content
Business logic Audit finding Intermediate

`reduce_position` doesn’t update margin mapping correctly

The reduce_position function reduces a position's margin amount but fails to add the reduced amount back to the user's margin mapping, so the user cannot withdraw that margin. This causes users to lose their margin tokens.

0xbepresent Follow with Dug, GimelSec and 3 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/143

Found by

0xbepresent, Dug, GimelSec, Yuki, n33k, twicek

Summary

reduce_position function decrease the margin amount of the position but doesn't add it back to the user's margin mapping, making it impossible to withdraw the margin.

Vulnerability Detail

After selling some position tokens back against debt tokens using reduce_position function, debt_shares and margin_amount are reduced proportionally to keep leverage the same as before:

Vault.vy#L313-L330

Solidity
debt_amount: uint256 = self._debt(_position_uid)
    margin_debt_ratio: uint256 = position.margin_amount * PRECISION / debt_amount


    amount_out_received: uint256 = self._swap(
        position.position_token, position.debt_token, _reduce_by_amount, min_amount_out
    )


    # reduce margin and debt, keep leverage as before
    reduce_margin_by_amount: uint256 = (
        amount_out_received * margin_debt_ratio / PRECISION
    )
    reduce_debt_by_amount: uint256 = amount_out_received - reduce_margin_by_amount


    position.margin_amount -= reduce_margin_by_amount


    burnt_debt_shares: uint256 = self._repay(position.debt_token, reduce_debt_by_amount)
    position.debt_shares -= burnt_debt_shares
    position.position_amount -= _reduce_by_amount

However, even though some of the margin have been paid back (position.margin_amount has been reduced), self.margin[position.account][position.debt_token] mapping hasn't been updated by adding reduce_margin_by_amount which would allow the user to withdraw his margin.

Impact

Users will lose their margin tokens.

Code Snippet

Vault.vy#L313-L330

Tool used

Manual Review

Recommendation

Consider modifying the code like this:

Solidity
    reduce_debt_by_amount: uint256 = amount_out_received - reduce_margin_by_amount


    position.margin_amount -= reduce_margin_by_amount
+   self.margin[position.account][position.debt_token] += reduce_margin_by_amount

    burnt_debt_shares: uint256 = self._repay(position.debt_token, reduce_debt_by_amount)
    position.debt_shares -= burnt_debt_shares
    position.position_amount -= _reduce_by_amount

Discussion

Unstoppable-DeFi

https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/6

maarcweiss

Fixed by adding back margin to the user's margin mapping while reducing the position

The original report as published on Sherlock, by 0xbepresent. 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

Likely the reduce_position function updates the position's margin_amount but omits updating the user's margin mapping with the reduced amount.

Impact

Users will lose their margin tokens.

What the attacker needed

None

The fix

The issue was fixed by adding the reduced margin back to the user's margin mapping.

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.

When you close part of a leveraged position, the system should return the margin you put up to your withdrawable balance. Here, the code reduces the margin recorded in the position but forgets to credit it back to your account, so that money is stuck.

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 0xbepresent

View all

13 more disclosed reports, best first.

More from Unstoppable

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.