Submitted by anon, also found by rvierdiiev, hals (1, 2), Koolex, cccz, AkshaySrivastav, bin2chen, 0xsomeone, Aymen0909, brgltd, shealtielanz, zzzitron, peanuts, Mohandes, tsvetanovv, J4de, ladboy233, and max10afternoon
When depositing an ERC20 token into the L1ERC20Bridge, the _verifyDepositLimit function plays a pivotal role. This functions primary purpose is to ascertain whether there is a predefined deposit limit in place for the given token and, if such a cap exists, to determine if the users cumulative deposit amount surpasses this imposed restriction. The users total deposited amount is meticulously tracked and maintained using the totalDepositedAmountPerUser mapping.
https://github.com/code-423n4/2023-10-zksync/blob/main/code/contracts/ethereum/contracts/bridge/L1ERC20Bridge.sol#L188
https://github.com/code-423n4/2023-10-zksync/blob/main/code/contracts/ethereum/contracts/bridge/L1ERC20Bridge.sol#L348
This same mechanism is applied when a user seeks to reclaim their failed deposit. In this scenario, the claimFailedDeposit function is invoked, which once again triggers the _verifyDepositLimit function. However, in this context, the objective is to adjust the totalDepositedAmountPerUser mapping by decreasing the total deposited amount due to the claiming failed transaction.
https://github.com/code-423n4/2023-10-zksync/blob/main/code/contracts/ethereum/contracts/bridge/L1ERC20Bridge.sol#L278
https://github.com/code-423n4/2023-10-zksync/blob/main/code/contracts/ethereum/contracts/bridge/L1ERC20Bridge.sol#L345
This implementation can give rise to significant issues, which can be illustrated through the following scenarios:
Scenario 1: Suppose theres no deposit limitation imposed on tokenX, indicated by limitData.depositLimitation = false. Alice (an honest user) initiates a deposit of 100 tokenX. However, these deposits unfortunately fail. At a later point, the owner decides to enforce a deposit limit on this token by employing the setDepositLimit function within the AllowList contract. Now, Alice seeks to claim her previously failed deposits. However, when the _verifyDepositLimit function is invoked during this process, the if-condition is met. Yet, the issue arises when the amount 100 is going to be deducted from totalDepositedAmountPerUser[tokenX][Alice] (which equals 0). This results in an underflow, causing the transaction to revert. So, Alice can not claim her failed deposit in this scenario.
In summary, if there was no deposit limit set for a token initially, and users experienced failed deposit transactions for that token, they encounter difficulties in claiming those failed deposits when a deposit limit is subsequently enforced.
Scenario 2: Consider a scenario in which theres no deposit limitation for tokenY initially. However, Bob (a malicious user) monitors the owners transaction that is going to set a deposit limit for tokenY, specifying tokenDeposit[tokenY].depositCap = 100. Immediately, Bob deposits 100 tokenY three times, a total of 300 tokenY, deliberately ensuring that these transactions fail on L2 due to a low _l2TxGasLimit. Subsequently, the owners transaction to establish a deposit limit of 100 for tokenY is executed. Now, Bob decides to deposit another 100 tokenY, resulting in totalDepositedAmountPerUser[tokenY][Bob] being set to 100.
However, Bob intends to deposit another 100 tokenY. As the condition require(totalDepositedAmountPerUser[_l1Token][_depositor] + _amount <= limitData.depositCap, "d1"); is examined, this requirement stipulates that totalDepositedAmountPerUser[tokenY][Bob] + 100 must not exceed the deposit cap of 100. Bobs attempt to deposit an additional 100 tokenY is therefore denied.
Nonetheless, Bob realizes he can exploit a potential vulnerability. He decides to claim one of his three failed transactions that occurred prior to the deposit cap set up. As a result, totalDepositedAmountPerUser[tokenY][Bob] decreases by 100, effectively resetting it to 0. Now Bob can once again deposit 100 tokenY, despite having previously reached the imposed deposit limit. Bob continues this process, claiming his failed deposits after each attempt, thus resetting totalDepositedAmountPerUser[tokenY][Bob] and allowing him to deposit 400 tokenY, exceeding the deposit limit of 100.
In summary, this implementation opens the door for malicious users to circumvent deposit limits by arranging failed deposits prior to the imposition of such limits. Subsequently, by claiming these failed deposits, they can effectively increase their deposit limit by resetting totalDepositedAmountPerUser.
The function _verifyDepositLimit should be revised such that it tracks the deposited amount of users even if there is no deposit limitation in place.
https://github.com/code-423n4/2023-10-zksync/blob/main/code/contracts/ethereum/contracts/zksync/facets/Mailbox.sol#L275
Context
miladpiri (zkSync) confirmed, but disagreed with severity and commented:
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
