DoS and gas griefing of calls to Prime.updateScores()
The updateScores function in the Prime contract fails to increment the loop counter when a user's score is already updated, causing an infinite loop and out-of-gas revert. An attacker can front-run a legitimate updateScores call with a call that updates one of the same users, causing the original call to revert and delaying score updates.
0xDetermination Follow with SBSecurity, al88nsk and 19 others
Originally disclosed on Code4rena
Submitted by 0xDetermination, also found by SBSecurity, maanas (1, 2), al88nsk, PwnStars, Pessimistic, dethera, ThreeSigma, 0xblackskull, xAriextz, blutorque, neumo, tsvetanovv, sces60107, pina, oakcobalt, Breeje, ADM, tapir, said, debo, 0xweb3boy, and Satyam_Sharma
https://github.com/code-423n4/2023-09-venus/blob/main/contracts/Tokens/Prime/Prime.sol#L200-L230
https://github.com/code-423n4/2023-09-venus/blob/main/tests/hardhat/Prime/Prime.ts#L294-L301
updateScores() is meant to be called to update the scores of many users after reward alpha is changed or reward multipliers are changed. An attacker can cause calls to Prime.updateScores() to out-of-gas revert, delaying score updates. Rewards will be distributed incorrectly until scores are properly updated.
Proof of Concept
updateScores() will run out of gas and revert if any of the users passed in the argument array have already been updated. This is due to the continue statement and the incrementing location of i:
function updateScores(address[] memory users) external {
if (pendingScoreUpdates == 0) revert NoScoreUpdatesRequired();
if (nextScoreUpdateRoundId == 0) revert NoScoreUpdatesRequired();
for (uint256 i = 0; i < users.length; ) {
address user = users[i];
if (!tokens[user].exists) revert UserHasNoPrimeToken();
if (isScoreUpdated[nextScoreUpdateRoundId][user]) continue;
...
unchecked {
i++;
}
emit UserScoreUpdated(user);
}
}
An attacker can frontrun calls to updateScores() with a call to updateScores(), passing in a 1-member array of one of the addresses in the frontran call’s argument array. The frontran call will run out of gas and revert, and only one of the users intended to be updated will actually be updated.
Test
Paste and run the below test in the ‘mint and burn’ scenario in Prime.ts (line 302)
it("dos_updateScores", async () => {
//setup 3 users
await prime.issue(true, [user1.getAddress(), user2.getAddress(), user3.getAddress()]);
await xvs.connect(user1).approve(xvsVault.address, bigNumber18.mul(1000));
await xvsVault.connect(user1).deposit(xvs.address, 0, bigNumber18.mul(1000));
await xvs.connect(user2).approve(xvsVault.address, bigNumber18.mul(1000));
await xvsVault.connect(user2).deposit(xvs.address, 0, bigNumber18.mul(1000));
await xvs.connect(user3).approve(xvsVault.address, bigNumber18.mul(1000));
await xvsVault.connect(user3).deposit(xvs.address, 0, bigNumber18.mul(1000));
//change alpha
await prime.updateAlpha(1, 5);
//user 1 frontruns updateScores([many users]) with updateScores([solely user3])
await prime.connect(user1).updateScores([user3.getAddress()])
//frontran call should revert
await expect(prime.updateScores([user1.getAddress(), user2.getAddress(), user3.getAddress()])).to.be.reverted;
});
Tools Used
Hardhat
Recommended Mitigation Steps
Increment i such that continue doesn’t result in infinite looping:
function updateScores(address[] memory users) external {
...
+ for (uint256 i = 0; i < users.length; ++i} ) {
- for (uint256 i = 0; i < users.length; ) {
...
- unchecked {
- i++;
- }
...
}
}
0xRobocop (Lookout) commented:
Finding is correct. The function
updateScorewill unsuccessfully incrementiand hence will not skip the user, causing the call to revert.Consider QA, since the function can be called again.
Marking as primary for sponsor review anyways.
chechu (Venus) confirmed, but disagreed with severity and commented:
Consider QA.
updateScorescould be invoked again.
I agree with medium severity. Take the scenario where each user update costs 10_000 gas.
- 100 users need to be updated =
10_000 * 100= 1_000_000 gas.- Attacker frontruns as described above, costing themselves ~10,000 gas.
- OOG error will cause original caller to spend 1_000_000 gas.
- Original caller tries again.
- Attacker frontruns again.
- OOG error will cause original caller to spend another 1_000_000 gas.
This griefing attack can cause a significant amount of required spending.
Fixed here.
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 loop counter is not incremented when the continue statement is executed, leading to an infinite loop.
Impact
An attacker can repeatedly cause legitimate updateScores calls to revert, wasting gas and delaying score updates, which may lead to incorrect reward distribution until scores are updated.
What the attacker needed
Attacker has a Prime token; Attacker can front-run transactions
The fix
The loop counter should be incremented in the for loop statement to ensure it increments even when continue is executed.
How it was found
- Set up three users and give them Prime tokens and XVS deposits.
- Change the reward alpha parameter.
- Attacker calls updateScores with an array containing only one user (user3).
- A legitimate caller then calls updateScores with an array containing user1, user2, and user3.
- The legitimate call reverts because user3 was already updated, causing an out-of-gas error.
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 function that updates scores for many users has a bug: if it encounters a user whose score is already updated, it gets stuck in an infinite loop and runs out of gas, so the whole transaction fails. An attacker can exploit this by quickly making a small update that includes one of the users in a pending large update, causing the large update to fail and waste gas.
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.