_loadContext() uses the wrong pendingGlobal
The strategy library loaded the per-account pending order instead of the market-wide pending order when computing the current, minimum and maximum position. This made those position values wrong and could cause the rebalance logic to compute incorrect targets in rare edge cases.
Source: https://github.com/sherlock-audit/2024-02-perennial-v2-3-judging/issues/17
Found by
bin2chen
Summary
StrategyLib._loadContext() is using the incorrect pendingGlobal, causing currentPosition, minPosition, and maxPosition to be incorrect, leading to incorrect rebalance operation.
Vulnerability Detail
In StrategyLib._loadContext(), there is a need to compute currentPosition, minPosition, and maxPosition.
The code as follows:
function _loadContext(
Registration memory registration
) private view returns (MarketStrategyContext memory marketContext) {
...
// current position
@> Order memory pendingGlobal = registration.market.pendings(address(this));
marketContext.currentPosition = registration.market.position();
marketContext.currentPosition.update(pendingGlobal);
marketContext.minPosition = marketContext.currentAccountPosition.maker
.unsafeSub(marketContext.currentPosition.maker
.unsafeSub(marketContext.currentPosition.skew().abs()).min(marketContext.closable));
marketContext.maxPosition = marketContext.currentAccountPosition.maker
.add(marketContext.riskParameter.makerLimit.unsafeSub(marketContext.currentPosition.maker));
}
The code above pendingGlobal = registration.market.pendings(address(this)); is wrong
It takes the address(this)'s pendingLocal.
The correct approach is to use pendingGlobal = registration.market.pending();.
Impact
Since pendingGlobal is wrong, currentPosition, minPosition and maxPosition are all wrong.
affects subsequent rebalance calculations, such as target.position etc.
rebalance does not work properly
Code Snippet
Tool used
Manual Review
Recommendation
function _loadContext(
Registration memory registration
) private view returns (MarketStrategyContext memory marketContext) {
...
// current position
- Order memory pendingGlobal = registration.market.pendings(address(this));
+ Order memory pendingGlobal = registration.market.pending();
marketContext.currentPosition = registration.market.position();
marketContext.currentPosition.update(pendingGlobal);
marketContext.minPosition = marketContext.currentAccountPosition.maker
.unsafeSub(marketContext.currentPosition.maker
.unsafeSub(marketContext.currentPosition.skew().abs()).min(marketContext.closable));
marketContext.maxPosition = marketContext.currentAccountPosition.maker
.add(marketContext.riskParameter.makerLimit.unsafeSub(marketContext.currentPosition.maker));
}
Discussion
sherlock-admin4
2 comment(s) were left on this issue during the judging contest.
panprog commented:
valid medium, it influences the rebalance process only in very rare edge cases
takarez commented:
the reason for it should have been said.
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/299
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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 called the per-account pending lookup with the contract's own address instead of the market-wide pending lookup, so the wrong pending order was applied to the position calculation.
Impact
Because the pending order was wrong, the current, minimum and maximum positions were all wrong, which affected later rebalance calculations such as the target position; a judge noted this only matters in very rare edge cases.
What the attacker needed
Vault strategy uses this library; rare edge case in pending orders
The fix
The code was changed to read the market-wide pending order instead of the per-account one, and the protocol team fixed it in a pull request that the lead senior watson signed off on.
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 smart contract keeps track of orders that are waiting to be processed. There are two kinds: orders for one specific account, and orders for the whole market. This code picked the account-level one when it needed the market-level one, so its idea of where the position stands was off, and the automatic rebalancing could act on wrong numbers.
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.