Submitted by chainlight
When calculating the remainder of the div instruction, the circuit needs to verify that the remainder is less than the divisor. It does this by subtracting the divisor from the remainder and enforcing that the borrow flow is true.
https://github.com/code-423n4/2023-10-zksync/blob/main/code/era-zkevm_circuits/src/main_vm/opcodes/mul_div.rs#L314
However, the code fails to range constrain the result of the subtraction. This allows subtraction_result_unchecked to contain limbs that are not representable by a 32-bit unsigned integer. We can use this to force remainder_is_less_than_divisor to be true even if the remainder is actually larger than the divisor.
A malicious validator could generate and submit a proof with incorrect behavior of the div instruction. This would allow the validator to manipulate the behavior of smart contracts that use a div instruction. For example, the validator could manipulate the calculated price during the execution of an on-chain DEX and steal all of the assets in the DEX. Since every smart contract that uses a div instruction is affected, it is impossible to enumerate all potential impacts.
For the ease of testing, we forked the zkSync Era test harness into a monorepo containing the VM and circuit code: https://github.com/chainlight-io/zksync-era-boojum-test-harness. The patch below can be applied to the test code to demonstrate the vulnerability:
We demonstrate the vulnerability by modifying the witness generation code to generate witnesses that should not be provable when the value 1337 is in a source operand. There are two tests that will run: source value of 1336 to show the normal behavior, and source value of 1337 to show the vulnerable behavior.
The relevant output of run.sh is the VM registers during the execution trace and is included below:
We see that the result of the div instruction in the normal example is as expected: 1336 / 1336 = 1. However, in the vulnerable example, the result is incorrect: 1337 / 1337 = 1337. While we chose to set the result to the same value as the source operand, it could be other values as well.
The subtraction_result_unchecked variable should be range constrained. An example fix might look like:
miladpiri (zkSync) confirmed
Alex the Entreprenerd (judge) commented:
