Skip to content
Business logic Audit finding Intermediate

`_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.

WatchPug Follow

Originally disclosed on Sherlock

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.

Solidity
// 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:

  1. skew(): 999/1000
  2. fundingMaker: 0.999 of the funding
  3. 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

https://github.com/sherlock-audit/2023-07-perennial/blob/main/perennial-v2/packages/perennial/contracts/types/Version.sol#L207-L215

Tool used

Manual Review

Recommendation

The correct formula to calculate the amount of funding belonging to the maker side should be:

MARKDOWN
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

The original report as published on Sherlock, by WATCHPUG. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

  1. Set long/major position to 1000, short/minor to 1, and maker to 1.
  2. Calculate skew as 999/1000.
  3. Observe that fundingMaker becomes 0.999 of the funding and fundingShort becomes 0.001.
  4. 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.

More by WatchPug

View all

197 more disclosed reports, best first.

More from Perennial V2 Update #1

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.