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

================= Bug Report (1/1) ==================
## Source: GitHub Issue
## URL: https://github.com/opencv/opencv/issues/9723
## Description:
Issue: opencv/opencv#9723
Title: Out of bounds write causes Segmentation Fault
State: closed
Created by: blendin
Created at: 2017-09-26 22:19:03+00:00
Labels: bug, category: imgcodecs, category: vulnerability

Issue Body:
**System information (version)**

* OpenCV => 3.3 (the latest commit: 7475d23 2017-09-26)
* Operating System / Platform => Linux
* Compiler => gcc

**Detailed description**

An invalid writing occurs in the FillUniColor and FillUniGray functions in opencv/modules/imgcodecs/src/utils.cpp

**POC**
https://github.com/blendin/pocs/blob/master/opencv/0.OOB_Write_FillUniColor

**Steps to reproduce**
```c++
#include <opencv2/opencv.hpp>

int main(void) {
    cv::imread("0.OOB_Write_FillUniColor")
}
```

**Crash Details**
```
ASAN:DEADLYSIGNAL
=================================================================
==23351==ERROR: AddressSanitizer: SEGV on unknown address 0x7f9f027530fd (pc 0x7f9f01405bd0 bp 0x000000006060 sp 0x7fff2f321208 T0)
==23351==The signal is caused by a WRITE memory access.
    #0 0x7f9f01405bcf in FillUniColor(unsigned char*, unsigned char*&, int, int, int&, int, int, PaletteEntry) (/usr/local/lib/libopencv_imgcodecs.so.3.3+0x73bcf)
    #1 0x7f9f01414895 in cv::BmpDecoder::readData(cv::Mat&) (/usr/local/lib/libopencv_imgcodecs.so.3.3+0x82895)
    #2 0x7f9f013fc642 in cv::imread_(cv::String const&, int, int, cv::Mat*) (/usr/local/lib/libopencv_imgcodecs.so.3.3+0x6a642)
    #3 0x7f9f013fbe6b in cv::imread(cv::String const&, int) (/usr/local/lib/libopencv_imgcodecs.so.3.3+0x69e6b)
    #4 0x4f118b in main /lz/targets/opencv.cc:7:15
    #5 0x7f9f0011682f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #6 0x41a6e8 in _start (/lz/targets/verify+0x41a6e8)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/local/lib/libopencv_imgcodecs.so.3.3+0x73bcf) in FillUniColor(unsigned char*, unsigned char*&, int, int, int&, int, int, PaletteEntry)
==23351==ABORTING
```




Comments:
Comment by carnil on 2018-01-03 05:49:22+00:00:
This issue was assigned CVE-2017-1000450

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 e8357c0544c82beda237defc0abc90ee28992ee7
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;
}