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:
- State Update Lag: The global
balanceis updated after external calls return. - Lack of Reentrancy Guard: The contract lacks a global
nonReentrantmodifier, 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).
- Attacker calls
unstake(Service A). _withdrawreadsbalance(e.g., 100 ETH) intoupdatedBalance.- The contract sends rewards for Service A.
- The attacker intercepts control in the
receive()fallback and callsunstake(Service B). - Reentrancy: The inner
unstake(Service B)execution reads the stale globalbalance(still 100 ETH) because the outer execution has not yet updated it. - The inner execution calculates the new balance (e.g., 90 ETH) and writes it to storage.
- The inner execution finishes. Control returns to the outer
unstake(Service A). - State Overwrite: The outer execution continues using its local
updatedBalance(calculated based on the initial 100 ETH). It overwrites the globalbalance(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
- Add Reentrancy Guard: Inherit from
ReentrancyGuardand apply thenonReentrantmodifier to all external state-changing functions (stake,unstake,claim,checkpointAndClaim,forcedUnstake). - Fix CEI Pattern: Refactor
_withdrawto update the global state before performing external transfers.
Proof of Concept
View detailed Proof of Concept
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
- The attacker calls unstake for Service A.
- The withdrawal function reads the current global balance into a local variable.
- The contract sends rewards for Service A.
- The attacker re-enters from the receive fallback and calls unstake for Service B.
- The inner call reads the stale balance, calculates a lower balance and writes it to storage.
- 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.