Coverage Report

Created: 2025-10-12 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/fuzz/qpdf_crypt_insecure_fuzzer.cc
Line
Count
Source
1
#include <qpdf/Buffer.hh>
2
#include <qpdf/BufferInputSource.hh>
3
#include <qpdf/Pl_DCT.hh>
4
#include <qpdf/Pl_Discard.hh>
5
#include <qpdf/Pl_Flate.hh>
6
#include <qpdf/Pl_PNGFilter.hh>
7
#include <qpdf/Pl_RunLength.hh>
8
#include <qpdf/Pl_TIFFPredictor.hh>
9
#include <qpdf/QPDF.hh>
10
#include <qpdf/QPDFPageObjectHelper.hh>
11
#include <qpdf/QPDFWriter.hh>
12
#include <qpdf/QUtil.hh>
13
#include <cstdlib>
14
15
class FuzzHelper
16
{
17
  public:
18
    FuzzHelper(unsigned char const* data, size_t size);
19
    void run();
20
21
  private:
22
    std::shared_ptr<QPDF> getQpdf();
23
    std::shared_ptr<QPDFWriter> getWriter(std::shared_ptr<QPDF>);
24
    void doWrite(std::shared_ptr<QPDFWriter> w);
25
    void testWrite();
26
    void doChecks();
27
28
    Buffer input_buffer;
29
    Pl_Discard discard;
30
};
31
32
FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
33
    // We do not modify data, so it is safe to remove the const for Buffer
34
312k
    input_buffer(const_cast<unsigned char*>(data), size)
35
312k
{
36
312k
}
37
38
std::shared_ptr<QPDF>
39
FuzzHelper::getQpdf()
40
274k
{
41
274k
    auto is =
42
274k
        std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer));
43
274k
    auto qpdf = QPDF::create();
44
274k
    qpdf->setMaxWarnings(200);
45
274k
    qpdf->processInputSource(is);
46
274k
    return qpdf;
47
274k
}
48
49
std::shared_ptr<QPDFWriter>
50
FuzzHelper::getWriter(std::shared_ptr<QPDF> qpdf)
51
75.4k
{
52
75.4k
    auto w = std::make_shared<QPDFWriter>(*qpdf);
53
75.4k
    w->setOutputPipeline(&this->discard);
54
75.4k
    w->setDecodeLevel(qpdf_dl_all);
55
75.4k
    return w;
56
75.4k
}
57
58
void
59
FuzzHelper::doWrite(std::shared_ptr<QPDFWriter> w)
60
73.6k
{
61
73.6k
    try {
62
73.6k
        w->write();
63
73.6k
    } catch (QPDFExc const& e) {
64
6.71k
        std::cerr << e.what() << '\n';
65
6.71k
    } catch (std::runtime_error const& e) {
66
3.46k
        std::cerr << e.what() << '\n';
67
3.46k
    }
68
73.6k
}
69
70
void
71
FuzzHelper::testWrite()
72
189k
{
73
    // Write in various ways to exercise QPDFWriter
74
75
189k
    std::shared_ptr<QPDF> q;
76
189k
    std::shared_ptr<QPDFWriter> w;
77
78
189k
    q = getQpdf();
79
189k
    w = getWriter(q);
80
189k
    w->setStaticID(true);
81
189k
    w->setObjectStreamMode(qpdf_o_disable);
82
189k
    w->setR3EncryptionParametersInsecure(
83
189k
        "u", "o", true, true, true, true, true, true, qpdf_r3p_full);
84
189k
    doWrite(w);
85
189k
}
86
87
void
88
FuzzHelper::doChecks()
89
296k
{
90
    // Limit the memory used to decompress JPEG files during fuzzing. Excessive memory use during
91
    // fuzzing is due to corrupt JPEG data which sometimes cannot be detected before
92
    // jpeg_start_decompress is called. During normal use of qpdf very large JPEGs can occasionally
93
    // occur legitimately and therefore must be allowed during normal operations.
94
296k
    Pl_DCT::setMemoryLimit(100'000'000);
95
296k
    Pl_DCT::setScanLimit(50);
96
97
296k
    Pl_PNGFilter::setMemoryLimit(1'000'000);
98
296k
    Pl_RunLength::setMemoryLimit(1'000'000);
99
296k
    Pl_TIFFPredictor::setMemoryLimit(1'000'000);
100
296k
    Pl_Flate::memory_limit(200'000);
101
102
    // Do not decompress corrupt data. This may cause extended runtime within jpeglib without
103
    // exercising additional code paths in qpdf, and potentially causing counterproductive timeouts.
104
296k
    Pl_DCT::setThrowOnCorruptData(true);
105
106
    // Get as much coverage as possible in parts of the library that
107
    // might benefit from fuzzing.
108
296k
    std::cerr << "\ninfo: starting testWrite\n";
109
296k
    testWrite();
110
296k
}
111
112
void
113
FuzzHelper::run()
114
274k
{
115
    // The goal here is that you should be able to throw anything at
116
    // libqpdf and it will respond without any memory errors and never
117
    // do anything worse than throwing a QPDFExc or
118
    // std::runtime_error. Throwing any other kind of exception,
119
    // segfaulting, or having a memory error (when built with
120
    // appropriate sanitizers) will all cause abnormal exit.
121
274k
    try {
122
274k
        doChecks();
123
274k
    } catch (QPDFExc const& e) {
124
182k
        std::cerr << "QPDFExc: " << e.what() << '\n';
125
182k
    } catch (std::runtime_error const& e) {
126
1.90k
        std::cerr << "runtime_error: " << e.what() << '\n';
127
1.90k
    }
128
274k
}
129
130
extern "C" int
131
LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
132
312k
{
133
312k
#ifndef _WIN32
134
    // Used by jpeg library to work around false positives in memory
135
    // sanitizer.
136
312k
    setenv("JSIMD_FORCENONE", "1", 1);
137
312k
#endif
138
312k
    FuzzHelper f(data, size);
139
312k
    f.run();
140
312k
    return 0;
141
312k
}