`_accumulateFunding()` maker will get the wrong amount of funding fee
The funding fee calculation for the maker side in `_accumulateFunding()` is incorrect, causing the maker to receive the entire funding fee instead of a proportional share when the skew between long and short positions exceeds the maker's position. An attacker could exploit this to unfairly gain funding fees at the expense of other participants.
Source: https://github.com/sherlock-audit/2023-07-perennial-judging/issues/139
Found by
WATCHPUG
Vulnerability Detail
The formula that calculates the amount of funding in Version#_accumulateFunding() on the maker side is incorrect. This leads to an incorrect distribution of funding between the minor and the maker's side.
// Redirect net portion of minor's side to maker
if (fromPosition.long.gt(fromPosition.short)) {
fundingValues.fundingMaker = fundingValues.fundingShort.mul(Fixed6Lib.from(fromPosition.skew().abs()));
fundingValues.fundingShort = fundingValues.fundingShort.sub(fundingValues.fundingMaker);
}
if (fromPosition.short.gt(fromPosition.long)) {
fundingValues.fundingMaker = fundingValues.fundingLong.mul(Fixed6Lib.from(fromPosition.skew().abs()));
fundingValues.fundingLong = fundingValues.fundingLong.sub(fundingValues.fundingMaker);
}
PoC
Given:
- long/major: 1000
- short/minor: 1
- maker: 1
Then:
- skew(): 999/1000
- fundingMaker: 0.999 of the funding
- fundingShort: 0.001 of the funding
While the maker only matches for 1 of the major part and contributes to half of the total short side, it takes the entire funding.
Impact
Code Snippet
Tool used
Manual Review
Recommendation
The correct formula to calculate the amount of funding belonging to the maker side should be:
fundingMakerRatio = min(maker, major - minor) / min(major, minor + maker)
fundingMaker = fundingMakerRatio * fundingMinor
Discussion
sherlock-admin
2 comment(s) were left on this issue during the judging contest.
141345 commented:
m
panprog commented:
medium because incorrect result only starts appearing if abs(long-short) > maker and the larger the difference, the more incorrect the split of funding is. But this situation is exceptional case, most of the time abs(long-short) < maker due to efficiency and liquidity limits
arjun-io
We'd like to re-open this as it does appear to be a valid issue. Medium severity seems correct here
arjun-io
Fixed: https://github.com/equilibria-xyz/perennial-v2/pull/64
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 formula for the maker's funding share incorrectly uses the absolute skew multiplied by the minor side's funding, rather than a ratio based on the maker's actual contribution relative to the total matched amount.
Impact
The maker could receive an unfairly large portion of funding fees, potentially causing losses to other participants; the issue is considered medium severity because it only occurs when the absolute difference between long and short exceeds the maker's position.
What the attacker needed
Skew exceeds maker position
How it was found
- Set long/major position to 1000, short/minor to 1, and maker to 1.
- Calculate skew as 999/1000.
- Observe that fundingMaker becomes 0.999 of the funding and fundingShort becomes 0.001.
- Note that the maker, despite only matching 1 of the major part and contributing to half of the short side, receives the entire funding.
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 to keep prices aligned. The code that splits these fees between different participant groups (makers and takers) used a flawed formula, so one group got too much. This could let someone manipulate the system to earn more fees than they should.
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.