Skip to content

Fix out-of-range exceptions due to missing Protobuf map keys - #1149

Open
mhucka wants to merge 3 commits into
tensorflow:masterfrom
mhucka:fix-b510432627
Open

mhucka wants to merge 3 commits into
tensorflow:masterfrom
mhucka:fix-b510432627

Conversation

@mhucka

@mhucka mhucka commented Nov 22, 2026

Copy link
Copy Markdown
Member

This is a fix for the vulnerability reported in b/510432627, "Unhandled std::out_of_range Exception due to Missing Protobuf Map Keys". According to the report, multiple C++ operations process serialized quantum circuits and access internal argument maps using .at(). If a user provides a Program proto where required keys (e.g., exponent, phase_exponent, control_qubits) are missing, .at() throws a std::out_of_range exception. Within TensorFlow custom Ops, throwing an unhandled exception inside a thread pool (e.g., via TransformRangeConcurrently) escapes the worker wrapper and abruptly terminates the entire python/TensorFlow process (std::terminate). This constitutes an exploitable Denial of Service.

This PR follows the remediation approach defined in the report, which is to use use .find() or .contains() to validate the map keys and return a graceful error using OP_REQUIRES or returning a failing Status.

This work was assisted by Google Gemini.

This is a fix for the vulnerability reported in b/510432627, "Unhandled
std::out_of_range Exception due to Missing Protobuf Map Keys". According
to the report, multiple C++ operations process serialized quantum
circuits and access internal argument maps using `.at()`. If a user
provides a `Program` proto where required keys (e.g., `exponent`,
`phase_exponent`, `control_qubits`) are missing, `.at()` throws a
`std::out_of_range` exception. Within TensorFlow custom Ops, throwing an
unhandled exception inside a thread pool (e.g., via
`TransformRangeConcurrently`) escapes the worker wrapper and abruptly
terminates the entire python/TensorFlow process (`std::terminate`). This
constitutes an exploitable Denial of Service.

This PR follows the remediation approach defined in the report, which is
to use use `.find()` or `.contains()` to validate the map keys and
return a graceful error using `OP_REQUIRES` or returning a failing
`Status`.

This work was assisted by Google Gemini.
@mhucka
mhucka marked this pull request as ready for review November 22, 2026 05:49

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves the robustness of circuit parsing and operation argument resolution by replacing direct map lookups with safe find checks across several files, including tfq_ps_decompose_op.cc, tfq_ps_weights_from_symbols_op.cc, circuit_parser_qsim.cc, and program_resolution.cc. Corresponding unit tests have also been added to verify that missing required arguments are handled gracefully and raise appropriate errors. A critical issue was identified in tfq_ps_weights_from_symbols_op.cc where the code logs an error status upon encountering missing arguments but fails to return early, which would cause the execution to proceed and dereference invalid iterators, leading to potential segmentation faults.

Comment on lines +97 to 124
const auto exp_it = cur_op_map.find("exponent");
if (TF_PREDICT_FALSE(exp_it == cur_op_map.end())) {
Status local = tensorflow::errors::InvalidArgument(
"Could not find arg: exponent in op.");
NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
}
const Arg& exponent = exp_it->second;
if (exponent.arg_case() == Arg::ArgCase::kSymbol) {
// this gate has parameterized exponent.
const absl::string_view symbol_name = exponent.symbol();
if (!symbols_map.contains(symbol_name)) {
const auto sym_it = symbols_map.find(symbol_name);
if (TF_PREDICT_FALSE(sym_it == symbols_map.end())) {
// Should never happen. raise error.
OP_REQUIRES(context, false,
tensorflow::errors::InvalidArgument(
"A circuit contains a sympy.Symbol not found "
"in symbols!"));
Status local = tensorflow::errors::InvalidArgument(
"A circuit contains a sympy.Symbol not found "
"in symbols!");
NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
}
const auto scalar_it = cur_op_map.find("exponent_scalar");
if (TF_PREDICT_FALSE(scalar_it == cur_op_map.end())) {
Status local = tensorflow::errors::InvalidArgument(
"Could not find arg: exponent_scalar in op.");
NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
}
output_results.at(i)
.at(symbols_map.at(symbol_name))
.push_back(cur_op.args()
.at("exponent_scalar")
.arg_value()
.float_value());
.at(sym_it->second)
.push_back(scalar_it->second.arg_value().float_value());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

If any of the required arguments (exponent, exponent_scalar, or the symbol in symbols_map) are missing, the code logs an error status but does not return or break. This causes the execution to proceed and dereference the end() iterator of the map (e.g., exp_it->second, sym_it->second, or scalar_it->second), leading to undefined behavior and a segmentation fault/crash. Adding return; statements after recording the error status is critical to prevent these crashes.

            const auto exp_it = cur_op_map.find("exponent");
            if (TF_PREDICT_FALSE(exp_it == cur_op_map.end())) {
              Status local = tensorflow::errors::InvalidArgument(
                  "Could not find arg: exponent in op.");
              NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
              return;
            }
            const Arg& exponent = exp_it->second;
            if (exponent.arg_case() == Arg::ArgCase::kSymbol) {
              // this gate has parameterized exponent.
              const absl::string_view symbol_name = exponent.symbol();
              const auto sym_it = symbols_map.find(symbol_name);
              if (TF_PREDICT_FALSE(sym_it == symbols_map.end())) {
                // Should never happen. raise error.
                Status local = tensorflow::errors::InvalidArgument(
                    "A circuit contains a sympy.Symbol not found "
                    "in symbols!");
                NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
                return;
              }
              const auto scalar_it = cur_op_map.find("exponent_scalar");
              if (TF_PREDICT_FALSE(scalar_it == cur_op_map.end())) {
                Status local = tensorflow::errors::InvalidArgument(
                    "Could not find arg: exponent_scalar in op.");
                NESTED_FN_STATUS_SYNC(parse_status, local, p_lock);
                return;
              }
              output_results.at(i)
                  .at(sym_it->second)
                  .push_back(scalar_it->second.arg_value().float_value());
            }

@mhucka mhucka changed the title Fix exceptions due to missing Protobuf map keys Fix out-of-range exceptions due to missing Protobuf map keys Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址