Zero reward rate calculation impedes low-decimals token distributions
The reward rate calculation in BathBuddy uses integer division, which can round down to zero for tokens with low decimals or short distribution periods. An attacker could exploit this to cause zero rewards to be distributed, forcing the owner to adjust parameters.
Submitted by 0xNineDec, also found by adriro, Ruhum, joestakey, Ignite, bytes032, Ace-30, Ace-30, KingNFT, AlexCzm, SaeedAlipoor01988, ktg, rvierdiiev, cccz, SpicyMeatball and J4de.
Several (token, distribution period) couples are not feasible in BathBuddy, forcing the owner to set shorter distributions or providing more amount of tokens to be distributed.
In BathBuddy, the reward rate of a distribution is calculated following a linear distribution according to the amount of rewards to be distributed across the period:
function notifyRewardAmount(
uint256 reward,
IERC20 rewardsToken
) external onlyOwner updateReward(address(0), address(rewardsToken)) {
if (block.timestamp >= periodFinish[address(rewardsToken)]) {
rewardRates[address(rewardsToken)] = reward.div(
rewardsDuration[address(rewardsToken)]
);
} else {
uint256 remaining = periodFinish[address(rewardsToken)].sub(
block.timestamp
);
uint256 leftover = remaining.mul(
rewardRates[address(rewardsToken)]
);
rewardRates[address(rewardsToken)] = reward.add(leftover).div(
rewardsDuration[address(rewardsToken)]
);
}
The rate for each reward token spreads the reward amount across the rewardsDuration:
rewardRates[address(rewardsToken)] = reward.div(
rewardsDuration[address(rewardsToken)]
);
Because the SafeMath implementation asks for compiler versions over 0.8.0 to execute the legacy division a/b, the reward rate could be zero for some distribution configurations, forcing the owner to reduce the duration or increase the reward amount. Bear in mind, that the implementation of the BathBuddy allows using arbitrary tokens for rewards.
Proof of Concept
- The owner launches a distribution of WBTC and is forced to set a pair of
rewardandrewardsDuration[address(rewardsToken)]such that their integer division yields at least in1. - Considering that the rate is
1, the year has (365 * 24 * 60 * 60 = 31.53e6) seconds which yields in0.31536 WBTC($8,500 @ 27,000 USD/BTC)at minimum of rewards per user.
The following test refactors the bath-buddy.ts deploy fixture with the following:
--- a/test/hardhat-tests/bath-buddy.ts
+++ b/test/hardhat-tests/bath-buddy.ts
@@ -44,9 +44,9 @@ describe("RubiconV2 rewards system", function () {
);
const testStableCoin = await testCoinFactory.deploy(
owner.address,
- "Test Stablecoin",
- "TUSDC",
- 6
+ "Test WBTC",
+ "WBTC",
+ 8
);
- await testStableCoin.transfer(buddy.address, parseUnits("10000", 6));
+ await testStableCoin.transfer(buddy.address, parseUnits("0.315", 8));
await buddy.notifyRewardAmount(
- parseUnits("10000", 6),
+ parseUnits("0.315", 8),
+ testStableCoin.address
+ );
And the test is the same as the one provided (should get rewards from both Comptroller and BathBuddy) only calculating the amount of rewards received by getting the balance before and after claiming them:
it("gets abusive amount of rewards per year", async function () {
const {
owner,
testCoin,
testStableCoin,
testRewardCoin,
bathTEST,
buddy,
bathHouse,
comptroller,
} = await loadFixture(buddySetupFixtureAudit);
expect(await comptroller.getCompAddress()).to.be.equal(
testRewardCoin.address
);
const b0 = await testStableCoin.balanceOf(owner.address);
await testCoin.approve(bathTEST.address, parseUnits("1000"));
await bathTEST.mint(parseUnits("1000"));
const earned = await buddy.earned(owner.address, testStableCoin.address);
const earnedComp = await testRewardCoin.balanceOf(owner.address);
expect(earnedComp).to.be.equal(0);
// skip a year
await time.increaseTo((await time.latest()) + 365 * 24 * 60 * 60);
bathHouse.claimRewards([buddy.address], [testStableCoin.address]);
const earned2 = await testStableCoin.balanceOf(owner.address);
console.log(`Rewards earned per year: ${earned2.sub(b0)}`)
const earnedComp2 = await testRewardCoin.balanceOf(owner.address);
expect(earned).to.be.lt(earned2);
expect(earnedComp).to.be.lt(earnedComp2);
});
It can be seen that setting the distribution amount to 0.315e8 (slightly below the amount of seconds per year) yields in zero rewards:
Rewards earned per year: 0
And setting the distribution amount slightly over the seconds per year (0.3154e8) successfully yields in rewards:
Rewards earned per year: 31536000
Recommended Mitigation Steps
Increase the precision scale of the reward rate calculation using a factor (e.g. 10**8) multiplied to to the amount when calculating the distribution’s rate.
Tough call in deciding between this and #1293 for best report, but going with this.
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 integer division truncation in the reward rate calculation, which can yield zero when the reward amount is smaller than the distribution duration.
Impact
For certain token and duration combinations, the reward rate becomes zero, so no rewards are distributed, forcing the owner to shorten the duration or increase the reward amount.
What the attacker needed
Owner sets a distribution with low-decimals token; Reward amount small relative to duration
The fix
Increase the precision scale of the reward rate calculation using a factor (e.g. 10**8) multiplied to the amount when calculating the distribution’s rate.
How it was found
- The owner launches a distribution of WBTC and is forced to set a pair of reward and rewardsDuration such that their integer division yields at least 1.
- Considering that the rate is 1, the year has 31.53e6 seconds which yields in 0.31536 WBTC at minimum of rewards per user.
- The test refactors the bath-buddy.ts deploy fixture with the following changes.
- The test is the same as the one provided only calculating the amount of rewards received by getting the balance before and after claiming them.
- It can be seen that setting the distribution amount to 0.315e8 yields in zero rewards.
- And setting the distribution amount slightly over the seconds per year (0.3154e8) successfully yields in rewards.
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 a smart contract calculates how many tokens to give out per second, it divides the total reward by the number of seconds. If the result is less than one, the division rounds down to zero, so no rewards are ever given. This can happen with tokens that have few decimal places or when the reward is small compared to the duration.