Coverage Report

Created: 2023-02-25 06:44

/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
301k
static void WriteInt(std::stringstream &out, uint32_t x) {
11
301k
  x = __builtin_bswap32(x);
12
301k
  out.write((char *)&x, sizeof(x));
13
301k
}
14
15
42.1k
static void WriteByte(std::stringstream &out, uint8_t x) {
16
42.1k
  out.write((char *)&x, sizeof(x));
17
42.1k
}
18
19
18.3k
static std::string Compress(const std::string &s) {
20
18.3k
  std::string out(s.size() + 100, '\0');
21
18.3k
  size_t out_len = out.size();
22
18.3k
  compress((uint8_t *)&out[0], &out_len, (uint8_t *)s.data(), s.size());
23
18.3k
  out.resize(out_len);
24
18.3k
  return out;
25
18.3k
}
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
123k
                       const std::string &chunk, bool compress = false) {
34
123k
  std::string compressed;
35
123k
  const std::string *s = &chunk;
36
123k
  if (compress) {
37
6.38k
    compressed = Compress(chunk);
38
6.38k
    s = &compressed;
39
6.38k
  }
40
123k
  uint32_t len = s->size();
41
123k
  uint32_t crc = crc32(crc32(0, (const unsigned char *)type, 4),
42
123k
                       (const unsigned char *)s->data(), s->size());
43
123k
  WriteInt(out, len);
44
123k
  out.write(type, 4);
45
123k
  out.write(s->data(), s->size());
46
123k
  WriteInt(out, crc);
47
123k
}
48
49
18.1k
std::string ProtoToPng(const PngProto &png_proto) {
50
18.1k
  std::stringstream all;
51
18.1k
  const unsigned char header[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
52
18.1k
  all.write((const char*)header, sizeof(header));
53
18.1k
  std::stringstream ihdr_str;
54
18.1k
  auto &ihdr = png_proto.ihdr();
55
  // Avoid large images.
56
  // They may have interesting bugs, but OOMs are going to kill fuzzing.
57
18.1k
  uint32_t w = std::min(ihdr.width(), 4096U);
58
18.1k
  uint32_t h = std::min(ihdr.height(), 4096U);
59
18.1k
  WriteInt(ihdr_str, w);
60
18.1k
  WriteInt(ihdr_str, h);
61
18.1k
  WriteInt(ihdr_str, ihdr.other1());
62
18.1k
  WriteByte(ihdr_str, ihdr.other2());
63
18.1k
  WriteChunk(all, "IHDR", ihdr_str.str());
64
65
114k
  for (size_t i = 0, n = png_proto.chunks_size(); i < n; i++) {
66
95.9k
    auto &chunk = png_proto.chunks(i);
67
95.9k
    if (chunk.has_plte()) {
68
3.01k
      WriteChunk(all, "PLTE", chunk.plte().data());
69
92.9k
    } else if (chunk.has_idat()) {
70
6.38k
      WriteChunk(all, "IDAT", chunk.idat().data(), true);
71
86.5k
    } else if (chunk.has_iccp()) {
72
11.9k
      std::stringstream iccp_str;
73
11.9k
      iccp_str << "xyz";  // don't fuzz iCCP name field.
74
11.9k
      WriteByte(iccp_str, 0);
75
11.9k
      WriteByte(iccp_str, 0);
76
11.9k
      auto compressed_data = Compress(chunk.iccp().data());
77
11.9k
      iccp_str.write(compressed_data.data(), compressed_data.size());
78
11.9k
      WriteChunk(all, "iCCP", iccp_str.str());
79
74.5k
    } else if (chunk.has_other_chunk()) {
80
66.1k
      auto &other_chunk = chunk.other_chunk();
81
66.1k
      char type[5] = {0};
82
66.1k
      if (other_chunk.has_known_type()) {
83
62.9k
        static const char * known_chunks[] = {
84
62.9k
            "bKGD", "cHRM", "dSIG", "eXIf", "gAMA", "hIST", "iCCP",
85
62.9k
            "iTXt", "pHYs", "sBIT", "sPLT", "sRGB", "sTER", "tEXt",
86
62.9k
            "tIME", "tRNS", "zTXt", "sCAL", "pCAL", "oFFs",
87
62.9k
        };
88
62.9k
        size_t known_chunks_size =
89
62.9k
            sizeof(known_chunks) / sizeof(known_chunks[0]);
90
62.9k
        size_t chunk_idx = other_chunk.known_type() % known_chunks_size;
91
62.9k
        memcpy(type, known_chunks[chunk_idx], 4);
92
62.9k
      } else if (other_chunk.has_unknown_type()) {
93
2.72k
        uint32_t unknown_type_int = other_chunk.unknown_type();
94
2.72k
        memcpy(type, &unknown_type_int, 4);
95
2.72k
      } else {
96
421
        continue;
97
421
      }
98
65.6k
      type[4] = 0;
99
65.6k
      WriteChunk(all, type, other_chunk.data());
100
65.6k
    }
101
95.9k
  }
102
18.1k
  WriteChunk(all, "IEND", "");
103
104
18.1k
  std::string res = all.str();
105
18.1k
  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
18.1k
  return res;
112
18.1k
}
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
18.1k
DEFINE_PROTO_FUZZER(const PngProto &png_proto) {
118
18.1k
  auto s = ProtoToPng(png_proto);
119
18.1k
  FuzzPNG((const uint8_t*)s.data(), s.size());
120
18.1k
}