`setStartBlock()` doesn't change the block at which already existing pools will start accumulating points
The setStartBlock() function changes the global start block for point accumulation but does not update the lastRewardBlock of existing pools, so those pools continue to use the old start block. This can cause points to be awarded before the new start block or delay rewards until the old start block is reached.
0xAadi Follow with EgisSecurity, KupiaSec and 13 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-05-sophon-judging/issues/108
The protocol has acknowledged this issue.
Found by
0xAadi, EgisSecurity, KupiaSec, MightyRaju, ZdravkoHr., araj, blackhole, dhank, h2134, jecikpo, serial-coder, underdog, utsav, whitehair0330, yamato, zzykxx
Summary
The function setStartBlock() can be called by the owner to change the block number at which points will start accumulating. When it's called, the block at which already existing pools will start accumulating points will not change. Already existing pools will:
- Start accumulating points from the old
startBlockif the newstartBlockis set after the old one. - Not accumulate rewards until the old
startBlockis reached if the newstartBlockis set before the old one.
Vulnerability Detail
This happens because updatePool() considers the pool lastRewardBlock as the block number from which points should start accumulating and setStartBlock() never updates the lastRewardBlock of the already existing pools to the new startBlock.
POC
Runnable POC that showcases point 1 explained above. Can be copy-pasted in SophonFarming.t.sol:
function test_SettingStartBlockDoesntUpdatePools() public {
address alice = makeAddr("alice");
uint256 amountToDeposit = sDAI.convertToAssets(1e18);
vm.prank(alice);
dai.approve(address(sophonFarming), type(uint256).max);
deal(address(dai), alice, amountToDeposit);
//-> Pools original `startBlock` is `1`
//-> Admin changes `startBlock` to `100`
vm.prank(deployer);
sophonFarming.setStartBlock(100);
//-> Alice deposits at block `90`, which is after the previous `startBlock` (1) but before the current `startBlock` (100)
vm.roll(90);
vm.prank(alice);
sophonFarming.depositDai(amountToDeposit, 0);
//-> After 9 blocks, at block `99`, Alice has accumulated rewards but she shouldn't have because the current `startBlock` (100) has not been reached yet
vm.roll(99);
vm.prank(alice);
sophonFarming.withdraw(0, type(uint256).max);
assertEq(sophonFarming.pendingPoints(0, alice), 74999999999999999999);
}
Can be run with:
forge test --match-test test_SettingStartBlockDoesntUpdatePools -vvvvv
Impact
When setStartBlock() is called the block at which already existing pools will start accumulating points will not change.
Code Snippet
Tool used
Manual Review
Recommendation
In setStartBlock() loop over all of the existing pools and adjust each pool lastRewardBlock to the new startBlock. Furthermore setStartBlock() should revert if the new startBlock is lower than the current block.number as this would create problems in points distribution accounting if the above fix is implemented.
Discussion
sherlock-admin4
1 comment(s) were left on this issue during the judging contest.
0xmystery commented:
valid because lastRewardBlock for each pool should indeed sync with the latest startBlock (best because the report is succinct and comprehensive explaining each of the two scenarios)
mcbvictor0x
Issue is no longer relevant since we removed the global startBlock setting: https://github.com/sophon-org/farming-contracts/commit/18b59d5d1a987caf821245ab83505ca6f62904e6
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
Impact
Points may be distributed incorrectly: either early accumulation before the new start block or delayed rewards until the old start block is reached.
What the attacker needed
Owner calls setStartBlock(); existing pools present
How it was found
- Admin calls setStartBlock() to change the start block.
- Alice deposits after the old start block but before the new start block.
- Alice withdraws before the new start block and still has accumulated points.
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.
In a farming contract, users earn points based on blocks. The owner can change the block where points start, but existing pools ignore this change because they store their own start block. So users might get points too early or too late.
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.