Commit 34729e37e001a92e5c7da250bd3118e052c79e64
1 parent
fe1fffe8
Limit memory used by Pl_PNGFilter and Pl_TIFFPredictor during fuzzing
Showing
5 changed files
with
41 additions
and
0 deletions
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> |
| ... | ... | @@ -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); | ... | ... |
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/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 | ... | ... |