Project: opencv (opencv/opencv), language: c++
Vulnerability instance: opencv.cve-2017-18009
Sanitizer: address

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/opencv/opencv/issues/10479
## Description:
Issue: opencv/opencv#10479
Title: Buffer overflow in cv::HdrDecoder::checkSignature
State: closed
Created by: Mambaboy
Created at: 2018-01-01 03:37:50+00:00
Labels: bug, category: imgcodecs, category: vulnerability

Issue Body:
## System information (version)

OpenCV => 3.3.1
Operating System / Platform => Ubuntu 16.04
Compiler => clang++
Detailed description

## Detailed description

A buffer overflow occurs in function cv::HdrDecoder::checkSignature in file modules/imgcodecs/src/grfmt_hdr.cpp:106:.
The crash details as follows:
```
=================================================================
==12333==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x609000000ecc at pc 0x000001a5ef1f bp 0x7fff12ca2300 sp 0x7fff12ca1ab0
READ of size 10 at 0x609000000ecc thread T0
    #0 0x1a5ef1e in memcmp /home/opencv/fuzz/libfuzzer-workshop/src/llvm/projects/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:774
    #1 0x45a8e6 in cv::HdrDecoder::checkSignature(cv::String const&) const /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/grfmt_hdr.cpp:106:9
    #2 0x423bce in cv::findDecoder(cv::String const&) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/loadsave.cpp:216:33
    #3 0x419023 in cv::imread_(cv::String const&, int, int, cv::Mat*) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/loadsave.cpp:402:19
    #4 0x418a80 in cv::imread(cv::String const&, int) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/loadsave.cpp:638:5
    #5 0x413f25 in main /home/opencv/fuzz/target/opencv-3.3.1-test-crash/samples/cpp/example_cmake/example.cpp:54:11
    #6 0x7f8794948f44 in __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:287
    #7 0x4130c5 in _start (/home/opencv/verify+0x4130c5)

0x609000000ecc is located 0 bytes to the right of 12-byte region [0x609000000ec0,0x609000000ecc)
allocated by thread T0 here:
    #0 0x1a92c35 in __interceptor_posix_memalign /home/opencv/fuzz/libfuzzer-workshop/src/llvm/projects/compiler-rt/lib/asan/asan_malloc_linux.cc:157
    #1 0x6bb169 in cv::fastMalloc(unsigned long) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/core/src/alloc.cpp:64:8
    #2 0xb31682 in cv::String::allocate(unsigned long) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/core/src/stl.cpp:50:23
    #3 0x43ba2e in cv::String::substr(unsigned long, unsigned long) const /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/core/include/opencv2/core/cvstd.hpp:780:12
    #4 0x419023 in cv::imread_(cv::String const&, int, int, cv::Mat*) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/loadsave.cpp:402:19
    #5 0x418a80 in cv::imread(cv::String const&, int) /home/opencv/fuzz/target/opencv-3.3.1-test-crash/modules/imgcodecs/src/loadsave.cpp:638:5
    #6 0x413f25 in main /home/opencv/fuzz/target/opencv-3.3.1-test-crash/samples/cpp/example_cmake/example.cpp:54:11
    #7 0x7f8794948f44 in __libc_start_main /build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:287

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/opencv/fuzz/libfuzzer-workshop/src/llvm/projects/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc:774 in memcmp
Shadow bytes around the buggy address:
  0x0c127fff8180: 00 00 00 00 fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8190: 00 00 00 00 00 fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff81a0: 00 00 fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff81b0: fa fa fa fa fa fa fa fa 00 fa fa fa fa fa fa fa
  0x0c127fff81c0: fa fa fa fa fa fa fa fa fd fd fd fd fd fa fa fa
=>0x0c127fff81d0: fa fa fa fa fa fa fa fa 00[04]fa fa fa fa fa fa
  0x0c127fff81e0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff81f0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8210: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c127fff8220: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==12333==ABORTING
```

