https://github.com/devZygo/zygoAuditHacken
When merging two positions, the protocol incorrectly updates the old position’s accFundingFeePerSize (the cumulative funding fee rate) to the new position’s stored value instead of the current global funding fee state. This allows attackers to exploit stale funding rate data, leading to systematic undercharging or overcharging of funding fees.
Impact
Protocol/User Fund Loss: Attackers can arbitrage the discrepancy between stale and current funding rates to drain protocol funds or unfairly profit.
Permanent Financial Imbalances: Incorrect fee calculations create unrecoverable losses for LPs or traders.
Deploy the PerpDex contract.
Open two positions:
Trigger a funding rate update (e.g., via market volatility), setting the global accFeePerSize to 300.
Merge Position A and B. Observe:
oldPosition.accFundingFeePerSize = newPosition.accFundingFeePerSize; // Sets to 200 instead of 300
After merging, the funding fee for the merged position is computed as:
fee = size * (currentAccFeePerSize - mergedAccFeePerSize) // 300 - 200 = 100
Protocol loses 100 units of fees per merged position.
Attacker gains 100 units of profit.
Actors:
Steps:
Global accFeePerSize = 100.
Alice opens Position A (accFundingFeePerSize = 100).
Market conditions push accFeePerSize to 200.
Opens Position B (accFundingFeePerSize = 200).
Merges Position B with Alice’s Position A.
Merged position’s accFundingFeePerSize is 200 (instead of current global rate 200).
Global accFeePerSize rises to 300.
Correct Liability: 300 - 100 = 200.
Actual Liability: 300 - 200 = 100.
Protocol loses 100 units per merged position.
Bob profits by avoiding 100 units of fees.