CVE-2022-35997: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. If `tf.sparse.cross` receives an input `separator` that is not a scalar, it gives a `CHECK` fail that can be used to trigger a denial of service attack. We have patched the issue in GitHub commit 83dcb4dbfa094e33db084e97c4d0531a559e0ebf. 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.
CWE description: The product contains an assert() or similar statement that can be triggered by an attacker, which leads to an application exit or other behavior that is more severe than necessary.
CVSS v3 base score: 7.5
Published: 2022-09-16T23:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/sparse_cross_op.cc, tensorflow/python/kernel_tests/sparse_ops/sparse_cross_op_test.py
Commit date: 2022-07-14 10:55:24 -0700
Commit message: Fix check failure in SparseCrossV2Op by adding check for scalar value for separator.

PiperOrigin-RevId: 461001180

Diff: diff --git a/tensorflow/core/kernels/sparse_cross_op.cc b/tensorflow/core/kernels/sparse_cross_op.cc
index 134d35172e02c..8a770020f0474 100644
--- a/tensorflow/core/kernels/sparse_cross_op.cc
+++ b/tensorflow/core/kernels/sparse_cross_op.cc
@@ -22,16 +22,18 @@ limitations under the License.
 
 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
 #include "tensorflow/core/framework/kernel_def_builder.h"
 #include "tensorflow/core/framework/op_def_builder.h"
 #include "tensorflow/core/framework/op_kernel.h"
+#include "tensorflow/core/framework/op_requires.h"
 #include "tensorflow/core/framework/tensor.h"
 #include "tensorflow/core/framework/tensor_shape.h"
 #include "tensorflow/core/framework/types.h"
 #include "tensorflow/core/framework/types.pb.h"
 #include "tensorflow/core/lib/core/stringpiece.h"
 #include "tensorflow/core/lib/strings/str_util.h"
+#include "tensorflow/core/platform/errors.h"
 #include "tensorflow/core/platform/fingerprint.h"
 #include "tensorflow/core/platform/strong_hash.h"
 #include "tensorflow/core/util/work_sharder.h"
 
 namespace tensorflow {
@@ -830,10 +832,14 @@ class SparseCrossV2Op : public OpKernel {
         context, ValidateInput(indices_list_in, values_list_in, shapes_list_in,
                                dense_list_in, internal_type));
 
     const Tensor* sep_t;
     OP_REQUIRES_OK(context, context->input("sep", &sep_t));
+    OP_REQUIRES(context, TensorShapeUtils::IsScalar(sep_t->shape()),
+                errors::InvalidArgument("Input separator should be a scalar. "
+                                        "Received: ",
+                                        sep_t->DebugString()));
     const tstring separator = sep_t->scalar<tstring>()();
 
     std::vector<std::unique_ptr<ColumnInterface<tstring>>> columns =
         GenerateColumnsFromInput<tstring>(indices_list_in, values_list_in,
                                           shapes_list_in, dense_list_in);
diff --git a/tensorflow/python/kernel_tests/sparse_ops/sparse_cross_op_test.py b/tensorflow/python/kernel_tests/sparse_ops/sparse_cross_op_test.py
index 28a133d982fea..94f170454d7b5 100644
--- a/tensorflow/python/kernel_tests/sparse_ops/sparse_cross_op_test.py
+++ b/tensorflow/python/kernel_tests/sparse_ops/sparse_cross_op_test.py
@@ -871,10 +871,18 @@ class SparseCrossV2OpTest(BaseSparseCrossOpTest):
         sep='_X_')
     out = sparse_tensor.SparseTensor(inds, vals, shapes)
     with self.cached_session():
       self._assert_sparse_tensor_empty(self.evaluate(out))
 
+  def testNonScalarInput(self):
+    with self.assertRaisesRegex(errors.InvalidArgumentError,
+                                'Input separator should be a scalar.'):
+      self.evaluate(sparse_ops.sparse_cross(
+          inputs=[],
+          name='a',
+          separator=constant_op.constant(['a', 'b'], dtype=dtypes.string)))
+
 
 class SparseCrossHashedOpTest(BaseSparseCrossOpTest):
 
   @test_util.run_deprecated_v1
   def test_hashed_zero_bucket_no_hash_key(self):

Fixed code: #include "tensorflow/core/framework/op_requires.h"
#include "tensorflow/core/platform/errors.h"
    OP_REQUIRES(context, TensorShapeUtils::IsScalar(sep_t->shape()),
                errors::InvalidArgument("Input separator should be a scalar. "
                                        "Received: ",
                                        sep_t->DebugString()));
  def testNonScalarInput(self):
    with self.assertRaisesRegex(errors.InvalidArgumentError,
                                'Input separator should be a scalar.'):
      self.evaluate(sparse_ops.sparse_cross(
          inputs=[],
          name='a',
          separator=constant_op.constant(['a', 'b'], dtype=dtypes.string)))