Heap buffer overflow due to incorrect hash function in TensorFlow
Description
TensorFlow is an open source platform for machine learning. In version 2.8.0, the TensorKey hash function used total estimated AllocatedBytes(), which (a) is an estimate per tensor, and (b) is a very poor hash function for constants (e.g. int32_t). It also tried to access individual tensor bytes through tensor.data() of size AllocatedBytes(). This led to ASAN failures because the AllocatedBytes() is an estimate of total bytes allocated by a tensor, including any pointed-to constructs (e.g. strings), and does not refer to contiguous bytes in the .data() buffer. The discoverers could not use this byte vector anyway because types such as tstring include pointers, whereas they needed to hash the string values themselves. This issue is patched in Tensorflow versions 2.9.0 and 2.8.1.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
tensorflowPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
tensorflow-cpuPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
tensorflow-gpuPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
Affected products
1- Range: == 2.8.0
Patches
11b85a28d395dFix TensorKey hash function.
2 files changed · +17 −11
tensorflow/core/framework/tensor_key.h+17 −9 modified@@ -16,6 +16,7 @@ limitations under the License. #define TENSORFLOW_CORE_FRAMEWORK_TENSOR_KEY_H_ #include "tensorflow/core/framework/tensor.h" +#include "tensorflow/core/framework/types.h" namespace tensorflow { @@ -32,8 +33,7 @@ class TensorKey : public Tensor { } if (DataTypeCanUseMemcpy(t1.dtype())) { return t1.tensor_data() == t2.tensor_data(); - } - if (t1.dtype() == DT_STRING) { + } else if (t1.dtype() == DT_STRING) { const auto s1 = t1.unaligned_flat<tstring>(); const auto s2 = t2.unaligned_flat<tstring>(); for (int64_t i = 0, n = t1.NumElements(); i < n; ++i) { @@ -42,6 +42,9 @@ class TensorKey : public Tensor { } } return true; + } else { + DCHECK(false) << "Unimplemented dtype " << DataTypeString(t1.dtype()) + << std::endl; } return false; } @@ -53,14 +56,19 @@ class TensorKey : public Tensor { // Needed for absl hash function. template <typename H> friend H AbslHashValue(H h, const TensorKey& k) { - const uint8* d = static_cast<uint8*>(k.data()); - size_t s = k.AllocatedBytes(); - std::vector<uint8> vec; - vec.reserve(s); - for (int i = 0; i < s; i++) { - vec.push_back(d[i]); + if (DataTypeCanUseMemcpy(k.dtype())) { + return H::combine(std::move(h), k.tensor_data()); + } else if (k.dtype() == DT_STRING) { + const auto strs = k.unaligned_flat<tstring>(); + for (int64_t i = 0, n = k.NumElements(); i < n; ++i) { + h = H::combine(std::move(h), strs(i)); + } + return h; + } else { + DCHECK(false) << "Unimplemented dtype " << DataTypeString(k.dtype()) + << std::endl; } - return H::combine(std::move(h), s); + return h; } };
tensorflow/python/kernel_tests/data_structures/BUILD+0 −2 modified@@ -165,8 +165,6 @@ tf_py_test( grpc_enabled = True, tags = [ "no_windows", # TODO(b/192259628) - "noasan", # TODO(b/164696004) - "notsan", # TODO(b/164696004) ], deps = [ "//tensorflow/python:array_ops",
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
7- github.com/advisories/GHSA-hc2f-7r5r-r2hgghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2022-29210ghsaADVISORY
- github.com/tensorflow/tensorflow/blob/f3b9bf4c3c0597563b289c0512e98d4ce81f886e/tensorflow/core/framework/tensor_key.hghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/commit/1b85a28d395dc91f4d22b5f9e1e9a22e92ccecd6ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.8.1ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.9.0ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/security/advisories/GHSA-hc2f-7r5r-r2hgghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.