Skip to content
Smart contracts Audit finding Intermediate

Staking#_unstake removes votes from wrong person if msg.sender != owner

The unstake function removes voting power from the caller instead of the token owner when an approved third party unstakes on behalf of the owner. This can leave the caller with zero votes and their NFT permanently trapped, and allows manipulation of voting power.

cccz Follow with 0x52

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-11-frankendao-judging/issues/30

Found by

cccz, 0x52

Summary

Staking#_unstake allows any msg.sender to unstake tokens for any owner that has approved them. The issue is that even when msg.sender != owner the votes are removed from msg.sender instead of owner. The result is that the owner keeps their votes and msg.sender loses theirs. This could be abused to hijack or damage voting.

Vulnerability Detail

address owner = ownerOf(_tokenId);
if (msg.sender != owner && !isApprovedForAll[owner][msg.sender] && msg.sender != getApproved[_tokenId]) revert NotAuthorized();

Staking#_unstake allows any msg.sender to unstake tokens for any owner that has approved them.

uint lostVotingPower;
for (uint i = 0; i < numTokens; i++) {
    lostVotingPower += _unstakeToken(_tokenIds[i], _to);
}

votesFromOwnedTokens[msg.sender] -= lostVotingPower;
// Since the delegate currently has the voting power, it must be removed from their balance
// If the user doesn't delegate, delegates(msg.sender) will return self
tokenVotingPower[getDelegate(msg.sender)] -= lostVotingPower;
totalTokenVotingPower -= lostVotingPower;

After looping through _unstakeToken all accumulated votes are removed from msg.sender. The problem with this is that msg.sender is allowed to unstake tokens for users other than themselves and in these cases they will lose votes rather than the user who owns the token.

Example: User A and User B both stake tokens and have 10 votes each. User A approves User B to unstake their tokens. User B calls unstake for User A. User B is msg.sender and User A is owner. The votes should be removed from owner but instead are removed from msg.sender. The result is that after unstaking User B has a vote balance of 0 while still having their locked token and User B has a vote balance of 10 and their token back. Now User B is unable to unstake their token because their votes will underflow on unstake, permanently trapping their NFT.

Impact

Votes are removed incorrectly if msg.sender != owner. By extension this would forever trap msg.sender tokens in the contract.

Code Snippet

https://github.com/sherlock-audit/2022-11-frankendao/blob/main/src/Staking.sol#L427-L458

Tool used

Manual Review

Recommendation

Remove the ability for users to unstake for other users

Discussion

zobront

Fixed: https://github.com/Solidity-Guild/FrankenDAO/pull/14

jack-the-pug

Fix confirmed

The original report as published on Sherlock, by cccz. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 deducts voting power from msg.sender rather than from the token owner, even when msg.sender is an approved third party.

Impact

Votes are removed incorrectly when msg.sender is not the owner, which could permanently trap the caller's tokens in the contract and may be abused to hijack or damage voting.

What the attacker needed

User A approves User B to unstake; User B calls unstake for User A

The fix

The ability for users to unstake on behalf of others was removed, and the fix was confirmed.

How it was found

  1. User A and User B each stake tokens and have 10 votes.
  2. User A approves User B to unstake their tokens.
  3. User B calls unstake for User A's tokens.
  4. The contract removes votes from User B (msg.sender) instead of User A (owner).
  5. User B's vote balance becomes 0 while their own tokens remain locked.
  6. User B cannot unstake their own tokens because their votes would underflow.

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.

In this staking contract, users can let others unstake their tokens for them. But when someone else unstakes your token, the contract mistakenly takes the voting power away from that helper instead of from you. This can leave the helper unable to unstake their own tokens because their voting power goes negative.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by cccz

View all

104 more disclosed reports, best first.

More from FrankenDAO

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.