When `stakerCountInTree` Increases, Some Users May Receive Less Interest
A staking contract's tier boundary update logic fails to promote a user from tier 3 to tier 2 when the total staker count increases, causing that user to receive a lower interest rate than their correct tier entitles them to.
Source: https://github.com/sherlock-audit/2025-05-layeredge-judging/issues/230
Found by
Cybrid, Hecok, Hurricane, Pablo, X77, benjamin_0923, durov, future, jkoppel, kazan, lukris02, montecristo, rudhra1749, shiazinho, slavina, t0x1c
Summary
In the _checkBoundariesAndRecord function, when stakerCountInTree increases, a user in tier 3 is not promoted to tier 2 when it should be.
Root Cause
The root cause is that when stakerCountInTree increases, users who need to transition from tier 3 to tier 2 are not properly updated.
At this point, the ranking of new_t1 + new_t2 could be moved from tier 3 to tier 2.
However, in line 897, only the ranking of old_t1 + old_t2 is updated instead of considering new_t1 + new_t2.
function _checkBoundariesAndRecord(bool isRemoval) internal {
...
// Tier 2 boundary handling
if (new_t1 + new_t2 > 0) {
if (new_t2 != old_t2) {
// Need to update all users between the old and new tier 2 boundaries
uint256 old_boundary = old_t1 + old_t2;
uint256 new_boundary = new_t1 + new_t2;
if (new_boundary > old_boundary) {
// Promotion case: update all users from old_boundary+1 to new_boundary
for (uint256 rank = old_boundary + 1; rank <= new_boundary; rank++) {
_findAndRecordTierChange(rank, n);
}
} else {
// Demotion case: update all users from new_boundary+1 to old_boundary
for (uint256 rank = new_boundary + 1; rank <= old_boundary; rank++) {
_findAndRecordTierChange(rank, n);
}
}
}
// Handle case where Tier 2 count stays the same
else if (isRemoval) {
_findAndRecordTierChange(new_t1 + new_t2, n);
896: } else if (!isRemoval) {
_findAndRecordTierChange(old_t1 + old_t2, n);
}
}
}
Internal pre-conditions
N/A
External pre-conditions
N/A
Attack Path
Consider the scenario where oldStakerCountInTree = 14 and newStakerCountInTree = 15.
The tier distribution changes from (2, 4, 8) to (3, 4, 8), but the seventh-ranked user's tier does not change from tier 3 to tier 2.
PoC
function toString(uint256 value) internal pure returns (string memory result) {
assembly {
result := add(mload(0x40), 0x80)
mstore(0x40, add(result, 0x20)) // Allocate memory.
mstore(result, 0) // Zeroize the slot after the string.
let end := result // Cache the end of the memory to calculate the length later.
let w := not(0) // Tsk.
for { let temp := value } 1 {} {
result := add(result, w) // `sub(result, 1)`.
mstore8(result, add(48, mod(temp, 10)))
temp := div(temp, 10) // Keep dividing `temp` until zero.
if iszero(temp) { break }
}
let n := sub(end, result)
result := sub(result, 0x20) // Move the pointer 32 bytes back to make room for the length.
mstore(result, n) // Store the length.
}
}
function test_LayerEdgeStaking_checkBoundariesAndRecord() public {
vm.startPrank(admin);
uint256 userAmount = 10_000 * 1e18;
address[20] memory users;
for (uint256 i = 0; i < 20; i++) {
users[i] = makeAddr(string(abi.encodePacked("users", toString(i))));
token.transfer(users[i], userAmount);
}
vm.stopPrank();
for (uint256 i = 1; i <= 15; i++) {
vm.startPrank(users[i]);
token.approve(address(staking), MIN_STAKE);
staking.stake(MIN_STAKE);
vm.stopPrank();
}
address user = users[7];
vm.warp(block.timestamp + 365 days);
uint256 userAPY = staking.getUserAPY(user);
vm.startPrank(user);
uint256 beforeUserAmount = token.balanceOf(user);
staking.claimInterest();
uint256 afterUserAmount = token.balanceOf(user);
console2.log("Procotol User APY : %d", userAPY);
console2.log("Actual Received APY : %d", 100e18 * (afterUserAmount - beforeUserAmount) / MIN_STAKE);
vm.stopPrank();
}
forge test --match-test "test_LayerEdgeStaking_checkBoundariesAndRecord" -vv Result:
Ran 1 test for test/stake/LayerEdgeStakingTest.t.sol:LayerEdgeStakingTest
[PASS] test_LayerEdgeStaking_checkBoundariesAndRecord() (gas: 6634274)
Logs:
Procotol User APY : 35000000000000000000
Actual Received APY : 20000000000000000000
After mitigation, Result:
Ran 1 test for test/stake/LayerEdgeStakingTest.t.sol:LayerEdgeStakingTest
[PASS] test_LayerEdgeStaking_checkBoundariesAndRecord() (gas: 6956510)
Logs:
Procotol User APY : 35000000000000000000
Actual Received APY : 35000000000000000000
Impact
- Some Users receive less interest.
Mitigation
if (new_t1 + new_t2 > 0) {
if (new_t2 != old_t2) {
// Need to update all users between the old and new tier 2 boundaries
uint256 old_boundary = old_t1 + old_t2;
uint256 new_boundary = new_t1 + new_t2;
if (new_boundary > old_boundary) {
// Promotion case: update all users from old_boundary+1 to new_boundary
for (uint256 rank = old_boundary + 1; rank <= new_boundary; rank++) {
_findAndRecordTierChange(rank, n);
}
} else {
// Demotion case: update all users from new_boundary+1 to old_boundary
for (uint256 rank = new_boundary + 1; rank <= old_boundary; rank++) {
_findAndRecordTierChange(rank, n);
}
}
}
// Handle case where Tier 2 count stays the same
else if (isRemoval) {
_findAndRecordTierChange(new_t1 + new_t2, n);
} else if (!isRemoval) {
897: _findAndRecordTierChange(old_t1 + old_t2, n);
+ _findAndRecordTierChange(new_t1 + new_t2, n);
}
}
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 code updates the tier for the old boundary rank instead of the new boundary rank when the staker count increases without changing the tier 2 count.
Impact
Some users receive less interest than they should, as shown by a test where the actual received APY was 20% instead of the protocol's 35%.
What the attacker needed
StakerCountInTree increases; tier 2 count remains unchanged
How it was found
- Set up 20 users with tokens and have 15 of them stake the minimum amount.
- Select the user at index 7 (the seventh-ranked staker).
- Advance time by 365 days.
- Have that user claim interest and compare the received APY to the protocol's stated APY.
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 this staking system, users are placed into tiers that determine their interest rate. When new people join, the boundaries between tiers shift, and some users should move to a better tier. A bug in the code means that when the total number of stakers goes up, one user who should move up stays in the lower tier and earns less interest.
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.