Skip to content
Smart contracts Audit finding Advanced

Wrong encoding of the data in the sendCompressedBytecode function

The bootloader's sendCompressedBytecode function lacked a check that originalBytecodeOffset equals afterSelectorPtr + 64 and used unsafe arithmetic, allowing the operator to pass extra bytes or malformed calldata. This could let the operator spend part of the transaction gas limit or force the Compressor::publishCompressedBytecode call to revert.

Audittens Follow

Originally disclosed on Code4rena

Submitted by Audittens

Lines of code

https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/bootloader/bootloader.yul#L1244-L1245 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/bootloader/bootloader.yul#L1255-L1259 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/bootloader/bootloader.yul#L1365 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/bootloader/bootloader.yul#L1542 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/scripts/process.ts#L123 https://github.com/code-423n4/2023-10-zksync/blob/1fb4649b612fac7b4ee613df6f6b7d921ddd6b0d/code/system-contracts/contracts/Compressor.sol#L54-L82

Description

There is enforcement in the bootloader::sendCompressedBytecode function that the data to be used for the call of the Compressor::publishCompressedBytecode function is encoded in a standard way.

But this check contains bugs:

  1. There is no check that the originalBytecodeOffset is equal to afterSelectorPtr + 64. This leads to the possibility of passing a great number of unused bytes as calldata of the call of the Compressor::publishCompressedBytecode function. In this case, the calldata will have the following format:
4                               bytes : `publishCompressedBytecode` selector
32                              bytes : offset for `_bytecode` parameter                                                  = V
32                              bytes : offset for `_rawCompressedData` parameter                                         = V + 32 + rounded_len(_bytecode)
(V - 64)                        bytes : any bytes that will be ignored in the `publishCompressedBytecode` function
32                              bytes : length of `_bytecode` parameter                                                   = len(_bytecode)
rounded_len(_bytecode)          bytes : `_bytecode` parameter                                                             = _bytecode
32                              bytes : length of `_rawCompressedData` parameter                                          = len(_rawCompressedData)
rounded_len(_rawCompressedData) bytes : `_rawCompressedData` parameter                                                    = _rawCompressedData
  1. There are used unsafe “raw” arithmetic operations in the calculations of the offsets, while the safe analogs should be used. This leads to the possibility of passing absolutely wrong data so the call of the Compressor::publishCompressedBytecode function will revert. As this bug does not provide any strong attack vector, I will present only the idea of how to achieve this, not the whole calldata. The offset of the _bytecode parameter should be equal (2**256) - 64 (so the originalBytecodeOffset will pass the checkOffset check), rest of the parameters is easy to determine over this. The same happens in the bootloader::validateBytes function, as in the bootloader::lengthRoundedByWords function the overflow happens, leading to the same outcome.

Impact

The ability of the operator to pass up to ~COMPRESSED_BYTECODES_SLOTS * 32 = ~1048576 additional bytes into the call of the Compressor::publishCompressedBytecode function. Such possibility of spending part of the transaction gas limit makes the operator able to actually decrease the L2 transaction gas limit, which leads to the possibility of manipulation of the amount of gas used for the transaction execution.

The ability of the operator to pass encoding-related checks of the bootloader::sendCompressedBytecode function, which leads to the possibility of forcing the Compressor::publishCompressedBytecode function call to revert.

Recommended Mitigation Steps

  1. You can add the following check into the bootloader::sendCompressedBytecode function:
if iszero(eq(add(afterSelectorPtr, 64), originalBytecodeOffset)) {
    assertionError("Compression calldata incorrect")
}
  1. You can use safeAdd instead of add in the corresponding calculations in the bootloader::sendCompressedBytecode function.

Alex the Entreprenerd (judge) commented:

The Warden has demonstrated how unsafe arithmetic could be used by the operator to pass incorrect data that “checks” down to the expected values. This could be used to pass incorrect compressed data as well as abuse gas costs as means to cause end users to be overcharged.

vladbochok (zkSync) confirmed


The original report as published on Code4rena, by Audittens. 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

Likely the bootloader's sendCompressedBytecode function did not verify that originalBytecodeOffset equals afterSelectorPtr + 64 and used unsafe arithmetic instead of safeAdd, allowing malformed calldata to pass checks.

Impact

The operator could pass up to about 1,048,576 additional bytes into the call, spending part of the transaction gas limit and potentially manipulating gas used for execution, or force the Compressor::publishCompressedBytecode call to revert.

What the attacker needed

Operator privileges

The fix

The bootloader should add a check that originalBytecodeOffset equals afterSelectorPtr + 64 and use safeAdd instead of add in the offset calculations.

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.

The bootloader is a special program that prepares transactions on zkSync. It checked some data before calling a compressor contract but missed a check on where the data starts and used unchecked maths. This let the operator add extra bytes or break the call, which could waste gas or make the call fail.

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 Audittens

View all

19 more disclosed reports, best first.

More from zkSync Era

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.