CVE-2021-37687 (C++, published 2021-08-12T23:15Z)
Description: value: TensorFlow is an end-to-end open source platform for machine learning. In affected versions TFLite's [`GatherNd` implementation](https://github.com/tensorflow/tensorflow/blob/149562d49faa709ea80df1d99fc41d005b81082a/tensorflow/lite/kernels/gather_nd.cc#L124) does not support negative indices but there are no checks for this situation. Hence, an attacker can read arbitrary data from the heap by carefully crafting a model with negative values in `indices`. Similar issue exists in [`Gather` implementation](https://github.com/tensorflow/tensorflow/blob/149562d49faa709ea80df1d99fc41d005b81082a/tensorflow/lite/kernels/gather.cc). We have patched the issue in GitHub commits bb6a0383ed553c286f87ca88c207f6774d5c4a8f and eb921122119a6b6e470ee98b89e65d721663179d. The fix will be included in TensorFlow 2.6.0. We will also cherrypick this commit on TensorFlow 2.5.1, TensorFlow 2.4.3, and TensorFlow 2.3.4, as these are also affected and still in supported range.
Severity: LOW (CVSS v3 base score: 5.5)
Weakness: CWE-125 \u2014 Out-of-bounds Read
The product reads data past the end, or before the beginning, of the intended buffer.
Repository: https://github.com/tensorflow/tensorflow
Files changed:
tensorflow/lite/kernels/gather.cc
Commit message: Prevent heap OOB read in TFLite's `gather.cc`.

Passing negative indices is illegal but there was a missing check so that resulted in OOB accesses.

PiperOrigin-RevId: 387231300
Change-Id: I3111b54b2f232638d795be17efc46abe4ede6bf8


Vulnerable code:
TfLiteStatus Gather(const TfLiteGatherParams& params, const TfLiteTensor* input,
                    const TfLiteTensor* positions, TfLiteTensor* output) {
        return Gather<float, int32_t>(*params, input, positions, output);
        return Gather<uint8_t, int32_t>(*params, input, positions, output);
        return Gather<int8_t, int32_t>(*params, input, positions, output);
        return Gather<int16_t, int32_t>(*params, input, positions, output);
        return Gather<int32_t, int32_t>(*params, input, positions, output);
        return Gather<int64_t, int32_t>(*params, input, positions, output);
        return Gather<bool, int32_t>(*params, input, positions, output);
        return Gather<float, int64_t>(*params, input, positions, output);
        return Gather<uint8_t, int64_t>(*params, input, positions, output);
        return Gather<int8_t, int64_t>(*params, input, positions, output);
        return Gather<int16_t, int64_t>(*params, input, positions, output);
        return Gather<int32_t, int64_t>(*params, input, positions, output);
        return Gather<int64_t, int64_t>(*params, input, positions, output);
        return Gather<bool, int64_t>(*params, input, positions, output);

Fixed code:
TfLiteStatus Gather(TfLiteContext* context, const TfLiteGatherParams& params,
                    const TfLiteTensor* input, const TfLiteTensor* positions,
                    TfLiteTensor* output) {
  const PositionsT* indexes = GetTensorData<PositionsT>(positions);
  bool indices_has_only_positive_elements = true;
  const size_t num_indices = positions->bytes / sizeof(PositionsT);
  for (size_t i = 0; i < num_indices; i++) {
    if (indexes[i] < 0) {
      indices_has_only_positive_elements = false;
      break;
    }
  }
  TF_LITE_ENSURE(context, indices_has_only_positive_elements);


  bool indices_has_only_positive_elements = true;
  const size_t num_indices = positions->bytes / sizeof(PositionT);
  for (size_t i = 0; i < num_indices; i++) {
    if (indexes[i] < 0) {
      indices_has_only_positive_elements = false;
      break;
    }
  }
  TF_LITE_ENSURE(context, indices_has_only_positive_elements);

        return Gather<float, int32_t>(context, *params, input, positions,
                                      output);
        return Gather<uint8_t, int32_t>(context, *params, input, positions,
                                        output);
        return Gather<int8_t, int32_t>(context, *params, input, positions,
                                       output);
        return Gather<int16_t, int32_t>(context, *params, input, positions,
                                        output);
        return Gather<int32_t, int32_t>(context, *params, input, positions,
                                        output);
        return Gather<int64_t, int32_t>(context, *params, input, positions,
                                        output);
        return Gather<bool, int32_t>(context, *params, input, positions,
                                     output);
        return Gather<float, int64_t>(context, *params, input, positions,
                                      output);
        return Gather<uint8_t, int64_t>(context, *params, input, positions,
                                        output);
        return Gather<int8_t, int64_t>(context, *params, input, positions,
                                       output);
        return Gather<int16_t, int64_t>(context, *params, input, positions,
                                        output);
        return Gather<int32_t, int64_t>(context, *params, input, positions,
                                        output);
        return Gather<int64_t, int64_t>(context, *params, input, positions,
                                        output);
        return Gather<bool, int64_t>(context, *params, input, positions,
                                     output);