Slashing NativeVault will lead to locked ETH for the users
A flaw in the slashing and withdrawal logic of NativeVault causes users to receive less ETH than they are entitled to after a slashing event, with the remaining ETH permanently locked in the contract. An attacker cannot directly exploit this, but users lose funds when they withdraw after being slashed.
zanderbyte Follow with anonymousjoe, lanrebayode77 and 2 others
Originally disclosed on Code4rena
Submitted by zanderbyte, also found by anonymousjoe, lanrebayode77, sl1, and KupiaSec
https://github.com/code-423n4/2024-07-karak/blob/f5e52fdcb4c20c4318d532a9f08f7876e9afb321/src/NativeVault.sol#L238 https://github.com/code-423n4/2024-07-karak/blob/f5e52fdcb4c20c4318d532a9f08f7876e9afb321/src/NativeVault.sol#L277 https://github.com/code-423n4/2024-07-karak/blob/f5e52fdcb4c20c4318d532a9f08f7876e9afb321/src/NativeVault.sol#L348 https://github.com/code-423n4/2024-07-karak/blob/f5e52fdcb4c20c4318d532a9f08f7876e9afb321/src/NativeVault.sol#L512
The Karak protocol includes a slashing mechanism that allows the contract owner to penalize stakers by reducing their staked assets in the event of malicious activity by the operator. If a user with a staked balance of 32 ETH is subject to a 3 ETH slashing, they should ideally be able to withdraw the remaining 29 ETH. However, due to a flaw in the implementation, when the user attempts to fully withdraw their ETH, they are only able to withdraw less than the actual remaining amount, with some ETH becoming permanently locked in the protocol.
Specifically, if all share tokens are burned during the withdrawal process, the user cannot access the remaining locked ETH. This results in users receiving fewer ETH than they are entitled to, with the excess ETH becoming inaccessible and permanently locked within the protocol.
Proof of Concept
Consider the following simplified scenario, where Alice want to restake her 32 ETH into Karak protocol, using 1 validator and no rewards acumulated for simplicity. In this example, although Alice initially had 32 ETH staked and should be able to withdraw 29 ETH (after a 3 ETH slashing), she ends up receiving only 26 ETH. The remaining 3 ETH becomes permanently locked in the contract. The steps she need to perform are the following:
-
Staking and Initial Setup:
- Alice call
createNodeto point her withdrawal credentials to it - She then calls
validateWithdrawalCredentialsfollowed bystartSnapshotandvalidateSnapshotProofs. After these actions, the state will look like this:totalAssets = 32e18totalSupply = 32e18totalRestakedETH = 32e18(in her Node struct)
- Alice call
-
Slashing:
- A slashing event occurs for 3 ETH, redusing the
totalAssetsto 29e18.
- A slashing event occurs for 3 ETH, redusing the
-
Withdraw from Beacon Chain:
- Alice decides to withdraw all her ETH from the Beacon Chain, resulting in her node balance being 32 ETH.
- To withdraw her ETH from the node, she performs the following operations:
startSnapshot,validateSnapshotProofs,startWithdraw, andfinishWithdraw.
-
Starting Snapshot
- When
startSnapshotis executed,_transferToSlashStoreis called, slashing 3 ETH from her node and transferring them toSlashStore. This reducestotalRestakedETHto29e18in her Node struct and saves the new snapshot withnodeBalanceWei = 29e18.
- When
-
Validate snapshot proofs
- Alice calls
validateSnapshotProofsto validate her balance proofs:
- Alice calls
int256 balanceDeltaWei = self.validateSnapshotProof(
nodeOwner, validatorDetails, balanceContainer.containerRoot, balanceProofs[i]
);
balanceDeltaWei = -32e18(the difference betweennewBalanceWeiof 0 andprevBalanceWeiof 32e18)snapshot.remainigProofs = 0since Alice has only one validator- After validation
_updateSnapshotfinalize snapshot due to no remaining active validators, where the balance of the user will be decreased due to the following calculation:
int256 totalDeltaWei = int256(snapshot.nodeBalanceWei) + snapshot.balanceDeltaWei;
-
This results in
29e18 - 32e18 = -3e18, reducing Alice’s shares by 3e18. The state after execution will be:node.totalRestakedETH: 26e18node.withdrawableCreditedNodeETH: 29e18balanceOf(alice) = 28689655172413793104totalAssets = 26e18totalSupply = 28689655172413793104 -
Starting Withdrawal
- Alice calls
startWithdraw. There is a check to prevent a user from withdrawing more than they actually can, but in Alice’s situation, it will be less than what she should withdraw:
- Alice calls
if (weiAmount > withdrawableWei(msg.sender) - self.nodeOwnerToWithdrawAmount[msg.sender]) {
revert WithdrawMoreThanMax();
}
withdrawableWei(msg.sender)will return the minimum between:
function withdrawableWei(address nodeOwner) public view nodeExists(nodeOwner) returns (uint256) {
return
Math.min(convertToAssets(balanceOf(nodeOwner)), _state().ownerToNode[nodeOwner].withdrawableCreditedNodeETH);
}
convertToAssetsis calculated as:
(shares * (totalAssets + 1)) / (totalSupply + 1) which is:
(28689655172413793104 * (26e18 + 1)) / (28689655172413793104 + 1) = 26e18 while
_state().ownerToNode[nodeOwner].withdrawableCreditedNodeETH is 29e18 (the actual withdrawable wei after slashing)
-
Finishing Withdrawal:
finishWithdrawalsends the maximum withdrawableWei (26e18) to Alice, burning all her tokens and causing 3 ETH to remain stuck in the node.
Recommended Mitigation Steps
Due to the complexity and the current limitations in testing the implementation of the slashing mechanism, a precise fix is difficult to pinpoint. In the example provided above, the problem occurs at step 5 where _decreaseBalance is called again reducing the totalAssets by another 3e18 (the slashed amount). If totalAssets are not decreased again and stays 29e18 in the next step , withdrawableWei(msg.sender) would correctly return the minimum between 28999999999999999999 and 29000000000000000000.
However, to address the root cause of the issue, a better mechanism for handling slashing should be implemented and tested.
karan-andalusia (Karak) confirmed via duplicate issue #29
Great find!
The crux of this issue is that
nodeBalanceWeiis calculated after_transferToSlashStore()is called, sonode.nodeAddress.balancewill have already decreased before the unattributed balance can be added tototalRestakedETH.This causes a loss of funds for the node owner as he does not receive shares for the unattributed balance that was lost, as such, I believe high severity is appropriate.
This mitigation only burns the ETH that has already been credited to the user consequently avoiding this scenario.
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 node balance is calculated after transferring slashed ETH to the SlashStore, so the unattributed balance is not credited to the user, and then the balance is decreased again during snapshot validation, reducing totalAssets twice.
Impact
Users who are slashed and then withdraw receive less ETH than they should, and the remaining ETH is permanently locked in the protocol.
What the attacker needed
User has staked ETH in NativeVault; a slashing event occurs; user withdraws from Beacon Chain and completes withdrawal
The fix
The mitigation only burns the ETH that has already been credited to the user, avoiding the double reduction of totalAssets.
How it was found
- Alice creates a node and sets up staking with 32 ETH, then starts and validates a snapshot.
- A slashing event of 3 ETH occurs, reducing totalAssets to 29 ETH.
- Alice withdraws all ETH from the Beacon Chain, making her node balance 32 ETH.
- She starts a new snapshot, which transfers the 3 ETH slashed amount to SlashStore and sets nodeBalanceWei to 29 ETH.
- She validates snapshot proofs, which decreases her shares by 3 ETH and reduces totalAssets to 26 ETH.
- She starts and finishes withdrawal, receiving only 26 ETH instead of 29 ETH, leaving 3 ETH locked.
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 someone stakes ETH, they get shares representing their portion of the total pool. If part of their stake is taken away as a penalty (slashing), the total pool shrinks, so each share should be worth less. This report shows that after a penalty, the maths for converting shares back to ETH is done twice, so the user gets even less than they should, and the extra ETH is stuck forever.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.