Unit difference between transaction encoding and bootloader memory constant
The overhead calculation for transactions in zkSync used inconsistent units: encoding length in bytes but bootloader memory in words (32 bytes). This caused the overhead to be calculated 32 times larger than intended, overcharging users for some transactions.
Submitted by minhtrng
Lines of code
https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/bootloader/bootloader.yul#L1847 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/contracts/ethereum/contracts/zksync/libraries/TransactionValidator.sol#L145
Impact
The overhead can be calculated incorrectly for some transactions and the user will get charged 32 times more in those cases than what he ought to be charged.
Proof of Concept
The overhead is calculated as the maximum of:
- Overhead for slot.
- Overhead for memory occupation in the bootloader.
- Overhead for single instance circuits.
2. is calculated like this:
Note: please see calculation in warden’s original submission.
In other words, the overhead for 2. is proportional to the percentage of bootloader memory that the transaction will use.
The issue is that in the actual implementation the units used for EncodingLength and BootloaderMemory are different. EncodingLength is denominated in bytes, while BootloaderMemory is denominated in words (32 bytes).
Looking at the implementations:
//TransactionValidator.getOverheadForTransaction
uint256 overheadForLength = Math.ceilDiv(_encodingLength * batchOverheadGas, BOOTLOADER_TX_ENCODING_SPACE);
//bootloader.getTransactionUpfrontOverhead
let overheadForLength := ceilDiv(
safeMul(txEncodeLen, totalBatchOverhead, "ad"),
BOOTLOADER_MEMORY_FOR_TXS()
)
_encodingLength/txEncodeLen are a result of abi.encode(transaction).length and are hence expressed in bytes. BOOTLOADER_TX_ENCODING_SPACE/BOOTLOADER_MEMORY_FOR_TXS() have the value 273132 in the current config. While the specific number may be subject to change, it is relevant that it is not expressed in bytes, but in words (32 bytes each).
The number 273132 is derived from the current occupation of the word indices [250141...523261] (difference is slightly off with 273120). Note that this range is derived from my own tracing of the bootloaders memory slots with the current config, so there might be a minor error in either the configuration or my own attempt of enumerating the slots. The docs state that [252189..523261] should be the range (difference 271072). Either way the minor difference is negligible and does not affect the stated impact. Due to the different units used, the denominator in above formula is 32 times smaller than it should be, hence, calculating a 32 times larger overhead
Recommended Mitigation Steps
Multiply BOOTLOADER_TX_ENCODING_SPACE and BOOTLOADER_MEMORY_FOR_TXS() by 32 when using them in the calculation. Alternatively, change those values to be in bytes from the get-go, which should be fine as they don’t seem to be used anywhere else currently
miladpiri (zkSync) confirmed and commented:
To be precise, the users might indeed get overcharged, though likely not catastrophically for a typical transaction. So, medium severity is fair.
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 overhead formula divided a byte-denominated encoding length by a word-denominated bootloader memory constant, causing a unit mismatch that inflated the result by a factor of 32.
Impact
Users could be overcharged by a factor of 32 for some transactions, though the project confirmed it is likely not catastrophic for a typical transaction.
What the attacker needed
None
The fix
The constants should be multiplied by 32 when used in the calculation, or redefined to be in bytes from the start.
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.
In zkSync, the fee for a transaction includes an overhead based on how much memory it uses in the bootloader. The code compared a size in bytes with a size in words (each word is 32 bytes), so the overhead was calculated as if the transaction used 32 times more memory than it actually did. This meant users could be charged much more than they should be.
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.