MultiInvoker closableAmount the calculation logic is wrong
The MultiInvoker contract incorrectly calculates the closable amount by using the latest position's magnitude instead of the pending position's magnitude when updating the previous magnitude in a loop. This can cause the liquidation fee calculation to be wrong, potentially leading to reverts in some liquidation cases.
Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/41
Found by
bin2chen
Summary
in MultiInvoker._latest()
The incorrect use of previousMagnitude = latestPosition.magnitude() has led to an error in the calculation of closableAmount. This has caused errors in judgments that use this variable, such as _liquidationFee().
Vulnerability Detail
There are currently multiple places where the user's closable needs to be calculated, such as market.update().
The calculation formula is as follows in the code:
Market.sol
function _processPendingPosition(Context memory context, Position memory newPendingPosition) private {
context.pendingCollateral = context.pendingCollateral
.sub(newPendingPosition.fee)
.sub(Fixed6Lib.from(newPendingPosition.keeper));
context.closable = context.closable
.sub(context.previousPendingMagnitude
.sub(newPendingPosition.magnitude().min(context.previousPendingMagnitude)));
@> context.previousPendingMagnitude = newPendingPosition.magnitude();
if (context.previousPendingMagnitude.gt(context.maxPendingMagnitude))
context.maxPendingMagnitude = newPendingPosition.magnitude();
}
It will loop through pendingPostion, and each loop will set the variable context.previousPendingMagnitude = newPendingPosition.magnitude(); to be used as the basis for the calculation of the next pendingPostion.
closableAmount is also calculated in MultiInvoker._latest(). The current implementation is as follows:
function _latest(
IMarket market,
address account
) internal view returns (Position memory latestPosition, Fixed6 latestPrice, UFixed6 closableAmount) {
// load latest price
OracleVersion memory latestOracleVersion = market.oracle().latest();
latestPrice = latestOracleVersion.price;
IPayoffProvider payoff = market.payoff();
if (address(payoff) != address(0)) latestPrice = payoff.payoff(latestPrice);
// load latest settled position
uint256 latestTimestamp = latestOracleVersion.timestamp;
latestPosition = market.positions(account);
closableAmount = latestPosition.magnitude();
UFixed6 previousMagnitude = closableAmount;
// scan pending position for any ready-to-be-settled positions
Local memory local = market.locals(account);
for (uint256 id = local.latestId + 1; id <= local.currentId; id++) {
// load pending position
Position memory pendingPosition = market.pendingPositions(account, id);
pendingPosition.adjust(latestPosition);
// virtual settlement
if (pendingPosition.timestamp <= latestTimestamp) {
if (!market.oracle().at(pendingPosition.timestamp).valid) latestPosition.invalidate(pendingPosition);
latestPosition.update(pendingPosition);
previousMagnitude = latestPosition.magnitude();
closableAmount = previousMagnitude;
// process pending positions
} else {
closableAmount = closableAmount
.sub(previousMagnitude.sub(pendingPosition.magnitude().min(previousMagnitude)));
@> previousMagnitude = latestPosition.magnitude();
}
}
}
This method also loops through pendingPosition, but incorrectly uses latestPosition.magnitude() to set previousMagnitude, previousMagnitude = latestPosition.magnitude();.
The correct way should be previousMagnitude = currentPendingPosition.magnitude() like market.sol.
This mistake leads to an incorrect calculation of closableAmount.
Impact
The calculation of closableAmount is incorrect, which leads to errors in the judgments that use this variable, such as _liquidationFee().
Code Snippet
Tool used
Manual Review
Recommendation
function _latest(
IMarket market,
address account
) internal view returns (Position memory latestPosition, Fixed6 latestPrice, UFixed6 closableAmount) {
// load latest price
OracleVersion memory latestOracleVersion = market.oracle().latest();
latestPrice = latestOracleVersion.price;
IPayoffProvider payoff = market.payoff();
if (address(payoff) != address(0)) latestPrice = payoff.payoff(latestPrice);
// load latest settled position
uint256 latestTimestamp = latestOracleVersion.timestamp;
latestPosition = market.positions(account);
closableAmount = latestPosition.magnitude();
UFixed6 previousMagnitude = closableAmount;
// scan pending position for any ready-to-be-settled positions
Local memory local = market.locals(account);
for (uint256 id = local.latestId + 1; id <= local.currentId; id++) {
// load pending position
Position memory pendingPosition = market.pendingPositions(account, id);
pendingPosition.adjust(latestPosition);
// virtual settlement
if (pendingPosition.timestamp <= latestTimestamp) {
if (!market.oracle().at(pendingPosition.timestamp).valid) latestPosition.invalidate(pendingPosition);
latestPosition.update(pendingPosition);
previousMagnitude = latestPosition.magnitude();
closableAmount = previousMagnitude;
// process pending positions
} else {
closableAmount = closableAmount
.sub(previousMagnitude.sub(pendingPosition.magnitude().min(previousMagnitude)));
- previousMagnitude = latestPosition.magnitude();
+ previousMagnitude = pendingPosition.magnitude();
}
}
}
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
panprog commented:
valid medium, can cause MultiInvoker liquidation to revert in some cases
nevillehuang
@kbrizzle @arjun-io could you kindly review this issue too?
kbrizzle
Resolved as a side effect of: https://github.com/equilibria-xyz/perennial-v2/pull/165. (Deprecation of the Liquidate action from the MultiInvoker)
panprog
Mitigation Review:
Fixed. Calculation of previousMagnitude and closable is no longer necessary as there is no liquidation action in MultiInvoker anymore.
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 code mistakenly assigns the latest position's magnitude to previousMagnitude instead of the current pending position's magnitude, unlike the correct implementation in Market.sol.
Impact
The incorrect closableAmount can cause errors in judgments that use it, such as _liquidationFee(), and may cause MultiInvoker liquidation to revert in some cases.
What the attacker needed
MultiInvoker used for liquidation
The fix
The issue was fixed as a side effect of deprecating the Liquidate action from MultiInvoker, making the calculation unnecessary.
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 smart contract, a variable called closableAmount is used to decide how much of a position can be closed. The code updates a helper variable incorrectly inside a loop, so the closable amount becomes wrong. This can make the liquidation fee calculation incorrect and sometimes cause the liquidation to fail.
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.