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