Skip to content
Smart contracts Audit finding Advanced

Insolvency via Cross-Service Reentrancy in StakingBase._withdraw

The staking contract's withdrawal function updates its recorded balance only after sending funds, so a caller who controls two services can re-enter during the transfer and have the outer call overwrite the inner call's balance deduction. This leaves the contract's recorded balance higher than the funds it actually holds, making it insolvent.

fullstop Follow with 0xbrett8571, 0xnija and 31 others

Originally disclosed on Code4rena

Submitted by fullstop, also found by 0xbrett8571, 0xnija, 1ultimat3, Agontuk, BlueSheep, brandon, bransdotcom, chuvak, coinsspor, Cryptor, demonhat, Dest1ny_rs, Diavolo, Drothon, edoscoba, eightzerofour, hexcoded0033, hiram, ht111111, ifex445, kind0dev, Konzy, LMLLayersystem, noxpenguin, psyone, recr4sh, rookishere, shalevhvs, shibu0x, SpicyMeatball, Tupaia, Yifan, and yonko

autonolas-registries/../contracts/staking/StakingBase.sol #L929-L948

Finding description and impact

The _withdraw function in StakingBase.sol violates the Checks-Effects-Interactions (CEI) pattern. It caches the global balance into a local variable updatedBalance, performs external calls (transfers) via a loop, and only updates the global balance state variable at the very end of the function.

Root Cause:

  1. State Update Lag: The global balance is updated after external calls return.
  2. Lack of Reentrancy Guard: The contract lacks a global nonReentrant modifier, allowing cross-service reentrancy even if single-service reentrancy is blocked by state deletion.

Attack Scenario:

An attacker controls two service IDs (Service A and Service B).

  1. Attacker calls unstake(Service A).
  2. _withdraw reads balance (e.g., 100 ETH) into updatedBalance.
  3. The contract sends rewards for Service A.
  4. The attacker intercepts control in the receive() fallback and calls unstake(Service B).
  5. Reentrancy: The inner unstake(Service B) execution reads the stale global balance (still 100 ETH) because the outer execution has not yet updated it.
  6. The inner execution calculates the new balance (e.g., 90 ETH) and writes it to storage.
  7. The inner execution finishes. Control returns to the outer unstake(Service A).
  8. State Overwrite: The outer execution continues using its local updatedBalance (calculated based on the initial 100 ETH). It overwrites the global balance (e.g., setting it to 90 ETH), effectively erasing the deduction made by Service B.

Impact:

The contract’s recorded balance becomes higher than its actual physical ETH balance (Insolvency). This accounting discrepancy accumulates with every attack.

Recommended mitigation steps

  1. Add Reentrancy Guard: Inherit from ReentrancyGuard and apply the nonReentrant modifier to all external state-changing functions (stake, unstake, claim, checkpointAndClaim, forcedUnstake).
  2. Fix CEI Pattern: Refactor _withdraw to update the global state before performing external transfers.

Proof of Concept

View detailed Proof of Concept


The original report as published on Code4rena, by fullstop. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 withdrawal function follows the wrong order: it sends funds before updating the global balance, and there is no reentrancy guard to stop a second call during the transfer.

Impact

The contract's recorded balance becomes higher than its actual ETH holdings, so it is insolvent, and the discrepancy grows with each attack.

What the attacker needed

Attacker controls two service IDs; attacker can receive ETH in a fallback

The fix

The withdrawal function should update the global balance before making external transfers, and the contract should use a reentrancy guard on all external state-changing functions.

How it was found

  1. The attacker calls unstake for Service A.
  2. The withdrawal function reads the current global balance into a local variable.
  3. The contract sends rewards for Service A.
  4. The attacker re-enters from the receive fallback and calls unstake for Service B.
  5. The inner call reads the stale balance, calculates a lower balance and writes it to storage.
  6. Control returns to the outer call, which overwrites the global balance with its own stale calculation.

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 keeps a running total of how much money it thinks it holds. This one pays out money before it updates that total, so an attacker can start a second withdrawal while the first is still paying out. The second withdrawal's update gets overwritten, and the contract ends up believing it has more money than it really does.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by fullstop

View all

6 more disclosed reports, best first.

More from Olas

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.