Repository navigation
Conversation
This fixes b/510434181, "Out-of-Bounds Iterator Arithmetic and Segfault
in getOpForPISP". The vulnerability report flagged the getOpForPISP
helper function for constructing new qubit ranges using arithmetic on
iterators (such as `cur_op_qubits.begin() + use_target`) without
validating that the array has sufficient elements. If a program were
submitted submits a program proto with a PISP gate containing fewer than
2 qubits (or 0), these expressions produce iterators outside valid
bounds.
Normally, Cirq in Python prevents you from putting a 2-qubit gate on 0,
1, or 3 qubits. However, tfq_ps_decompose accepts raw serialized
strings:(`tf.string`) directly from Python, so a caller or fuzzer or AI
agent can pass a hand-crafted Program protobuf where an Operation has
the wrong number of qubits attached. This is unlikely to happen in
realistic uses because other parts of TFQ would probably catch such
invalid constructs, but it is not too difficult to guard against, and we
may as well address this in-principle vulnerability to save ourselves
the trouble of dealing with future reports about the same issue.
This PR changes tfq_ps_decompose_op.cc as follows:
1. Inside the loop over operations in Compute, each of the four gate
branches now checks cur_op.qubits_size() (the C++ protobuf
equivalent of len(cur_op.qubits)) before doing anything else.
2. In getOpForPISP, replacing iterator math with direct indexing.
3. Report errors more safely across parallel CPU threads, using
NESTED_FN_STATUS_SYNC.
In addition to that, there are some new tests.
This work was done with the help of Google Gemini.
mhucka
marked this pull request as ready for review
October 29, 2026 04:44
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes b/510434181, "Out-of-Bounds Iterator Arithmetic and Segfault in getOpForPISP". The vulnerability report flagged the getOpForPISP helper function for constructing new qubit ranges using arithmetic on iterators (such as
cur_op_qubits.begin() + use_target) without validating that the array has sufficient elements. If a caller submits a program proto with a PISP gate containing fewer than 2 qubits (or 0), these expressions produce iterators outside valid bounds.Normally, Cirq in Python prevents you from putting a 2-qubit gate on 0, 1, or 3 qubits. However, tfq_ps_decompose accepts raw serialized strings (
tf.string) directly from Python, so a caller or fuzzer or AI agent can pass a hand-crafted Program protobuf where an Operation has the wrong number of qubits attached. This is unlikely to happen in realistic uses because other parts of TFQ would probably catch such invalid constructs, but it is not too difficult to guard against, and we may as well address this in-principle vulnerability to save ourselves the trouble of dealing with future reports about the same issue.This PR changes tfq_ps_decompose_op.cc as follows:
Inside the loop over operations in Compute, each of the four gate branches now checks cur_op.qubits_size() (the C++ protobuf equivalent of len(cur_op.qubits)) before doing anything else.
In getOpForPISP, iterator math has been replaced with direct indexing.
Errors are more safely reported across parallel CPU threads, using NESTED_FN_STATUS_SYNC.
In addition to that, there are some new tests.
This work was done with the help of Google Gemini.