BOLDUpgradeAction.sol will fail to upgrade contracts due to error in the perform function
The BOLDUpgradeAction contract's cleanupOldRollup function iterates over stakers using an index while forceRefundStaker removes stakers from the list, causing an array out-of-bounds error. This prevents the upgrade and deployment of new BOLD contracts.
SpicyMeatball Follow with dontonka and josephdara
Originally disclosed on Code4rena
Submitted by SpicyMeatball, also found by dontonka and josephdara
Impact
An error in the BOLDUpgradeAction.sol contract prevents it from upgrading and deploying new BOLD contracts.
Proof of Concept
The perform function serves as the entry point in the BOLDUpgradeAction.sol and is responsible for migrating stakers from the old rollup and deploying the challenge manager with a new rollup contract.
One of the first subroutines in this function is the cleanupOldRollup().
function perform(address[] memory validators) external {
// tidy up the old rollup - pause it and refund stakes
cleanupOldRollup();
This subroutine pauses the old rollup contract and attempts to refund existing stakers.
function cleanupOldRollup() private {
IOldRollupAdmin(address(OLD_ROLLUP)).pause();
>> uint64 stakerCount = ROLLUP_READER.stakerCount();
// since we for-loop these stakers we set an arbitrary limit - we dont
// expect any instances to have close to this number of stakers
if (stakerCount > 50) {
stakerCount = 50;
}
for (uint64 i = 0; i < stakerCount; i++) {
>> address stakerAddr = ROLLUP_READER.getStakerAddress(i);
OldStaker memory staker = ROLLUP_READER.getStaker(stakerAddr);
if (staker.isStaked && staker.currentChallenge == 0) {
address[] memory stakersToRefund = new address[](1);
stakersToRefund[0] = stakerAddr;
IOldRollupAdmin(address(OLD_ROLLUP)).forceRefundStaker(stakersToRefund);
}
}
// upgrade the rollup to one that allows validators to withdraw even whilst paused
DoubleLogicUUPSUpgradeable(address(OLD_ROLLUP)).upgradeSecondaryTo(IMPL_PATCHED_OLD_ROLLUP_USER);
}
This function contains a bug that prevents execution of the subsequent procedures. Let’s check the forceRefundStaker in the old rollup contract.
According to: https://docs.arbitrum.io/build-decentralized-apps/reference/useful-addresses
Proxy: https://etherscan.io/address/0x5eF0D09d1E6204141B4d37530808eD19f60FBa35 Implementation: https://etherscan.io/address/0x72f193d0f305f532c87a4b9d0a2f407a3f4f585f#code
RollupAdminLogic.sol
function forceRefundStaker(address[] calldata staker) external override whenPaused {
require(staker.length > 0, "EMPTY_ARRAY");
for (uint256 i = 0; i < staker.length; i++) {
require(_stakerMap[staker[i]].currentChallenge == NO_CHAL_INDEX, "STAKER_IN_CHALL");
reduceStakeTo(staker[i], 0);
>> turnIntoZombie(staker[i]);
}
emit OwnerFunctionCalled(22);
}
RollupCore.sol
function turnIntoZombie(address stakerAddress) internal {
Staker storage staker = _stakerMap[stakerAddress];
_zombies.push(Zombie(stakerAddress, staker.latestStakedNode));
>> deleteStaker(stakerAddress);
}
function deleteStaker(address stakerAddress) private {
Staker storage staker = _stakerMap[stakerAddress];
require(staker.isStaked, "NOT_STAKED");
uint64 stakerIndex = staker.index;
_stakerList[stakerIndex] = _stakerList[_stakerList.length - 1];
_stakerMap[_stakerList[stakerIndex]].index = stakerIndex;
>> _stakerList.pop();
delete _stakerMap[stakerAddress];
}
From the above code, it is evident that the staker’s address is eventually deleted from the _stakerList, causing the array to shrink. As a result, the cleanupOldRollup function will throw an “array out-of-bounds” error because it tries to iterate through an array with the original number of elements.
Coded POC
Here we use mainnet fork with only the cleanupOldRollup function.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
import {Test} from "forge-std/Test.sol";
import "forge-std/console.sol";
struct OldStaker {
uint256 amountStaked;
uint64 index;
uint64 latestStakedNode;
// currentChallenge is 0 if staker is not in a challenge
uint64 currentChallenge; // 1. cannot have current challenge
bool isStaked; // 2. must be staked
}
interface IOldRollup {
function pause() external;
function forceRefundStaker(address[] memory stacker) external;
function getStakerAddress(uint64 stakerNum) external view returns (address);
function stakerCount() external view returns (uint64);
function getStaker(address staker) external view returns (OldStaker memory);
}
contract C4 is Test {
IOldRollup oldRollup;
address admin;
function setUp() public {
uint256 forkId = vm.createFork("https://rpc.ankr.com/eth");
vm.selectFork(forkId);
oldRollup = IOldRollup(0x5eF0D09d1E6204141B4d37530808eD19f60FBa35);
admin = 0x3ffFbAdAF827559da092217e474760E2b2c3CeDd;
}
function test_Cleanup() public {
vm.startPrank(admin);
oldRollup.pause();
uint64 stakerCount = oldRollup.stakerCount();
// since we for-loop these stakers we set an arbitrary limit - we dont
// expect any instances to have close to this number of stakers
if (stakerCount > 50) {
stakerCount = 50;
}
for (uint64 i = 0; i < stakerCount; i++) {
// FAILS with panic: array out-of-bounds access
address stakerAddr = oldRollup.getStakerAddress(i);
OldStaker memory staker = oldRollup.getStaker(stakerAddr);
if (staker.isStaked && staker.currentChallenge == 0) {
address[] memory stakersToRefund = new address[](1);
stakersToRefund[0] = stakerAddr;
oldRollup.forceRefundStaker(stakersToRefund);
}
}
}
}
Tools Used
Foundry
Recommended Mitigation Steps
function cleanupOldRollup() private {
IOldRollupAdmin(address(OLD_ROLLUP)).pause();
uint64 stakerCount = ROLLUP_READER.stakerCount();
// since we for-loop these stakers we set an arbitrary limit - we dont
// expect any instances to have close to this number of stakers
if (stakerCount > 50) {
stakerCount = 50;
}
+ for (uint64 i = 0; i < stakerCount;) {
address stakerAddr = ROLLUP_READER.getStakerAddress(i);
OldStaker memory staker = ROLLUP_READER.getStaker(stakerAddr);
if (staker.isStaked && staker.currentChallenge == 0) {
address[] memory stakersToRefund = new address[](1);
stakersToRefund[0] = stakerAddr;
IOldRollupAdmin(address(OLD_ROLLUP)).forceRefundStaker(stakersToRefund);
+ stakerCount -= 1;
+ } else {
+ i++;
+ }
}
godzillaba (Arbitrum) confirmed
gzeoneth (Arbitrum) commented:
Fixed with https://github.com/OffchainLabs/bold/pull/654/.
Keeping this report as best as the mitigation takes into account the
ifcondition.
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
The loop uses a fixed staker count while the underlying staker list shrinks as stakers are refunded, leading to an out-of-bounds access.
Impact
The upgrade and deployment of new BOLD contracts will fail, blocking the migration.
What the attacker needed
Old rollup has stakers; caller is admin
The fix
The loop should adjust the staker count when a staker is removed and only increment the index otherwise; the text states this was fixed.
How it was found
- Pause the old rollup contract.
- Read the total staker count and cap it at 50.
- Loop over indices from 0 to the capped count.
- For each staker, if staked and not in a challenge, call forceRefundStaker.
- forceRefundStaker removes the staker from the list, shrinking it.
- The loop continues to the original count and fails with an out-of-bounds error.
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.
A smart contract tries to refund all stakers by looping over a list. But each refund removes that staker from the list, so the list gets shorter while the loop keeps using the old length. Eventually the loop asks for an item that no longer exists, and the whole upgrade fails.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.