`reduce_margin_by_amount` in `Vault.reduce_position` is wrongly calculated
The margin-to-debt ratio in the reduce_position function is calculated using only the debt amount, not the total position value, so when a position is reduced the margin is over-reduced and the debt is under-reduced, changing the leverage. An attacker could exploit this to alter the leverage of a position, potentially gaining more margin than intended.
Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/85
Found by
GimelSec
Summary
In Vault.reduce_position, some position tokens are sold and debt and margin are reduced. The remaining amount of debt and margin should maintain the same leverage as before. However, the reduced amount is wrongly calculated and as a result, the leverage is changed.
Vulnerability Detail
The calculation of reduced amount is implemented in Vault.reduce_position.
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L314
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L322
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L324
def reduce_position(
_position_uid: bytes32, _reduce_by_amount: uint256, _min_amount_out: uint256
) -> uint256:
…
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
…
Suppose that the debt amount is 9000 USDC and the margin amount is also 9000 USDC. And the amount_out_received is 9000 USDC. Let’s take a look of the calculation:
// margin_debt_ratio: uint256 = position.margin_amount * PRECISION / debt_amount
margin_debt_ration = 9000 * PRECISION / 9000 = PRECISION
// reduce_margin_by_amount: uint256 = (
// amount_out_received * margin_debt_ratio / PRECISION
// )
reduce_margin_by_amount = 9000 * PRECISION / PRECISION = 9000
// reduce_debt_by_amount: uint256 = amount_out_received - reduce_margin_by_amount
reduce_debt_by_amount = 9000 - 9000 = 0
// position.margin_amount -= reduce_margin_by_amount
position.margin_amount = 9000
We can find out that the reduced amount of margin is 9000 and the reduced amount of debt is 0. The leverage is changed.
I also wrote a test to demonstrate the issue. (The test case is different from the previous case)
def test_reduce_position(vault, owner, weth, usdc, mock_router, eth_usd_oracle):
eth_usd_oracle.set_answer(9000_00000000)
assert vault.swap_router() == mock_router.address
uid, amount_bought = vault.open_position(
owner, # account
weth, # position_token
2 * 10**18, # min_position_amount_out
usdc, # debt_token
9000 * 10**6, # debt_amount
9000 * 10**6, # margin_amount
)
position_before = vault.positions(uid)
position_amount_before = position_before[6]
assert position_amount_before == 2 * 10**18
margin_amount_before = position_before[3]
assert margin_amount_before == 9000 * 10**6
debt_amount_before = vault.debt(uid)
assert debt_amount_before == 9000 * 10**6
vault.reduce_position(uid, int(position_amount_before/3), 6000 * 10**6)
position_after = vault.positions(uid)
debt_amount_after = vault.debt_shares_to_amount(usdc.address, position_after[4])
print(debt_amount_after)
assert debt_amount_after == pytest.approx(6000 * 10**6, abs = 10000000) // @audit it would fail since debt_amount_after is 9000 * 10**6
margin_amount_after = position_after[3]
print(margin_amount_after)
assert margin_amount_after == pytest.approx(6000 * 10**6, abs = 1000000)
position_amount_after = position_after[6]
Besides, I want to mention that the usage of pytest.approx is incorrect in test_reduce_position.
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/tests/vault/test_reduce_position.py#L56
assert debt_amount_after == pytest.approx(6000 * 10**6, 10000000)
If the approximation means 6000 * 10 ** 6 ± 10000000, it should be pytest.approx(6000 * 10**6, abs=10000000). On the other hand, pytest.approx(6000 * 10**6, 10000000) considers numbers within a relative tolerance of the expected value. The approximation 6000 * 10 ** 6 ± (6000 * 10**6 * 10000000) has too large of a range of values.
Impact
The reduced amounts of debt and margin are wrongly calculated. The leverage is changed after reduce_position.
Code Snippet
https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L314 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L322 https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/contracts/margin-dex/Vault.vy#L324
Tool used
Manual Review
Recommendation
The correct calculation should be:
def reduce_position(
_position_uid: bytes32, _reduce_by_amount: uint256, _min_amount_out: uint256
) -> uint256:
…
- margin_debt_ratio: uint256 = position.margin_amount * PRECISION / debt_amount
+ margin_debt_ratio: uint256 = position.margin_amount * PRECISION / ( debt_amount + position.margin_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
self.positions[_position_uid] = position
log PositionReduced(position.account, _position_uid, position, amount_out_received)
return amount_out_received
Discussion
Unstoppable-DeFi
https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/3
maarcweiss
Fixed by using debt_amount + position.margin_amount instead of debt_amount
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 margin-to-debt ratio was calculated as margin divided by debt alone, instead of margin divided by the sum of debt and margin, so the proportion used to split the received amount was incorrect.
Impact
The leverage of the position is changed after a partial reduction, which could lead to incorrect debt and margin balances and potentially allow a user to manipulate their position's risk profile.
What the attacker needed
A position must exist; the position must be partially reduced
The fix
The calculation should use the sum of debt and margin as the denominator for the margin-to-debt ratio, and this was fixed by the project.
How it was found
- Open a position with a given debt and margin amount.
- Call reduce_position with a portion of the position amount.
- Observe that the debt amount after reduction is not reduced as expected, while the margin is over-reduced.
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.
In a leveraged trading position, you borrow money (debt) and put up your own money (margin). When you partially close the position, the code should reduce both your debt and your margin in the same proportion so your leverage stays the same. Here, the code used the wrong formula, so it reduced your margin too much and your debt too little, changing your risk level.
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.