Vulnerability: Memory leak in decoding PNG images ### Impact
When [decoding PNG images](https://github.com/tensorflow/tensorflow/blob/a1320ec1eac186da1d03f033109191f715b2b130/tensorflow/core/kernels/image/decode_image_op.cc#L322-L416) TensorFlow can produce a memory leak if the image is invalid.
After calling `png::CommonInitDecode(..., &decode)`, the `decode` value contains allocated buffers which can only be freed by calling `png::CommonFreeDecode(&decode)`. However, several error case in the function implementation invoke the `OP_REQUIRES` macro which immediately terminates the execution of the function, without allowing for the memory free to occur.
  
### Patches   
We have patched the issue in GitHub commit [ab51e5b813573dc9f51efa335aebcf2994125ee9](https://github.com/tensorflow/tensorflow/commit/ab51e5b813573dc9f51efa335aebcf2994125ee9).

The fix will be included in TensorFlow 2.8.0. We will also cherrypick this commit on TensorFlow 2.7.1, TensorFlow 2.6.3, and TensorFlow 2.5.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-fq6p-6334-8gr4
CWE: CWE-401
Severity score: 4.3
Project: https://github.com/tensorflow/tensorflow
Published: 2022-02-09
File: tensorflow/core/kernels/image/decode_image_op.cc
Commit message: Prevent memory leak in decoding PNG images.

PiperOrigin-RevId: 409300653
Change-Id: I6182124c545989cef80cefd439b659095920763b
Commit author: Mihai Maruseac

Vulnerable version (before fix):
 limitations under the License. #include <cstdint> #include <memory>  #define EIGEN_USE_THREADS  #include "absl/strings/escaping.h" class DecodeImageV2Op : public OpKernel {         context, png::CommonInitDecode(input, channels_, channel_bits, &decode),         errors::InvalidArgument("Invalid PNG. Failed to initialize decoder."));      // Verify that width and height are not too large:     // - verify width and height don't overflow int.     // - width can later be multiplied by channels_ and sizeof(uint16), so

Fixed version (after fix):
 limitations under the License. #include <cstdint> #include <memory>  #include "tensorflow/core/lib/gtl/cleanup.h"  #define EIGEN_USE_THREADS  #include "absl/strings/escaping.h" class DecodeImageV2Op : public OpKernel {         context, png::CommonInitDecode(input, channels_, channel_bits, &decode),         errors::InvalidArgument("Invalid PNG. Failed to initialize decoder."));      // If we reach this point, then there is data in `decode` which must be     // freed by the time we end execution in this function. We cannot call     // `png::CommonFreeDecode()` before an `OP_REQUIRES` because if     // `OP_REQUIRES` constraint is satisfied then the data would be freed     // prematurely. Instead, let's use a `Cleanup` object.     auto cleanup = gtl::MakeCleanup([&decode]() {       std::cerr << "Cleanup called...\n";       png::CommonFreeDecode(&decode);     });      // Verify that width and height are not too large:     // - verify width and height don't overflow int.     // - width can later be multiplied by channels_ and sizeof(uint16), so

Patch:
patch: @@ -18,6 +18,8 @@ limitations under the License.
 #include <cstdint>
 #include <memory>
 
+#include "tensorflow/core/lib/gtl/cleanup.h"
+
 #define EIGEN_USE_THREADS
 
 #include "absl/strings/escaping.h"
@@ -326,6 +328,16 @@ class DecodeImageV2Op : public OpKernel {
         context, png::CommonInitDecode(input, channels_, channel_bits, &decode),
         errors::InvalidArgument("Invalid PNG. Failed to initialize decoder."));
 
+    // If we reach this point, then there is data in `decode` which must be
+    // freed by the time we end execution in this function. We cannot call
+    // `png::CommonFreeDecode()` before an `OP_REQUIRES` because if
+    // `OP_REQUIRES` constraint is satisfied then the data would be freed
+    // prematurely. Instead, let's use a `Cleanup` object.
+    auto cleanup = gtl::MakeCleanup([&decode]() {
+      std::cerr << "Cleanup called...\n";
+      png::CommonFreeDecode(&decode);
+    });
+
     // Verify that width and height are not too large:
     // - verify width and height don't overflow int.
     // - width can later be multiplied by channels_ and sizeof(uint16), so
path: tensorflow/core/kernels/image/decode_image_op.cc