User might be able to double withdraw during migration
During the zkSync Era upgrade, a user could finalize an ERC20 withdrawal on the old L1ERC20Bridge and then finalize the same withdrawal again on the new L1SharedBridge if the withdrawal's L2 batch number equals or exceeds the predefined eraFirstPostUpgradeBatch. This allows a double withdrawal of funds under specific edge conditions.
Submitted by oakcobalt
Impact
A user might be able to double withdraw during migration in some edge conditions: (1) if their withdrawal tx is included in a batch number the same or after eraFirstPostUpgradeBatch; (2)And if the user finalizeWithdrawal on the old L1ERC20Bridge.sol before L1ERC20Bridge.sol is upgraded.
Proof of Concept
The current migration process takes place in steps, which might allow edge conditions to occur. StepA: deploy new contracts(including L1SharedBridge); StepB:Era upgrade and L2 system contracts upgrade(at this point old L1ERC20Bridge still works); StepC: Upgrade L2 bridge and L1ERC20Brdige.sol. Then migrate funds to the new L1sharedBridge.
Since L1ERC20Bridge.sol is upgraded at the end. An edge condition can occur between StepA and StepB, where a user can still withdraw ERC20 tokens on the old L1ERC20Brdige.sol.
Scenario: If a user finalizeWithdrawal ERC20 tokens on the old L1ERC20Bridge.sol first, they can withdraw again on L1SharedBridge.sol as long as the _l2batchNumber of the withdraw tx equals or is greater than eraFirstPostUpgradeBatch. In other words, _isEraLegacyWithdrawal check can be invalidated.
- stepA: L1SharedBridge.sol will be deployed and initialized with a pre-determined value
eraFirstPostUpgradeBatch; - UserA can initiate an ERC20 withdrawal tx on L2 bridge(currently old version);
- stepB: Era upgrade is performed. UserA’s withdrawal tx is included in the same
eraFirstPostUpgradeBatchnumber; - UserA
finializeWithdrawalon the old L1ERC20Brdige.sol.UserA should receive funds because funds haven’t been migrated yet; - stepC: new L1ERC20Bridge.sol is upgraded and funds(ERC20) are migrated to L1SharedBridge.sol;
- UserA
finializeWithdrawalon L1SharedBridge.sol. This time,_isEraLegacyWithdrawal(_chainId, _l2BatchNumber)check will be bypassed,because user’s_l2BatchNumber==eraFirstPostUpgradeBatch; UserA can receive the withdrawal funds a second time;
//code/contracts/ethereum/contracts/bridge/L1SharedBridge.sol
function finalizeWithdrawal(
uint256 _chainId,
uint256 _l2BatchNumber,
uint256 _l2MessageIndex,
uint16 _l2TxNumberInBatch,
bytes calldata _message,
bytes32[] calldata _merkleProof
) external override {
//@audit-info when _l2BatchNumber>= eraFirstPostUpgradeBatch, `_isEraLegacyWithdrawal()` return false, checking on withdrawal status on legacyERC20bridge will be bypassed.
|> if (_isEraLegacyWithdrawal(_chainId, _l2BatchNumber)) {
require(
!legacyBridge.isWithdrawalFinalized(
_l2BatchNumber,
_l2MessageIndex
),
"ShB: legacy withdrawal"
);
}
_finalizeWithdrawal(
_chainId,
_l2BatchNumber,
_l2MessageIndex,
_l2TxNumberInBatch,
_message,
_merkleProof
);
}
//code/contracts/ethereum/contracts/bridge/L1SharedBridge.sol
function _isEraLegacyWithdrawal(
uint256 _chainId,
uint256 _l2BatchNumber
) internal view returns (bool) {
return
(_chainId == ERA_CHAIN_ID) &&
|> (_l2BatchNumber < eraFirstPostUpgradeBatch); //@audit-info note:when _l2BatchNumber>= eraFirstPostUpgradeBatch, `_isEraLegacyWithdrawal()` return false.
}
As seen, checking on legacyERC20bridge withdrawal status will only be performed when _isEraLegacyWithdrawal returns true. But due to _l2BatchNumber of a withdrawal tx during the upgrade can equal or be greater than a predefined eraFirstPostUpgradeBatch. _isEraLegacyWithdrawal check can be based on false assumptions on _l2BatchNumber and eraFirstPostUpgradeBatch, allowing double withdrawal on edge cases.
Recommended Mitigation Steps
Consider adding a grace period during and following an upgrade, during which time legacyWithdrawal status will always be checked.
razzorsec (zkSync) confirmed, but disagreed with severity and commented:
Realistically invalid, since we will not finalize the upgrade batch. But an interesting thing to remember, so we would consider this as Low, as it is mostly about managing the server.
The Warden specifies that under certain circumstances, it is possible to perform a duplicate withdrawal when the system is in the midst of an upgrade.
The Sponsor specifies that this issue is realistically invalid, however, the Sponsor’s statement relies on being aware of the flaw and acting actively against it (i.e. not finalizing the upgrade batch) which I do not consider adequate justification to lower the severity of this exhibit.
I believe a medium-risk grade is appropriate based on the fact that it illustrates a code flaw that will arise under operations permitted by the smart contracts which we cannot presume the Sponsor was aware of.
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
Likely the legacy withdrawal status check is only performed when the L2 batch number is less than eraFirstPostUpgradeBatch, so withdrawals in batches at or after that number bypass the check and can be finalized on both bridges.
Impact
A user could withdraw the same ERC20 tokens twice, but the sponsor states this is realistically invalid because they will not finalize the upgrade batch, and the judge rated it medium.
What the attacker needed
Withdrawal tx in batch >= eraFirstPostUpgradeBatch; finalize on old bridge before upgrade; upgrade batch not finalized (sponsor condition)
The fix
The report recommends adding a grace period during and after an upgrade where the legacy withdrawal status is always checked.
How it was found
- Deploy and initialize L1SharedBridge with a predefined eraFirstPostUpgradeBatch.
- User initiates an ERC20 withdrawal on the old L2 bridge.
- Perform Era upgrade so the withdrawal transaction is included in the same eraFirstPostUpgradeBatch number.
- User finalizes the withdrawal on the old L1ERC20Bridge and receives funds.
- Upgrade L1ERC20Bridge and migrate ERC20 funds to L1SharedBridge.
- User finalizes the same withdrawal on L1SharedBridge, bypassing the legacy withdrawal check, and receives funds a second time.
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.
When a blockchain system is upgraded, old and new versions of a bridge contract can briefly coexist. This report shows that if a user withdraws tokens using the old contract, they might be able to withdraw the same tokens again using the new contract because a safety check is skipped for certain batch numbers. The result is that the user gets paid twice.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.