Coverage Report

Created: 2023-03-17 06:27

/src/png_proto_fuzzer_example.cc
Line
Count
Source (jump to first uncovered line)
1
// Example fuzzer for PNG using protos.
2
#include <string>
3
#include <sstream>
4
#include <fstream>
5
#include <zlib.h>  // for crc32
6
7
#include "libprotobuf-mutator/src/libfuzzer/libfuzzer_macro.h"
8
#include "png_fuzz_proto.pb.h"
9
10
289k
static void WriteInt(std::stringstream &out, uint32_t x) {
11
289k
  x = __builtin_bswap32(x);
12
289k
  out.write((char *)&x, sizeof(x));
13
289k
}
14
15
39.5k
static void WriteByte(std::stringstream &out, uint8_t x) {
16
39.5k
  out.write((char *)&x, sizeof(x));
17
39.5k
}
18
19
16.9k
static std::string Compress(const std::string &s) {
20
16.9k
  std::string out(s.size() + 100, '\0');
21
16.9k
  size_t out_len = out.size();
22
16.9k
  compress((uint8_t *)&out[0], &out_len, (uint8_t *)s.data(), s.size());
23
16.9k
  out.resize(out_len);
24
16.9k
  return out;
25
16.9k
}
26
27
// Chunk is written as:
28
//  * 4-byte length
29
//  * 4-byte type
30
//  * the data itself
31
//  * 4-byte crc (of type and data)
32
static void WriteChunk(std::stringstream &out, const char *type,
33
118k
                       const std::string &chunk, bool compress = false) {
34
118k
  std::string compressed;
35
118k
  const std::string *s = &chunk;
36
118k
  if (compress) {
37
5.97k
    compressed = Compress(chunk);
38
5.97k
    s = &compressed;
39
5.97k
  }
40
118k
  uint32_t len = s->size();
41
118k
  uint32_t crc = crc32(crc32(0, (const unsigned char *)type, 4),
42
118k
                       (const unsigned char *)s->data(), s->size());
43
118k
  WriteInt(out, len);
44
118k
  out.write(type, 4);
45
118k
  out.write(s->data(), s->size());
46
118k
  WriteInt(out, crc);
47
118k
}
48
49
17.6k
std::string ProtoToPng(const PngProto &png_proto) {
50
17.6k
  std::stringstream all;
51
17.6k
  const unsigned char header[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
52
17.6k
  all.write((const char*)header, sizeof(header));
53
17.6k
  std::stringstream ihdr_str;
54
17.6k
  auto &ihdr = png_proto.ihdr();
55
  // Avoid large images.
56
  // They may have interesting bugs, but OOMs are going to kill fuzzing.
57
17.6k
  uint32_t w = std::min(ihdr.width(), 4096U);
58
17.6k
  uint32_t h = std::min(ihdr.height(), 4096U);
59
17.6k
  WriteInt(ihdr_str, w);
60
17.6k
  WriteInt(ihdr_str, h);
61
17.6k
  WriteInt(ihdr_str, ihdr.other1());
62
17.6k
  WriteByte(ihdr_str, ihdr.other2());
63
17.6k
  WriteChunk(all, "IHDR", ihdr_str.str());
64
65
109k
  for (size_t i = 0, n = png_proto.chunks_size(); i < n; i++) {
66
91.4k
    auto &chunk = png_proto.chunks(i);
67
91.4k
    if (chunk.has_plte()) {
68
2.89k
      WriteChunk(all, "PLTE", chunk.plte().data());
69
88.5k
    } else if (chunk.has_idat()) {
70
5.97k
      WriteChunk(all, "IDAT", chunk.idat().data(), true);
71
82.5k
    } else if (chunk.has_iccp()) {
72
10.9k
      std::stringstream iccp_str;
73
10.9k
      iccp_str << "xyz";  // don't fuzz iCCP name field.
74
10.9k
      WriteByte(iccp_str, 0);
75
10.9k
      WriteByte(iccp_str, 0);
76
10.9k
      auto compressed_data = Compress(chunk.iccp().data());
77
10.9k
      iccp_str.write(compressed_data.data(), compressed_data.size());
78
10.9k
      WriteChunk(all, "iCCP", iccp_str.str());
79
71.5k
    } else if (chunk.has_other_chunk()) {
80
63.5k
      auto &other_chunk = chunk.other_chunk();
81
63.5k
      char type[5] = {0};
82
63.5k
      if (other_chunk.has_known_type()) {
83
60.4k
        static const char * known_chunks[] = {
84
60.4k
            "bKGD", "cHRM", "dSIG", "eXIf", "gAMA", "hIST", "iCCP",
85
60.4k
            "iTXt", "pHYs", "sBIT", "sPLT", "sRGB", "sTER", "tEXt",
86
60.4k
            "tIME", "tRNS", "zTXt", "sCAL", "pCAL", "oFFs",
87
60.4k
        };
88
60.4k
        size_t known_chunks_size =
89
60.4k
            sizeof(known_chunks) / sizeof(known_chunks[0]);
90
60.4k
        size_t chunk_idx = other_chunk.known_type() % known_chunks_size;
91
60.4k
        memcpy(type, known_chunks[chunk_idx], 4);
92
60.4k
      } else if (other_chunk.has_unknown_type()) {
93
2.71k
        uint32_t unknown_type_int = other_chunk.unknown_type();
94
2.71k
        memcpy(type, &unknown_type_int, 4);
95
2.71k
      } else {
96
382
        continue;
97
382
      }
98
63.1k
      type[4] = 0;
99
63.1k
      WriteChunk(all, type, other_chunk.data());
100
63.1k
    }
101
91.4k
  }
102
17.6k
  WriteChunk(all, "IEND", "");
103
104
17.6k
  std::string res = all.str();
105
17.6k
  if (const char *dump_path = getenv("PROTO_FUZZER_DUMP_PATH")) {
106
    // With libFuzzer binary run this to generate a PNG file x.png:
107
    // PROTO_FUZZER_DUMP_PATH=x.png ./a.out proto-input
108
0
    std::ofstream of(dump_path);
109
0
    of.write(res.data(), res.size());
110
0
  }
111
17.6k
  return res;
112
17.6k
}
113
114
// The actual fuzz target that consumes the PNG data.
115
extern "C" int FuzzPNG(const uint8_t* data, size_t size);
116
117
17.6k
DEFINE_PROTO_FUZZER(const PngProto &png_proto) {
118
17.6k
  auto s = ProtoToPng(png_proto);
119
17.6k
  FuzzPNG((const uint8_t*)s.data(), s.size());
120
17.6k
}