Repository navigation
Conversation
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.
There was a problem hiding this comment.
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.
| 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()); | ||
| } |
There was a problem hiding this comment.
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());
}
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 aProgramproto where required keys (e.g.,exponent,phase_exponent,control_qubits) are missing,.at()throws astd::out_of_rangeexception. Within TensorFlow custom Ops, throwing an unhandled exception inside a thread pool (e.g., viaTransformRangeConcurrently) 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 usingOP_REQUIRESor returning a failingStatus.This work was assisted by Google Gemini.