/src/qpdf/fuzz/qpdf_crypt_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 | | |
14 | | #include <cstdlib> |
15 | | |
16 | | class FuzzHelper |
17 | | { |
18 | | public: |
19 | | FuzzHelper(unsigned char const* data, size_t size); |
20 | | void run(); |
21 | | |
22 | | private: |
23 | | std::shared_ptr<QPDF> getQpdf(); |
24 | | std::shared_ptr<QPDFWriter> getWriter(std::shared_ptr<QPDF>); |
25 | | void doWrite(std::shared_ptr<QPDFWriter> w); |
26 | | void testWrite(); |
27 | | void doChecks(); |
28 | | |
29 | | Buffer input_buffer; |
30 | | Pl_Discard discard; |
31 | | }; |
32 | | |
33 | | FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) : |
34 | | // We do not modify data, so it is safe to remove the const for Buffer |
35 | 295k | input_buffer(const_cast<unsigned char*>(data), size) |
36 | 295k | { |
37 | 295k | } |
38 | | |
39 | | std::shared_ptr<QPDF> |
40 | | FuzzHelper::getQpdf() |
41 | 259k | { |
42 | 259k | auto is = |
43 | 259k | std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer)); |
44 | 259k | auto qpdf = QPDF::create(); |
45 | 259k | qpdf->setMaxWarnings(200); |
46 | 259k | qpdf->processInputSource(is); |
47 | 259k | return qpdf; |
48 | 259k | } |
49 | | |
50 | | std::shared_ptr<QPDFWriter> |
51 | | FuzzHelper::getWriter(std::shared_ptr<QPDF> qpdf) |
52 | 69.0k | { |
53 | 69.0k | auto w = std::make_shared<QPDFWriter>(*qpdf); |
54 | 69.0k | w->setOutputPipeline(&this->discard); |
55 | 69.0k | w->setDecodeLevel(qpdf_dl_all); |
56 | 69.0k | return w; |
57 | 69.0k | } |
58 | | |
59 | | void |
60 | | FuzzHelper::doWrite(std::shared_ptr<QPDFWriter> w) |
61 | 67.3k | { |
62 | 67.3k | try { |
63 | 67.3k | w->write(); |
64 | 67.3k | } catch (QPDFExc const& e) { |
65 | 5.95k | std::cerr << e.what() << '\n'; |
66 | 5.95k | } catch (std::runtime_error const& e) { |
67 | 3.48k | std::cerr << e.what() << '\n'; |
68 | 3.48k | } |
69 | 67.3k | } |
70 | | |
71 | | void |
72 | | FuzzHelper::testWrite() |
73 | 172k | { |
74 | | // Write in various ways to exercise QPDFWriter |
75 | | |
76 | 172k | std::shared_ptr<QPDF> q; |
77 | 172k | std::shared_ptr<QPDFWriter> w; |
78 | | |
79 | 172k | q = getQpdf(); |
80 | 172k | w = getWriter(q); |
81 | 172k | w->setStaticID(true); |
82 | 172k | w->setLinearization(true); |
83 | 172k | w->setR6EncryptionParameters("u", "o", true, true, true, true, true, true, qpdf_r3p_full, true); |
84 | 172k | doWrite(w); |
85 | 172k | } |
86 | | |
87 | | void |
88 | | FuzzHelper::doChecks() |
89 | 279k | { |
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 | 279k | Pl_DCT::setMemoryLimit(100'000'000); |
95 | 279k | Pl_DCT::setScanLimit(50); |
96 | | |
97 | 279k | Pl_PNGFilter::setMemoryLimit(1'000'000); |
98 | 279k | Pl_RunLength::setMemoryLimit(1'000'000); |
99 | 279k | Pl_TIFFPredictor::setMemoryLimit(1'000'000); |
100 | 279k | 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 | 279k | Pl_DCT::setThrowOnCorruptData(true); |
105 | | |
106 | | // Get as much coverage as possible in parts of the library that |
107 | | // might benefit from fuzzing. |
108 | 279k | std::cerr << "\ninfo: starting testWrite\n"; |
109 | 279k | testWrite(); |
110 | 279k | } |
111 | | |
112 | | void |
113 | | FuzzHelper::run() |
114 | 259k | { |
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 | 259k | try { |
122 | 259k | doChecks(); |
123 | 259k | } catch (QPDFExc const& e) { |
124 | 172k | std::cerr << "QPDFExc: " << e.what() << '\n'; |
125 | 172k | } catch (std::runtime_error const& e) { |
126 | 705 | std::cerr << "runtime_error: " << e.what() << '\n'; |
127 | 705 | } |
128 | 259k | } |
129 | | |
130 | | extern "C" int |
131 | | LLVMFuzzerTestOneInput(unsigned char const* data, size_t size) |
132 | 295k | { |
133 | 295k | #ifndef _WIN32 |
134 | | // Used by jpeg library to work around false positives in memory |
135 | | // sanitizer. |
136 | 295k | setenv("JSIMD_FORCENONE", "1", 1); |
137 | 295k | #endif |
138 | 295k | FuzzHelper f(data, size); |
139 | 295k | f.run(); |
140 | 295k | return 0; |
141 | 295k | } |