battleElo is at risk of underflow revert, which may DOS voting
The battleElo function in EloCalculator.sol can underflow when a validator casts a vote with certain battle results, causing a revert. This blocks validators from submitting votes with those specific results, potentially disrupting governance for low-maturity services.
Submitted by oakcobalt, also found by testnate
In EloCalculator.sol, the battleElo() function calculates a new maturity score based on a series of battle results:
function battleElo(uint256 currentRating, uint8[] memory battles) public view returns (uint256) {
uint256 eloA = 1000;
uint256 eloB = 1000;
for (uint256 i = 0; i < battles.length; i++) {
uint256 result = mapBattleResultToGameResult(battles[i]);
(uint256 change, bool negative) = Elo.ratingChange(eloB, eloA, result, k);
change = _roundUp(change, 100);
if (negative) {
eloA -= change;
eloB += change;
} else {
eloA += change;
eloB -= change;
}
}
return currentRating + eloA - 1000; //@audit Potential underflow here
}
When calculating results of a series of battle, battleElo will subtract currentRating by the difference in rate change. currentRating + eloA - 1000
Under current AgentDao implementation in _castVote → _updateMaturity → _calcMaturity, uint8[] memory battles can result in a negative change to the currentRating. This case is allowed in the protocol because we see ServiceNft handles the case when _maturities(proposalId) < _maturities(prevServiceId) in updateImpact.
When a validator casts a vote that results in a negative change in currentRating, this might cause currentRating + eloA - 1000 underflow revert when currentRating is already low.
Impact
DOS conditions:
- Validators may be unable to submit valid votes with specific battle results.
- The governance mechanism may be partially blocked for low-maturity services.
- The voting process could be disrupted when trying to express certain opinions.
Recommended mitigation steps
In battleElo, consider checking and handling currentRating + eloA - 1000 underflow case and return a minimal rating value when the underflow case is triggered.
Proof of Concept
Test logics:
- Base proposal:
proposal1executed. - Check
proposal1’s maturity. validatorA’s voting power is twice asvalidatorB.proposal2is created (intended as an upgrade fromproposal1).validatorAcast a Vote with [0, 0, 0, 0, 0, 0, 1, 0, 1, 0].castVoterevert due to underflow.castVotewith specific results DOSsed.
Added unit test underflow DOS certain voting in test/rewardsV2.js. Run test with: npx hardhat test test/rewardsV2.js:
it.only("underflow DOS certain voting", async function () {
//setup:
// 1. base proposal: proposal1 executed
// 2. check proposal1's maturity
// 3. validatorA's voting power is twice as validatorB.
// 4. proposal2 is created (intended as an upgrade from proposal1)
// 5. validatorA cast a Vote with [0, 0, 0, 0, 0, 0, 1, 0, 1, 0]
// 6. castVote revert due to underflow. castVote with specific results DOSsed.
const base = await loadFixture(deployWithAgent);
const { serviceNft, contributionNft, agentNft, virtualToken } = base;
const {
founder,
contributor1,
contributor2,
validator1,
validator2,
trader,
} = await getAccounts();
// Setup LP and voting power
const agentToken = await ethers.getContractAt(
"AgentToken",
base.agent.token
);
const veToken = await ethers.getContractAt(
"AgentVeToken",
base.agent.veToken
);
const lp = await ethers.getContractAt("IERC20", base.agent.lp);
// Delegate founder's votes to validator1 (founder holds a large amount of tokens)
await veToken.connect(founder).delegate(validator1.address);
// Set up validator2 with LP tokens - similar to the validator uptime test
const router = await ethers.getContractAt(
"IUniswapV2Router02",
process.env.UNISWAP_ROUTER
);
// Verify voting power
const daoAddr = (await agentNft.virtualInfo(1)).dao;
const agentDAO = await ethers.getContractAt("AgentDAO", daoAddr);
const validator1Votes = await veToken.getVotes(validator1.address);
console.log("Validator1 votes:", formatEther(validator1Votes));
// Step 1: Create base proposal and get it executed
const proposalId1 = await createContribution(
1, // virtualId
0, // coreId (Cognitive Core)
0, // parentId
true, // isModel
0, // no dataset dependency
"Base contribution",
base,
contributor1.address,
[validator1], // Only validator1 votes on this one
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
);
// Step 2: Check proposalId1's maturity
const maturity1 = await serviceNft.getMaturity(proposalId1);
console.log("Base proposal maturity:", maturity1.toString());
expect(maturity1).to.be.gt(0);
// Step 3: Create proposal2 (intended as an upgrade)
const descHash2 = getDescHash("Improved contribution");
const mintCalldata2 = await getMintServiceCalldata(
serviceNft,
1,
descHash2
);
await agentDAO.propose(
[serviceNft.target],
[0],
[mintCalldata2],
"Improved contribution"
);
// Get the proposalId
const events = await agentDAO.queryFilter(
agentDAO.filters.ProposalCreated,
-1
);
const proposalId2 = events[events.length - 1].args[0];
// Create contribution NFT
await contributionNft.mint(
contributor2.address,
1, // virtualId
0, // coreId
TOKEN_URI,
proposalId2,
0, // parentId
true, // isModel
0 // no datasetId
);
// Step 4: Validator1 casts honest forVote with battles results
const honestVoteParams = abi.encode(
["uint8[] memory"],
[[0, 0, 0, 0, 0, 0, 1, 0, 1, 0]]
);
await expect(
agentDAO
.connect(validator1)
.castVoteWithReasonAndParams(
proposalId2,
1,
"Good improvement",
honestVoteParams
)
).to.be.revertedWithPanic(0x11);
console.log(
"validator1 vote for proposal2 reverted with 0x11 underflow panic code"
);
});
Test results:
RewardsV2
Validator1 votes: 9999999.999999999999999
Base proposal maturity: 100
validator1 vote for proposal2 reverted with 0x11 underflow panic code
✔ underflow DOS certain voting (1540ms)
1 passing (2s)
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 arithmetic expression `currentRating + eloA - 1000` can underflow when `eloA` is less than 1000 and `currentRating` is small, because the subtraction is performed without checking for underflow.
Impact
Validators may be unable to submit valid votes with specific battle results, potentially disrupting governance for low-maturity services.
What the attacker needed
A low-maturity service; validator with specific battle results
The fix
The battleElo function should check for and handle the underflow case, returning a minimal rating value when triggered.
How it was found
- Execute a base proposal and check its maturity.
- Ensure validatorA has twice the voting power of validatorB.
- Create a second proposal intended as an upgrade.
- ValidatorA casts a vote with battle results [0,0,0,0,0,0,1,0,1,0].
- The castVote transaction reverts with an underflow panic (0x11).
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 calculates a new score by adding and subtracting numbers. If the result would be negative, the contract crashes instead of handling it. This means some votes cannot be cast, which could stop the voting system from working properly.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.