CVE-2022-35940 (C++, published 2022-09-16T20:15Z)
Description: value: TensorFlow is an open source platform for machine learning. The `RaggedRangOp` function takes an argument `limits` that is eventually used to construct a `TensorShape` as an `int64`. If `limits` is a very large float, it can overflow when converted to an `int64`. This triggers an `InvalidArgument` but also throws an abort signal that crashes the program. We have patched the issue in GitHub commit 37cefa91bee4eace55715eeef43720b958a01192. The fix will be included in TensorFlow 2.10.0. We will also cherrypick this commit on TensorFlow 2.9.1, TensorFlow 2.8.1, and TensorFlow 2.7.2, as these are also affected and still in supported range. There are no known workarounds for this issue.
Severity: nan (CVSS v3 base score: 7.5)
Weakness: CWE-190 \u2014 Integer Overflow or Wraparound
The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
Repository: https://github.com/tensorflow/tensorflow
Files changed:
tensorflow/core/kernels/ragged_range_op.cc
tensorflow/core/kernels/ragged_range_op_test.cc
tensorflow/python/ops/ragged/ragged_range_op_test.py
Commit message: [security] Fix int overflow in RaggedRangeOp.

PiperOrigin-RevId: 461749624


Vulnerable code:
      rt_nested_splits(row + 1) =
          rt_nested_splits(row) + RangeSize(start, limit, delta);

 private:
  // Returns the number of elements in the specified range.
  SPLITS_TYPE RangeSize(T start, T limit, T delta) {
    if (((delta > 0) && (limit < start)) || ((delta < 0) && (limit > start))) {
      return 0;
    }
    // The following is copied from tensorflow::RangeOp::Compute().
    return (std::is_integral<T>::value
                ? ((std::abs(limit - start) + std::abs(delta) - 1) /
                   std::abs(delta))
                : std::ceil(std::abs((limit - start) / delta)));
  }
    self.assertAllEqual(
        ragged_math_ops.range(0, 5, 1), [list(range(0, 5, 1))])

Fixed code:
#include <cstdint>
      int64_t size;  // The number of elements in the specified range.
      if (((delta > 0) && (limit < start)) ||
          ((delta < 0) && (limit > start))) {
        size = 0;
      } else if (std::is_integral<T>::value) {
        // The following is copied from tensorflow::RangeOp::Compute().
        size = Eigen::divup(Eigen::numext::abs(limit - start),
                            Eigen::numext::abs(delta));
      } else {
        // The following is copied from tensorflow::RangeOp::Compute().
        auto size_auto =
            Eigen::numext::ceil(Eigen::numext::abs((limit - start) / delta));
        OP_REQUIRES(
            context, size_auto <= std::numeric_limits<int64_t>::max(),
            errors::InvalidArgument("Requires ((limit - start) / delta) <= ",
                                    std::numeric_limits<int64_t>::max()));
        size = static_cast<int64_t>(size_auto);
      }
      rt_nested_splits(row + 1) = rt_nested_splits(row) + size;
#include <gtest/gtest.h>
TEST_F(RaggedRangeOpTest, RangeSizeOverflow) {
  BuildRaggedRangeGraph<float>();
  AddInputFromArray<float>(TensorShape({2}), {1.1, 0.1});    // starts
  AddInputFromArray<float>(TensorShape({2}), {10.0, 1e10});  // limits
  AddInputFromArray<float>(TensorShape({2}), {1, 1e-10});    // deltas

  EXPECT_EQ(absl::StrCat("Requires ((limit - start) / delta) <= ",
                         std::numeric_limits<int64_t>::max()),
            RunOpKernel().error_message());
}

    self.assertAllEqual(ragged_math_ops.range(0, 5, 1), [list(range(0, 5, 1))])
    with self.assertRaisesRegex(errors.InvalidArgumentError,
                                r'Requires \(\(limit - start\) / delta\) <='):
      self.evaluate(ragged_math_ops.range(0.1, 1e10, 1e-10))