|
| 1 | +// libpng_read_fuzzer.cc |
| 2 | +// Copyright 2017-2018 Glenn Randers-Pehrson |
| 3 | +// Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style license that may |
| 5 | +// be found in the LICENSE file https://cs.chromium.org/chromium/src/LICENSE |
| 6 | + |
| 7 | +// Last changed in libpng 1.6.35 [July 15, 2018] |
| 8 | + |
| 9 | +// The modifications in 2017 by Glenn Randers-Pehrson include |
| 10 | +// 1. addition of a PNG_CLEANUP macro, |
| 11 | +// 2. setting the option to ignore ADLER32 checksums, |
| 12 | +// 3. adding "#include <string.h>" which is needed on some platforms |
| 13 | +// to provide memcpy(). |
| 14 | +// 4. adding read_end_info() and creating an end_info structure. |
| 15 | +// 5. adding calls to png_set_*() transforms commonly used by browsers. |
| 16 | + |
| 17 | +#include <stddef.h> |
| 18 | +#include <stdint.h> |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#define PNG_INTERNAL |
| 24 | +#include "png.h" |
| 25 | + |
| 26 | +#define PNG_CLEANUP \ |
| 27 | + if(png_handler.png_ptr) \ |
| 28 | + { \ |
| 29 | + if (png_handler.row_ptr) \ |
| 30 | + png_free(png_handler.png_ptr, png_handler.row_ptr); \ |
| 31 | + if (png_handler.end_info_ptr) \ |
| 32 | + png_destroy_read_struct(&png_handler.png_ptr, &png_handler.info_ptr,\ |
| 33 | + &png_handler.end_info_ptr); \ |
| 34 | + else if (png_handler.info_ptr) \ |
| 35 | + png_destroy_read_struct(&png_handler.png_ptr, &png_handler.info_ptr,\ |
| 36 | + nullptr); \ |
| 37 | + else \ |
| 38 | + png_destroy_read_struct(&png_handler.png_ptr, nullptr, nullptr); \ |
| 39 | + png_handler.png_ptr = nullptr; \ |
| 40 | + png_handler.row_ptr = nullptr; \ |
| 41 | + png_handler.info_ptr = nullptr; \ |
| 42 | + png_handler.end_info_ptr = nullptr; \ |
| 43 | + } |
| 44 | + |
| 45 | +struct BufState { |
| 46 | + const uint8_t* data; |
| 47 | + size_t bytes_left; |
| 48 | +}; |
| 49 | + |
| 50 | +struct PngObjectHandler { |
| 51 | + png_infop info_ptr = nullptr; |
| 52 | + png_structp png_ptr = nullptr; |
| 53 | + png_infop end_info_ptr = nullptr; |
| 54 | + png_voidp row_ptr = nullptr; |
| 55 | + BufState* buf_state = nullptr; |
| 56 | + |
| 57 | + ~PngObjectHandler() { |
| 58 | + if (row_ptr) |
| 59 | + png_free(png_ptr, row_ptr); |
| 60 | + if (end_info_ptr) |
| 61 | + png_destroy_read_struct(&png_ptr, &info_ptr, &end_info_ptr); |
| 62 | + else if (info_ptr) |
| 63 | + png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); |
| 64 | + else |
| 65 | + png_destroy_read_struct(&png_ptr, nullptr, nullptr); |
| 66 | + delete buf_state; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +void user_read_data(png_structp png_ptr, png_bytep data, size_t length) { |
| 71 | + BufState* buf_state = static_cast<BufState*>(png_get_io_ptr(png_ptr)); |
| 72 | + if (length > buf_state->bytes_left) { |
| 73 | + png_error(png_ptr, "read error"); |
| 74 | + } |
| 75 | + memcpy(data, buf_state->data, length); |
| 76 | + buf_state->bytes_left -= length; |
| 77 | + buf_state->data += length; |
| 78 | +} |
| 79 | + |
| 80 | +static const int kPngHeaderSize = 8; |
| 81 | + |
| 82 | +// Entry point for LibFuzzer. |
| 83 | +// Roughly follows the libpng book example: |
| 84 | +// http://www.libpng.org/pub/png/book/chapter13.html |
| 85 | +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 86 | + if (size < kPngHeaderSize) { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + std::vector<unsigned char> v(data, data + size); |
| 91 | + if (png_sig_cmp(v.data(), 0, kPngHeaderSize)) { |
| 92 | + // not a PNG. |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + PngObjectHandler png_handler; |
| 97 | + png_handler.png_ptr = nullptr; |
| 98 | + png_handler.row_ptr = nullptr; |
| 99 | + png_handler.info_ptr = nullptr; |
| 100 | + png_handler.end_info_ptr = nullptr; |
| 101 | + |
| 102 | + png_handler.png_ptr = png_create_read_struct |
| 103 | + (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 104 | + if (!png_handler.png_ptr) { |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + png_handler.info_ptr = png_create_info_struct(png_handler.png_ptr); |
| 109 | + if (!png_handler.info_ptr) { |
| 110 | + PNG_CLEANUP |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + png_handler.end_info_ptr = png_create_info_struct(png_handler.png_ptr); |
| 115 | + if (!png_handler.end_info_ptr) { |
| 116 | + PNG_CLEANUP |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + png_set_crc_action(png_handler.png_ptr, PNG_CRC_QUIET_USE, PNG_CRC_QUIET_USE); |
| 121 | +#ifdef PNG_IGNORE_ADLER32 |
| 122 | + png_set_option(png_handler.png_ptr, PNG_IGNORE_ADLER32, PNG_OPTION_ON); |
| 123 | +#endif |
| 124 | + |
| 125 | + // Setting up reading from buffer. |
| 126 | + png_handler.buf_state = new BufState(); |
| 127 | + png_handler.buf_state->data = data + kPngHeaderSize; |
| 128 | + png_handler.buf_state->bytes_left = size - kPngHeaderSize; |
| 129 | + png_set_read_fn(png_handler.png_ptr, png_handler.buf_state, user_read_data); |
| 130 | + png_set_sig_bytes(png_handler.png_ptr, kPngHeaderSize); |
| 131 | + |
| 132 | + if (setjmp(png_jmpbuf(png_handler.png_ptr))) { |
| 133 | + PNG_CLEANUP |
| 134 | + return 0; |
| 135 | + } |
| 136 | + |
| 137 | + // Reading. |
| 138 | + png_read_info(png_handler.png_ptr, png_handler.info_ptr); |
| 139 | + |
| 140 | + // reset error handler to put png_deleter into scope. |
| 141 | + if (setjmp(png_jmpbuf(png_handler.png_ptr))) { |
| 142 | + PNG_CLEANUP |
| 143 | + return 0; |
| 144 | + } |
| 145 | + |
| 146 | + png_uint_32 width, height; |
| 147 | + int bit_depth, color_type, interlace_type, compression_type; |
| 148 | + int filter_type; |
| 149 | + |
| 150 | + if (!png_get_IHDR(png_handler.png_ptr, png_handler.info_ptr, &width, |
| 151 | + &height, &bit_depth, &color_type, &interlace_type, |
| 152 | + &compression_type, &filter_type)) { |
| 153 | + PNG_CLEANUP |
| 154 | + return 0; |
| 155 | + } |
| 156 | + |
| 157 | + // This is going to be too slow. |
| 158 | + if (width && height > 100000000 / width) { |
| 159 | + PNG_CLEANUP |
| 160 | +#ifdef HAS_DUMMY_CRASH |
| 161 | + #ifdef __aarch64__ |
| 162 | + asm volatile (".word 0xf7f0a000\n"); |
| 163 | + #else |
| 164 | + asm("ud2"); |
| 165 | + #endif |
| 166 | +#endif |
| 167 | + return 0; |
| 168 | + } |
| 169 | + |
| 170 | + // Set several transforms that browsers typically use: |
| 171 | + png_set_gray_to_rgb(png_handler.png_ptr); |
| 172 | + png_set_expand(png_handler.png_ptr); |
| 173 | + png_set_packing(png_handler.png_ptr); |
| 174 | + png_set_scale_16(png_handler.png_ptr); |
| 175 | + png_set_tRNS_to_alpha(png_handler.png_ptr); |
| 176 | + |
| 177 | + int passes = png_set_interlace_handling(png_handler.png_ptr); |
| 178 | + |
| 179 | + png_read_update_info(png_handler.png_ptr, png_handler.info_ptr); |
| 180 | + |
| 181 | + png_handler.row_ptr = png_malloc( |
| 182 | + png_handler.png_ptr, png_get_rowbytes(png_handler.png_ptr, |
| 183 | + png_handler.info_ptr)); |
| 184 | + |
| 185 | + for (int pass = 0; pass < passes; ++pass) { |
| 186 | + for (png_uint_32 y = 0; y < height; ++y) { |
| 187 | + png_read_row(png_handler.png_ptr, |
| 188 | + static_cast<png_bytep>(png_handler.row_ptr), nullptr); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + png_read_end(png_handler.png_ptr, png_handler.end_info_ptr); |
| 193 | + |
| 194 | + PNG_CLEANUP |
| 195 | + return 0; |
| 196 | +} |
| 197 | + |
0 commit comments