Lack of access control in AgentNftV2::addValidator() enables unauthorized validator injection and causes reward accounting inconsistencies
The addValidator function on the AgentNftV2 contract had no access control, so anyone could add a validator to a predicted upcoming virtual ID. Because mint later adds the founder as a validator again, the duplicate entry inflated the shared validator pool rewards, which could be withdrawn and leave the contract short of funds for legitimate reward claims.
joicygiore Follow with Astroboy, BRONZEDISC and 18 others
Originally disclosed on Code4rena
Submitted by joicygiore, also found by Astroboy, BRONZEDISC, classic-k, CoheeYang, Damboy, DanielTan_MetaTrust, danzero, debo, gmh5225, gregom, hail_the_lord, hecker_trieu_tien, hirosyama, holtzzx, io10, levi_104, Olugbenga, PotEater, shui, and testnate
The AgentNftV2::addValidator() function lacks any form of access control. While the mint() function of AgentNftV2 does enforce role-based restrictions (MINTER_ROLE), a malicious actor can exploit the AgentFactoryV2::executeApplication() logic to predict and obtain the next virtualId through a call to IAgentNft(nft).nextVirtualId().
By doing so, an attacker can preemptively call addValidator() and append a validator to _validators[virtualId]. Later, when AgentNftV2::mint() is called, it invokes _addValidator() again, causing the validator to be added a second time. This results in a duplicate validator entry for the same virtual ID.
// AgentNftV2::mint()
function mint(
uint256 virtualId,
address to,
string memory newTokenURI,
address payable theDAO,
address founder,
uint8[] memory coreTypes,
address pool,
address token
) external onlyRole(MINTER_ROLE) returns (uint256) {
require(virtualId == _nextVirtualId, "Invalid virtualId");
_nextVirtualId++;
_mint(to, virtualId);
_setTokenURI(virtualId, newTokenURI);
VirtualInfo storage info = virtualInfos[virtualId];
info.dao = theDAO;
info.coreTypes = coreTypes;
info.founder = founder;
IERC5805 daoToken = GovernorVotes(theDAO).token();
info.token = token;
VirtualLP storage lp = virtualLPs[virtualId];
lp.pool = pool;
lp.veToken = address(daoToken);
_stakingTokenToVirtualId[address(daoToken)] = virtualId;
@> _addValidator(virtualId, founder);
@> _initValidatorScore(virtualId, founder);
return virtualId;
}
// AgentNftV2::addValidator()
// Expected to be called by `AgentVeToken::stake()` function
function addValidator(uint256 virtualId, address validator) public {
if (isValidator(virtualId, validator)) {
return;
}
_addValidator(virtualId, validator);
_initValidatorScore(virtualId, validator);
}
// ValidatorRegistry::_addValidator()
function _addValidator(uint256 virtualId, address validator) internal {
_validatorsMap[virtualId][validator] = true;
@> _validators[virtualId].push(validator);
emit NewValidator(virtualId, validator);
}
// AgentFactoryV2::executeApplication()
function executeApplication(uint256 id, bool canStake) public noReentrant {
// This will bootstrap an Agent with following components:
// C1: Agent Token
// C2: LP Pool + Initial liquidity
// C3: Agent veToken
// C4: Agent DAO
// C5: Agent NFT
// C6: TBA
// C7: Stake liquidity token to get veToken
// SNIP...
// C5
@> uint256 virtualId = IAgentNft(nft).nextVirtualId();
@> IAgentNft(nft).mint(
virtualId,
_vault,
application.tokenURI,
dao,
application.proposer,
application.cores,
lp,
token
);
application.virtualId = virtualId;
// SNIP...
}
The reward mechanism in AgentRewardV2 relies on iterating over validator lists to compute and distribute rewards. If a validator is duplicated due to the aforementioned issue, the reward distribution logic - particularly in _distributeValidatorRewards() — recalculates and overwrites the validator’s rewards.
This does not break reward allocation for individual validators due to overwriting. However, the shared variable validatorPoolRewards accumulates validator-level residuals multiple times due to the duplicated validator entries. As a result, validatorPoolRewards can become overstated relative to the actual token amount deposited.
When withdrawValidatorPoolRewards() is eventually called, it transfers this erroneously accumulated excess amount to the designated address. This reduces the contract’s balance below what is required to cover valid validator rewards, ultimately resulting in reward claim failures unless someone manually tops up the shortfall.
// AgentRewardV2::distributeRewards()
function distributeRewards(uint256 amount) public onlyGov returns (uint32) {
require(amount > 0, "Invalid amount");
@> IERC20(rewardToken).safeTransferFrom(_msgSender(), address(this), amount);
RewardSettingsCheckpoints.RewardSettings memory settings = getRewardSettings();
uint256 protocolShares = _distributeProtocolRewards(amount);
uint256 agentShares = amount - protocolShares;
_prepareAgentsRewards(agentShares, settings);
return SafeCast.toUint32(_mainRewards.length - 1);
}
// AgentRewardV2::_distributeValidatorRewards()
function _distributeValidatorRewards(
uint256 amount,
uint256 virtualId,
uint48 rewardId,
uint256 totalStaked
) private {
IAgentNft nft = IAgentNft(agentNft);
// Calculate weighted validator shares
uint256 validatorCount = nft.validatorCount(virtualId);
uint256 totalProposals = nft.totalProposals(virtualId);
for (uint256 i = 0; i < validatorCount; i++) {
address validator = nft.validatorAt(virtualId, i);
// Get validator revenue by votes weightage
address stakingAddress = getVirtualTokenAddress(nft, virtualId);
uint256 votes = IERC5805(stakingAddress).getVotes(validator);
uint256 validatorRewards = (amount * votes) / totalStaked;
// Calc validator reward based on participation rate
uint256 participationReward = totalProposals == 0
? 0
: (validatorRewards * nft.validatorScore(virtualId, validator)) / totalProposals;
@> _validatorRewards[validator][rewardId] = participationReward;
@> validatorPoolRewards += validatorRewards - participationReward;
}
}
// AgentRewardV2::withdrawValidatorPoolRewards()
function withdrawValidatorPoolRewards(address recipient) external onlyGov {
require(validatorPoolRewards > 0, "No validator pool rewards");
IERC20(rewardToken).safeTransfer(recipient, validatorPoolRewards);
validatorPoolRewards = 0;
}
Recommended mitigation steps
Add access control to addValidator():
- function addValidator(uint256 virtualId, address validator) public {
+ function addValidator(uint256 virtualId, address validator) public onlyRole(VALIDATOR_ADMIN_ROLE) {
if (isValidator(virtualId, validator)) {
return;
}
_addValidator(virtualId, validator);
_initValidatorScore(virtualId, validator);
}
Virtuals marked as informative
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 addValidator function was declared public with no role check, and mint unconditionally adds the founder again without checking whether that validator was already present.
Impact
The shared validator pool rewards could be overstated and withdrawn, leaving the contract unable to pay valid validator rewards until someone tops it up; individual validator rewards themselves are not broken.
What the attacker needed
Attacker can call the public contract; attacker can predict the next virtual ID; rewards are later distributed and pool withdrawn
The fix
The report recommends restricting addValidator to a dedicated admin role so only authorised callers can add validators.
How it was found
- Read the next virtual ID from the NFT contract's nextVirtualId function.
- Call addValidator with that ID and an attacker-chosen address before mint runs.
- Wait for the factory to call mint, which adds the founder as a validator a second time.
- Reward distribution iterates the duplicated list and overstates the shared validator pool rewards.
- The governance address withdraws the overstated pool rewards, leaving the contract short.
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 is a program on a blockchain that anyone can call. This one had a function for adding a 'validator' (a participant who earns rewards) that anyone was allowed to call, not just the contract's own code. An attacker could guess the next ID and add themselves early, which later caused the reward bookkeeping to count the same person twice and overpay from a shared pot.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.