# Attacker can manipulate the sorted queue in log sorter to emit reverted logs and events

zkSync log sorter circuit lacks constraints, letting an attacker order reverted write/rollback pairs as `wr rw` so reverted L1 logs and events are emitted.

- Payout: Contest reward
- Severity: high
- Program: zkSync
- Credited: xuwinnie
- Disclosed: 2024-02-29
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2023-10-zksync-findings/issues/761
- Library page: https://bugbountylibrary.com/r/c4%3A2023-10-zksync%3AH-02
- Tags: domain:infra domain:web3 impact:integrity kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:business-logic

**Target:** zkSync log sorter ZK circuit (sorted queue constraints)
**Root cause:** The circuit only enforces ascending timestamps and adjacency of equal-timestamp write/rollback pairs, but never requires the first popped element to be a write nor forbids two consecutive rollbacks, so a `wr rw` ordering passes all constraints.
**Steps:**
1. Observe constraints: ascending timestamps, equal-key pairs must be write-then-rollback, equal timestamps share written value, and a write is added to result only if next item differs and isn't rollback.
2. Construct sorted queue `wr rw wr rw` where each adjacent pair shares a timestamp.
3. Verify all constraints pass: keys ascending, equal-key pairs are write/rollback, values equal.
4. Result: the second and fourth (reverted) logs get added to the result queue, emitting reverted L1 logs/events.
**How found:** Code-reading of the log sorter circuit constraints; noticed the enforcement only covers ascending order and equal-timestamp adjacency, then hypothesized an interleaved `wr rw` ordering that satisfies every constraint but violates intended semantics.
**Exploitation:** Attacker submits a crafted sorted queue ordering reverted write/rollback pairs as `wr rw`, satisfying all circuit constraints while causing reverted L1 logs and events to be emitted to the result queue, producing undefined behavior potentially leading to exploits.
**Insight:** When auditing ZK circuits, check that constraints fully encode the intended state machine—here the missing invariant is 'first popped element must be a write' and 'no two consecutive rollbacks'; look for orderings that satisfy local pairwise constraints but violate global semantics.
