userTotalStaked invariant will be broken due to vulnerable implementations in release()
The release() function updates a user's self or community stake amount but fails to update the userTotalStaked accounting variable, breaking the invariant. After a slash and full release, the user's withdrawable amount is limited by the stale userTotalStaked, causing an underflow and loss of funds.
Submitted by oakcobalt, also found by oakcobalt, Stormy, and 0xDING99YA
userTotalStaked invariant will be broken due to vulnerable implementations in release(). Users might lose funds due to underflow errors in withdraw methods.
Proof of Concept
According to the readme, userTotalStaked invariant should always hold true:
userTotalStaked[address] = selfStakes[address].amount + sum(communityStakes[address][x].amount for all x staked on by this address)
However, this can be broken in release() flow due to userTotalStaked is not updated together with selfStakes[address].amount or communityStakes[address][x].amount.
//id-staking-v2/contracts/IdentityStaking.sol
function release(
address staker,
address stakee,
uint88 amountToRelease,
uint16 slashRound
) external onlyRole(RELEASER_ROLE) whenNotPaused {
...
if (staker == stakee) {
...
selfStakes[staker].slashedAmount -= amountToRelease;
//@audit selfStakes[staker].amount is updated but `userTotalStaked` is not
|> selfStakes[staker].amount += amountToRelease;
} else {
...
communityStakes[staker][stakee].slashedAmount -= amountToRelease;
//@audit communityStakes[staker].amount is updated but `userTotalStaked` is not
|> communityStakes[staker][stakee].amount += amountToRelease;
}
...
For comparison, in other flows such as staking, withdrawing and slashing, userTotalStaked is always updated in sync with selfStakes/communityStakes.
POC:
- Alice
selfStakes()100000 ether andcommunityStakes()100000 at round 1. - Alice’s
selfStakeandcommunityStakeare slashed by 80% each. - Alice appealed and was released the full slashed amount. Alice’s staked balance is restored to 100000 ether each. But
userTotalStakedis not restored. - Alice’s unlocked but cannot withdraw the 100000x2 ether balance due to underflow. She can only withdraw 20000x2 ether.
See test below:
In id-staking-v2/test/IdentityStaking.ts, first add import { PANIC_CODES} from "@nomicfoundation/hardhat-chai-matchers/panic";.
Then copy this test inside describe("slashing/releasing/burning tests", function () {.
it.only("userTotalStaked is broken, user lose funds", async function(){
//Step2: Round1 - slash Alice's self and community stake of 80000 each
await this.identityStaking
.connect(this.owner)
.slash(
this.selfStakers.slice(0, 1),
this.communityStakers.slice(0, 1),
this.communityStakees.slice(0, 1),
80,
);
//Step2: Round1 - Alice's community/self stake is 20000 after slashing
expect(
(
await this.identityStaking.communityStakes(
this.communityStakers[0],
this.communityStakees[0],
)
).amount,
).to.equal(20000);
//Step2: Round1 - total slashed amount 80000 x 2
expect(await this.identityStaking.totalSlashed(1)).to.equal(160000);
//Step3: Round1 - Alice appealed and full slash amount is released 80000 x 2
await this.identityStaking
.connect(this.owner)
.release(this.selfStakers[0], this.selfStakers[0], 80000, 1);
await this.identityStaking
.connect(this.owner)
.release(this.communityStakers[0], this.communityStakees[0], 80000, 1);
//Step3: Round1 - After release, Alice has full staked balance 100000 x 2
expect((await this.identityStaking.selfStakes(this.selfStakers[0])).amount).to.equal(100000);
expect((await this.identityStaking.communityStakes(this.communityStakers[0],this.communityStakees[0])).amount).to.equal(100000);
expect(await this.identityStaking.totalSlashed(1)).to.equal(0);
// Alice's lock expired
await time.increase(twelveWeeksInSeconds + 1);
//Step4: Alice trying to withdraw 100000 x 2 from selfStake and communityStake. Tx reverted with underflow error.
await expect((this.identityStaking.connect(this.userAccounts[0]).withdrawSelfStake(100000))).to.be.revertedWithPanic(PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW);
await expect((this.identityStaking.connect(this.userAccounts[0]).withdrawCommunityStake(this.communityStakees[0],100000))).to.be.revertedWithPanic(PANIC_CODES.ARITHMETIC_UNDER_OR_OVERFLOW);
//Step4: Alice could only withdraw 20000 x 2. Alice lost 80000 x 2.
await this.identityStaking.connect(this.userAccounts[0]).withdrawSelfStake(20000);
await this.identityStaking.connect(this.userAccounts[0]).withdrawCommunityStake(this.communityStakees[0],20000);
})
Tools Used
Hardhat
Recommended Mitigation Steps
In release(), also update userTotalStaked.
Alex the Entreprenerd (judge) commented:
The warden has shown how, due to incorrect accounting,
userTotalStakedmay end up being less than intended, causing a loss of funds to users.Due to the impact, I agree with High Severity.
This PR fixes the
userTotalStakedinvariant (accounting error) here.
Status: Mitigation confirmed. Full details in reports from oakcobalt, Stormy and 0xDING99YA.
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 release() function updates selfStakes or communityStakes amounts without also updating the corresponding userTotalStaked value, breaking the invariant that the total equals the sum of individual stakes.
Impact
Users could lose funds because they cannot withdraw their full restored stake due to an underflow error, as demonstrated in the proof of concept.
What the attacker needed
User has staked funds; stake is slashed; slashed amount is released via release(); lock period expires
The fix
The release() function should also update userTotalStaked to keep it in sync with the individual stake amounts; the text states this was fixed in a later PR.
How it was found
- Alice stakes 100000 in self and community stakes at round 1.
- Alice's self and community stakes are slashed by 80% each.
- Alice appeals and the full slashed amount is released, restoring her stake balances but not userTotalStaked.
- After lock expiry, Alice tries to withdraw the full 100000 from each stake but the transaction reverts with an arithmetic underflow; she can only withdraw 20000 from each.
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 tracks how much each person has staked in two places: the individual stake record and a running total. When a slashed stake is released back, only the individual record is increased, so the running total stays too low. Later, when the person tries to withdraw their full stake, the contract subtracts from the too-low total and crashes, leaving them unable to access part of their money.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.