Vault.settle(account=coordinator) will lose profitShares
The Vault.settle function credits profit shares to the coordinator by writing directly to storage, but does not update the in-memory [redacted].shares. If the settlement account is the coordinator, the final save overwrites the credited shares with the old value, causing the coordinator to lose profit shares.
Source: https://github.com/sherlock-audit/2025-01-perennial-v2-4-update-judging/issues/24
Found by
0x1982us
Summary
Vault.settle() turns a portion of the profit into profitShares for coordinator.
Use method _credit() to save profitShares to storage. But it doesn't update the memory variable context.local.shares.
It doesn't take into account the case where account == coordinator.
This way, if you maliciously specify that the settlement account is coordinator, settle() will end up overwriting the new value with the old value in memory.
Resulting in loss of profitShares.
Root Cause
/Vault.sol#L390
settle() call _credit()
function _settle(Context memory context, address account) private {
...
while (
context.global.current > context.global.latest &&
context.latestTimestamp >= (nextCheckpoint = _checkpoints[context.global.latest + 1].read()).timestamp
) {
// process checkpoint
UFixed6 profitShares;
(context.mark, profitShares) = nextCheckpoint.complete(
context.mark,
context.parameter,
_checkpointAtId(context, nextCheckpoint.timestamp)
);
context.global.shares = context.global.shares.add(profitShares);
@=> _credit(coordinator, profitShares);
in Vault.sol#L424
save to storage but don't update context.local.shares
function _credit(address account, UFixed6 shares) internal virtual {
Account memory local = _accounts[account].read();
local.shares = local.shares.add(shares);
@=> _accounts[account].store(local);
}
but at the last _saveContext() save context.local.shares to storage, it will override _credit() value if account == coordinator
function _saveContext(Context memory context, address account) private {
@=> if (account != address(0)) _accounts[account].store(context.local);
_accounts[address(0)].store(context.global);
_checkpoints[context.currentId].store(context.currentCheckpoint);
mark = context.mark;
}
Internal Pre-conditions
No response
External Pre-conditions
No response
Attack Path
Example context[coordinator].local.shares = 0
- anyone call
settle(account = coordinator) - suppose profitShares = 10
- in
_credit()save storage_accounts[coordinator].shares = profitShares = 10, butcontext.local.sharesstill 0 - at last
_saveContext()will overwrite_accounts[coordinator].shares = context.local.shares = 0 - coordinator lose 10 shares
Impact
coordinator lose profitShares
PoC
No response
Mitigation
like Market.sol#_credit(), if coordinator == context.account , only change context.local.shares
- function _credit(address account, UFixed6 shares) internal virtual {
+ function _credit(Context memory context, address account, UFixed6 shares) internal virtual {
+ if (account == context.account) context.local.shares = context.local.shares.add(shares)
+ else {
Account memory local = _accounts[account].read();
local.shares = local.shares.add(shares);
_accounts[account].store(local);
+ }
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/equilibria-xyz/perennial-v2/pull/563
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
The _credit function updates storage but not the in-memory [redacted].shares, and _saveContext later overwrites storage with the stale memory value when the account is the coordinator.
Impact
The coordinator loses the profit shares that should have been credited.
What the attacker needed
None
The fix
The protocol team fixed this issue by updating the _credit function to modify [redacted].shares when the account matches the context account, as shown in the mitigation diff.
How it was found
- Anyone calls settle with account set to the coordinator address.
- The _credit function writes the profit shares to storage for the coordinator.
- The in-memory [redacted].shares remains unchanged (e.g., zero).
- The final _saveContext call overwrites the coordinator's stored shares with the stale memory value, losing the profit shares.
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.
When the vault settles, it gives a share of the profit to a special account called the coordinator. The code saves this new share amount to the database but forgets to update a temporary copy in memory. Later, it saves that old temporary copy back to the database, wiping out the new shares. So the coordinator ends up with less 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.