Vulnerability: Null-dereference in Tensorflow ### Impact
When decoding a tensor from protobuf, TensorFlow might do a null-dereference if attributes of some mutable arguments to some operations are missing from the proto. This is [guarded by a `DCHECK`](https://github.com/tensorflow/tensorflow/blob/a1320ec1eac186da1d03f033109191f715b2b130/tensorflow/core/framework/full_type_util.cc#L104-L106):

```cc
  const auto* attr = attrs.Find(arg->s()); 
  DCHECK(attr != nullptr);
  if (attr->value_case() == AttrValue::kList) {
    // ...
  }
```
However, `DCHECK` is a no-op in production builds and an assertion failure in debug builds. In the first case execution proceeds to the dereferencing of the null pointer, whereas in the second case it results in a crash due to the assertion failure.

### Patches
We have patched the issue in GitHub commit [8a513cec4bec15961fbfdedcaa5376522980455c](https://github.com/tensorflow/tensorflow/commit/8a513cec4bec15961fbfdedcaa5376522980455c).

The fix will be included in TensorFlow 2.8.0. We will also cherrypick this commit on TensorFlow 2.7.1, and TensorFlow 2.6.3, as these are also affected and still in supported range.

### For more information
Please consult [our security guide](https://github.com/tensorflow/tensorflow/blob/master/SECURITY.md) for more information regarding the security model and how to contact us with issues and questions.
Vulnerability ID: GHSA-9p77-mmrw-69c7
CWE: CWE-476
Severity score: 6.5
Project: https://github.com/tensorflow/tensorflow
Published: 2022-02-09
File: tensorflow/core/framework/full_type_util.cc
Commit message: Prevent null dereference read in `SpecializeType()`

For some adversarial protos, the attribute for a key might not exist.

PiperOrigin-RevId: 408382090
Change-Id: Ie7eabe532c9ff280fce5dce1f6cdb93c76c2e040
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 limitations under the License. #include "tensorflow/core/framework/op_def.pb.h" #include "tensorflow/core/framework/types.h" #include "tensorflow/core/platform/statusor.h"  namespace tensorflow {  StatusOr<FullTypeDef> SpecializeType(const AttrSlice& attrs,       auto* arg = t->mutable_args(i);       if (arg->type_id() == TFT_VAR) {         const auto* attr = attrs.Find(arg->s());         DCHECK(attr != nullptr);         if (attr->value_case() == AttrValue::kList) {           const auto& attr_list = attr->list();           arg->set_type_id(TFT_PRODUCT);

Fixed version (after fix):
 limitations under the License. #include "tensorflow/core/framework/op_def.pb.h" #include "tensorflow/core/framework/types.h" #include "tensorflow/core/platform/statusor.h" #include "tensorflow/core/protobuf/error_codes.pb.h"  namespace tensorflow {  StatusOr<FullTypeDef> SpecializeType(const AttrSlice& attrs,       auto* arg = t->mutable_args(i);       if (arg->type_id() == TFT_VAR) {         const auto* attr = attrs.Find(arg->s());         if (attr == nullptr) {           return Status(               error::INVALID_ARGUMENT,               absl::StrCat("Could not find an attribute for key ", arg->s()));         }         if (attr->value_case() == AttrValue::kList) {           const auto& attr_list = attr->list();           arg->set_type_id(TFT_PRODUCT);

Patch:
patch: @@ -22,6 +22,7 @@ limitations under the License.
 #include "tensorflow/core/framework/op_def.pb.h"
 #include "tensorflow/core/framework/types.h"
 #include "tensorflow/core/platform/statusor.h"
+#include "tensorflow/core/protobuf/error_codes.pb.h"
 
 namespace tensorflow {
 
@@ -102,7 +103,11 @@ StatusOr<FullTypeDef> SpecializeType(const AttrSlice& attrs,
       auto* arg = t->mutable_args(i);
       if (arg->type_id() == TFT_VAR) {
         const auto* attr = attrs.Find(arg->s());
-        DCHECK(attr != nullptr);
+        if (attr == nullptr) {
+          return Status(
+              error::INVALID_ARGUMENT,
+              absl::StrCat("Could not find an attribute for key ", arg->s()));
+        }
         if (attr->value_case() == AttrValue::kList) {
           const auto& attr_list = attr->list();
           arg->set_type_id(TFT_PRODUCT);
path: tensorflow/core/framework/full_type_util.cc