Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent heap OOB read in TFLite's gather_nd.cc. #51324

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions tensorflow/lite/kernels/gather_nd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ TfLiteStatus GatherNdString(const TfLiteTensor* params,
template <typename IndicesT>
TfLiteStatus EvalGatherNd(TfLiteContext* context, const TfLiteTensor* params,
const TfLiteTensor* indices, TfLiteTensor* output) {
bool indices_has_only_positive_elements = true;
const auto* indices_values = GetTensorData<IndicesT>(indices);
const size_t num_indices = indices->bytes / sizeof(IndicesT);
for (size_t i = 0; i < num_indices; i++) {
if (indices_values[i] < 0) {
indices_has_only_positive_elements = false;
break;
}
}
TF_LITE_ENSURE(context, indices_has_only_positive_elements);

switch (params->type) {
case kTfLiteFloat32:
return GatherNd<float, IndicesT>(params, indices, output);
Expand Down