When malicious behavior occurs and DSS requests slashing against vault during 2 day period after SLASHING_WINDOW of 7 days is passed after staker initiates a withdrawal, token amount to be slashed is calculated to be higher than what it should be
The slashing amount is calculated from the vault's total assets, which includes tokens queued for withdrawal that should no longer be slashable after the slashing window. A DSS can therefore slash more tokens than intended, potentially taking funds from a staker who initiated a withdrawal before any malicious behaviour.
Submitted by rbserver, also found by MrValioBg and Shaheen
Impact
According to https://github.com/code-423n4/2024-07-karak?tab=readme-ov-file#stakers, Stakers can initiate a withdrawal, subject to a MIN_WITHDRAW_DELAY of 9 days, and DSS can slash any malicious behavior occurring before the withdrawal initiation for up to 7 days. Since MIN_WITHDRAW_DELAY equals SLASHING_WINDOW + SLASHING_VETO_WINDOW, the staker’s withdrawal should be safe without being associated with any malicious behavior and hence not slashable when the DSS does not request any slashing against the corresponding vault within the SLASHING_WINDOW of 7 days after such withdrawal is initiated.
uint256 public constant SLASHING_WINDOW = 7 days;
uint256 public constant SLASHING_VETO_WINDOW = 2 days;
uint256 public constant MIN_STAKE_UPDATE_DELAY = SLASHING_WINDOW + SLASHING_VETO_WINDOW;
uint256 public constant MIN_WITHDRAWAL_DELAY = SLASHING_WINDOW + SLASHING_VETO_WINDOW;
During the 2 day period after the SLASHING_WINDOW of 7 days is passed after the staker’s withdrawal is initiated, such as when just couple minutes are passed after such SLASHING_WINDOW is reached, the staker’s withdrawal cannot be finished but a malicious behavior can occur and the DSS can make a slashing request against the corresponding vault; in this situation, the staker’s withdrawal mentioned previously should not be subject to such slashing though. At that time, when the DSS calls the Core.requestSlashing function, which further calls the SlasherLib.requestSlashing and SlasherLib.fetchEarmarkedStakes functions, the token amount to be slashed is calculated as the DSS’s allowed slashing percentage of the vault’s totalAssets(), which includes the token amount corresponding to the staker’s withdrawal. Because the staker’s withdrawal should not be slashable, the token amount to be slashed actually should be the DSS’s allowed slashing percentage of a value that equals the vault’s totalAssets() minus the token amount corresponding to the staker’s withdrawal. Thus, the DSS can unfairly slash more underlying token amount from the vault than it should be allowed.
function requestSlashing(
CoreLib.Storage storage self,
IDSS dss,
SlashRequest memory slashingMetadata,
uint256 nonce
) external returns (QueuedSlashing memory queuedSlashing) {
validateRequestSlashingParams(self, slashingMetadata, dss);
uint256[] memory earmarkedStakes = fetchEarmarkedStakes(slashingMetadata);
queuedSlashing = QueuedSlashing({
dss: dss,
timestamp: uint96(block.timestamp),
operator: slashingMetadata.operator,
vaults: slashingMetadata.vaults,
earmarkedStakes: earmarkedStakes,
nonce: nonce
});
self.slashingRequests[calculateRoot(queuedSlashing)] = true;
...
}
function fetchEarmarkedStakes(SlashRequest memory slashingMetadata)
internal
view
returns (uint256[] memory earmarkedStakes)
{
earmarkedStakes = new uint256[](slashingMetadata.vaults.length);
for (uint256 i = 0; i < slashingMetadata.vaults.length; ++i) {
earmarkedStakes[i] = Math.mulDiv(
slashingMetadata.slashPercentagesWad[i],
IKarakBaseVault(slashingMetadata.vaults[i]).totalAssets(),
Constants.MAX_SLASHING_PERCENT_WAD
);
}
}
Moreover, when the MIN_WITHDRAW_DELAY of 9 days is passed after the staker’s withdrawal is initiated, the staker can call the Vault.finishRedeem function for finishing his withdrawal request. Since SLASHING_VETO_WINDOW is 2 days, the DSS can call the Core.finalizeSlashing function for finalizing its slashing request just after the MIN_WITHDRAW_DELAY of 9 days is passed after the initiation of the staker’s withdrawal if the DSS’s slashing request was made when just couple minutes were passed after the SLASHING_WINDOW for such withdrawal of the staker was reached. Because both the staker’s Vault.finishRedeem transaction and the DSS’s Core.finalizeSlashing transaction are sent at the similar time, a malicious miner can place and execute the DSS’s Core.finalizeSlashing transaction before the staker’s Vault.finishRedeem transaction. In this case, the staker’s withdrawal can be unfairly slashed by the DSS even though it should not be slashed.
function finishRedeem(bytes32 withdrawalKey)
external
nonReentrant
whenFunctionNotPaused(Constants.PAUSE_VAULT_FINISH_REDEEM)
{
(VaultLib.State storage state, VaultLib.Config storage config) = _storage();
WithdrawLib.QueuedWithdrawal memory startedWithdrawal = state.validateQueuedWithdrawal(withdrawalKey);
...
}
function validateQueuedWithdrawal(State storage self, bytes32 withdrawalKey)
internal
view
returns (WithdrawLib.QueuedWithdrawal memory qdWithdrawal)
{
qdWithdrawal = self.withdrawalMap[withdrawalKey];
...
if (qdWithdrawal.start + Constants.MIN_WITHDRAWAL_DELAY > block.timestamp) {
revert MinWithdrawDelayNotPassed();
}
}
function finalizeSlashing(SlasherLib.QueuedSlashing memory queuedSlashing)
external
nonReentrant
whenFunctionNotPaused(Constants.PAUSE_CORE_FINALIZE_SLASHING)
{
_self().finalizeSlashing(queuedSlashing);
...
}
function finalizeSlashing(CoreLib.Storage storage self, QueuedSlashing memory queuedSlashing) external {
...
if (queuedSlashing.timestamp + Constants.SLASHING_VETO_WINDOW > block.timestamp) {
revert MinSlashingDelayNotPassed();
}
...
}
Proof of Concept
The following steps can occur for the first described scenario where the DSS unfairly slashes more underlying token amount from the vault than it should be allowed.
-
The vault holds
100underlying tokens and has only one staker who owns100shares. -
On Day 0, the staker initiates his withdrawal for redeeming
50shares. -
During the
SLASHING_WINDOWof 7 days between Day 0 and Day 7, no malicious behavior occurs, and the DSS does not request any slashing against the vault.- Therefore, the staker’s withdrawal is safe and should not be slashable, and the staker should receive
50 * 100 / 100 = 50underlying tokens for his withdrawal.
- Therefore, the staker’s withdrawal is safe and should not be slashable, and the staker should receive
-
On Day 8, a malicious behavior occurs, and the DSS with a 50% allowed slashing percentage requests to slash the vault.
- The token amount to be slashed by the DSS is calculated to be
50% * 100 = 50underlying tokens. - However, because the staker’s withdrawal initiated on Day 0 should not be subject to such slashing, the token amount to be slashed by the DSS should equal
50% * (100 - 50) = 25underlying tokens.
- The token amount to be slashed by the DSS is calculated to be
-
On Day 9, the
MIN_WITHDRAW_DELAYof 9 days is passed after the initiation of the staker’s withdrawal so the staker finishes his withdrawal and does receive50 * 100 / 100 = 50underlying tokens from the vault. -
On Day 10, the DSS finalizes the slashing and slashes
50underlying tokens calculated on Day 8, which is more than25underlying tokens that it should slash, from the vault. Therefore, the DSS unfairly slashes more underlying tokens from the vault than it should be allowed.
The following steps can occur for the second described scenario where the staker’s withdrawal can be unfairly slashed by the DSS even though it should not be slashed.
-
The vault holds
150underlying tokens and has two stakers in which Staker A owns100shares and Staker B owns50shares. -
On Day 0, Staker A initiates his withdrawal for redeeming
100shares. -
During the
SLASHING_WINDOWof 7 days between Day 0 and Day 7, no malicious behavior occurs, and the DSS does not request any slashing against the vault.- Therefore, Staker A’s withdrawal is safe and should not be slashable, and Staker A should receive
100 * 150 / (100 + 50) = 100underlying tokens for his withdrawal.
- Therefore, Staker A’s withdrawal is safe and should not be slashable, and Staker A should receive
-
A malicious behavior occurs when 3 minutes are passed after Day 7 starts, and the DSS with a 50% allowed slashing percentage requests to slash the vault when 5 minutes are passed after Day 7 starts.
- The token amount to be slashed by the DSS is calculated to be
50% * 150 = 75underlying tokens. - However, because Staker A’s withdrawal initiated on Day 0 should not be subject to such slashing, the token amount to be slashed by the DSS should equal
50% * (150 - 100) = 25underlying tokens.
- The token amount to be slashed by the DSS is calculated to be
-
On Day 9, the
MIN_WITHDRAW_DELAYof 9 days is passed after the initiation of Staker A’s withdrawal so the staker calls theVault.finishRedeemfunction for finishing his withdrawal. -
When 5 minutes are passed after Day 9 starts, the DSS calls the
Core.finalizeSlashingfunction for finalizing the slashing. -
Both Staker A’s
Vault.finishRedeemtransaction and the DSS’sCore.finalizeSlashingtransaction are sent at the similar time, a malicious miner places and executes the DSS’sCore.finalizeSlashingtransaction before Staker A’sVault.finishRedeemtransaction. -
When the DSS’s
Core.finalizeSlashingtransaction is executed, the DSS slashes75underlying tokens calculated in Step 4, which is more than25underlying tokens that it should slash, from the vault. -
When Staker A’s
Vault.finishRedeemtransaction is executed, Staker A receives100 * (150 - 75) / (100 + 50) = 50underlying tokens, which is less than100underlying tokens that it should receive for his withdrawal, from the vault. Therefore, Staker A’s withdrawal is unfairly slashed by the DSS even though it should not be slashed.
Recommended Mitigation Steps
One way to mitigate this issue is to update the Vault.finishRedeem function to allow the staker’s withdrawal to be finished after the SLASHING_WINDOW of 7 days is passed after such withdrawal is initiated if the DSS does not request any slashing against the corresponding vault within such SLASHING_WINDOW and only enforce the MIN_WITHDRAW_DELAY of 9 days on the withdrawal if the DSS has requested to slash the corresponding vault within such SLASHING_WINDOW.
Ideally the protocol doesn’t provide any financial advantage (to any individual) for slashing any operator. So if a staker provides a quite low priority gas fees then it might stay in the mempool for long enough to be slashed for activity which it wasn’t responsible for. So imo the staker’s should provide enough priority gas fees for
finishRedeemto prevent the aforementioned. cc: @dewpe
Would re-rate to a non-issue.
From a technical standpoint, the user can withdraw on the exact second that the 9th day hits but they could delay it forever if they really wanted. It’s ultimately up to them to finish it in a timely manner. On the frontend we can state “hey a slashing has started so withdraw your funds right when the 9th day hits”. In normal operations, a DSS would be written so that it slashes users from only the active operator set and that code can be reviewed by stakers and operators before depositing and delegating. If you let them out at 7 days if there isn’t a slashing request then you may run into some timing games if there is a slashing request occurs then.
From a philosophical standpoint, DSS contracts would be audited and agreeing to allocate assets to them or to an operator that allocates to them means you agree to whatever slashing conditions the DSS implements. In theory, the DSS can slash you for 100% right when you register if it wanted.
MiloTruck (judge) decreased severity to Medium and commented:
Agree that the second scenario is unrealistic - it is entirely the staker’s responsibility to ensure they withdraw their funds on time. If the staker calls
finishRedeem()with a priority fee so low that his transaction remains in the mempool for an extended period of time, it is considered a user mistake.However, the warden does make a valid point that calculating the amount of assets to slash based on
totalAssets()will include assets queued for withdrawal.@dewpe Isn’t it an issue if the DSS ends up slashing a higher percentage of the remaining assets? For example:
- Vault has 100 tokens.
- User requests a withdrawal of 50 tokens.
- DSS calls
requestSlashing()to slash 50% of assets, which is calculated as 50 tokens.- User withdraws 50 tokens.
finalizeSlashing()slashes the remaining 50 tokens, leaving 0 tokens in the vault.Shouldn’t the calculation in
requestSlashing()exclude assets in pending withdrawals that have passedSLASHING_WINDOW? Although in practice this isn’t easy to implement.
@MiloTruck, the example seems apt and to mitigate it we are thinking of computing
earmarkedStakesduringfinalizeSlashing, this way all the stakers that are staked will only be slashed.
Fixed.
This mitigation computes the slashing amount in finalize slashing.
Status: Mitigation confirmed. Full details in reports from 0xCiphky, KupiaSec and sl1.
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 slashing amount is computed from the vault's total assets at request time without excluding assets in withdrawals that have passed the slashing window.
Impact
A DSS could slash more tokens than intended, potentially reducing or eliminating a staker's withdrawal; the second scenario involving a malicious miner is considered unrealistic by the judge, but the over-slashing calculation was accepted and fixed.
What the attacker needed
A vault with staked assets; a staker initiates a withdrawal; no slashing request during the 7-day window; a DSS requests slashing after the window
The fix
The slashing amount should be computed at finalization time so that only currently staked assets are slashed; the project says this was fixed.
How it was found
- The vault holds 100 tokens and one staker owns 100 shares.
- On Day 0 the staker initiates a withdrawal for 50 shares.
- No malicious behaviour occurs and no slashing is requested during the 7-day slashing window.
- On Day 8 malicious behaviour occurs and the DSS requests a 50% slash, calculated as 50 tokens instead of 25.
- On Day 9 the staker finishes the withdrawal and receives 50 tokens.
- On Day 10 the DSS finalizes the slash of 50 tokens, more than the 25 that should be allowed.
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 you ask to take your money out of a staking vault, there is a waiting period. During part of that time, the vault can still be punished (slashed) for bad behaviour. The report says the punishment is calculated on the whole vault balance, including money already set aside for withdrawal, so the punishment can be too big and can even eat into your withdrawal.
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.