Delegation not revoked on withdrawal allows reward and voting power inflation post-maturity
The withdraw function does not clear a staker's delegation when they fully withdraw, leaving historical delegation records intact. An attacker could repeatedly stake and withdraw after maturity to inflate a delegatee's apparent uptime and claim excessive rewards.
yaractf Follow with Ishenxx and nnamdi0482
Originally disclosed on Code4rena
Submitted by yaractf, also found by Ishenxx and nnamdi0482
Finding description and impact
Stakers delegate their voting power using the stake() function, which internally calls _delegate(receiver, delegatee) and records the delegatee via checkpointing at the current clock() (block number). Upon maturity (matureAt), the withdraw() function allows full withdrawal of tokens, including by the stakers.
function stake(uint256 amount, address receiver, address delegatee) public {
require(canStake || totalSupply() == 0, "Staking is disabled for private agent"); // Either public or first staker
address sender = _msgSender();
require(amount > 0, "Cannot stake 0");
require(IERC20(assetToken).balanceOf(sender) >= amount, "Insufficient asset token balance");
require(IERC20(assetToken).allowance(sender, address(this)) >= amount, "Insufficient asset token allowance");
IAgentNft registry = IAgentNft(agentNft);
uint256 virtualId = registry.stakingTokenToVirtualId(address(this));
require(!registry.isBlacklisted(virtualId), "Agent Blacklisted");
if (totalSupply() == 0) {
initialLock = amount;
}
registry.addValidator(virtualId, delegatee);
IERC20(assetToken).safeTransferFrom(sender, address(this), amount);
_mint(receiver, amount);
@> _delegate(receiver, delegatee);
_balanceCheckpoints[receiver].push(clock(), SafeCast.toUint208(balanceOf(receiver)));
}
However, the withdraw() function does not call _delegate(sender, address(0)) to clean up delegations when a user fully withdraws their stake. This leaves the previous delegation entry active, even though the staker now has zero voting power.
function withdraw(uint256 amount) public noReentrant {
address sender = _msgSender();
require(balanceOf(sender) >= amount, "Insufficient balance");
if ((sender == founder) && ((balanceOf(sender) - amount) < initialLock)) {
require(block.timestamp >= matureAt, "Not mature yet");
}
@>
_burn(sender, amount);
_balanceCheckpoints[sender].push(clock(), SafeCast.toUint208(balanceOf(sender)));
IERC20(assetToken).safeTransfer(sender, amount);
}
Once maturity is reached, a malicious user can exploit this flaw by:
- The user stakes tokens and delegates.
- Then immediately withdraws them when maturity.
- Repeats the cycle multiple times, possibly in the same block.
Each cycle leaves delegation entries intact while reducing the actual staked balance to zero.
The system uses getPastDelegates() in reward logic to compute historical rewards based on:
function getClaimableStakerRewards(
address account,
uint256 virtualId
) public view returns (uint256 totalClaimable, uint256 numRewards) {
Claim memory claim = _stakerClaims[account][virtualId];
numRewards = Math.min(LOOP_LIMIT + claim.rewardCount, getAgentRewardCount(virtualId));
IAgentVeToken veToken = IAgentVeToken(IAgentNft(agentNft).virtualLP(virtualId).veToken);
IAgentDAO dao = IAgentDAO(IAgentNft(agentNft).virtualInfo(virtualId).dao);
for (uint i = claim.rewardCount; i < numRewards; i++) {
AgentReward memory agentReward = getAgentReward(virtualId, i);
Reward memory reward = getReward(agentReward.rewardIndex);
@> address delegatee = veToken.getPastDelegates(account, reward.blockNumber);
uint256 uptime = dao.getPastScore(delegatee, reward.blockNumber);
uint256 stakedAmount = veToken.getPastBalanceOf(account, reward.blockNumber);
uint256 stakerReward = (agentReward.stakerAmount * stakedAmount) / agentReward.totalStaked;
stakerReward = (stakerReward * uptime) / agentReward.totalProposals;
totalClaimable += stakerReward;
}
}
As a result, repeated delegation records combined with zeroed balances allow the delegatee to appear historically more active, and to receive inflated uptime-based rewards; even though the stake was not present at the time.
Impact
- Reward distribution becomes exploitable by staking and undelegating rapidly after maturity.
- Delegatees receive excessive rewards due to inflated uptime scores based on
getPastDelegates(). - Governance power and monetary reward flows become decoupled from actual stake and participation.
- Fairness of the protocol is compromised; malicious users can game the system at the expense of honest stakers and voters.
Recommended mitigation steps
Update the withdraw() function to automatically clear a user’s delegation when their token balance drops to zero. This prevents the retention of voting power or reward eligibility after the staker has exited.
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
Impact
A malicious user could inflate a delegatee's uptime score and claim excessive rewards, compromising the fairness of reward distribution and governance.
What the attacker needed
Stake tokens and delegate; wait until maturity; repeat stake and withdraw
The fix
The withdraw function should automatically clear a user's delegation when their token balance drops to zero.
How it was found
- Stake tokens and delegate to a chosen address.
- After maturity, withdraw the staked tokens.
- Repeat the stake and withdraw cycle multiple times, possibly in the same block.
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 stake tokens, you can choose someone to delegate your voting power to. The system records this delegation, but when you withdraw all your tokens, it forgets to remove that record. An attacker can exploit this by staking and withdrawing repeatedly to make it look like their delegatee was active for longer, earning more rewards than deserved.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.