Unstaking does not update the mapping sETHUserClaimForKnot
When a user unstakes part of their sETH, the contract pays out the ETH owed but does not update the record of how much has already been claimed. The user is then left with a claim record based on the larger original stake, so they can claim little or nothing on their remaining stake.
Submitted by HE1M, also found by 9svR6w
If a user stakes some sETH, and after some time decides to unstake some amount of sETH, later s/he will not be qualified or be less qualified to claim ETH on the remaining staked sETH.
Proof of Concept
Suppose Alice stakes 5 sETH by calling stake(...).
https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/syndicate/Syndicate.sol#L203
So, we will have:
sETHUserClaimForKnot[BLS][Alice] = (5 * 10^18 * accumulatedETHPerFreeFloatingShare) / PRECISIONsETHStakedBalanceForKnot[BLS][Alice] = 5 * 10^18sETHTotalStakeForKnot[BLS] += 5 * 10^18
Later, Alice decides to unstake 3 sETH by calling unstake(...).
https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/syndicate/Syndicate.sol#L245
So, all ETH owed to Alice will be paid: https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/syndicate/Syndicate.sol#L257
Then, we will have:
sETHUserClaimForKnot[BLS][Alice] = (5 * 10^18 * accumulatedETHPerFreeFloatingShare) / PRECISIONsETHStakedBalanceForKnot[BLS][Alice] = 2 * 10^18sETHTotalStakeForKnot[BLS] -= 3 * 10^18
It is clear that the mapping sETHStakedBalanceForKnot is decreased as expected, but the mapping sETHUserClaimForKnot is not changed. In other words, the mapping sETHUserClaimForKnot is still holding the claimed amount based on the time 5 sETH were staked.
If, after some time, the ETH is accumulated per free floating share for the BLS public key that Alice was staking for, Alice will be qualified to some more ETH to claim (because she has still 2 sETH staked).
If Alice unstakes by calling unstake(...) or claim ETH by calling claimAsStaker(...), in both calls, the function calculateUnclaimedFreeFloatingETHShare will be called to calculate the amount of unclaimed ETH:
https://github.com/code-423n4/2022-11-stakehouse/blob/4b6828e9c807f2f7c569e6d721ca1289f7cf7112/contracts/syndicate/Syndicate.sol#L652
In this function, we will have:
stakedBal = sETHStakedBalanceForKnot[BLS][Alice]= 2 * 10^18userShare = (newAccumulatedETHPerShare * stakedBal) / PRECISION
The return value which is unclaimed ETH will be:
userShare - sETHUserClaimForKnot[BLS][Alice] =
(newAccumulatedETHPerShare * 2 * 10^18) / PRECISION - (5 * 10^18 * accumulatedETHPerFreeFloatingShare) / PRECISION
This return value is not correct (it is highly possible to be smaller than 0, and as a result Alice can not claim anything), because the claimed ETH is still based on the time when 5 sETH were staked, not on the time when 2 sETH were remaining/staked.
The vulnerability is that during unstaking, the mapping sETHUserClaimForKnot is not updated to the correct value. In other words, this mapping is updated in _claimAsStaker, but it is updated based on 5 sETH staked, later when 3 sETH are unstaked, this mapping should be again updated based on the remaing sETH (which is 2 sETH).
As a result, Alice can not claim ETH or she will qualify for less amount.
Recommended Mitigation Steps
The following line should be added on line 274:
sETHUserClaimForKnot[_blsPubKey][msg.sender] =
(accumulatedETHPerShare * sETHStakedBalanceForKnot[_blsPubKey][msg.sender]) / PRECISION
vince0656 (Stakehouse) confirmed
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 unstake function pays out owed ETH but fails to reset the user's claim checkpoint to match their reduced staked balance.
Impact
A user who unstakes part of their stake could be unable to claim, or claim less, ETH on the sETH they still have staked.
What the attacker needed
User has staked sETH; user unstakes part of the stake
The fix
The unstake function should update the user's claim checkpoint to the accumulated ETH per share multiplied by their remaining staked balance.
How it was found
- Alice stakes 5 sETH, which sets her claim record and staked balance.
- Alice unstakes 3 sETH, which pays her owed ETH and reduces her staked balance to 2 sETH.
- Her claim record is not updated and still reflects 5 sETH.
- When more ETH accumulates, her unclaimed amount is calculated from 2 sETH minus the old 5 sETH claim, giving a negative or tiny result.
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 user puts in some tokens and the contract keeps a note of how much they have already earned. When they take some tokens back out, the contract pays what they are owed but forgets to update that note. The note still says they earned a lot, so later the contract thinks they have already been paid and gives them little or nothing for the tokens still in.
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.