This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
GimelSec - reduce_margin_by_amount
in Vault.reduce_position
is wrongly calculated
#85
Labels
Fix Submitted
Fix to the issue has been submitted
High
A valid High severity issue
Reward
A payout will be made for this issue
Sponsor Confirmed
The sponsor acknowledged this issue is valid
Will Fix
The sponsor confirmed this issue will be fixed
GimelSec
high
reduce_margin_by_amount
inVault.reduce_position
is wrongly calculatedSummary
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
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:
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)
Besides, I want to mention that the usage of
pytest.approx
is incorrect intest_reduce_position
.https://github.com/sherlock-audit/2023-06-unstoppable/blob/main/unstoppable-dex-audit/tests/vault/test_reduce_position.py#L56
If the approximation means
6000 * 10 ** 6 ± 10000000
, it should bepytest.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 approximation6000 * 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:
The text was updated successfully, but these errors were encountered: