2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/BufferInputSource.hh>
|
2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/Pl_Discard.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QPDF.hh>
|
|
|
|
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFOutlineDocumentHelper.hh>
|
2019-06-14 16:34:23 +00:00
|
|
|
#include <qpdf/QPDFPageDocumentHelper.hh>
|
|
|
|
#include <qpdf/QPDFPageLabelDocumentHelper.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#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:
|
2022-04-15 23:44:07 +00:00
|
|
|
virtual ~DiscardContents() = default;
|
2022-04-02 21:14:10 +00:00
|
|
|
virtual void
|
|
|
|
handleObject(QPDFObjectHandle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void
|
|
|
|
handleEOF()
|
|
|
|
{
|
|
|
|
}
|
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 testPages();
|
|
|
|
void testOutlines();
|
|
|
|
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 = std::shared_ptr<InputSource>(
|
2022-02-06 16:40:24 +00:00
|
|
|
new BufferInputSource("fuzz input", &this->input_buffer));
|
2022-04-09 18:49:10 +00:00
|
|
|
auto qpdf = std::make_shared<QPDF>();
|
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
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2019-06-14 16:34:23 +00:00
|
|
|
w->write();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc const& e) {
|
2019-06-14 16:34:23 +00:00
|
|
|
std::cerr << e.what() << std::endl;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error const& e) {
|
2019-06-21 21:39:19 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
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->setObjectStreamMode(qpdf_o_disable);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setR3EncryptionParameters(
|
2022-02-08 14:18:08 +00:00
|
|
|
"u", "o", true, true, true, true, true, true, qpdf_r3p_full);
|
2019-06-14 16:34:23 +00:00
|
|
|
doWrite(w);
|
|
|
|
|
|
|
|
q = getQpdf();
|
|
|
|
w = getWriter(q);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setDeterministicID(true);
|
2019-06-14 16:34:23 +00:00
|
|
|
w->setObjectStreamMode(qpdf_o_generate);
|
2019-06-21 21:39:19 +00:00
|
|
|
w->setLinearization(true);
|
2019-06-14 16:34:23 +00:00
|
|
|
doWrite(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::testPages()
|
|
|
|
{
|
|
|
|
// Parse all content streams, and exercise some helpers that
|
|
|
|
// operate on pages.
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDF> q = getQpdf();
|
2019-06-14 16:34:23 +00:00
|
|
|
QPDFPageDocumentHelper pdh(*q);
|
|
|
|
QPDFPageLabelDocumentHelper pldh(*q);
|
|
|
|
QPDFOutlineDocumentHelper odh(*q);
|
|
|
|
QPDFAcroFormDocumentHelper afdh(*q);
|
2019-06-21 21:39:19 +00:00
|
|
|
afdh.generateAppearancesIfNeeded();
|
|
|
|
pdh.flattenAnnotations();
|
2019-06-14 16:34:23 +00:00
|
|
|
std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages();
|
|
|
|
DiscardContents discard_contents;
|
|
|
|
int pageno = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
|
|
|
|
iter != pages.end();
|
|
|
|
++iter) {
|
2019-06-14 16:34:23 +00:00
|
|
|
QPDFPageObjectHelper& page(*iter);
|
|
|
|
++pageno;
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2019-06-21 21:39:19 +00:00
|
|
|
page.coalesceContentStreams();
|
2021-01-02 19:05:17 +00:00
|
|
|
page.parseContents(&discard_contents);
|
2020-12-31 18:23:49 +00:00
|
|
|
page.getImages();
|
2019-06-14 16:34:23 +00:00
|
|
|
pldh.getLabelForPage(pageno);
|
2019-06-21 21:39:19 +00:00
|
|
|
QPDFObjectHandle page_obj(page.getObjectHandle());
|
|
|
|
page_obj.getJSON(true).unparse();
|
|
|
|
odh.getOutlinesForPage(page_obj.getObjGen());
|
2019-06-14 16:34:23 +00:00
|
|
|
|
|
|
|
std::vector<QPDFAnnotationObjectHelper> annotations =
|
|
|
|
afdh.getWidgetAnnotationsForPage(page);
|
|
|
|
for (std::vector<QPDFAnnotationObjectHelper>::iterator annot_iter =
|
|
|
|
annotations.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
annot_iter != annotations.end();
|
|
|
|
++annot_iter) {
|
2019-06-14 16:34:23 +00:00
|
|
|
QPDFAnnotationObjectHelper& aoh = *annot_iter;
|
|
|
|
afdh.getFieldForAnnotation(aoh);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc& e) {
|
|
|
|
std::cerr << "page " << pageno << ": " << e.what() << std::endl;
|
2019-06-14 16:34:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::testOutlines()
|
|
|
|
{
|
2022-04-09 18:49:10 +00:00
|
|
|
std::shared_ptr<QPDF> q = getQpdf();
|
2022-04-02 21:14:10 +00:00
|
|
|
std::list<std::vector<QPDFOutlineObjectHelper>> queue;
|
2019-06-14 16:34:23 +00:00
|
|
|
QPDFOutlineDocumentHelper odh(*q);
|
|
|
|
queue.push_back(odh.getTopLevelOutlines());
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!queue.empty()) {
|
2019-07-03 17:34:02 +00:00
|
|
|
std::vector<QPDFOutlineObjectHelper>& outlines = *(queue.begin());
|
|
|
|
for (std::vector<QPDFOutlineObjectHelper>::iterator iter =
|
2019-06-14 16:34:23 +00:00
|
|
|
outlines.begin();
|
2022-04-02 21:14:10 +00:00
|
|
|
iter != outlines.end();
|
|
|
|
++iter) {
|
2019-06-14 16:34:23 +00:00
|
|
|
QPDFOutlineObjectHelper& ol = *iter;
|
|
|
|
ol.getDestPage();
|
|
|
|
queue.push_back(ol.getKids());
|
|
|
|
}
|
|
|
|
queue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FuzzHelper::doChecks()
|
|
|
|
{
|
|
|
|
// Get as much coverage as possible in parts of the library that
|
|
|
|
// might benefit from fuzzing.
|
|
|
|
testWrite();
|
|
|
|
testPages();
|
|
|
|
testOutlines();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
2019-06-14 16:34:23 +00:00
|
|
|
doChecks();
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (QPDFExc const& e) {
|
2019-06-14 16:34:23 +00:00
|
|
|
std::cerr << "QPDFExc: " << e.what() << std::endl;
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::runtime_error const& e) {
|
2019-06-14 16:34:23 +00:00
|
|
|
std::cerr << "runtime_error: " << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
extern "C" int
|
|
|
|
LLVMFuzzerTestOneInput(unsigned char const* data, size_t size)
|
2019-06-14 16:34:23 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|