CVE-2022-29208 (Python, published 2022-05-20T23:15Z)
Description: value: TensorFlow is an open source platform for machine learning. Prior to versions 2.9.0, 2.8.1, 2.7.2, and 2.6.4, the implementation of `tf.raw_ops.EditDistance` has incomplete validation. Users can pass negative values to cause a segmentation fault based denial of service. In multiple places throughout the code, one may compute an index for a write operation. However, the existing validation only checks against the upper bound of the array. Hence, it is possible to write before the array by massaging the input to generate negative values for `loc`. Versions 2.9.0, 2.8.1, 2.7.2, and 2.6.4 contain a patch for this issue.
Severity: LOW (CVSS v3 base score: 7.1)
Weakness: CWE-787 \u2014 Out-of-bounds Write
The product writes data past the end, or before the beginning, of the intended buffer.
Repository: https://github.com/tensorflow/tensorflow
Files changed:
tensorflow/core/kernels/edit_distance_op.cc
tensorflow/python/kernel_tests/array_ops/edit_distance_op_test.py
Commit message: Fix tf.raw_ops.EditDistance vulnerability with negative indices.

Check that indices are non-negative. Fix several identical code sites.
Clean up grammar in error message.

PiperOrigin-RevId: 445442017


Vulnerable code:
            ctx, loc < output_elements,
                             " which would require in writing to outside of "
            ctx, loc < output_elements,
                             " which would require in writing to outside of "
            ctx, loc < output_elements,
                             " which would require in writing to outside of "
          ctx, loc < output_elements,
                           " which would require in writing to outside of the "
          ctx, loc < output_elements,
                           " which would require in writing to outside of the "

Fixed code:
            ctx, 0 <= loc && loc < output_elements,
                             " which would require writing to outside of "
            ctx, 0 <= loc && loc < output_elements,
                             " which would require writing to outside of "
            ctx, 0 <= loc && loc < output_elements,
                             " which would require writing to outside of "
          ctx, 0 <= loc && loc < output_elements,
                           " which would require writing to outside of the "
          ctx, 0 <= loc && loc < output_elements,
                           " which would require writing to outside of the "
  def testEditDistanceBadIndices(self):
    hypothesis_indices = np.full((3, 3), -1250999896764, dtype=np.int64)
    hypothesis_values = np.empty(3, dtype=np.int64)
    hypothesis_shape = np.empty(3, dtype=np.int64)
    truth_indices = np.full((3, 3), -1250999896764, dtype=np.int64)
    truth_values = np.full([3], 2, dtype=np.int64)
    truth_shape = np.full([3], 2, dtype=np.int64)
    expected_output = []  # dummy; ignored

    self._testEditDistance(
        hypothesis=(hypothesis_indices, hypothesis_values, hypothesis_shape),
        truth=(truth_indices, truth_values, truth_shape),
        normalize=False,
        expected_output=expected_output,
        expected_err_re=(r"inner product -\d+ which would require writing "
                         "to outside of the buffer for the output tensor")
    )