## Steps to reproduce

Please refer to the following url for the testcases:
https://github.com/Epeius/NBPOC/blob/master/poc_out_of_bound_read_01



Comments:
Comment by Epeius on 2018-01-02 08:09:44+00:00:
This issue was assigned [CVE-2017-18009](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-18009)

Build script:
#!/bin/bash -eu
# Minimized build script with only core build commands
set -eu
mkdir -p opencv/build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=$WORK \
  -DBUILD_SHARED_LIBS=OFF -DOPENCV_GENERATE_PKGCONFIG=ON \
  -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_FORCE_3RDPARTY_BUILD=ON ..
make -j$(nproc)
make install -j$(nproc)

Dockerfile:
FROM hwiwonlee/secb.base:latest
RUN apt-get update && apt-get install -y build-essential cmake pkg-config
RUN git clone https://github.com/opencv/opencv opencv
RUN git -C opencv checkout af4d6f34d8d05b0a9a0278728823272e49a63898
WORKDIR $SRC/opencv
COPY build.sh $SRC/

Additional files:
filename: fuzzer_temp_file.h
content: // Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Adapter utility from fuzzer input to a temporary file, for fuzzing APIs that
// require a file instead of an input buffer.

#ifndef FUZZER_TEMP_FILE_H_
#define FUZZER_TEMP_FILE_H_

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// Pure-C interface for creating and cleaning up temporary files.

static char* fuzzer_get_tmpfile(const uint8_t* data, size_t size) {
  char* filename_buffer = strdup("/tmp/generate_temporary_file.XXXXXX");
  if (!filename_buffer) {
    perror("Failed to allocate file name buffer.");
    abort();
  }
  const int file_descriptor = mkstemp(filename_buffer);
  if (file_descriptor < 0) {
    perror("Failed to make temporary file.");
    abort();
  }
  FILE* file = fdopen(file_descriptor, "wb");
  if (!file) {
    perror("Failed to open file descriptor.");
    close(file_descriptor);
    abort();
  }
  const size_t bytes_written = fwrite(data, sizeof(uint8_t), size, file);
  if (bytes_written < size) {
    close(file_descriptor);
    fprintf(stderr, "Failed to write all bytes to file (%zu out of %zu)",
            bytes_written, size);
    abort();
  }
  fclose(file);
  return filename_buffer;
}

static void fuzzer_release_tmpfile(char* filename) {
  if (unlink(filename) != 0) {
    perror("WARNING: Failed to delete temporary file.");
  }
  free(filename);
}

// C++ RAII object for creating temporary files.

#ifdef __cplusplus
class FuzzerTemporaryFile {
 public:
  FuzzerTemporaryFile(const uint8_t* data, size_t size)
      : filename_(fuzzer_get_tmpfile(data, size)) {}

  ~FuzzerTemporaryFile() { fuzzer_release_tmpfile(filename_); }

  const char* filename() const { return filename_; }

 private:
  char* filename_;
};
#endif

#endif  // FUZZER_TEMP_FILE_H_

filename: imdecode_fuzzer.cc
content: #include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs/legacy/constants_c.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  std::vector<uint8_t> image_data = {data, data + size};
  // TODO: Try other image types than CV_8UC1.
  cv::Mat data_matrix =
      cv::Mat(1, image_data.size(), CV_8UC1, image_data.data());
  try {
    cv::Mat decoded_matrix = cv::imdecode(data_matrix, CV_LOAD_IMAGE_UNCHANGED);
  } catch (cv::Exception e) {
    // Do nothing.
  }
  return 0;
}


filename: imread_fuzzer.cc
content: #include <cstddef>
#include <cstdint>
#include <unistd.h>

#include <opencv2/opencv.hpp>
#include "fuzzer_temp_file.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  const FuzzerTemporaryFile file(data, size);
  try {
    cv::Mat matrix = cv::imread(file.filename());
  } catch (cv::Exception e) {
    // Do nothing.
  }
  return 0;
}