2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2024-07-02 13:04:53 +00:00
|
|
|
#include <qpdf/Pl_DCT.hh>
|
2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/Pl_Discard.hh>
|
2024-07-28 18:53:46 +00:00
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2024-07-18 11:28:26 +00:00
|
|
|
#include <qpdf/Pl_PNGFilter.hh>
|
|
|
|
#include <qpdf/Pl_TIFFPredictor.hh>
|
2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFPageObjectHelper.hh>
|
|
|
|
#include <qpdf/QPDFWriter.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
2020-10-22 10:27:25 +00:00
|
|
|
#include <cstdlib>
|
2019-06-14 16:34:23 +00:00
|
|
|
|
|
|
|
class DiscardContents: public QPDFObjectHandle::ParserCallbacks
|
|
|
|
{
|
|
|
|
public:
|
2023-05-20 13:25:46 +00:00
|
|
|
~DiscardContents() override = default;
|
|
|
|
void
|
|
|
|
handleObject(QPDFObjectHandle) override
|
2019-06-14 16:34:23 +00:00
|
|
|
{
|
|
|
|
}
|
2023-05-20 13:25:46 +00:00
|
|
|
void
|
|
|
|
handleEOF() override
|
2019-06-14 16:34:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FuzzHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FuzzHelper(unsigned char const* data, size_t size);
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDF> getQpdf();
|
|
|
|
std::shared_ptr<QPDFWriter> getWriter(std::shared_ptr<QPDF>);
|
|
|
|
void doWrite(std::shared_ptr<QPDFWriter> w);
|
2019-06-14 16:34:23 +00:00
|
|
|
void testWrite();
|
|
|
|
void doChecks();
|
|
|
|
|
|
|
|
Buffer input_buffer;
|
|
|
|
Pl_Discard discard;
|
|
|
|
};
|
|
|
|
|
|
|
|
FuzzHelper::FuzzHelper(unsigned char const* data, size_t size) :
|
|
|
|
// We do not modify data, so it is safe to remove the const for Buffer
|
|
|
|
input_buffer(const_cast<unsigned char*>(data), size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDF>
|
2019-06-14 16:34:23 +00:00
|
|
|
FuzzHelper::getQpdf()
|
|
|
|
{
|
2022-04-09 18:49:10 +00:00
|
|
|
auto is =
|
2022-02-06 16:40:24 +00:00
|
|
|
std::shared_ptr<InputSource>(new BufferInputSource("fuzz input", &this->input_buffer));
|
2022-09-02 12:53:27 +00:00
|
|
|
auto qpdf = QPDF::create();
|
2024-07-27 20:18:06 +00:00
|
|
|
qpdf->setMaxWarnings(200);
|
2019-06-14 16:34:23 +00:00
|
|
|
qpdf->processInputSource(is);
|
|
|
|
return qpdf;
|
|
|
|
}
|
|
|
|
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDFWriter>
|
|
|
|
FuzzHelper::getWriter(std::shared_ptr<QPDF> qpdf)
|
2019-06-14 16:34:23 +00:00
|
|
|
{
|
2022-04-09 18:49:10 +00:00
|
|
|
auto w = std::make_shared<QPDFWriter>(*qpdf);
|
2019-06-14 16:34:23 +00:00
|
|
|
w->setOutputPipeline(&this->discard);
|
|
|
|
w->setDecodeLevel(qpdf_dl_all);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-09 18:49:10 +00:00
|
|
|
FuzzHelper::doWrite(std::shared_ptr<QPDFWriter> w)
|
2019-06-14 16:34:23 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
w->write();
|
|
|
|
} catch (QPDFExc const& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
2019-06-21 21:39:19 +00:00
|
|
|
} catch (std::runtime_error const& e) {
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
2019-06-14 16:34:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::testWrite()
|
|
|
|
{
|
|
|
|
// Write in various ways to exercise QPDFWriter
|
|
|
|
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDF> q;
|
|
|
|
std::shared_ptr<QPDFWriter> w;
|
2019-06-14 16:34:23 +00:00
|
|
|
|
|
|
|
q = getQpdf();
|
|
|
|
w = getWriter(q);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setDeterministicID(true);
|
|
|
|
w->setQDFMode(true);
|
2019-06-14 16:34:23 +00:00
|
|
|
doWrite(w);
|
|
|
|
|
|
|
|
q = getQpdf();
|
|
|
|
w = getWriter(q);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setStaticID(true);
|
2019-06-14 16:34:23 +00:00
|
|
|
w->setLinearization(true);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setR6EncryptionParameters("u", "o", true, true, true, true, true, true, qpdf_r3p_full, true);
|
2019-06-14 16:34:23 +00:00
|
|
|
doWrite(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::doChecks()
|
|
|
|
{
|
2024-07-02 13:04:53 +00:00
|
|
|
// Limit the memory used to decompress JPEG files during fuzzing. Excessive memory use during
|
|
|
|
// fuzzing is due to corrupt JPEG data which sometimes cannot be detected before
|
|
|
|
// jpeg_start_decompress is called. During normal use of qpdf very large JPEGs can occasionally
|
|
|
|
// occur legitimately and therefore must be allowed during normal operations.
|
2024-08-03 09:57:21 +00:00
|
|
|
Pl_DCT::setMemoryLimit(100'000'000);
|
2024-08-25 16:02:53 +00:00
|
|
|
Pl_DCT::setScanLimit(50);
|
2024-07-02 13:04:53 +00:00
|
|
|
|
2024-07-22 12:25:21 +00:00
|
|
|
Pl_PNGFilter::setMemoryLimit(1'000'000);
|
|
|
|
Pl_TIFFPredictor::setMemoryLimit(1'000'000);
|
2024-08-03 09:57:21 +00:00
|
|
|
Pl_Flate::setMemoryLimit(1'000'000);
|
2024-07-18 11:28:26 +00:00
|
|
|
|
2024-07-02 13:04:53 +00:00
|
|
|
// Do not decompress corrupt data. This may cause extended runtime within jpeglib without
|
|
|
|
// exercising additional code paths in qpdf, and potentially causing counterproductive timeouts.
|
|
|
|
Pl_DCT::setThrowOnCorruptData(true);
|
|
|
|
|
2019-06-14 16:34:23 +00:00
|
|
|
// Get as much coverage as possible in parts of the library that
|
|
|
|
// might benefit from fuzzing.
|
2024-06-30 12:01:50 +00:00
|
|
|
std::cerr << "\ninfo: starting testWrite\n";
|
2019-06-14 16:34:23 +00:00
|
|
|
testWrite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::run()
|
|
|
|
{
|
|
|
|
// The goal here is that you should be able to throw anything at
|
|
|
|
// libqpdf and it will respond without any memory errors and never
|
|
|
|
// do anything worse than throwing a QPDFExc or
|
|
|
|
// std::runtime_error. Throwing any other kind of exception,
|
|
|
|
// segfaulting, or having a memory error (when built with
|
|
|
|
// appropriate sanitizers) will all cause abnormal exit.
|
|
|
|
try {
|
|
|
|
doChecks();
|
|
|
|
} catch (QPDFExc const& e) {
|
|
|
|
std::cerr << "QPDFExc: " << e.what() << std::endl;
|
|
|
|
} catch (std::runtime_error const& e) {
|
|
|
|
std::cerr << "runtime_error: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int
|
|
|
|
LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
|
|
|
|
{
|
2020-10-22 10:27:25 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
// Used by jpeg library to work around false positives in memory
|
|
|
|
// sanitizer.
|
|
|
|
setenv("JSIMD_FORCENONE", "1", 1);
|
|
|
|
#endif
|
2019-06-14 16:34:23 +00:00
|
|
|
FuzzHelper f(data, size);
|
|
|
|
f.run();
|
|
|
|
return 0;
|
|
|
|
}
|