CVE-2021-41206 (Python, published 2021-11-05T22:15Z)
Description: value: TensorFlow is an open source platform for machine learning. In affected versions several TensorFlow operations are missing validation for the shapes of the tensor arguments involved in the call. Depending on the API, this can result in undefined behavior and segfault or `CHECK`-fail related crashes but in some scenarios writes and reads from heap populated arrays are also possible. We have discovered these issues internally via tooling while working on improving/testing GPU op determinism. As such, we don't have reproducers and there will be multiple fixes for these issues. These fixes will be included in TensorFlow 2.7.0. We will also cherrypick these commits on TensorFlow 2.6.1, TensorFlow 2.5.2, and TensorFlow 2.4.4, as these are also affected and still in supported range.
Severity: MEDIUM (CVSS v3 base score: 7.8)
Weakness: CWE-354 \u2014 Improper Validation of Integrity Check Value
The product does not validate or incorrectly validates the integrity check values or "checksums" of a message. This may prevent it from detecting if the data has been modified or corrupted in transmission.
Repository: https://github.com/tensorflow/tensorflow
Files changed:
tensorflow/core/kernels/linalg/tridiagonal_matmul_op_gpu.cu.cc
tensorflow/python/kernel_tests/tridiagonal_matmul_op_test.py
Commit message: Add shape checks to GPU TridiagonalMatMul.

When given invalid shapes, the GPU TridiagonalMatMul op could read invalid or uninitialized GPU memory.

PiperOrigin-RevId: 401775483
Change-Id: Ib5500aeb8225e50d4ce790b06d2c34751f544ad8


Vulnerable code:

Fixed code:
    OP_REQUIRES(
        context, ndims >= 2,
        errors::InvalidArgument("Input must have rank >= 2, but got ", ndims));
    OP_REQUIRES_OK(context, ValidateInputTensor(superdiag, "superdiag", rhs));
    OP_REQUIRES_OK(context, ValidateInputTensor(maindiag, "maindiag", rhs));
    OP_REQUIRES_OK(context, ValidateInputTensor(subdiag, "subdiag", rhs));

 private:
  Status ValidateInputTensor(const Tensor& tensor,
                             const std::string& tensor_name,
                             const Tensor& rhs) {
    const int ndims = rhs.dims();
    if (tensor.dims() != ndims) {
      return errors::InvalidArgument(tensor_name,
                                     " must have same rank as rhs, but got ",
                                     tensor.dims(), " and ", ndims);
    }
    for (int i = 0; i < ndims - 2; i++) {
      if (tensor.dim_size(i) != rhs.dim_size(i)) {
        return errors::InvalidArgument(
            tensor_name,
            " must have same outer dimensions as rhs, but for index ", i,
            ", got ", tensor.dim_size(i), " and ", rhs.dim_size(i));
      }
    }
    if (tensor.dim_size(ndims - 2) != 1) {
      return errors::InvalidArgument(
          tensor_name, "'s second-to-last dimension must be 1, but got ",
          tensor.dim_size(ndims - 2));
    }
    if (tensor.dim_size(ndims - 1) != rhs.dim_size(ndims - 2)) {
      return errors::InvalidArgument(tensor_name,
                                     "'s last dimension size must be rhs's "
                                     "second-to-last dimension size, but got ",
                                     tensor.dim_size(ndims - 1), " and ",
                                     rhs.dim_size(ndims - 2));
    }
    return Status::OK();
  }
from tensorflow.python.eager import context
from tensorflow.python.framework import errors_impl
from tensorflow.python.ops import linalg_ops
  def _testErrorWithShapesEager(self, exception_regex, superdiag_shape,
                                maindiag_shape, subdiag_shape, rhs_shape):
    with context.eager_mode():
      superdiag = array_ops.ones(superdiag_shape)
      maindiag = array_ops.ones(maindiag_shape)
      subdiag = array_ops.ones(subdiag_shape)
      rhs = array_ops.ones(rhs_shape)
      with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                  exception_regex):
        linalg_ops.tridiagonal_mat_mul(superdiag, maindiag, subdiag, rhs)

  def testInvalidShapesEagerGpu(self):
    if not test.is_gpu_available():
      self.skipTest('Test requires GPU')
    self._testErrorWithShapesEager('Input must have rank >= 2, but got ',
                                   [2], [2], [2], [2])
    self._testErrorWithShapesEager(
        'superdiag must have same rank as rhs, but got 3 and 2',
        [2, 1, 2], [2, 1], [2, 1], [2, 2])
    self._testErrorWithShapesEager(
        'maindiag must have same outer dimensions as rhs, but for index 0, got '
        '3 and 2',
        [2, 1, 2], [3, 1, 2], [2, 1, 2], [2, 2, 2])
    self._testErrorWithShapesEager(
        "subdiag's second-to-last dimension must be 1, but got 3",
        [2, 1, 2], [2, 1, 2], [2, 3, 2], [2, 2, 2])
    self._testErrorWithShapesEager(
        "subdiag's last dimension size must be rhs's second-to-last dimension "
        "size, but got 3 and 2",
        [2, 1, 2], [2, 1, 2], [2, 1, 3], [2, 2, 2])