Commit 5be057caf0417ff9b748c5d6ee459e7756ea84df
Committed by
GitHub
Merge pull request #1247 from m-holger/fuzz
Adjust fuzzer warning and memory limits
Showing
10 changed files
with
57 additions
and
23 deletions
fuzz/CMakeLists.txt
| ... | ... | @@ -99,6 +99,7 @@ set(CORPUS_OTHER |
| 99 | 99 | 16301.fuzz |
| 100 | 100 | 16953.fuzz |
| 101 | 101 | 17630.fuzz |
| 102 | + 17630a.fuzz | |
| 102 | 103 | 18241.fuzz |
| 103 | 104 | 18247.fuzz |
| 104 | 105 | 23172.fuzz |
| ... | ... | @@ -129,7 +130,6 @@ set(CORPUS_OTHER |
| 129 | 130 | 70055.fuzz |
| 130 | 131 | 70245.fuzz |
| 131 | 132 | 70306.fuzz |
| 132 | - 4826608268017664.fuzz | |
| 133 | 133 | ) |
| 134 | 134 | |
| 135 | 135 | set(CORPUS_DIR ${CMAKE_CURRENT_BINARY_DIR}/qpdf_corpus) | ... | ... |
fuzz/qpdf_extra/4826608268017664.fuzz renamed to fuzz/qpdf_extra/17630a.fuzz
No preview for this file type
fuzz/qpdf_fuzzer.cc
| ... | ... | @@ -2,6 +2,8 @@ |
| 2 | 2 | #include <qpdf/BufferInputSource.hh> |
| 3 | 3 | #include <qpdf/Pl_DCT.hh> |
| 4 | 4 | #include <qpdf/Pl_Discard.hh> |
| 5 | +#include <qpdf/Pl_PNGFilter.hh> | |
| 6 | +#include <qpdf/Pl_TIFFPredictor.hh> | |
| 5 | 7 | #include <qpdf/QPDF.hh> |
| 6 | 8 | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
| 7 | 9 | #include <qpdf/QPDFOutlineDocumentHelper.hh> |
| ... | ... | @@ -57,7 +59,7 @@ FuzzHelper::getQpdf() |
| 57 | 59 | auto is = |
| 58 | 60 | std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer)); |
| 59 | 61 | auto qpdf = QPDF::create(); |
| 60 | - qpdf->setMaxWarnings(20); | |
| 62 | + qpdf->setMaxWarnings(500); | |
| 61 | 63 | qpdf->processInputSource(is); |
| 62 | 64 | return qpdf; |
| 63 | 65 | } |
| ... | ... | @@ -179,6 +181,9 @@ FuzzHelper::doChecks() |
| 179 | 181 | // occur legitimately and therefore must be allowed during normal operations. |
| 180 | 182 | Pl_DCT::setMemoryLimit(1'000'000'000); |
| 181 | 183 | |
| 184 | + Pl_PNGFilter::setMemoryLimit(1'000'000'000); | |
| 185 | + Pl_TIFFPredictor::setMemoryLimit(1'000'000'000); | |
| 186 | + | |
| 182 | 187 | // Do not decompress corrupt data. This may cause extended runtime within jpeglib without |
| 183 | 188 | // exercising additional code paths in qpdf, and potentially causing counterproductive timeouts. |
| 184 | 189 | Pl_DCT::setThrowOnCorruptData(true); | ... | ... |
include/qpdf/QPDF.hh
| ... | ... | @@ -228,9 +228,9 @@ class QPDF |
| 228 | 228 | QPDF_DLL |
| 229 | 229 | void setSuppressWarnings(bool); |
| 230 | 230 | |
| 231 | - // Set the maximum number of warnings to output. Subsequent warnings are suppressed. | |
| 231 | + // Set the maximum number of warnings. A QPDFExc is thrown if the limit is exceeded. | |
| 232 | 232 | QPDF_DLL |
| 233 | - void setMaxWarnings(int); | |
| 233 | + void setMaxWarnings(size_t); | |
| 234 | 234 | |
| 235 | 235 | // By default, QPDF will try to recover if it finds certain types of errors in PDF files. If |
| 236 | 236 | // turned off, it will throw an exception on the first such problem it finds without attempting |
| ... | ... | @@ -1501,7 +1501,7 @@ class QPDF |
| 1501 | 1501 | bool provided_password_is_hex_key{false}; |
| 1502 | 1502 | bool ignore_xref_streams{false}; |
| 1503 | 1503 | bool suppress_warnings{false}; |
| 1504 | - int max_warnings{0}; | |
| 1504 | + size_t max_warnings{0}; | |
| 1505 | 1505 | bool attempt_recovery{true}; |
| 1506 | 1506 | bool check_mode{false}; |
| 1507 | 1507 | std::shared_ptr<EncryptionParameters> encp; | ... | ... |
libqpdf/Pl_PNGFilter.cc
| ... | ... | @@ -7,6 +7,11 @@ |
| 7 | 7 | #include <cstring> |
| 8 | 8 | #include <stdexcept> |
| 9 | 9 | |
| 10 | +namespace | |
| 11 | +{ | |
| 12 | + unsigned long long memory_limit{0}; | |
| 13 | +} // namespace | |
| 14 | + | |
| 10 | 15 | static int |
| 11 | 16 | abs_diff(int a, int b) |
| 12 | 17 | { |
| ... | ... | @@ -41,6 +46,9 @@ Pl_PNGFilter::Pl_PNGFilter( |
| 41 | 46 | if ((bpr == 0) || (bpr > (UINT_MAX - 1))) { |
| 42 | 47 | throw std::runtime_error("PNGFilter created with invalid columns value"); |
| 43 | 48 | } |
| 49 | + if (memory_limit > 0 && bpr > (memory_limit / 2U)) { | |
| 50 | + throw std::runtime_error("PNGFilter memory limit exceeded"); | |
| 51 | + } | |
| 44 | 52 | this->bytes_per_row = bpr & UINT_MAX; |
| 45 | 53 | this->buf1 = QUtil::make_shared_array<unsigned char>(this->bytes_per_row + 1); |
| 46 | 54 | this->buf2 = QUtil::make_shared_array<unsigned char>(this->bytes_per_row + 1); |
| ... | ... | @@ -54,6 +62,12 @@ Pl_PNGFilter::Pl_PNGFilter( |
| 54 | 62 | } |
| 55 | 63 | |
| 56 | 64 | void |
| 65 | +Pl_PNGFilter::setMemoryLimit(unsigned long long limit) | |
| 66 | +{ | |
| 67 | + memory_limit = limit; | |
| 68 | +} | |
| 69 | + | |
| 70 | +void | |
| 57 | 71 | Pl_PNGFilter::write(unsigned char const* data, size_t len) |
| 58 | 72 | { |
| 59 | 73 | size_t left = this->incoming - this->pos; | ... | ... |
libqpdf/Pl_TIFFPredictor.cc
| ... | ... | @@ -7,6 +7,11 @@ |
| 7 | 7 | #include <climits> |
| 8 | 8 | #include <stdexcept> |
| 9 | 9 | |
| 10 | +namespace | |
| 11 | +{ | |
| 12 | + unsigned long long memory_limit{0}; | |
| 13 | +} // namespace | |
| 14 | + | |
| 10 | 15 | Pl_TIFFPredictor::Pl_TIFFPredictor( |
| 11 | 16 | char const* identifier, |
| 12 | 17 | Pipeline* next, |
| ... | ... | @@ -31,10 +36,19 @@ Pl_TIFFPredictor::Pl_TIFFPredictor( |
| 31 | 36 | if ((bpr == 0) || (bpr > (UINT_MAX - 1))) { |
| 32 | 37 | throw std::runtime_error("TIFFPredictor created with invalid columns value"); |
| 33 | 38 | } |
| 39 | + if (memory_limit > 0 && bpr > (memory_limit / 2U)) { | |
| 40 | + throw std::runtime_error("TIFFPredictor memory limit exceeded"); | |
| 41 | + } | |
| 34 | 42 | this->bytes_per_row = bpr & UINT_MAX; |
| 35 | 43 | } |
| 36 | 44 | |
| 37 | 45 | void |
| 46 | +Pl_TIFFPredictor::setMemoryLimit(unsigned long long limit) | |
| 47 | +{ | |
| 48 | + memory_limit = limit; | |
| 49 | +} | |
| 50 | + | |
| 51 | +void | |
| 38 | 52 | Pl_TIFFPredictor::write(unsigned char const* data, size_t len) |
| 39 | 53 | { |
| 40 | 54 | auto end = data + len; | ... | ... |
libqpdf/QPDF.cc
| ... | ... | @@ -332,7 +332,7 @@ QPDF::setSuppressWarnings(bool val) |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | void |
| 335 | -QPDF::setMaxWarnings(int val) | |
| 335 | +QPDF::setMaxWarnings(size_t val) | |
| 336 | 336 | { |
| 337 | 337 | m->max_warnings = val; |
| 338 | 338 | } |
| ... | ... | @@ -504,13 +504,11 @@ QPDF::inParse(bool v) |
| 504 | 504 | void |
| 505 | 505 | QPDF::warn(QPDFExc const& e) |
| 506 | 506 | { |
| 507 | + if (m->max_warnings > 0 && m->warnings.size() >= m->max_warnings) { | |
| 508 | + stopOnError("Too many warnings - file is too badly damaged"); | |
| 509 | + } | |
| 507 | 510 | m->warnings.push_back(e); |
| 508 | 511 | if (!m->suppress_warnings) { |
| 509 | - if (m->max_warnings > 0 && m->warnings.size() > 20) { | |
| 510 | - *m->log->getWarn() << "WARNING: too many warnings - additional warnings suppressed\n"; | |
| 511 | - m->suppress_warnings = true; | |
| 512 | - return; | |
| 513 | - } | |
| 514 | 512 | *m->log->getWarn() << "WARNING: " << m->warnings.back().what() << "\n"; |
| 515 | 513 | } |
| 516 | 514 | } | ... | ... |
libqpdf/QPDF_json.cc
| ... | ... | @@ -233,13 +233,12 @@ provide_data(std::shared_ptr<InputSource> is, qpdf_offset_t start, qpdf_offset_t |
| 233 | 233 | class QPDF::JSONReactor: public JSON::Reactor |
| 234 | 234 | { |
| 235 | 235 | public: |
| 236 | - JSONReactor(QPDF& pdf, std::shared_ptr<InputSource> is, bool must_be_complete, int max_warnings) : | |
| 236 | + JSONReactor(QPDF& pdf, std::shared_ptr<InputSource> is, bool must_be_complete) : | |
| 237 | 237 | pdf(pdf), |
| 238 | 238 | is(is), |
| 239 | 239 | must_be_complete(must_be_complete), |
| 240 | 240 | descr(std::make_shared<QPDFValue::Description>( |
| 241 | - QPDFValue::JSON_Descr(std::make_shared<std::string>(is->getName()), ""))), | |
| 242 | - max_warnings(max_warnings) | |
| 241 | + QPDFValue::JSON_Descr(std::make_shared<std::string>(is->getName()), ""))) | |
| 243 | 242 | { |
| 244 | 243 | for (auto& oc: pdf.m->obj_cache) { |
| 245 | 244 | if (oc.second.object->getTypeCode() == ::ot_reserved) { |
| ... | ... | @@ -292,8 +291,7 @@ class QPDF::JSONReactor: public JSON::Reactor |
| 292 | 291 | std::shared_ptr<InputSource> is; |
| 293 | 292 | bool must_be_complete{true}; |
| 294 | 293 | std::shared_ptr<QPDFValue::Description> descr; |
| 295 | - int errors{0}; | |
| 296 | - int max_warnings{0}; | |
| 294 | + bool errors{false}; | |
| 297 | 295 | bool saw_qpdf{false}; |
| 298 | 296 | bool saw_qpdf_meta{false}; |
| 299 | 297 | bool saw_objects{false}; |
| ... | ... | @@ -316,21 +314,18 @@ class QPDF::JSONReactor: public JSON::Reactor |
| 316 | 314 | void |
| 317 | 315 | QPDF::JSONReactor::error(qpdf_offset_t offset, std::string const& msg) |
| 318 | 316 | { |
| 319 | - ++errors; | |
| 317 | + errors = true; | |
| 320 | 318 | std::string object = this->cur_object; |
| 321 | 319 | if (is->getName() != pdf.getFilename()) { |
| 322 | 320 | object += " from " + is->getName(); |
| 323 | 321 | } |
| 324 | - this->pdf.warn(qpdf_e_json, object, offset, msg); | |
| 325 | - if (max_warnings > 0 && errors >= max_warnings) { | |
| 326 | - throw std::runtime_error("errors found in JSON"); | |
| 327 | - } | |
| 322 | + pdf.warn(qpdf_e_json, object, offset, msg); | |
| 328 | 323 | } |
| 329 | 324 | |
| 330 | 325 | bool |
| 331 | 326 | QPDF::JSONReactor::anyErrors() const |
| 332 | 327 | { |
| 333 | - return errors > 0; | |
| 328 | + return errors; | |
| 334 | 329 | } |
| 335 | 330 | |
| 336 | 331 | void |
| ... | ... | @@ -825,7 +820,7 @@ QPDF::updateFromJSON(std::shared_ptr<InputSource> is) |
| 825 | 820 | void |
| 826 | 821 | QPDF::importJSON(std::shared_ptr<InputSource> is, bool must_be_complete) |
| 827 | 822 | { |
| 828 | - JSONReactor reactor(*this, is, must_be_complete, m->max_warnings); | |
| 823 | + JSONReactor reactor(*this, is, must_be_complete); | |
| 829 | 824 | try { |
| 830 | 825 | JSON::parse(*is, &reactor); |
| 831 | 826 | } catch (std::runtime_error& e) { | ... | ... |
libqpdf/qpdf/Pl_PNGFilter.hh
| ... | ... | @@ -24,6 +24,10 @@ class Pl_PNGFilter: public Pipeline |
| 24 | 24 | unsigned int bits_per_sample = 8); |
| 25 | 25 | ~Pl_PNGFilter() override = default; |
| 26 | 26 | |
| 27 | + // Limit the memory used. | |
| 28 | + // NB This is a static option affecting all Pl_PNGFilter instances. | |
| 29 | + static void setMemoryLimit(unsigned long long limit); | |
| 30 | + | |
| 27 | 31 | void write(unsigned char const* data, size_t len) override; |
| 28 | 32 | void finish() override; |
| 29 | 33 | ... | ... |
libqpdf/qpdf/Pl_TIFFPredictor.hh
| ... | ... | @@ -22,6 +22,10 @@ class Pl_TIFFPredictor: public Pipeline |
| 22 | 22 | unsigned int bits_per_sample = 8); |
| 23 | 23 | ~Pl_TIFFPredictor() override = default; |
| 24 | 24 | |
| 25 | + // Limit the memory used. | |
| 26 | + // NB This is a static option affecting all Pl_TIFFPredictor instances. | |
| 27 | + static void setMemoryLimit(unsigned long long limit); | |
| 28 | + | |
| 25 | 29 | void write(unsigned char const* data, size_t len) override; |
| 26 | 30 | void finish() override; |
| 27 | 31 | ... | ... |