CVE-2022-35985: Reachable Assertion (CWE-617)
value: TensorFlow is an open source platform for machine learning. If `LRNGrad` is given an `output_image` input tensor that is not 4-D, it results in a `CHECK` fail that can be used to trigger a denial of service attack. We have patched the issue in GitHub commit bd90b3efab4ec958b228cd7cfe9125be1c0cf255. 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-16T22:15Z
Repository: https://github.com/tensorflow/tensorflow
Language: Python
Files changed: tensorflow/core/kernels/lrn_op.cc, tensorflow/python/kernel_tests/nn_ops/lrn_op_test.py
Commit date: 2022-07-13 09:52:14 -0700
Commit message: Fix security vulnerability with LRNGradOp

PiperOrigin-RevId: 460738938

Diff: diff --git a/tensorflow/core/kernels/lrn_op.cc b/tensorflow/core/kernels/lrn_op.cc
index 31aaf018329b5..0d6d24d3dce2d 100644
--- a/tensorflow/core/kernels/lrn_op.cc
+++ b/tensorflow/core/kernels/lrn_op.cc
@@ -666,11 +666,12 @@ class LRNGradOp : public OpKernel {
     OP_REQUIRES(
         context,
         in_image.dim_size(0) == batch && in_image.dim_size(1) == rows &&
             in_image.dim_size(2) == cols && in_image.dim_size(3) == depth &&
             out_image.dim_size(0) == batch && out_image.dim_size(1) == rows &&
-            out_image.dim_size(2) == cols && out_image.dim_size(3) == depth,
+            out_image.dim_size(2) == cols && out_image.dim_size(3) == depth &&
+            out_image.dims() == 4,
         errors::InvalidArgument(
             "input_grads, input_image, and out_image should have the same "
             "shape"));
 
     Tensor* output = nullptr;
diff --git a/tensorflow/python/kernel_tests/nn_ops/lrn_op_test.py b/tensorflow/python/kernel_tests/nn_ops/lrn_op_test.py
index 9fb7724f69537..f44c7316845b2 100644
--- a/tensorflow/python/kernel_tests/nn_ops/lrn_op_test.py
+++ b/tensorflow/python/kernel_tests/nn_ops/lrn_op_test.py
@@ -18,15 +18,17 @@ import copy
 
 import numpy as np
 
 from tensorflow.python.framework import constant_op
 from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import errors_impl
 from tensorflow.python.framework import test_util
 from tensorflow.python.ops import array_ops
 from tensorflow.python.ops import gradient_checker
 from tensorflow.python.ops import gradients_impl
 from tensorflow.python.ops import nn
+from tensorflow.python.ops import random_ops
 import tensorflow.python.ops.nn_grad  # pylint: disable=unused-import
 from tensorflow.python.platform import test
 
 
 class LRNOpTest(test.TestCase):
@@ -109,10 +111,45 @@ class LRNOpTest(test.TestCase):
       r = grad.eval(feed_dict=params)
     expected = np.ones(shape).astype("f")
     self.assertAllClose(r, expected)
     self.assertShapeEqual(expected, grad)
 
+  @test_util.run_in_graph_and_eager_modes
+  def testIncompatibleInputAndOutputImageShapes(self):
+    depth_radius = 1
+    bias = 1.59018219
+    alpha = 0.117728651
+    beta = 0.404427052
+    input_grads = random_ops.random_uniform(
+        shape=[4, 4, 4, 4],
+        minval=-10000,
+        maxval=10000,
+        dtype=dtypes.float32,
+        seed=-2033)
+    input_image = random_ops.random_uniform(
+        shape=[4, 4, 4, 4],
+        minval=-10000,
+        maxval=10000,
+        dtype=dtypes.float32,
+        seed=-2033)
+    invalid_output_image = random_ops.random_uniform(
+        shape=[4, 4, 4, 4, 4, 4],
+        minval=-10000,
+        maxval=10000,
+        dtype=dtypes.float32,
+        seed=-2033)
+    with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
+      self.evaluate(
+          nn.lrn_grad(
+              input_grads=input_grads,
+              input_image=input_image,
+              output_image=invalid_output_image,
+              depth_radius=depth_radius,
+              bias=bias,
+              alpha=alpha,
+              beta=beta))
+
   def _RunAndVerifyGradients(self, dtype):
     with self.cached_session():
       # random shape
       shape = np.random.randint(1, 5, size=4)
       # Make depth at least 2 to make it meaningful

Vulnerable code:             out_image.dim_size(2) == cols && out_image.dim_size(3) == depth,
Fixed code:             out_image.dim_size(2) == cols && out_image.dim_size(3) == depth &&
            out_image.dims() == 4,
from tensorflow.python.framework import errors_impl
from tensorflow.python.ops import random_ops
  @test_util.run_in_graph_and_eager_modes
  def testIncompatibleInputAndOutputImageShapes(self):
    depth_radius = 1
    bias = 1.59018219
    alpha = 0.117728651
    beta = 0.404427052
    input_grads = random_ops.random_uniform(
        shape=[4, 4, 4, 4],
        minval=-10000,
        maxval=10000,
        dtype=dtypes.float32,
        seed=-2033)
    input_image = random_ops.random_uniform(
        shape=[4, 4, 4, 4],
        minval=-10000,
        maxval=10000,
        dtype=dtypes.float32,
        seed=-2033)
    invalid_output_image = random_ops.random_uniform(
        shape=[4, 4, 4, 4, 4, 4],
        minval=-10000,
        maxval=10000,
        dtype=dtypes.float32,
        seed=-2033)
    with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
      self.evaluate(
          nn.lrn_grad(
              input_grads=input_grads,
              input_image=input_image,
              output_image=invalid_output_image,
              depth_radius=depth_radius,
              bias=bias,
              alpha=alpha,
              beta=beta))