Mismatching funding fees can result in the protocol incurring a deficit or insolvency risk
When funding fees are calculated, if one side's fee is capped, the other side's fee is not adjusted accordingly, leading to a mismatch where the total funding fees paid by one side do not equal the fees received by the other. This discrepancy can cause the pool to overstate its total value, potentially allowing positions to be opened without sufficient collateral and risking insolvency.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/258
The protocol has acknowledged this issue.
Found by
mstpr-brainbot
Summary
When funding fees are calculated if it's high enough it can be capped for one side but the other side would not get the new adjusted funding fee. Result of it can end up for misplayed funding fees and even bad debt in some cases.
Vulnerability Detail
Assume there are: $150 totalLongOpenInterest $50 totalShortOpenInterest 10 seconds passed since the last interaction
Following the execution in MarketQueryProcess::getUpdateMarketFundingFeeRate() the following calculations will be done:
fundingRatePerSecond = 10^^-8
totalFundingFee = 150 * 10 * 10^^-8 = = 1.5 * 10^^-5
currentLongFundingFeePerQty = 1.5 * 10^^-5 / 150 = -10^^-7
shortFundingFeePerQtyDelta = 1.5 * 10^^-5 / 50 3 * 10 ^^-7 Assume the max cap is 2 * 10 ^^-7: We pick 210^-7 = 210^^-7
longFundingFeeRate = (-10^^-7 * 3600 / 10 )/ 10^^-5 = -3.6
shortFundingFeeRate = (2*10^^-7 * 3600 / 10) / 10^^-5 = 7.2
For short position that holds 50: realizedFundingFeeDelta = 50 * 2*10^^-7 = = 10^^-5
For long position that holds 150:
realizedFundingFeeDelta =
-150 * 10^^-7 =
= -1.5 * 10^^-5
As we can observe, longs pay 1.5 and shorts receive 1. There is a discrepancy of 0.5 in funding fees that are not paid to short users.
This discrepancy can lead to insolvency because of how the pool accounts for its total holdings. The pool's total value is calculated as the amount plus unsettledAmount, where unsettledAmount is essentially the accrued funding fees. If the long's 1.5 fee is accounted for as unsettled, the contract assumes this 1.5 will be paid back to shorts, so the protocol is always counting correctly in the long run. However, this assumption is incorrect because shorts will not receive the 1.5 funding fee; they will only receive 1 in our case. Therefore, the excess "0.5" accounted in the pool's total value is incorrect because it will never be returned by the other party.
Textual Proof of Concept: Assume the pool has 100 baseAmount and 10 unsettledFee, totaling 110 assets. Someone can open positions based on a value of 110, expecting that at the end of the day, when the unsettledFees are settled, 10 assets will be returned to the system. However, if the funding fee for the short party is capped, they will only receive 8 fees instead of 10. Consequently, the pool will incorrectly account for "2" assets.
Impact
Miscounting of pools total value. Positions that are opened will think there is enough funds but actually these fees will never returned by the other party, resulting a position opened without proper collateral. Hence, high.
Code Snippet
Tool used
Manual Review
Recommendation
If the maximum is picked then adjust the counter party's funding fee accordingly. Always give out the same funding fees for both parties. If longs pays 10 then shorts should receive the 10 and vice versa https://github.com/sherlock-audit/2024-05-elfi-protocol/blob/8a1a01804a7de7f73a04d794bf6b8104528681ad/elfi-perp-contracts/contracts/process/MarketQueryProcess.sol#L110-L161
Discussion
0xELFi02
Not a issue: Mechanically, it is neutral in the long term, and the mechanism balances the impact of funding fee imbalances.
nevillehuang
@0xELFi02 What exactly is the design choice here that makes it neutral in the long term to balance funding fee imbalance? Since it was not noted in the READ.ME, I believe this issue could be valid
Same comments applies for issue #33, #102, #258
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 funding fee calculation caps one side's fee without adjusting the other side's fee to maintain balance, causing a mismatch in the total funding fees exchanged.
Impact
The pool's total value is miscalculated, which could allow positions to be opened without proper collateral and may lead to insolvency.
What the attacker needed
Funding fee cap is reached; open interest imbalance
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 trading system, funding fees are periodic payments between traders with long and short positions. The bug is that when the system limits how much one side can pay or receive, it forgets to adjust the other side, so the amounts don't match. This makes the system think it has more money than it really does, which can lead to bad debt.
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.