# Unstake function reverts because of use of outdated/stale serviceIds array

Olas StakingBase.unstake reverts with out-of-bounds when checkpoint evicts services, leaving a stale serviceIds array whose indices no longer match setServiceIds.

- Payout: Audit contest
- Severity: medium
- Program: Olas
- Found by: Varun\_05
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-olas-findings/issues/57
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-olas%3AM-06
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** StakingBase.sol unstake/checkpoint staking contract
**Root cause:** checkpoint() returns the pre-eviction copy of serviceIds from _calculateStakingRewards, but _evict shrinks setServiceIds; unstake uses the stale array's index to index into the shorter setServiceIds, causing out-of-bounds.
**Steps:**
1. Have setServiceIds of length 10; service at index 9 is staked.
2. An inactive service at index 3 exceeds maxInactivityDuration; call checkpoint() so _evict pops it, shrinking setServiceIds to 9 and moving index-9 service to index 3.
3. checkpoint returns stale serviceIds of length 10 (pre-eviction copy).
4. Owner of the service now at index 3 calls unstake(serviceId); loop finds it at idx 9 in stale serviceIds, sets inSet=true.
5. `setServiceIds[9] = setServiceIds[8]` reverts out-of-bounds since setServiceIds.length is 9.
**How found:** Code-reading of unstake's checkpoint return handling: noticed checkpoint returns serviceIds captured before _evict mutates setServiceIds, so indices diverge after eviction.
**Exploitation:** Any staked service owner triggers unstake after a checkpoint that evicted at least one service; the transaction reverts, blocking unstaking and reward withdrawal until state changes.
**Insight:** When a function returns an array snapshot and a callee mutates the underlying storage array, verify index alignment between the snapshot and live storage before using snapshot indices to index storage.

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

**Unstake loop using stale serviceIds array**

```
uint256 idx;
        bool inSet;
        for (; idx < serviceIds.length; ++idx) {
            // Service is still in a global staking set if it is found in the services set,
            // and is not present in the evicted set
            if (evictServiceIds[idx] == serviceId) {
                break;
            } else if (serviceIds[idx] == serviceId) {
                inSet = true;
                break;
            }
        }

        // Get the unstaked service data
        reward = sInfo.reward;
        uint256[] memory nonces = sInfo.nonces;
        address multisig = sInfo.multisig;

        // Clear all the data about the unstaked service
        // Delete the service info struct
        delete mapServiceInfo[serviceId];

        // Update the set of staked service Ids
        // If the index was not found, the service was evicted and is not part of staked services set
        if (inSet) {
            setServiceIds[idx] = setServiceIds[setServiceIds.length - 1];
            setServiceIds.pop();
        }
```

**Evict shrinks setServiceIds via pop**

```
for (uint256 i = numEvictServices; i > 0; --i) {
            // Decrease the number of services
            totalNumServices--;
            // Get the evicted service index
            uint256 idx = serviceIndexes[i - 1];
            // Assign last service Id to the index that points to the evicted service Id
            setServiceIds[idx] = setServiceIds[totalNumServices];
            // Pop the last element
            setServiceIds.pop();
        }
```

**Checkpoint returns stale serviceIds after eviction**

```
// Evict inactive services
            if (numServices > 0) {
                _evict(evictServiceIds, serviceInactivity, numServices);
            }

            // Record the actual epoch length
            uint256 epochLength = block.timestamp - tsCheckpoint;
            // Record the current timestamp such that next calculations start from this point of time
            tsCheckpoint = block.timestamp;

            // Increase the epoch counter
            epochCounter = eCounter + 1;

            emit Checkpoint(eCounter, lastAvailableRewards, finalEligibleServiceIds, finalEligibleServiceRewards,
                epochLength);
        }

        return (serviceIds, serviceNonces, finalEligibleServiceIds, finalEligibleServiceRewards, evictServiceIds);
```

