| 1 |
// SPDX-FileCopyrightText: 2007 Dominik Seichter <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]> |
| 3 |
// SPDX-License-Identifier: MIT-0 |
| 4 |
|
| 5 |
/* |
| 6 |
Notes: |
| 7 |
|
| 8 |
1) out of memory tests don't run if Address Sanitizer (ASAN) is enabled because |
| 9 |
ASAN terminates the unit test process the first time it attempts to allocate |
| 10 |
too much memory (so running the tests with and without ASAN is recommended) |
| 11 |
|
| 12 |
2) PoDoFo log warnings about inconsistencies or values out of range are expected |
| 13 |
because the tests are supplying invalid values to check PoDoFo behaves correctly |
| 14 |
in those situations |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <limits> |
| 18 |
#include <sstream> |
| 19 |
|
| 20 |
#include <PdfTest.h> |
| 21 |
#include <podofo/private/PdfParser.h> |
| 22 |
|
| 23 |
using namespace std; |
| 24 |
using namespace PoDoFo; |
| 25 |
|
| 26 |
static string generateXRefEntries(size_t count); |
| 27 |
static bool canOutOfMemoryKillUnitTests(); |
| 28 |
static size_t getStackOverflowDepth(); |
| 29 |
static string generateNestedOutlinesPdf(bool includePages); |
| 30 |
|
| 31 |
// this value is from Table C.1 in Appendix C.2 Architectural Limits in PDF 32000-1:2008 |
| 32 |
// on 32-bit systems sizeof(PdfParser::TXRefEntry)=16 => max size of m_offsets=16*8,388,607 = 134 MB |
| 33 |
// on 64-bit systems sizeof(PdfParser::TXRefEntry)=24 => max size of m_offsets=16*8,388,607 = 201 MB |
| 34 |
constexpr unsigned maxNumberOfIndirectObjects = 8388607; |
| 35 |
|
| 36 |
namespace PoDoFo |
| 37 |
{ |
| 38 |
class PdfParserTest : public PdfParser |
| 39 |
{ |
| 40 |
public: |
| 41 |
PdfParserTest(PdfIndirectObjectList& objectList, string buff) |
| 42 |
: PdfParser(objectList), m_buffer(std::move(buff)), m_device(new SpanStreamDevice(m_buffer)) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
static void TestReadXRefContents(); |
| 47 |
static void TestMaxObjectCount(); |
| 48 |
static void TestMaxObjectCount2(); |
| 49 |
static void TestReadXRefStreamContents(); |
| 50 |
static void TestReadObjects(); |
| 51 |
static void TestIsPdfFile(); |
| 52 |
static void TestNestedArrays(); |
| 53 |
static void TestNestedDictionaries(); |
| 54 |
static void TestInvalidXRefEntries(); |
| 55 |
|
| 56 |
void ReadXRefContents(size_t offset, bool skipFollowPrevious) |
| 57 |
{ |
| 58 |
PdfParser::ReadXRefContents(*m_device, offset, skipFollowPrevious); |
| 59 |
} |
| 60 |
|
| 61 |
void ReadXRefSubsection(int64_t firstObject, int64_t objectCount) |
| 62 |
{ |
| 63 |
PdfParser::ReadXRefSubsection(*m_device, firstObject, objectCount); |
| 64 |
} |
| 65 |
|
| 66 |
void ReadXRefStreamContents(size_t offset, bool skipFollowPrevious) |
| 67 |
{ |
| 68 |
nullable<size_t> prevOffset; |
| 69 |
PdfParser::ReadXRefStreamContents(*m_device, offset, prevOffset); |
| 70 |
if (!skipFollowPrevious && prevOffset != nullptr) |
| 71 |
PdfParser::ReadXRefContents(*m_device, *prevOffset, false); |
| 72 |
} |
| 73 |
|
| 74 |
void ReadDocumentStructure() |
| 75 |
{ |
| 76 |
PdfParser::ReadDocumentStructure(*m_device); |
| 77 |
} |
| 78 |
|
| 79 |
void ReadObjects() |
| 80 |
{ |
| 81 |
PdfParser::ReadObjectEntries(*m_device); |
| 82 |
} |
| 83 |
|
| 84 |
void ReadHeader() |
| 85 |
{ |
| 86 |
PdfParser::ReadHeader(*m_device); |
| 87 |
} |
| 88 |
|
| 89 |
void ReadObjectsInternal() |
| 90 |
{ |
| 91 |
PdfParser::ReadObjectsInternal(*m_device); |
| 92 |
} |
| 93 |
|
| 94 |
const shared_ptr<InputStreamDevice>& GetDevice() { return m_device; } |
| 95 |
|
| 96 |
private: |
| 97 |
static void testReadXRefSubsection(); |
| 98 |
|
| 99 |
private: |
| 100 |
string m_buffer; |
| 101 |
shared_ptr<InputStreamDevice> m_device; |
| 102 |
}; |
| 103 |
} |
| 104 |
|
| 105 |
METHOD_AS_TEST_CASE(PdfParserTest::TestReadXRefContents, "TestReadXRefContents"); |
| 106 |
METHOD_AS_TEST_CASE(PdfParserTest::TestMaxObjectCount, "TestMaxObjectCount"); |
| 107 |
METHOD_AS_TEST_CASE(PdfParserTest::TestMaxObjectCount2, "TestMaxObjectCount2", "[.]"); |
| 108 |
METHOD_AS_TEST_CASE(PdfParserTest::TestReadXRefStreamContents, "TestReadXRefStreamContents"); |
| 109 |
METHOD_AS_TEST_CASE(PdfParserTest::TestIsPdfFile, "TestIsPdfFile"); |
| 110 |
METHOD_AS_TEST_CASE(PdfParserTest::TestNestedArrays, "TestNestedArrays"); |
| 111 |
METHOD_AS_TEST_CASE(PdfParserTest::TestNestedDictionaries, "TestNestedDictionaries"); |
| 112 |
METHOD_AS_TEST_CASE(PdfParserTest::TestInvalidXRefEntries, "TestInvalidXRefEntries"); |
| 113 |
|
| 114 |
TEST_CASE("TestRemoveStream") |
| 115 |
{ |
| 116 |
PdfMemDocument doc; |
| 117 |
doc.Load(TestUtils::GetTestInputFilePath("TestImage1.pdf")); |
| 118 |
auto& page = doc.GetPages().GetPageAt(0); |
| 119 |
auto& resources = page.GetResources(); |
| 120 |
auto& imageObj = *resources.GetResource(PdfResourceType::XObject, "XOb5"); |
| 121 |
REQUIRE(imageObj.HasStream()); |
| 122 |
(void)imageObj.MustGetStream(); |
| 123 |
REQUIRE(!imageObj.IsDirty()); |
| 124 |
imageObj.RemoveStream(); |
| 125 |
REQUIRE(imageObj.IsDirty()); |
| 126 |
REQUIRE(!imageObj.HasStream()); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_CASE("TestXRefRecovery1") |
| 130 |
{ |
| 131 |
PdfMemDocument doc; |
| 132 |
doc.Load(TestUtils::GetTestInputFilePath("TestXRefRecovery1.pdf")); |
| 133 |
|
| 134 |
auto testDoc = [](PdfDocument& doc) { |
| 135 |
auto& page = doc.GetPages().GetPageAt(0); |
| 136 |
vector<PdfTextEntry> entries; |
| 137 |
page.ExtractTextTo(entries); |
| 138 |
|
| 139 |
REQUIRE(entries[0].Text == "Hello world"); |
| 140 |
ASSERT_EQUAL(entries[0].X, 148.90299999999999); |
| 141 |
ASSERT_EQUAL(entries[0].Y, 722.75699999999995); |
| 142 |
ASSERT_EQUAL(entries[0].Length, 57.372); |
| 143 |
}; |
| 144 |
|
| 145 |
testDoc(doc); |
| 146 |
doc.Save(TestUtils::GetTestOutputFilePath("TestXRefRecovery1.pdf")); |
| 147 |
doc.Load(TestUtils::GetTestOutputFilePath("TestXRefRecovery1.pdf")); |
| 148 |
testDoc(doc); |
| 149 |
} |
| 150 |
|
| 151 |
TEST_CASE("TestXRefRecovery2") |
| 152 |
{ |
| 153 |
PdfMemDocument doc; |
| 154 |
doc.Load(TestUtils::GetTestInputFilePath("TestXRefRecovery2.pdf")); |
| 155 |
|
| 156 |
auto testDoc = [](PdfDocument& doc) { |
| 157 |
auto& page = doc.GetPages().GetPageAt(0); |
| 158 |
vector<PdfTextEntry> entries; |
| 159 |
page.ExtractTextTo(entries); |
| 160 |
|
| 161 |
REQUIRE(entries[0].Text == "PDF:B"); |
| 162 |
ASSERT_EQUAL(entries[0].X, 56.799999999999997); |
| 163 |
ASSERT_EQUAL(entries[0].Y, 724.60000000000002); |
| 164 |
ASSERT_EQUAL(entries[0].Length, 33.324000000000005); |
| 165 |
|
| 166 |
auto& obj = doc.GetObjects().MustGetObject(PdfReference(9, 0)); |
| 167 |
unique_ptr<PdfImage> image; |
| 168 |
REQUIRE(PdfImage::TryCreateFromObject(obj, image)); |
| 169 |
REQUIRE(image->GetWidth() == 156); |
| 170 |
REQUIRE(image->GetHeight() == 140); |
| 171 |
}; |
| 172 |
|
| 173 |
testDoc(doc); |
| 174 |
doc.Save(TestUtils::GetTestOutputFilePath("TestXRefRecovery2.pdf")); |
| 175 |
doc.Load(TestUtils::GetTestOutputFilePath("TestXRefRecovery2.pdf")); |
| 176 |
testDoc(doc); |
| 177 |
} |
| 178 |
|
| 179 |
void PdfParserTest::TestMaxObjectCount() |
| 180 |
{ |
| 181 |
PdfCommon::SetMaxObjectCount(numeric_limits<unsigned short>::max()); |
| 182 |
testReadXRefSubsection(); |
| 183 |
|
| 184 |
PdfCommon::SetMaxObjectCount(maxNumberOfIndirectObjects); |
| 185 |
testReadXRefSubsection(); |
| 186 |
} |
| 187 |
|
| 188 |
// NOTE: This test is too long to be normally done on every run |
| 189 |
void PdfParserTest::TestMaxObjectCount2() |
| 190 |
{ |
| 191 |
PdfCommon::SetMaxObjectCount(numeric_limits<unsigned>::max()); |
| 192 |
testReadXRefSubsection(); |
| 193 |
} |
| 194 |
|
| 195 |
void PdfParserTest::TestReadXRefContents() |
| 196 |
{ |
| 197 |
try |
| 198 |
{ |
| 199 |
// generate an xref section |
| 200 |
// xref |
| 201 |
// 0 3 |
| 202 |
// 0000000000 65535 f |
| 203 |
// 0000000018 00000 n |
| 204 |
// 0000000077 00000 n |
| 205 |
// trailer << /Root 1 0 R /Size 3 >> |
| 206 |
// startxref |
| 207 |
// 0 |
| 208 |
// %%EOF |
| 209 |
ostringstream oss; |
| 210 |
oss << "xref\r\n0 3\r\n"; |
| 211 |
oss << generateXRefEntries(3); |
| 212 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 213 |
oss << "startxref 0\r\n"; |
| 214 |
oss << "%EOF"; |
| 215 |
PdfIndirectObjectList objects; |
| 216 |
PdfParserTest parser(objects, oss.str()); |
| 217 |
parser.ReadXRefContents(0, false); |
| 218 |
// expected to succeed |
| 219 |
} |
| 220 |
catch (PdfError&) |
| 221 |
{ |
| 222 |
FAIL("Should not throw PdfError"); |
| 223 |
} |
| 224 |
catch (exception&) |
| 225 |
{ |
| 226 |
FAIL("Unexpected exception type"); |
| 227 |
} |
| 228 |
|
| 229 |
try |
| 230 |
{ |
| 231 |
// generate an xref section with missing xref entries |
| 232 |
// xref |
| 233 |
// 0 3 |
| 234 |
// 0000000000 65535 f |
| 235 |
// 0000000018 00000 n |
| 236 |
// |
| 237 |
// trailer << /Root 1 0 R /Size 3 >> |
| 238 |
// startxref |
| 239 |
// 0 |
| 240 |
// %%EOF |
| 241 |
ostringstream oss; |
| 242 |
oss << "xref\r\n0 3\r\n"; |
| 243 |
oss << generateXRefEntries(2); // 2 entries supplied, but expecting 3 entries |
| 244 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 245 |
oss << "startxref 0\r\n"; |
| 246 |
oss << "%EOF"; |
| 247 |
PdfIndirectObjectList objects; |
| 248 |
PdfParserTest parser(objects, oss.str()); |
| 249 |
parser.ReadXRefContents(0, false); |
| 250 |
} |
| 251 |
catch (PdfError& error) |
| 252 |
{ |
| 253 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidTrailer); |
| 254 |
} |
| 255 |
catch (exception&) |
| 256 |
{ |
| 257 |
FAIL("Unexpected exception type"); |
| 258 |
} |
| 259 |
|
| 260 |
try |
| 261 |
{ |
| 262 |
// TODO malformed entries are not detected |
| 263 |
// generate an xref section with badly formed xref entries |
| 264 |
// xref |
| 265 |
// 0 3 |
| 266 |
// 000000000 65535 |
| 267 |
// 00000000065535 x |
| 268 |
// 0000000 |
| 269 |
// 0000000018 00000 n |
| 270 |
// 0000000077 00000 n |
| 271 |
// trailer << /Root 1 0 R /Size 3 >> |
| 272 |
// startxref |
| 273 |
// 0 |
| 274 |
// %%EOF |
| 275 |
ostringstream oss; |
| 276 |
oss << "xref\r\n0 5\r\n"; |
| 277 |
oss << "000000000 65535\r\n"; |
| 278 |
oss << "00000000065535 x\r\n"; |
| 279 |
oss << "0000000\r\n"; |
| 280 |
oss << generateXRefEntries(2); |
| 281 |
oss << "trailer << /Root 1 0 R /Size 5 >>\r\n"; |
| 282 |
oss << "startxref 0\r\n"; |
| 283 |
oss << "%EOF"; |
| 284 |
PdfIndirectObjectList objects; |
| 285 |
PdfParserTest parser(objects, oss.str()); |
| 286 |
parser.ReadXRefContents(0, false); |
| 287 |
// succeeds reading badly formed xref entries - should it? |
| 288 |
} |
| 289 |
catch (PdfError& error) |
| 290 |
{ |
| 291 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidTrailer); |
| 292 |
} |
| 293 |
catch (exception&) |
| 294 |
{ |
| 295 |
FAIL("Unexpected exception type"); |
| 296 |
} |
| 297 |
|
| 298 |
// CVE-2017-8053 ReadXRefContents and ReadXRefStreamContents are mutually recursive |
| 299 |
// and can cause stack overflow |
| 300 |
|
| 301 |
try |
| 302 |
{ |
| 303 |
// generate an xref section and one XRef stream that references itself |
| 304 |
// via the /Prev entry (but use a slightly lower offset by linking to |
| 305 |
// whitespace discarded by the tokenizer just before the xref section) |
| 306 |
// xref |
| 307 |
// 0 1 |
| 308 |
// 000000000 65535 |
| 309 |
// 2 0 obj << /Type XRef /Prev offsetXrefStmObj2 >> stream ... endstream |
| 310 |
// trailer << /Root 1 0 R /Size 3 >> |
| 311 |
// startxref |
| 312 |
// offsetXrefStmObj2 |
| 313 |
// %%EOF |
| 314 |
ostringstream oss; |
| 315 |
|
| 316 |
// object stream contents - length excludes trailing whitespace |
| 317 |
string streamContents = |
| 318 |
"01 0E8A 0\r\n" |
| 319 |
"02 0002 00\r\n"; |
| 320 |
size_t streamContentsLength = streamContents.size() - strlen("\r\n"); |
| 321 |
|
| 322 |
// xref section at offset 0 |
| 323 |
//size_t offsetXref = 0; |
| 324 |
oss << "xref\r\n0 1\r\n"; |
| 325 |
oss << generateXRefEntries(1); |
| 326 |
|
| 327 |
// XRef stream at offsetXrefStm1, but any /Prev entries pointing to any offset between |
| 328 |
// offsetXrefStm1Whitespace and offsetXrefStm1 point to the same /Prev section |
| 329 |
// because the PDF processing model says tokenizer must discard whitespace and comments |
| 330 |
size_t offsetXrefStm1Whitespace = oss.str().length(); |
| 331 |
oss << " \r\n"; |
| 332 |
oss << "% comments and leading white space are ignored - see PdfTokenizer::GetNextToken\r\n"; |
| 333 |
size_t offsetXrefStm1 = oss.str().length(); |
| 334 |
oss << "2 0 obj "; |
| 335 |
oss << "<< /Type /XRef "; |
| 336 |
oss << "/Length " << streamContentsLength << " "; |
| 337 |
oss << "/Index [2 2] "; |
| 338 |
oss << "/Size 3 "; |
| 339 |
oss << "/Prev " << offsetXrefStm1Whitespace << " "; // xref /Prev offset points back to start of this stream object |
| 340 |
oss << "/W [1 2 1] "; |
| 341 |
oss << "/Filter /ASCIIHexDecode "; |
| 342 |
oss << ">>\r\n"; |
| 343 |
oss << "stream\r\n"; |
| 344 |
oss << streamContents; |
| 345 |
oss << "endstream\r\n"; |
| 346 |
oss << "endobj\r\n"; |
| 347 |
|
| 348 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 349 |
oss << "startxref " << offsetXrefStm1 << "\r\n"; |
| 350 |
oss << "%EOF"; |
| 351 |
|
| 352 |
PdfIndirectObjectList objects; |
| 353 |
PdfParserTest parser(objects, oss.str()); |
| 354 |
parser.ReadXRefContents(offsetXrefStm1, false); |
| 355 |
// succeeds in current code - should it? |
| 356 |
} |
| 357 |
catch (PdfError& error) |
| 358 |
{ |
| 359 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 360 |
} |
| 361 |
catch (exception&) |
| 362 |
{ |
| 363 |
FAIL("Unexpected exception type"); |
| 364 |
} |
| 365 |
|
| 366 |
try |
| 367 |
{ |
| 368 |
// generate an xref section and two XRef streams that reference each other |
| 369 |
// via the /Prev entry |
| 370 |
// xref |
| 371 |
// 0 1 |
| 372 |
// 000000000 65535 |
| 373 |
// 2 0 obj << /Type XRef /Prev offsetXrefStmObj3 >> stream ... endstream |
| 374 |
// 3 0 obj << /Type XRef /Prev offsetXrefStmObj2 >> stream ... endstream |
| 375 |
// trailer << /Root 1 0 R /Size 3 >> |
| 376 |
// startxref |
| 377 |
// offsetXrefStmObj2 |
| 378 |
// %%EOF |
| 379 |
ostringstream oss; |
| 380 |
|
| 381 |
// object stream contents - length excludes trailing whitespace |
| 382 |
string streamContents = |
| 383 |
"01 0E8A 0\r\n" |
| 384 |
"02 0002 00\r\n"; |
| 385 |
size_t streamContentsLength = streamContents.size() - strlen("\r\n"); |
| 386 |
|
| 387 |
// xref section at offset 0 |
| 388 |
//size_t offsetXref = 0; |
| 389 |
oss << "xref\r\n0 1\r\n"; |
| 390 |
oss << generateXRefEntries(1); |
| 391 |
|
| 392 |
// xrefstm at offsetXrefStm1 |
| 393 |
size_t offsetXrefStm1 = oss.str().length(); |
| 394 |
oss << "2 0 obj "; |
| 395 |
oss << "<< /Type /XRef "; |
| 396 |
oss << "/Length " << streamContentsLength << " "; |
| 397 |
oss << "/Index [2 2] "; |
| 398 |
oss << "/Size 3 "; |
| 399 |
oss << "/Prev 185 "; // xref stream 1 sets xref stream 2 as previous in chain |
| 400 |
oss << "/W [1 2 1] "; |
| 401 |
oss << "/Filter /ASCIIHexDecode "; |
| 402 |
oss << ">>\r\n"; |
| 403 |
oss << "stream\r\n"; |
| 404 |
oss << streamContents; |
| 405 |
oss << "endstream\r\n"; |
| 406 |
oss << "endobj\r\n"; |
| 407 |
|
| 408 |
// xrefstm at offsetXrefStm2 |
| 409 |
size_t offsetXrefStm2 = oss.str().length(); |
| 410 |
REQUIRE(offsetXrefStm2 == 185); // hard-coded in /Prev entry in XrefStm1 above |
| 411 |
oss << "3 0 obj "; |
| 412 |
oss << "<< /Type /XRef "; |
| 413 |
oss << "/Length " << streamContentsLength << " "; |
| 414 |
oss << "/Index [2 2] "; |
| 415 |
oss << "/Size 3 "; |
| 416 |
oss << "/Prev " << offsetXrefStm1 << " "; // xref stream 2 sets xref stream 1 as previous in chain |
| 417 |
oss << "/W [1 2 1] "; |
| 418 |
oss << "/Filter /ASCIIHexDecode "; |
| 419 |
oss << ">>\r\n"; |
| 420 |
oss << "stream\r\n"; |
| 421 |
oss << streamContents; |
| 422 |
oss << "endstream\r\n"; |
| 423 |
oss << "endobj\r\n"; |
| 424 |
|
| 425 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 426 |
oss << "startxref " << offsetXrefStm2 << "\r\n"; |
| 427 |
oss << "%EOF"; |
| 428 |
|
| 429 |
PdfIndirectObjectList objects; |
| 430 |
PdfParserTest parser(objects, oss.str()); |
| 431 |
parser.ReadXRefContents(offsetXrefStm2, false); |
| 432 |
// succeeds in current code - should it? |
| 433 |
} |
| 434 |
catch (PdfError& error) |
| 435 |
{ |
| 436 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 437 |
} |
| 438 |
catch (exception&) |
| 439 |
{ |
| 440 |
FAIL("Unexpected exception type"); |
| 441 |
} |
| 442 |
|
| 443 |
try |
| 444 |
{ |
| 445 |
// generate an xref section and lots of XRef streams without loops but reference |
| 446 |
// the previous stream via the /Prev entry |
| 447 |
// xref |
| 448 |
// 0 1 |
| 449 |
// 000000000 65535 |
| 450 |
// 2 0 obj << /Type XRef >> stream ... endstream |
| 451 |
// 3 0 obj << /Type XRef /Prev offsetStreamObj(2) >> stream ... endstream |
| 452 |
// 4 0 obj << /Type XRef /Prev offsetStreamObj(3) >> stream ... endstream |
| 453 |
// ... |
| 454 |
// N 0 obj << /Type XRef /Prev offsetStreamObj(N-1) >> stream ... endstream |
| 455 |
// trailer << /Root 1 0 R /Size 3 >> |
| 456 |
// startxref |
| 457 |
// offsetStreamObj(N) |
| 458 |
// %%EOF |
| 459 |
ostringstream oss; |
| 460 |
size_t prevOffset = 0; |
| 461 |
size_t currentOffset = 0; |
| 462 |
|
| 463 |
// object stream contents - length excludes trailing whitespace |
| 464 |
string streamContents = |
| 465 |
"01 0E8A 0\r\n" |
| 466 |
"02 0002 00\r\n"; |
| 467 |
size_t streamContentsLength = streamContents.size() - strlen("\r\n"); |
| 468 |
|
| 469 |
// xref section at offset 0 |
| 470 |
//size_t offsetXref = 0; |
| 471 |
oss << "xref\r\n0 1\r\n"; |
| 472 |
oss << generateXRefEntries(1); |
| 473 |
|
| 474 |
// this caused stack overflow on macOS 64-bit with around 3000 streams |
| 475 |
// and on Windows 32-bit with around 1000 streams |
| 476 |
|
| 477 |
constexpr size_t maxXrefStreams = 10000; |
| 478 |
for (size_t i = 0; i < maxXrefStreams; i++) |
| 479 |
{ |
| 480 |
size_t objNo = i + 2; |
| 481 |
|
| 482 |
// xrefstm at currentOffset linked back to stream at prevOffset |
| 483 |
prevOffset = currentOffset; |
| 484 |
currentOffset = oss.str().length(); |
| 485 |
oss << objNo << " 0 obj "; |
| 486 |
oss << "<< /Type /XRef "; |
| 487 |
oss << "/Length " << streamContentsLength << " "; |
| 488 |
oss << "/Index [2 2] "; |
| 489 |
oss << "/Size 3 "; |
| 490 |
if (prevOffset > 0) |
| 491 |
oss << "/Prev " << prevOffset << " "; |
| 492 |
oss << "/W [1 2 1] "; |
| 493 |
oss << "/Filter /ASCIIHexDecode "; |
| 494 |
oss << ">>\r\n"; |
| 495 |
oss << "stream\r\n"; |
| 496 |
oss << streamContents; |
| 497 |
oss << "endstream\r\n"; |
| 498 |
oss << "endobj\r\n"; |
| 499 |
} |
| 500 |
|
| 501 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 502 |
oss << "startxref " << currentOffset << "\r\n"; |
| 503 |
oss << "%EOF"; |
| 504 |
|
| 505 |
PdfIndirectObjectList objects; |
| 506 |
PdfParserTest parser(objects, oss.str()); |
| 507 |
parser.ReadXRefContents(currentOffset, false); |
| 508 |
// succeeds in current code - should it? |
| 509 |
} |
| 510 |
catch (PdfError& error) |
| 511 |
{ |
| 512 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 513 |
} |
| 514 |
catch (exception&) |
| 515 |
{ |
| 516 |
FAIL("Unexpected exception type"); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
void PdfParserTest::testReadXRefSubsection() |
| 521 |
{ |
| 522 |
int64_t firstObject = 0; |
| 523 |
int64_t objectCount = 0; |
| 524 |
|
| 525 |
// TODO does ReadXRefSubsection with objectCount = 0 make sense ??? |
| 526 |
|
| 527 |
// CVE-2017-5855 m_offsets.resize() NULL ptr read |
| 528 |
// CVE-2017-6844 m_offsets.resize() buffer overwrite |
| 529 |
// false positives due to AFL setting allocator_may_return_null=1 which causes |
| 530 |
// ASAN to return NULL instead of throwing bad_alloc for out-of-memory conditions |
| 531 |
// https://github.com/mirrorer/afl/blob/master/docs/env_variables.txt#L248 |
| 532 |
// https://github.com/google/sanitizers/issues/295#issuecomment-234273218 |
| 533 |
// the test for CVE-2018-5296 below checks that PoDoFo restricts allocations |
| 534 |
|
| 535 |
// CVE-2018-5296 m_offsets.resize() malloc failure when large size specified |
| 536 |
// check PoDoFo throws PdfError and not anything derived from exception |
| 537 |
// check PoDoFo can't allocate unrestricted amounts of memory |
| 538 |
|
| 539 |
if (PdfCommon::GetMaxObjectCount() <= maxNumberOfIndirectObjects) |
| 540 |
{ |
| 541 |
try |
| 542 |
{ |
| 543 |
string strInput = generateXRefEntries(PdfCommon::GetMaxObjectCount()); |
| 544 |
PdfIndirectObjectList objects; |
| 545 |
PdfParserTest parser(objects, strInput); |
| 546 |
firstObject = 0; |
| 547 |
objectCount = PdfCommon::GetMaxObjectCount(); |
| 548 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 549 |
// expected to succeed |
| 550 |
} |
| 551 |
catch (PdfError&) |
| 552 |
{ |
| 553 |
FAIL("should not throw PdfError"); |
| 554 |
} |
| 555 |
catch (exception&) |
| 556 |
{ |
| 557 |
FAIL("Unexpected exception type"); |
| 558 |
} |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
// test has been called from testMaxObjectCount with PdfCommon::SetMaxObjectCount() |
| 563 |
// set to a large value (large allocs are tested in address space tests below) |
| 564 |
} |
| 565 |
|
| 566 |
// don't run the following test if PdfCommon::GetMaxObjectCount()+1 will overflow |
| 567 |
// in the numXRefEntries calculation below (otherwise we get an ASAN error) |
| 568 |
if (PdfCommon::GetMaxObjectCount() < numeric_limits<unsigned>::max()) |
| 569 |
{ |
| 570 |
// don't generate xrefs for high values of GetMaxObjectCount() e.g. don't try to generate 2**63 xrefs |
| 571 |
unsigned numXRefEntries = std::min(maxNumberOfIndirectObjects + 1, PdfCommon::GetMaxObjectCount() + 1); |
| 572 |
|
| 573 |
try |
| 574 |
{ |
| 575 |
string strInput = generateXRefEntries(numXRefEntries); |
| 576 |
PdfIndirectObjectList objects; |
| 577 |
PdfParserTest parser(objects, strInput); |
| 578 |
firstObject = 0; |
| 579 |
objectCount = (int64_t)PdfCommon::GetMaxObjectCount() + 1; |
| 580 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 581 |
FAIL("PdfError not thrown"); |
| 582 |
} |
| 583 |
catch (PdfError& error) |
| 584 |
{ |
| 585 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 586 |
} |
| 587 |
catch (exception&) |
| 588 |
{ |
| 589 |
FAIL("Wrong exception type"); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
// CVE-2018-5296 try to allocate more than address space size |
| 594 |
// should throw a bad_length exception in STL which is rethrown as a PdfError |
| 595 |
try |
| 596 |
{ |
| 597 |
// this attempts to allocate numeric_limits<size_t>::max()/2 * sizeof(TXRefEntry) |
| 598 |
// on 32-bit systems this allocates 2**31 * sizeof(TXRefEntry) = 2**31 * 16 (larger than 32-bit address space) |
| 599 |
// on LP64 (macOS,*nix) systems this allocates 2**63 * sizeof(TXRefEntry) = 2**63 * 24 (larger than 64-bit address space) |
| 600 |
// on LLP64 (Win64) systems this allocates 2**31 * sizeof(TXRefEntry) = 2**31 * 16 (smaller than 64-bit address space) |
| 601 |
string strInput = " "; |
| 602 |
PdfIndirectObjectList objects; |
| 603 |
PdfParserTest parser(objects, strInput); |
| 604 |
firstObject = 1; |
| 605 |
objectCount = numeric_limits<size_t>::max() / 2 - 1; |
| 606 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 607 |
FAIL("PdfError not thrown"); |
| 608 |
} |
| 609 |
catch (PdfError& error) |
| 610 |
{ |
| 611 |
// if objectCount > PdfCommon::GetMaxObjectCount() then we'll see PdfErrorCode::InvalidXRef |
| 612 |
// otherwise we'll see PdfErrorCode::ValueOutOfRange or PdfErrorCode::OutOfMemory (see testMaxObjectCount) |
| 613 |
REQUIRE((error.GetCode() == PdfErrorCode::InvalidXRef |
| 614 |
|| error.GetCode() == PdfErrorCode::ValueOutOfRange |
| 615 |
|| error.GetCode() == PdfErrorCode::OutOfMemory)); |
| 616 |
} |
| 617 |
catch (exception&) |
| 618 |
{ |
| 619 |
FAIL("Wrong exception type"); |
| 620 |
} |
| 621 |
|
| 622 |
// CVE-2018-5296 try to allocate 95% of VM address space size (which should always fail) |
| 623 |
if (!canOutOfMemoryKillUnitTests()) |
| 624 |
{ |
| 625 |
constexpr size_t maxObjects = numeric_limits<size_t>::max() / sizeof(PdfXRefEntry) / 100 * 95; |
| 626 |
|
| 627 |
try |
| 628 |
{ |
| 629 |
string strInput = " "; |
| 630 |
PdfIndirectObjectList objects; |
| 631 |
PdfParserTest parser(objects, strInput); |
| 632 |
firstObject = 1; |
| 633 |
objectCount = maxObjects; |
| 634 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 635 |
FAIL("PdfError not thrown"); |
| 636 |
} |
| 637 |
catch (PdfError& error) |
| 638 |
{ |
| 639 |
if (maxObjects >= (size_t)PdfCommon::GetMaxObjectCount()) |
| 640 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 641 |
else |
| 642 |
REQUIRE(error.GetCode() == PdfErrorCode::OutOfMemory); |
| 643 |
} |
| 644 |
catch (exception&) |
| 645 |
{ |
| 646 |
FAIL("Wrong exception type"); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
// CVE-2015-8981 happens because this->GetNextNumber() can return negative numbers |
| 651 |
// in range (LONG_MIN to LONG_MAX) so the xref section below causes a buffer underflow |
| 652 |
// because m_offsets[-5].bParsed is set to true when first entry is read |
| 653 |
// NOTE: vector operator[] is not bounds checked |
| 654 |
|
| 655 |
// xref |
| 656 |
// -5 5 |
| 657 |
// 0000000000 65535 f |
| 658 |
// 0000000018 00000 n |
| 659 |
// 0000000077 00000 n |
| 660 |
// 0000000178 00000 n |
| 661 |
// 0000000457 00000 n |
| 662 |
// trailer |
| 663 |
// << /Root 1 0 R |
| 664 |
// /Size 5 |
| 665 |
//>> |
| 666 |
// startxref |
| 667 |
// 565 |
| 668 |
// %%EOF |
| 669 |
|
| 670 |
try |
| 671 |
{ |
| 672 |
string strInput = "0000000000 65535 f\r\n"; |
| 673 |
PdfIndirectObjectList objects; |
| 674 |
PdfParserTest parser(objects, strInput); |
| 675 |
firstObject = -5LL; |
| 676 |
objectCount = 5; |
| 677 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 678 |
FAIL("PdfError not thrown"); |
| 679 |
} |
| 680 |
catch (PdfError& error) |
| 681 |
{ |
| 682 |
REQUIRE((error.GetCode() == PdfErrorCode::InvalidXRef || error.GetCode() == PdfErrorCode::InvalidXRef)); |
| 683 |
} |
| 684 |
catch (exception&) |
| 685 |
{ |
| 686 |
FAIL("Wrong exception type"); |
| 687 |
} |
| 688 |
|
| 689 |
// CVE-2015-8981 can also happen due to integer overflow in firstObject+objectCount |
| 690 |
// in the example below 2147483647=0x7FFF, so 0x7FFF + 0x7FFF = 0XFFFE = -2 on a 32-bit system |
| 691 |
// which means m_offsets.size()=5 because m_offsets.resize() is never called and |
| 692 |
// m_offsets[2147483647].bParsed is set to true when first entry is read |
| 693 |
// NOTE: vector operator[] is not bounds checked |
| 694 |
|
| 695 |
// 2147483647 2147483647 |
| 696 |
// 0000000000 65535 f |
| 697 |
// 0000000018 00000 n |
| 698 |
// 0000000077 00000 n |
| 699 |
// 0000000178 00000 n |
| 700 |
// 0000000457 00000 n |
| 701 |
// trailer |
| 702 |
// << /Root 1 0 R |
| 703 |
// /Size 5 |
| 704 |
//>> |
| 705 |
// startxref |
| 706 |
// 565 |
| 707 |
// %%EOF |
| 708 |
|
| 709 |
try |
| 710 |
{ |
| 711 |
string strInput = "0000000000 65535 f\r\n"; |
| 712 |
PdfIndirectObjectList objects; |
| 713 |
PdfParserTest parser(objects, strInput); |
| 714 |
firstObject = numeric_limits<unsigned>::max(); |
| 715 |
objectCount = numeric_limits<unsigned>::max(); |
| 716 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 717 |
FAIL("PdfError not thrown"); |
| 718 |
} |
| 719 |
catch (PdfError& error) |
| 720 |
{ |
| 721 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 722 |
} |
| 723 |
catch (exception&) |
| 724 |
{ |
| 725 |
FAIL("Wrong exception type"); |
| 726 |
} |
| 727 |
|
| 728 |
try |
| 729 |
{ |
| 730 |
string strInput = "0000000000 65535 f\r\n"; |
| 731 |
PdfIndirectObjectList objects; |
| 732 |
PdfParserTest parser(objects, strInput); |
| 733 |
firstObject = numeric_limits<int64_t>::max(); |
| 734 |
objectCount = numeric_limits<int64_t>::max(); |
| 735 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 736 |
FAIL("PdfError not thrown"); |
| 737 |
} |
| 738 |
catch (PdfError& error) |
| 739 |
{ |
| 740 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 741 |
} |
| 742 |
catch (exception&) |
| 743 |
{ |
| 744 |
FAIL("Wrong exception type"); |
| 745 |
} |
| 746 |
|
| 747 |
// test for integer overflows in ReadXRefSubsection (CVE-2017-5853) which caused |
| 748 |
// wrong buffer size to be calculated and then triggered buffer overflow (CVE-2017-6844) |
| 749 |
// the overflow checks in ReadXRefSubsection depend on the value returned by GetMaxObjectCount |
| 750 |
// if the value changes these checks need looked at again |
| 751 |
REQUIRE(PdfCommon::GetMaxObjectCount() <= numeric_limits<unsigned>::max()); |
| 752 |
|
| 753 |
// test CVE-2017-5853 signed integer overflow in firstObject + objectCount |
| 754 |
// CVE-2017-5853 1.1 - firstObject < 0 |
| 755 |
try |
| 756 |
{ |
| 757 |
string strInput = " "; |
| 758 |
PdfIndirectObjectList objects; |
| 759 |
PdfParserTest parser(objects, strInput); |
| 760 |
firstObject = -1LL; |
| 761 |
objectCount = 1; |
| 762 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 763 |
FAIL("PdfError not thrown"); |
| 764 |
} |
| 765 |
catch (PdfError& error) |
| 766 |
{ |
| 767 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 768 |
} |
| 769 |
catch (exception&) |
| 770 |
{ |
| 771 |
FAIL("Wrong exception type"); |
| 772 |
} |
| 773 |
|
| 774 |
// CVE-2017-5853 1.2 - firstObject = min value of unsigned |
| 775 |
try |
| 776 |
{ |
| 777 |
string strInput = " "; |
| 778 |
PdfIndirectObjectList objects; |
| 779 |
PdfParserTest parser(objects, strInput); |
| 780 |
firstObject = numeric_limits<unsigned>::min(); |
| 781 |
objectCount = 1; |
| 782 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 783 |
FAIL("PdfError not thrown"); |
| 784 |
} |
| 785 |
catch (PdfError& error) |
| 786 |
{ |
| 787 |
REQUIRE(error.GetCode() == PdfErrorCode::UnexpectedEOF); |
| 788 |
} |
| 789 |
catch (exception&) |
| 790 |
{ |
| 791 |
FAIL("Wrong exception type"); |
| 792 |
} |
| 793 |
|
| 794 |
// CVE-2017-5853 1.3 - firstObject = min value of int64_t |
| 795 |
try |
| 796 |
{ |
| 797 |
string strInput = " "; |
| 798 |
PdfIndirectObjectList objects; |
| 799 |
PdfParserTest parser(objects, strInput); |
| 800 |
firstObject = numeric_limits<int64_t>::min(); |
| 801 |
objectCount = 1; |
| 802 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 803 |
FAIL("PdfError not thrown"); |
| 804 |
} |
| 805 |
catch (PdfError& error) |
| 806 |
{ |
| 807 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 808 |
} |
| 809 |
catch (exception&) |
| 810 |
{ |
| 811 |
FAIL("Wrong exception type"); |
| 812 |
} |
| 813 |
|
| 814 |
// CVE-2017-5853 1.4 - firstObject = min value of size_t is zero (size_t is unsigned) |
| 815 |
// and zero is a valid value for firstObject |
| 816 |
|
| 817 |
// CVE-2017-5853 1.5 - firstObject = max value of unsigned |
| 818 |
try |
| 819 |
{ |
| 820 |
string strInput = " "; |
| 821 |
PdfIndirectObjectList objects; |
| 822 |
PdfParserTest parser(objects, strInput); |
| 823 |
firstObject = numeric_limits<unsigned>::max(); |
| 824 |
objectCount = 1; |
| 825 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 826 |
FAIL("PdfError not thrown"); |
| 827 |
} |
| 828 |
catch (PdfError& error) |
| 829 |
{ |
| 830 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 831 |
} |
| 832 |
catch (exception&) |
| 833 |
{ |
| 834 |
FAIL("Wrong exception type"); |
| 835 |
} |
| 836 |
|
| 837 |
// CVE-2017-5853 1.6 - firstObject = max value of int64_t |
| 838 |
try |
| 839 |
{ |
| 840 |
string strInput = " "; |
| 841 |
PdfIndirectObjectList objects; |
| 842 |
PdfParserTest parser(objects, strInput); |
| 843 |
firstObject = numeric_limits<int64_t>::max(); |
| 844 |
objectCount = 1; |
| 845 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 846 |
FAIL("PdfError not thrown"); |
| 847 |
} |
| 848 |
catch (PdfError& error) |
| 849 |
{ |
| 850 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 851 |
} |
| 852 |
catch (exception&) |
| 853 |
{ |
| 854 |
FAIL("Wrong exception type"); |
| 855 |
} |
| 856 |
|
| 857 |
// CVE-2017-5853 1.7 - firstObject = max value of size_t |
| 858 |
try |
| 859 |
{ |
| 860 |
string strInput = " "; |
| 861 |
PdfIndirectObjectList objects; |
| 862 |
PdfParserTest parser(objects, strInput); |
| 863 |
firstObject = numeric_limits<size_t>::max(); |
| 864 |
objectCount = 1; |
| 865 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 866 |
FAIL("PdfError not thrown"); |
| 867 |
} |
| 868 |
catch (PdfError& error) |
| 869 |
{ |
| 870 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 871 |
} |
| 872 |
catch (exception&) |
| 873 |
{ |
| 874 |
FAIL("Wrong exception type"); |
| 875 |
} |
| 876 |
|
| 877 |
// CVE-2017-5853 1.8 - firstObject = PdfCommon::GetMaxObjectCount() |
| 878 |
try |
| 879 |
{ |
| 880 |
string strInput = " "; |
| 881 |
PdfIndirectObjectList objects; |
| 882 |
PdfParserTest parser(objects, strInput); |
| 883 |
REQUIRE(PdfCommon::GetMaxObjectCount() > 0); |
| 884 |
firstObject = PdfCommon::GetMaxObjectCount(); |
| 885 |
objectCount = 1; |
| 886 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 887 |
FAIL("PdfError not thrown"); |
| 888 |
} |
| 889 |
catch (PdfError& error) |
| 890 |
{ |
| 891 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 892 |
} |
| 893 |
catch (exception&) |
| 894 |
{ |
| 895 |
FAIL("Wrong exception type"); |
| 896 |
} |
| 897 |
|
| 898 |
// CVE-2017-5853 2.1 - objectCount < 0 |
| 899 |
try |
| 900 |
{ |
| 901 |
string strInput = " "; |
| 902 |
PdfIndirectObjectList objects; |
| 903 |
PdfParserTest parser(objects, strInput); |
| 904 |
firstObject = 1; |
| 905 |
objectCount = -1LL; |
| 906 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 907 |
FAIL("PdfError not thrown"); |
| 908 |
} |
| 909 |
catch (PdfError& error) |
| 910 |
{ |
| 911 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 912 |
} |
| 913 |
catch (exception&) |
| 914 |
{ |
| 915 |
FAIL("Wrong exception type"); |
| 916 |
} |
| 917 |
|
| 918 |
// CVE-2017-5853 2.2 - objectCount = min value of int |
| 919 |
try |
| 920 |
{ |
| 921 |
string strInput = " "; |
| 922 |
PdfIndirectObjectList objects; |
| 923 |
PdfParserTest parser(objects, strInput); |
| 924 |
firstObject = 1; |
| 925 |
objectCount = numeric_limits<int>::min(); |
| 926 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 927 |
FAIL("PdfError not thrown"); |
| 928 |
} |
| 929 |
catch (PdfError& error) |
| 930 |
{ |
| 931 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 932 |
} |
| 933 |
catch (exception&) |
| 934 |
{ |
| 935 |
FAIL("Wrong exception type"); |
| 936 |
} |
| 937 |
|
| 938 |
// CVE-2017-5853 2.3 - objectCount = min value of int64_t |
| 939 |
try |
| 940 |
{ |
| 941 |
string strInput = " "; |
| 942 |
PdfIndirectObjectList objects; |
| 943 |
PdfParserTest parser(objects, strInput); |
| 944 |
firstObject = 1; |
| 945 |
objectCount = numeric_limits<int64_t>::min(); |
| 946 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 947 |
FAIL("PdfError not thrown"); |
| 948 |
} |
| 949 |
catch (PdfError& error) |
| 950 |
{ |
| 951 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 952 |
} |
| 953 |
catch (exception&) |
| 954 |
{ |
| 955 |
FAIL("Wrong exception type"); |
| 956 |
} |
| 957 |
|
| 958 |
// CVE-2017-5853 2.4 - objectCount = min value of size_t is zero (size_t is unsigned) |
| 959 |
// and zero is a valid value for firstObject |
| 960 |
// TODO |
| 961 |
|
| 962 |
// CVE-2017-5853 2.5 - objectCount = max value of unsigned |
| 963 |
try |
| 964 |
{ |
| 965 |
string strInput = " "; |
| 966 |
PdfIndirectObjectList objects; |
| 967 |
PdfParserTest parser(objects, strInput); |
| 968 |
firstObject = 1; |
| 969 |
objectCount = numeric_limits<unsigned>::max(); |
| 970 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 971 |
FAIL("PdfError not thrown"); |
| 972 |
} |
| 973 |
catch (PdfError& error) |
| 974 |
{ |
| 975 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 976 |
} |
| 977 |
catch (exception&) |
| 978 |
{ |
| 979 |
FAIL("Wrong exception type"); |
| 980 |
} |
| 981 |
|
| 982 |
// CVE-2017-5853 2.6 - objectCount = max value of int64_t |
| 983 |
try |
| 984 |
{ |
| 985 |
string strInput = " "; |
| 986 |
PdfIndirectObjectList objects; |
| 987 |
PdfParserTest parser(objects, strInput); |
| 988 |
firstObject = 1; |
| 989 |
objectCount = numeric_limits<int64_t>::max(); |
| 990 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 991 |
FAIL("PdfError not thrown"); |
| 992 |
} |
| 993 |
catch (PdfError& error) |
| 994 |
{ |
| 995 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 996 |
} |
| 997 |
catch (exception&) |
| 998 |
{ |
| 999 |
FAIL("Wrong exception type"); |
| 1000 |
} |
| 1001 |
|
| 1002 |
// CVE-2017-5853 2.7 - objectCount = max value of size_t |
| 1003 |
try |
| 1004 |
{ |
| 1005 |
string strInput = " "; |
| 1006 |
PdfIndirectObjectList objects; |
| 1007 |
PdfParserTest parser(objects, strInput); |
| 1008 |
firstObject = 1; |
| 1009 |
objectCount = numeric_limits<size_t>::max(); |
| 1010 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 1011 |
FAIL("PdfError not thrown"); |
| 1012 |
} |
| 1013 |
catch (PdfError& error) |
| 1014 |
{ |
| 1015 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRef); |
| 1016 |
} |
| 1017 |
catch (exception&) |
| 1018 |
{ |
| 1019 |
FAIL("Wrong exception type"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
// CVE-2017-5853 2.8 - objectCount = PdfCommon::GetMaxObjectCount() |
| 1023 |
try |
| 1024 |
{ |
| 1025 |
string strInput = " "; |
| 1026 |
PdfIndirectObjectList objects; |
| 1027 |
PdfParserTest parser(objects, strInput); |
| 1028 |
firstObject = 1; |
| 1029 |
objectCount = PdfCommon::GetMaxObjectCount(); |
| 1030 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 1031 |
FAIL("PdfError not thrown"); |
| 1032 |
} |
| 1033 |
catch (PdfError& error) |
| 1034 |
{ |
| 1035 |
REQUIRE(error.GetCode() == PdfErrorCode::ValueOutOfRange); |
| 1036 |
} |
| 1037 |
catch (exception&) |
| 1038 |
{ |
| 1039 |
FAIL("Wrong exception type"); |
| 1040 |
} |
| 1041 |
|
| 1042 |
// CVE-2017-5853 2.9 - finally - loop through a set of interesting bit patterns |
| 1043 |
static uint64_t s_values[] = |
| 1044 |
{ |
| 1045 |
//(1ull << 64) - 1, |
| 1046 |
//(1ull << 64), |
| 1047 |
//(1ull << 64) + 1, |
| 1048 |
(1ull << 63) - 1, |
| 1049 |
(1ull << 63), |
| 1050 |
(1ull << 63) + 1, |
| 1051 |
(1ull << 62) - 1, |
| 1052 |
(1ull << 62), |
| 1053 |
(1ull << 62) + 1, |
| 1054 |
|
| 1055 |
(1ull << 49) - 1, |
| 1056 |
(1ull << 49), |
| 1057 |
(1ull << 49) + 1, |
| 1058 |
(1ull << 48) - 1, |
| 1059 |
(1ull << 48), |
| 1060 |
(1ull << 48) + 1, |
| 1061 |
(1ull << 47) - 1, |
| 1062 |
(1ull << 47), |
| 1063 |
(1ull << 47) + 1, |
| 1064 |
|
| 1065 |
(1ull << 33) - 1, |
| 1066 |
(1ull << 33), |
| 1067 |
(1ull << 33) + 1, |
| 1068 |
(1ull << 32) - 1, |
| 1069 |
(1ull << 32), |
| 1070 |
(1ull << 32) + 1, |
| 1071 |
(1ull << 31) - 1, |
| 1072 |
(1ull << 31), |
| 1073 |
(1ull << 31) + 1, |
| 1074 |
|
| 1075 |
(1ull << 25) - 1, |
| 1076 |
(1ull << 33), |
| 1077 |
(1ull << 33) + 1, |
| 1078 |
(1ull << 24) - 1, |
| 1079 |
(1ull << 24), |
| 1080 |
(1ull << 24) + 1, |
| 1081 |
(1ull << 31) - 1, |
| 1082 |
(1ull << 31), |
| 1083 |
(1ull << 31) + 1, |
| 1084 |
|
| 1085 |
(1ull << 17) - 1, |
| 1086 |
(1ull << 17), |
| 1087 |
(1ull << 17) + 1, |
| 1088 |
(1ull << 16) - 1, |
| 1089 |
(1ull << 16), |
| 1090 |
(1ull << 16) + 1, |
| 1091 |
(1ull << 15) - 1, |
| 1092 |
(1ull << 15), |
| 1093 |
(1ull << 15) + 1, |
| 1094 |
|
| 1095 |
(uint64_t)-1, |
| 1096 |
0, |
| 1097 |
1 |
| 1098 |
}; |
| 1099 |
constexpr size_t numValues = sizeof(s_values) / sizeof(s_values[0]); |
| 1100 |
|
| 1101 |
for (size_t i = 0; i < numValues; i++) |
| 1102 |
{ |
| 1103 |
for (size_t j = 0; j < numValues; j++) |
| 1104 |
{ |
| 1105 |
try |
| 1106 |
{ |
| 1107 |
string strInput = " "; |
| 1108 |
PdfIndirectObjectList objects; |
| 1109 |
PdfParserTest parser(objects, strInput); |
| 1110 |
firstObject = s_values[i]; |
| 1111 |
objectCount = s_values[j]; |
| 1112 |
|
| 1113 |
if (canOutOfMemoryKillUnitTests() && (firstObject > maxNumberOfIndirectObjects || objectCount > maxNumberOfIndirectObjects)) |
| 1114 |
{ |
| 1115 |
// can't call this in test environments where an out-of-memory condition terminates |
| 1116 |
// unit test process before all tests have run (e.g. AddressSanitizer) |
| 1117 |
} |
| 1118 |
else |
| 1119 |
{ |
| 1120 |
parser.ReadXRefSubsection(firstObject, objectCount); |
| 1121 |
// some combinations of firstObject/objectCount from s_values are legal - so we expect to reach here sometimes |
| 1122 |
} |
| 1123 |
} |
| 1124 |
catch (PdfError& error) |
| 1125 |
{ |
| 1126 |
// other combinations of firstObject/objectCount from s_values are illegal |
| 1127 |
// if we reach here it should be an invalid xref value of some type |
| 1128 |
REQUIRE((error.GetCode() == PdfErrorCode::InvalidXRef || error.GetCode() == PdfErrorCode::ValueOutOfRange |
| 1129 |
|| error.GetCode() == PdfErrorCode::UnexpectedEOF |
| 1130 |
|| error.GetCode() == PdfErrorCode::OutOfMemory)); |
| 1131 |
} |
| 1132 |
catch (exception&) |
| 1133 |
{ |
| 1134 |
// and should never reach here |
| 1135 |
FAIL("Wrong exception type"); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
void PdfParserTest::TestReadXRefStreamContents() |
| 1142 |
{ |
| 1143 |
// test valid stream |
| 1144 |
try |
| 1145 |
{ |
| 1146 |
// generate an XRef stream with valid /W values |
| 1147 |
ostringstream oss; |
| 1148 |
size_t offsetStream; |
| 1149 |
size_t offsetEndstream; |
| 1150 |
|
| 1151 |
// XRef stream with 5 entries |
| 1152 |
size_t lengthXRefObject = 58; |
| 1153 |
size_t offsetXRefObject = oss.str().length(); |
| 1154 |
oss << "2 0 obj "; |
| 1155 |
oss << "<< /Type /XRef "; |
| 1156 |
oss << "/Length " << lengthXRefObject << " "; |
| 1157 |
oss << "/Index [2 2] "; |
| 1158 |
oss << "/Size 5 "; |
| 1159 |
oss << "/W [1 2 1] "; |
| 1160 |
oss << "/Filter /ASCIIHexDecode "; |
| 1161 |
oss << ">>\r\n"; |
| 1162 |
oss << "stream\r\n"; |
| 1163 |
offsetStream = oss.str().length(); |
| 1164 |
oss << "01 0E8A 00\r\n"; |
| 1165 |
oss << "02 0002 00\r\n"; |
| 1166 |
oss << "02 0002 01\r\n"; |
| 1167 |
oss << "02 0002 02\r\n"; |
| 1168 |
oss << "02 0002 03\r\n"; |
| 1169 |
offsetEndstream = oss.str().length(); |
| 1170 |
oss << "endstream\r\n"; |
| 1171 |
oss << "endobj\r\n"; |
| 1172 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1173 |
|
| 1174 |
// trailer |
| 1175 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1176 |
oss << "%%EOF"; |
| 1177 |
|
| 1178 |
PdfIndirectObjectList objects; |
| 1179 |
PdfParserTest parser(objects, oss.str()); |
| 1180 |
parser.ReadXRefStreamContents(offsetXRefObject, false); |
| 1181 |
// should succeed |
| 1182 |
} |
| 1183 |
catch (PdfError&) |
| 1184 |
{ |
| 1185 |
FAIL("Unexpected PdfError"); |
| 1186 |
} |
| 1187 |
catch (exception&) |
| 1188 |
{ |
| 1189 |
FAIL("Unexpected exception type"); |
| 1190 |
} |
| 1191 |
|
| 1192 |
// CVE-2018-5295: integer overflow caused by checking sum of /W entry values /W [ 1 2 9223372036854775807 ] |
| 1193 |
// see https://bugzilla.redhat.com/show_bug.cgi?id=1531897 (/W values used were extracted from PoC file) |
| 1194 |
try |
| 1195 |
{ |
| 1196 |
ostringstream oss; |
| 1197 |
size_t offsetStream; |
| 1198 |
size_t offsetEndstream; |
| 1199 |
|
| 1200 |
// XRef stream |
| 1201 |
size_t lengthXRefObject = 58; |
| 1202 |
size_t offsetXRefObject = 10; |
| 1203 |
oss << "%PDF-1.4\r\n"; |
| 1204 |
oss << "2 0 obj "; |
| 1205 |
oss << "<< /Type /XRef "; |
| 1206 |
oss << "/Length " << lengthXRefObject << " "; |
| 1207 |
oss << "/Index [2 2] "; |
| 1208 |
oss << "/Size 5 "; |
| 1209 |
oss << "/W [ 1 2 9223372036854775807 ] "; |
| 1210 |
oss << "/Filter /ASCIIHexDecode "; |
| 1211 |
oss << ">>\r\n"; |
| 1212 |
oss << "stream\r\n"; |
| 1213 |
offsetStream = oss.str().length(); |
| 1214 |
oss << "01 0E8A 00\r\n"; |
| 1215 |
oss << "02 0002 00\r\n"; |
| 1216 |
oss << "02 0002 01\r\n"; |
| 1217 |
oss << "02 0002 02\r\n"; |
| 1218 |
oss << "02 0002 03\r\n"; |
| 1219 |
offsetEndstream = oss.str().length(); |
| 1220 |
oss << "endstream\r\n"; |
| 1221 |
oss << "endobj\r\n"; |
| 1222 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // check /Length entry in XRef stream above |
| 1223 |
|
| 1224 |
// trailer |
| 1225 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1226 |
oss << "%%EOF"; |
| 1227 |
|
| 1228 |
auto inputStr = oss.str(); |
| 1229 |
PdfXRefEntries offsets; |
| 1230 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1231 |
PdfMemDocument doc; |
| 1232 |
// Parse a doc using XRef stream with invalid /W entries |
| 1233 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1234 |
FAIL("Should throw exception"); |
| 1235 |
} |
| 1236 |
catch (PdfError& error) |
| 1237 |
{ |
| 1238 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1239 |
} |
| 1240 |
catch (exception&) |
| 1241 |
{ |
| 1242 |
FAIL("Unexpected exception type"); |
| 1243 |
} |
| 1244 |
|
| 1245 |
// CVE-2017-8787: heap based overflow caused by unchecked /W entry values /W [ 1 -4 2 ] |
| 1246 |
// see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=861738 for value of /W array |
| 1247 |
try |
| 1248 |
{ |
| 1249 |
ostringstream oss; |
| 1250 |
size_t offsetStream; |
| 1251 |
size_t offsetEndstream; |
| 1252 |
|
| 1253 |
// XRef stream |
| 1254 |
size_t lengthXRefObject = 58; |
| 1255 |
size_t offsetXRefObject = 10; |
| 1256 |
oss << "%PDF-1.4\r\n"; |
| 1257 |
oss << "2 0 obj "; |
| 1258 |
oss << "<< /Type /XRef "; |
| 1259 |
oss << "/Length " << lengthXRefObject << " "; |
| 1260 |
oss << "/Index [2 2] "; |
| 1261 |
oss << "/Size 5 "; |
| 1262 |
oss << "/W [ 1 -4 2 ] "; |
| 1263 |
oss << "/Filter /ASCIIHexDecode "; |
| 1264 |
oss << ">>\r\n"; |
| 1265 |
oss << "stream\r\n"; |
| 1266 |
offsetStream = oss.str().length(); |
| 1267 |
oss << "01 0E8A 00\r\n"; |
| 1268 |
oss << "02 0002 00\r\n"; |
| 1269 |
oss << "02 0002 01\r\n"; |
| 1270 |
oss << "02 0002 02\r\n"; |
| 1271 |
oss << "02 0002 03\r\n"; |
| 1272 |
offsetEndstream = oss.str().length(); |
| 1273 |
oss << "endstream\r\n"; |
| 1274 |
oss << "endobj\r\n"; |
| 1275 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // check /Length entry in XRef stream above |
| 1276 |
|
| 1277 |
// trailer |
| 1278 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1279 |
oss << "%%EOF"; |
| 1280 |
|
| 1281 |
auto inputStr = oss.str(); |
| 1282 |
PdfXRefEntries offsets; |
| 1283 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1284 |
PdfMemDocument doc; |
| 1285 |
// Parse a doc using XRef stream with invalid /W entries |
| 1286 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1287 |
FAIL("Should throw exception"); |
| 1288 |
} |
| 1289 |
catch (PdfError& error) |
| 1290 |
{ |
| 1291 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1292 |
} |
| 1293 |
catch (exception&) |
| 1294 |
{ |
| 1295 |
FAIL("Unexpected exception type"); |
| 1296 |
} |
| 1297 |
|
| 1298 |
// /W entry values /W [ 4095 1 1 ] for data in form 02 0002 00 (doesn't match size of entry) |
| 1299 |
try |
| 1300 |
{ |
| 1301 |
ostringstream oss; |
| 1302 |
size_t offsetStream; |
| 1303 |
size_t offsetEndstream; |
| 1304 |
|
| 1305 |
// XRef stream |
| 1306 |
size_t lengthXRefObject = 58; |
| 1307 |
size_t offsetXRefObject = 10; |
| 1308 |
oss << "%PDF-1.4\r\n"; |
| 1309 |
oss << "2 0 obj "; |
| 1310 |
oss << "<< /Type /XRef "; |
| 1311 |
oss << "/Length " << lengthXRefObject << " "; |
| 1312 |
oss << "/Index [2 2] "; |
| 1313 |
oss << "/Size 5 "; |
| 1314 |
oss << "/W [ 4095 1 1 ] "; |
| 1315 |
oss << "/Filter /ASCIIHexDecode "; |
| 1316 |
oss << ">>\r\n"; |
| 1317 |
oss << "stream\r\n"; |
| 1318 |
offsetStream = oss.str().length(); |
| 1319 |
oss << "01 0E8A 00\r\n"; |
| 1320 |
oss << "02 0002 00\r\n"; |
| 1321 |
oss << "02 0002 01\r\n"; |
| 1322 |
oss << "02 0002 02\r\n"; |
| 1323 |
oss << "02 0002 03\r\n"; |
| 1324 |
offsetEndstream = oss.str().length(); |
| 1325 |
oss << "endstream\r\n"; |
| 1326 |
oss << "endobj\r\n"; |
| 1327 |
REQUIRE((offsetEndstream - offsetStream - strlen("\r\n")) == lengthXRefObject); // check /Length entry in XRef stream above |
| 1328 |
|
| 1329 |
// trailer |
| 1330 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1331 |
oss << "%%EOF"; |
| 1332 |
|
| 1333 |
auto inputStr = oss.str(); |
| 1334 |
PdfXRefEntries offsets; |
| 1335 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1336 |
PdfMemDocument doc; |
| 1337 |
// Parse a doc using XRef stream with invalid /W entries |
| 1338 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1339 |
FAIL("Should throw exception"); |
| 1340 |
} |
| 1341 |
catch (PdfError& error) |
| 1342 |
{ |
| 1343 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1344 |
} |
| 1345 |
catch (exception&) |
| 1346 |
{ |
| 1347 |
FAIL("Unexpected exception type"); |
| 1348 |
} |
| 1349 |
|
| 1350 |
// /W entry values /W [ 4 4 4 ] for data in form 02 0002 00 (doesn't match size of entry) |
| 1351 |
try |
| 1352 |
{ |
| 1353 |
ostringstream oss; |
| 1354 |
size_t offsetStream; |
| 1355 |
size_t offsetEndstream; |
| 1356 |
|
| 1357 |
// XRef stream |
| 1358 |
size_t lengthXRefObject = 58; |
| 1359 |
size_t offsetXRefObject = 10; |
| 1360 |
oss << "%PDF-1.4\r\n"; |
| 1361 |
oss << "2 0 obj "; |
| 1362 |
oss << "<< /Type /XRef "; |
| 1363 |
oss << "/Length " << lengthXRefObject << " "; |
| 1364 |
oss << "/Index [2 2] "; |
| 1365 |
oss << "/Size 5 "; |
| 1366 |
oss << "/W [ 4 4 4 ] "; |
| 1367 |
oss << "/Filter /ASCIIHexDecode "; |
| 1368 |
oss << ">>\r\n"; |
| 1369 |
oss << "stream\r\n"; |
| 1370 |
offsetStream = oss.str().length(); |
| 1371 |
oss << "01 0E8A 00\r\n"; |
| 1372 |
oss << "02 0002 00\r\n"; |
| 1373 |
oss << "02 0002 01\r\n"; |
| 1374 |
oss << "02 0002 02\r\n"; |
| 1375 |
oss << "02 0002 03\r\n"; |
| 1376 |
offsetEndstream = oss.str().length(); |
| 1377 |
oss << "endstream\r\n"; |
| 1378 |
oss << "endobj\r\n"; |
| 1379 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // check /Length entry in XRef stream above |
| 1380 |
|
| 1381 |
// trailer |
| 1382 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1383 |
oss << "%%EOF"; |
| 1384 |
|
| 1385 |
auto inputStr = oss.str(); |
| 1386 |
PdfXRefEntries offsets; |
| 1387 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1388 |
PdfMemDocument doc; |
| 1389 |
// Parse a doc using XRef stream with invalid /W entries |
| 1390 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1391 |
FAIL("Should throw exception"); |
| 1392 |
} |
| 1393 |
catch (PdfError& error) |
| 1394 |
{ |
| 1395 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1396 |
} |
| 1397 |
catch (exception&) |
| 1398 |
{ |
| 1399 |
FAIL("Unexpected exception type"); |
| 1400 |
} |
| 1401 |
|
| 1402 |
// /W entry values /W [ 1 4 4 ] (size=9) for data 01 0E8A 0\r\n02 0002 00\r\n (size=8 bytes) |
| 1403 |
try |
| 1404 |
{ |
| 1405 |
ostringstream oss; |
| 1406 |
size_t offsetStream; |
| 1407 |
size_t offsetEndstream; |
| 1408 |
|
| 1409 |
// XRef stream |
| 1410 |
size_t lengthXRefObject = 22; |
| 1411 |
size_t offsetXRefObject = 10; |
| 1412 |
oss << "%PDF-1.4\r\n"; |
| 1413 |
oss << "2 0 obj "; |
| 1414 |
oss << "<< /Type /XRef "; |
| 1415 |
oss << "/Length " << lengthXRefObject << " "; |
| 1416 |
oss << "/Index [2 2] "; |
| 1417 |
oss << "/Size 2 "; |
| 1418 |
oss << "/W [ 1 4 4 ] "; |
| 1419 |
oss << "/Filter /ASCIIHexDecode "; |
| 1420 |
oss << ">>\r\n"; |
| 1421 |
oss << "stream\r\n"; |
| 1422 |
offsetStream = oss.str().length(); |
| 1423 |
oss << "01 0E8A 00\r\n"; |
| 1424 |
oss << "02 0002 00\r\n"; |
| 1425 |
offsetEndstream = oss.str().length(); |
| 1426 |
oss << "endstream\r\n"; |
| 1427 |
oss << "endobj\r\n"; |
| 1428 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // check /Length entry in XRef stream above |
| 1429 |
|
| 1430 |
// trailer |
| 1431 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1432 |
oss << "%%EOF"; |
| 1433 |
|
| 1434 |
auto inputStr = oss.str(); |
| 1435 |
PdfXRefEntries offsets; |
| 1436 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1437 |
PdfMemDocument doc; |
| 1438 |
// Parse a doc using XRef stream with invalid /W entries |
| 1439 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1440 |
FAIL("Should throw exception"); |
| 1441 |
} |
| 1442 |
catch (PdfError& error) |
| 1443 |
{ |
| 1444 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1445 |
} |
| 1446 |
catch (exception&) |
| 1447 |
{ |
| 1448 |
FAIL("Unexpected exception type"); |
| 1449 |
} |
| 1450 |
|
| 1451 |
try |
| 1452 |
{ |
| 1453 |
ostringstream oss; |
| 1454 |
size_t offsetStream; |
| 1455 |
size_t offsetEndstream; |
| 1456 |
|
| 1457 |
size_t lengthXRefObject = 22; |
| 1458 |
oss << "%PDF-1.4\r\n"; |
| 1459 |
oss << "1 0 obj\r\n"; |
| 1460 |
// The catalog needs a /Pages key to pass PdfParser catalog validation |
| 1461 |
oss << "<< /Pages 1 0 R >>\r\n"; |
| 1462 |
oss << "endobj\r\n"; |
| 1463 |
size_t offsetXRefObject = oss.str().length(); |
| 1464 |
oss << "2 0 obj\r\n"; |
| 1465 |
oss << "<< /Type /XRef "; |
| 1466 |
oss << "/Length " << lengthXRefObject << " "; |
| 1467 |
oss << "/Index [1 2] "; |
| 1468 |
oss << "/Root 1 0 R "; |
| 1469 |
oss << "/Size 3 "; |
| 1470 |
oss << "/W [1 2 1] "; |
| 1471 |
oss << "/Filter /ASCIIHexDecode "; |
| 1472 |
oss << ">>\r\n"; |
| 1473 |
oss << "stream\r\n"; |
| 1474 |
offsetStream = oss.str().length(); |
| 1475 |
oss << "01 000A 00\r\n"; |
| 1476 |
oss << "01 001A 00\r\n"; |
| 1477 |
offsetEndstream = oss.str().length(); |
| 1478 |
oss << "endstream\r\n"; |
| 1479 |
oss << "endobj\r\n"; |
| 1480 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1481 |
|
| 1482 |
// trailer |
| 1483 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1484 |
oss << "%%EOF"; |
| 1485 |
|
| 1486 |
auto inputStr = oss.str(); |
| 1487 |
PdfXRefEntries offsets; |
| 1488 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1489 |
PdfMemDocument doc; |
| 1490 |
doc.Load(device); |
| 1491 |
} |
| 1492 |
catch (PdfError&) |
| 1493 |
{ |
| 1494 |
FAIL("Unexpected PdfError"); |
| 1495 |
} |
| 1496 |
catch (exception&) |
| 1497 |
{ |
| 1498 |
FAIL("Unexpected exception type"); |
| 1499 |
} |
| 1500 |
|
| 1501 |
// XRef stream with 5 entries but /Size 2 specified |
| 1502 |
try |
| 1503 |
{ |
| 1504 |
ostringstream oss; |
| 1505 |
size_t offsetStream; |
| 1506 |
size_t offsetEndstream; |
| 1507 |
|
| 1508 |
size_t lengthXRefObject = 58; |
| 1509 |
size_t offsetXRefObject = 10; |
| 1510 |
oss << "%PDF-1.4\r\n"; |
| 1511 |
oss << "2 0 obj "; |
| 1512 |
oss << "<< /Type /XRef "; |
| 1513 |
oss << "/Length " << lengthXRefObject << " "; |
| 1514 |
oss << "/Index [2 2] "; |
| 1515 |
oss << "/Size 10 "; |
| 1516 |
oss << "/W [1 2 1] "; |
| 1517 |
oss << "/Filter /ASCIIHexDecode "; |
| 1518 |
oss << ">>\r\n"; |
| 1519 |
oss << "stream\r\n"; |
| 1520 |
offsetStream = oss.str().length(); |
| 1521 |
oss << "01 0E8A 00\r\n"; |
| 1522 |
oss << "01 0002 00\r\n"; |
| 1523 |
oss << "01 0002 01\r\n"; |
| 1524 |
oss << "01 0002 02\r\n"; |
| 1525 |
oss << "01 0002 03\r\n"; |
| 1526 |
offsetEndstream = oss.str().length(); |
| 1527 |
oss << "endstream\r\n"; |
| 1528 |
oss << "endobj\r\n"; |
| 1529 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1530 |
|
| 1531 |
// trailer |
| 1532 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1533 |
oss << "%%EOF"; |
| 1534 |
|
| 1535 |
auto inputStr = oss.str(); |
| 1536 |
PdfXRefEntries offsets; |
| 1537 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1538 |
PdfMemDocument doc; |
| 1539 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1540 |
FAIL("Should throw exception"); |
| 1541 |
} |
| 1542 |
catch (PdfError& error) |
| 1543 |
{ |
| 1544 |
(void)error; |
| 1545 |
// FIXME We should throw on exact error |
| 1546 |
//REQUIRE(error.GetError() == PdfErrorCode::NoXRef); |
| 1547 |
} |
| 1548 |
catch (exception&) |
| 1549 |
{ |
| 1550 |
FAIL("Unexpected exception type"); |
| 1551 |
} |
| 1552 |
|
| 1553 |
// XRef stream with 5 entries but /Size 10 specified |
| 1554 |
try |
| 1555 |
{ |
| 1556 |
ostringstream oss; |
| 1557 |
size_t offsetStream; |
| 1558 |
size_t offsetEndstream; |
| 1559 |
|
| 1560 |
size_t lengthXRefObject = 58; |
| 1561 |
size_t offsetXRefObject = 10; |
| 1562 |
oss << "%PDF-1.4\r\n"; |
| 1563 |
oss << "2 0 obj "; |
| 1564 |
oss << "<< /Type /XRef "; |
| 1565 |
oss << "/Length " << lengthXRefObject << " "; |
| 1566 |
oss << "/Index [2 2] "; |
| 1567 |
oss << "/Size 10 "; |
| 1568 |
oss << "/W [1 2 1] "; |
| 1569 |
oss << "/Filter /ASCIIHexDecode "; |
| 1570 |
oss << ">>\r\n"; |
| 1571 |
oss << "stream\r\n"; |
| 1572 |
offsetStream = oss.str().length(); |
| 1573 |
oss << "01 0E8A 00\r\n"; |
| 1574 |
oss << "01 0002 00\r\n"; |
| 1575 |
oss << "01 0002 01\r\n"; |
| 1576 |
oss << "01 0002 02\r\n"; |
| 1577 |
oss << "01 0002 03\r\n"; |
| 1578 |
offsetEndstream = oss.str().length(); |
| 1579 |
oss << "endstream\r\n"; |
| 1580 |
oss << "endobj\r\n"; |
| 1581 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1582 |
|
| 1583 |
// trailer |
| 1584 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1585 |
oss << "%%EOF"; |
| 1586 |
|
| 1587 |
auto inputStr = oss.str(); |
| 1588 |
PdfXRefEntries offsets; |
| 1589 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1590 |
PdfMemDocument doc; |
| 1591 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1592 |
FAIL("Should throw exception"); |
| 1593 |
} |
| 1594 |
catch (PdfError& error) |
| 1595 |
{ |
| 1596 |
(void)error; |
| 1597 |
// FIXME We should throw on exact error |
| 1598 |
//REQUIRE(error.GetError() == PdfErrorCode::NoXRef); |
| 1599 |
} |
| 1600 |
catch (exception&) |
| 1601 |
{ |
| 1602 |
FAIL("Unexpected exception type"); |
| 1603 |
} |
| 1604 |
|
| 1605 |
// XRef stream with /Index [0 0] array |
| 1606 |
try |
| 1607 |
{ |
| 1608 |
ostringstream oss; |
| 1609 |
size_t offsetStream; |
| 1610 |
size_t offsetEndstream; |
| 1611 |
|
| 1612 |
size_t lengthXRefObject = 58; |
| 1613 |
size_t offsetXRefObject = 10; |
| 1614 |
oss << "%PDF-1.4\r\n"; |
| 1615 |
oss << "2 0 obj "; |
| 1616 |
oss << "<< /Type /XRef "; |
| 1617 |
oss << "/Length " << lengthXRefObject << " "; |
| 1618 |
oss << "/Index [0 0] "; |
| 1619 |
oss << "/Size 5 "; |
| 1620 |
oss << "/W [1 2 1] "; |
| 1621 |
oss << "/Filter /ASCIIHexDecode "; |
| 1622 |
oss << ">>\r\n"; |
| 1623 |
oss << "stream\r\n"; |
| 1624 |
offsetStream = oss.str().length(); |
| 1625 |
oss << "01 0E8A 00\r\n"; |
| 1626 |
oss << "02 0002 00\r\n"; |
| 1627 |
oss << "02 0002 01\r\n"; |
| 1628 |
oss << "02 0002 02\r\n"; |
| 1629 |
oss << "02 0002 03\r\n"; |
| 1630 |
offsetEndstream = oss.str().length(); |
| 1631 |
oss << "endstream\r\n"; |
| 1632 |
oss << "endobj\r\n"; |
| 1633 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1634 |
|
| 1635 |
// trailer |
| 1636 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1637 |
oss << "%%EOF"; |
| 1638 |
|
| 1639 |
auto inputStr = oss.str(); |
| 1640 |
PdfXRefEntries offsets; |
| 1641 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1642 |
PdfMemDocument doc; |
| 1643 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1644 |
FAIL("Should throw exception"); |
| 1645 |
} |
| 1646 |
catch (PdfError& error) |
| 1647 |
{ |
| 1648 |
(void)error; |
| 1649 |
// FIXME We should throw on exact error |
| 1650 |
//REQUIRE(error.GetError() == PdfErrorCode::NoXRef); |
| 1651 |
} |
| 1652 |
catch (exception&) |
| 1653 |
{ |
| 1654 |
FAIL("Unexpected exception type"); |
| 1655 |
} |
| 1656 |
|
| 1657 |
// XRef stream with /Index [-1 -1] array |
| 1658 |
try |
| 1659 |
{ |
| 1660 |
ostringstream oss; |
| 1661 |
size_t offsetStream; |
| 1662 |
size_t offsetEndstream; |
| 1663 |
|
| 1664 |
size_t lengthXRefObject = 58; |
| 1665 |
size_t offsetXRefObject = 10; |
| 1666 |
oss << "%PDF-1.4\r\n"; |
| 1667 |
oss << "2 0 obj "; |
| 1668 |
oss << "<< /Type /XRef "; |
| 1669 |
oss << "/Length " << lengthXRefObject << " "; |
| 1670 |
oss << "/Index [-1 -1] "; |
| 1671 |
oss << "/Size 5 "; |
| 1672 |
oss << "/W [1 2 1] "; |
| 1673 |
oss << "/Filter /ASCIIHexDecode "; |
| 1674 |
oss << ">>\r\n"; |
| 1675 |
oss << "stream\r\n"; |
| 1676 |
offsetStream = oss.str().length(); |
| 1677 |
oss << "01 0E8A 00\r\n"; |
| 1678 |
oss << "02 0002 00\r\n"; |
| 1679 |
oss << "02 0002 01\r\n"; |
| 1680 |
oss << "02 0002 02\r\n"; |
| 1681 |
oss << "02 0002 03\r\n"; |
| 1682 |
offsetEndstream = oss.str().length(); |
| 1683 |
oss << "endstream\r\n"; |
| 1684 |
oss << "endobj\r\n"; |
| 1685 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1686 |
|
| 1687 |
// trailer |
| 1688 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1689 |
oss << "%%EOF"; |
| 1690 |
|
| 1691 |
auto inputStr = oss.str(); |
| 1692 |
PdfXRefEntries offsets; |
| 1693 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1694 |
PdfMemDocument doc; |
| 1695 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1696 |
FAIL("Should throw exception"); |
| 1697 |
} |
| 1698 |
catch (PdfError& error) |
| 1699 |
{ |
| 1700 |
(void)error; |
| 1701 |
// FIXME We should throw on exact error |
| 1702 |
//REQUIRE(error.GetError() == PdfErrorCode::NoXRef); |
| 1703 |
} |
| 1704 |
catch (exception&) |
| 1705 |
{ |
| 1706 |
FAIL("Unexpected exception type"); |
| 1707 |
} |
| 1708 |
|
| 1709 |
// XRef stream with /Index array with no entries |
| 1710 |
try |
| 1711 |
{ |
| 1712 |
ostringstream oss; |
| 1713 |
size_t offsetStream; |
| 1714 |
size_t offsetEndstream; |
| 1715 |
|
| 1716 |
size_t lengthXRefObject = 58; |
| 1717 |
size_t offsetXRefObject = 10; |
| 1718 |
oss << "%PDF-1.4\r\n"; |
| 1719 |
oss << "2 0 obj "; |
| 1720 |
oss << "<< /Type /XRef "; |
| 1721 |
oss << "/Length " << lengthXRefObject << " "; |
| 1722 |
oss << "/Index [ ] "; |
| 1723 |
oss << "/Size 5 "; |
| 1724 |
oss << "/W [1 2 1] "; |
| 1725 |
oss << "/Filter /ASCIIHexDecode "; |
| 1726 |
oss << ">>\r\n"; |
| 1727 |
oss << "stream\r\n"; |
| 1728 |
offsetStream = oss.str().length(); |
| 1729 |
oss << "01 0E8A 00\r\n"; |
| 1730 |
oss << "02 0002 00\r\n"; |
| 1731 |
oss << "02 0002 01\r\n"; |
| 1732 |
oss << "02 0002 02\r\n"; |
| 1733 |
oss << "02 0002 03\r\n"; |
| 1734 |
offsetEndstream = oss.str().length(); |
| 1735 |
oss << "endstream\r\n"; |
| 1736 |
oss << "endobj\r\n"; |
| 1737 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1738 |
|
| 1739 |
// trailer |
| 1740 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1741 |
oss << "%%EOF"; |
| 1742 |
|
| 1743 |
auto inputStr = oss.str(); |
| 1744 |
PdfXRefEntries offsets; |
| 1745 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1746 |
PdfMemDocument doc; |
| 1747 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1748 |
FAIL("Should throw exception"); |
| 1749 |
} |
| 1750 |
catch (PdfError& error) |
| 1751 |
{ |
| 1752 |
(void)error; |
| 1753 |
// FIXME We should throw on exact error |
| 1754 |
//REQUIRE(error.GetError() == PdfErrorCode::NoXRef); |
| 1755 |
} |
| 1756 |
catch (exception&) |
| 1757 |
{ |
| 1758 |
FAIL("Unexpected exception type"); |
| 1759 |
} |
| 1760 |
|
| 1761 |
// XRef stream with /Index array with 3 entries |
| 1762 |
try |
| 1763 |
{ |
| 1764 |
ostringstream oss; |
| 1765 |
size_t offsetStream; |
| 1766 |
size_t offsetEndstream; |
| 1767 |
|
| 1768 |
size_t lengthXRefObject = 58; |
| 1769 |
size_t offsetXRefObject = 10; |
| 1770 |
oss << "%PDF-1.4\r\n"; |
| 1771 |
oss << "2 0 obj "; |
| 1772 |
oss << "<< /Type /XRef "; |
| 1773 |
oss << "/Length " << lengthXRefObject << " "; |
| 1774 |
oss << "/Index [2 2 2] "; |
| 1775 |
oss << "/Size 5 "; |
| 1776 |
oss << "/W [1 2 1] "; |
| 1777 |
oss << "/Filter /ASCIIHexDecode "; |
| 1778 |
oss << ">>\r\n"; |
| 1779 |
oss << "stream\r\n"; |
| 1780 |
offsetStream = oss.str().length(); |
| 1781 |
oss << "01 0E8A 00\r\n"; |
| 1782 |
oss << "02 0002 00\r\n"; |
| 1783 |
oss << "02 0002 01\r\n"; |
| 1784 |
oss << "02 0002 02\r\n"; |
| 1785 |
oss << "02 0002 03\r\n"; |
| 1786 |
offsetEndstream = oss.str().length(); |
| 1787 |
oss << "endstream\r\n"; |
| 1788 |
oss << "endobj\r\n"; |
| 1789 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1790 |
|
| 1791 |
// trailer |
| 1792 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1793 |
oss << "%%EOF"; |
| 1794 |
|
| 1795 |
auto inputStr = oss.str(); |
| 1796 |
PdfXRefEntries offsets; |
| 1797 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1798 |
PdfMemDocument doc; |
| 1799 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1800 |
FAIL("Should throw exception"); |
| 1801 |
} |
| 1802 |
catch (PdfError& error) |
| 1803 |
{ |
| 1804 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1805 |
} |
| 1806 |
catch (exception&) |
| 1807 |
{ |
| 1808 |
FAIL("Unexpected exception type"); |
| 1809 |
} |
| 1810 |
|
| 1811 |
// XRef stream with /Index array with 22 entries |
| 1812 |
try |
| 1813 |
{ |
| 1814 |
ostringstream oss; |
| 1815 |
size_t offsetStream; |
| 1816 |
size_t offsetEndstream; |
| 1817 |
|
| 1818 |
size_t lengthXRefObject = 58; |
| 1819 |
size_t offsetXRefObject = 10; |
| 1820 |
oss << "%PDF-1.4\r\n"; |
| 1821 |
oss << "2 0 obj "; |
| 1822 |
oss << "<< /Type /XRef "; |
| 1823 |
oss << "/Length " << lengthXRefObject << " "; |
| 1824 |
oss << "/Index [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22] "; |
| 1825 |
oss << "/Size 5 "; |
| 1826 |
oss << "/W [1 2 1] "; |
| 1827 |
oss << "/Filter /ASCIIHexDecode "; |
| 1828 |
oss << ">>\r\n"; |
| 1829 |
oss << "stream\r\n"; |
| 1830 |
offsetStream = oss.str().length(); |
| 1831 |
oss << "00 0000 00\r\n"; |
| 1832 |
oss << "00 0000 00\r\n"; |
| 1833 |
oss << "00 0000 00\r\n"; |
| 1834 |
oss << "00 0000 00\r\n"; |
| 1835 |
oss << "00 0000 00\r\n"; |
| 1836 |
offsetEndstream = oss.str().length(); |
| 1837 |
oss << "endstream\r\n"; |
| 1838 |
oss << "endobj\r\n"; |
| 1839 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 1840 |
|
| 1841 |
// trailer |
| 1842 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 1843 |
oss << "%%EOF"; |
| 1844 |
|
| 1845 |
auto inputStr = oss.str(); |
| 1846 |
PdfXRefEntries offsets; |
| 1847 |
auto device = std::make_shared<SpanStreamDevice>(inputStr); |
| 1848 |
PdfMemDocument doc; |
| 1849 |
doc.Load(device, PdfLoadOptions::SkipXRefRecovery); |
| 1850 |
FAIL("Should throw exception"); |
| 1851 |
} |
| 1852 |
catch (PdfError& error) |
| 1853 |
{ |
| 1854 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidXRefStream); |
| 1855 |
} |
| 1856 |
catch (exception&) |
| 1857 |
{ |
| 1858 |
FAIL("Unexpected exception type"); |
| 1859 |
} |
| 1860 |
} |
| 1861 |
|
| 1862 |
void PdfParserTest::TestReadObjects() |
| 1863 |
{ |
| 1864 |
// CVE-2017-8378 - m_offsets out-of-bounds access when referenced encryption dictionary object doesn't exist |
| 1865 |
try |
| 1866 |
{ |
| 1867 |
ostringstream oss; |
| 1868 |
oss << "%PDF-1.0\r\n"; |
| 1869 |
oss << "xref\r\n0 3\r\n"; |
| 1870 |
oss << generateXRefEntries(3); |
| 1871 |
oss << "trailer << /Root 1 0 R /Size 3 /Encrypt 3 0 R >>\r\n"; |
| 1872 |
oss << "startxref 0\r\n"; |
| 1873 |
oss << "%%EOF"; |
| 1874 |
PdfIndirectObjectList objects; |
| 1875 |
auto docbuff = oss.str(); |
| 1876 |
PdfParserTest parser(objects, docbuff); |
| 1877 |
parser.ReadDocumentStructure(); |
| 1878 |
parser.ReadObjects(); |
| 1879 |
FAIL("Should throw exception"); |
| 1880 |
} |
| 1881 |
catch (PdfError& error) |
| 1882 |
{ |
| 1883 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidEncryptionDict); |
| 1884 |
} |
| 1885 |
catch (exception&) |
| 1886 |
{ |
| 1887 |
FAIL("Unexpected exception type"); |
| 1888 |
} |
| 1889 |
} |
| 1890 |
|
| 1891 |
void PdfParserTest::TestIsPdfFile() |
| 1892 |
{ |
| 1893 |
bool expectedFail; |
| 1894 |
|
| 1895 |
try |
| 1896 |
{ |
| 1897 |
string strInput = "%PDF-1.0"; |
| 1898 |
PdfIndirectObjectList objects; |
| 1899 |
PdfParserTest parser(objects, strInput); |
| 1900 |
parser.ReadHeader(); |
| 1901 |
} |
| 1902 |
catch (PdfError&) |
| 1903 |
{ |
| 1904 |
FAIL("Unexpected PdfError"); |
| 1905 |
} |
| 1906 |
catch (...) |
| 1907 |
{ |
| 1908 |
FAIL("Wrong exception type"); |
| 1909 |
} |
| 1910 |
|
| 1911 |
try |
| 1912 |
{ |
| 1913 |
string strInput = "%PDF-1.1"; |
| 1914 |
PdfIndirectObjectList objects; |
| 1915 |
PdfParserTest parser(objects, strInput); |
| 1916 |
parser.ReadHeader(); |
| 1917 |
} |
| 1918 |
catch (PdfError&) |
| 1919 |
{ |
| 1920 |
FAIL("Unexpected PdfError"); |
| 1921 |
} |
| 1922 |
catch (...) |
| 1923 |
{ |
| 1924 |
FAIL("Wrong exception type"); |
| 1925 |
} |
| 1926 |
|
| 1927 |
try |
| 1928 |
{ |
| 1929 |
string strInput = "%PDF-1.7"; |
| 1930 |
PdfIndirectObjectList objects; |
| 1931 |
PdfParserTest parser(objects, strInput); |
| 1932 |
parser.ReadHeader(); |
| 1933 |
} |
| 1934 |
catch (PdfError&) |
| 1935 |
{ |
| 1936 |
FAIL("Unexpected PdfError"); |
| 1937 |
} |
| 1938 |
catch (...) |
| 1939 |
{ |
| 1940 |
FAIL("Wrong exception type"); |
| 1941 |
} |
| 1942 |
|
| 1943 |
try |
| 1944 |
{ |
| 1945 |
string strInput = "%PDF-2.0"; |
| 1946 |
PdfIndirectObjectList objects; |
| 1947 |
PdfParserTest parser(objects, strInput); |
| 1948 |
parser.ReadHeader(); |
| 1949 |
} |
| 1950 |
catch (PdfError&) |
| 1951 |
{ |
| 1952 |
FAIL("Unexpected PdfError"); |
| 1953 |
} |
| 1954 |
catch (exception&) |
| 1955 |
{ |
| 1956 |
FAIL("Wrong exception type"); |
| 1957 |
} |
| 1958 |
|
| 1959 |
expectedFail = true; |
| 1960 |
try |
| 1961 |
{ |
| 1962 |
string strInput = "%PDF-1.9"; |
| 1963 |
PdfIndirectObjectList objects; |
| 1964 |
PdfParserTest parser(objects, strInput); |
| 1965 |
parser.ReadHeader(); |
| 1966 |
expectedFail = false; |
| 1967 |
} |
| 1968 |
catch (PdfError&) |
| 1969 |
{ |
| 1970 |
// OK |
| 1971 |
} |
| 1972 |
catch (exception&) |
| 1973 |
{ |
| 1974 |
FAIL("Wrong exception type"); |
| 1975 |
} |
| 1976 |
REQUIRE(expectedFail); |
| 1977 |
|
| 1978 |
expectedFail = true; |
| 1979 |
try |
| 1980 |
{ |
| 1981 |
string strInput = "%!PS-Adobe-2.0"; |
| 1982 |
PdfIndirectObjectList objects; |
| 1983 |
PdfParserTest parser(objects, strInput); |
| 1984 |
parser.ReadHeader(); |
| 1985 |
expectedFail = false; |
| 1986 |
} |
| 1987 |
catch (PdfError&) |
| 1988 |
{ |
| 1989 |
// OK |
| 1990 |
} |
| 1991 |
catch (exception&) |
| 1992 |
{ |
| 1993 |
FAIL("Wrong exception type"); |
| 1994 |
} |
| 1995 |
REQUIRE(expectedFail); |
| 1996 |
|
| 1997 |
expectedFail = true; |
| 1998 |
try |
| 1999 |
{ |
| 2000 |
string strInput = "GIF89a"; |
| 2001 |
PdfIndirectObjectList objects; |
| 2002 |
PdfParserTest parser(objects, strInput); |
| 2003 |
parser.ReadHeader(); |
| 2004 |
expectedFail = false; |
| 2005 |
} |
| 2006 |
catch (PdfError&) |
| 2007 |
{ |
| 2008 |
// OK |
| 2009 |
} |
| 2010 |
catch (exception&) |
| 2011 |
{ |
| 2012 |
FAIL("Wrong exception type"); |
| 2013 |
} |
| 2014 |
REQUIRE(expectedFail); |
| 2015 |
} |
| 2016 |
|
| 2017 |
// CVE-2018-8002, CVE-2021-30470 |
| 2018 |
void PdfParserTest::TestNestedArrays() |
| 2019 |
{ |
| 2020 |
// test valid stream |
| 2021 |
// generate an XRef stream with no deeply nested arrays |
| 2022 |
ostringstream oss; |
| 2023 |
size_t offsetStream; |
| 2024 |
size_t offsetEndstream; |
| 2025 |
size_t offsetXRefObject; |
| 2026 |
string buffer; |
| 2027 |
|
| 2028 |
// XRef stream with 5 entries |
| 2029 |
constexpr size_t lengthXRefObject = 58; |
| 2030 |
|
| 2031 |
offsetXRefObject = oss.str().length(); |
| 2032 |
oss << "2 0 obj "; |
| 2033 |
oss << "<< /Type /XRef "; |
| 2034 |
oss << "/Length " << lengthXRefObject << " "; |
| 2035 |
oss << "/Index [2 2] "; |
| 2036 |
oss << "/Size 5 "; |
| 2037 |
oss << "/W [1 2 1] "; |
| 2038 |
oss << "/Filter /ASCIIHexDecode "; |
| 2039 |
oss << ">>\r\n"; |
| 2040 |
oss << "stream\r\n"; |
| 2041 |
offsetStream = oss.str().length(); |
| 2042 |
oss << "01 0E8A 00\r\n"; |
| 2043 |
oss << "02 0002 00\r\n"; |
| 2044 |
oss << "02 0002 01\r\n"; |
| 2045 |
oss << "02 0002 02\r\n"; |
| 2046 |
oss << "02 0002 03\r\n"; |
| 2047 |
offsetEndstream = oss.str().length(); |
| 2048 |
oss << "endstream\r\n"; |
| 2049 |
oss << "endobj\r\n"; |
| 2050 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 2051 |
|
| 2052 |
// trailer |
| 2053 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 2054 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 2055 |
oss << "%EOF"; |
| 2056 |
|
| 2057 |
buffer = oss.str(); |
| 2058 |
|
| 2059 |
{ |
| 2060 |
PdfIndirectObjectList objects; |
| 2061 |
PdfParserTest parser(objects, buffer); |
| 2062 |
parser.ReadXRefStreamContents(offsetXRefObject, false); |
| 2063 |
REQUIRE(true); |
| 2064 |
} |
| 2065 |
|
| 2066 |
// CVE-2021-30470 - lots of [[[[[]]]]] brackets represent nested arrays which caused stack overflow |
| 2067 |
try |
| 2068 |
{ |
| 2069 |
// generate an XRef stream with deeply nested arrays |
| 2070 |
oss.str(""); |
| 2071 |
const size_t maxNesting = getStackOverflowDepth(); // big enough to cause stack overflow |
| 2072 |
// XRef stream with 5 entries |
| 2073 |
offsetXRefObject = oss.str().length(); |
| 2074 |
oss << "2 0 obj "; |
| 2075 |
oss << "<< /Type /XRef "; |
| 2076 |
oss << "/Length " << lengthXRefObject << " "; |
| 2077 |
oss << "/Index [2 2] "; |
| 2078 |
oss << "/Size 5 "; |
| 2079 |
oss << "/W [1 2 1] "; |
| 2080 |
|
| 2081 |
// output [[[[[[[[[[[0]]]]]]]]]]] |
| 2082 |
for (size_t i = 0; i < maxNesting; i++) |
| 2083 |
{ |
| 2084 |
oss << "["; |
| 2085 |
} |
| 2086 |
oss << "0"; |
| 2087 |
for (size_t i = 0; i < maxNesting; i++) |
| 2088 |
{ |
| 2089 |
oss << "]"; |
| 2090 |
} |
| 2091 |
oss << " "; |
| 2092 |
|
| 2093 |
oss << "/Filter /ASCIIHexDecode "; |
| 2094 |
oss << ">>\r\n"; |
| 2095 |
oss << "stream\r\n"; |
| 2096 |
offsetStream = oss.str().length(); |
| 2097 |
oss << "01 0E8A 00\r\n"; |
| 2098 |
oss << "02 0002 00\r\n"; |
| 2099 |
oss << "02 0002 01\r\n"; |
| 2100 |
oss << "02 0002 02\r\n"; |
| 2101 |
oss << "02 0002 03\r\n"; |
| 2102 |
offsetEndstream = oss.str().length(); |
| 2103 |
oss << "endstream\r\n"; |
| 2104 |
oss << "endobj\r\n"; |
| 2105 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 2106 |
|
| 2107 |
// trailer |
| 2108 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 2109 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 2110 |
oss << "%EOF"; |
| 2111 |
|
| 2112 |
buffer = oss.str(); |
| 2113 |
|
| 2114 |
PdfIndirectObjectList objects; |
| 2115 |
PdfParserTest parser(objects, buffer); |
| 2116 |
parser.ReadXRefStreamContents(offsetXRefObject, false); |
| 2117 |
FAIL("Should throw exception"); |
| 2118 |
} |
| 2119 |
catch (PdfError& error) |
| 2120 |
{ |
| 2121 |
// this must match the error value thrown by PdfRecursionGuard |
| 2122 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2123 |
} |
| 2124 |
} |
| 2125 |
|
| 2126 |
// CVE-2018-8002, CVE-2021-30470 |
| 2127 |
void PdfParserTest::TestNestedDictionaries() |
| 2128 |
{ |
| 2129 |
// test valid stream |
| 2130 |
// generate an XRef stream with no deeply nested dictionaries |
| 2131 |
ostringstream oss; |
| 2132 |
size_t offsetStream; |
| 2133 |
size_t offsetEndstream; |
| 2134 |
size_t offsetXRefObject; |
| 2135 |
string buffer; |
| 2136 |
|
| 2137 |
// XRef stream with 5 entries |
| 2138 |
constexpr size_t lengthXRefObject = 58; |
| 2139 |
|
| 2140 |
offsetXRefObject = oss.str().length(); |
| 2141 |
oss << "2 0 obj "; |
| 2142 |
oss << "<< /Type /XRef "; |
| 2143 |
oss << "/Length " << lengthXRefObject << " "; |
| 2144 |
oss << "/Index [2 2] "; |
| 2145 |
oss << "/Size 5 "; |
| 2146 |
oss << "/W [1 2 1] "; |
| 2147 |
oss << "/Filter /ASCIIHexDecode "; |
| 2148 |
oss << ">>\r\n"; |
| 2149 |
oss << "stream\r\n"; |
| 2150 |
offsetStream = oss.str().length(); |
| 2151 |
oss << "01 0E8A 00\r\n"; |
| 2152 |
oss << "02 0002 00\r\n"; |
| 2153 |
oss << "02 0002 01\r\n"; |
| 2154 |
oss << "02 0002 02\r\n"; |
| 2155 |
oss << "02 0002 03\r\n"; |
| 2156 |
offsetEndstream = oss.str().length(); |
| 2157 |
oss << "endstream\r\n"; |
| 2158 |
oss << "endobj\r\n"; |
| 2159 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 2160 |
|
| 2161 |
// trailer |
| 2162 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 2163 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 2164 |
oss << "%EOF"; |
| 2165 |
|
| 2166 |
buffer = oss.str(); |
| 2167 |
|
| 2168 |
{ |
| 2169 |
PdfIndirectObjectList objects; |
| 2170 |
PdfParserTest parser(objects, buffer); |
| 2171 |
parser.ReadXRefStreamContents(offsetXRefObject, false); |
| 2172 |
REQUIRE(true); |
| 2173 |
} |
| 2174 |
|
| 2175 |
// CVE-2021-30470 - lots of <<<>>> brackets represent nested dictionaries which caused stack overflow |
| 2176 |
try |
| 2177 |
{ |
| 2178 |
// generate an XRef stream with deeply nested dictionaries |
| 2179 |
oss.str(""); |
| 2180 |
|
| 2181 |
const size_t maxNesting = getStackOverflowDepth(); // big enough to cause stack overflow |
| 2182 |
// XRef stream with 5 entries |
| 2183 |
offsetXRefObject = oss.str().length(); |
| 2184 |
oss << "2 0 obj "; |
| 2185 |
oss << "<< /Type /XRef "; |
| 2186 |
oss << "/Length " << lengthXRefObject << " "; |
| 2187 |
oss << "/Index [2 2] "; |
| 2188 |
oss << "/Size 5 "; |
| 2189 |
oss << "/W [1 2 1] "; |
| 2190 |
|
| 2191 |
// output << << << /Test 0 >> >> >> |
| 2192 |
for (size_t i = 0; i < maxNesting; i++) |
| 2193 |
{ |
| 2194 |
oss << "<< "; |
| 2195 |
} |
| 2196 |
oss << " /Test 0"; |
| 2197 |
for (size_t i = 0; i < maxNesting; i++) |
| 2198 |
{ |
| 2199 |
oss << " >>"; |
| 2200 |
} |
| 2201 |
oss << " "; |
| 2202 |
|
| 2203 |
oss << "/Filter /ASCIIHexDecode "; |
| 2204 |
oss << ">>\r\n"; |
| 2205 |
oss << "stream\r\n"; |
| 2206 |
offsetStream = oss.str().length(); |
| 2207 |
oss << "01 0E8A 00\r\n"; |
| 2208 |
oss << "02 0002 00\r\n"; |
| 2209 |
oss << "02 0002 01\r\n"; |
| 2210 |
oss << "02 0002 02\r\n"; |
| 2211 |
oss << "02 0002 03\r\n"; |
| 2212 |
offsetEndstream = oss.str().length(); |
| 2213 |
oss << "endstream\r\n"; |
| 2214 |
oss << "endobj\r\n"; |
| 2215 |
REQUIRE(offsetEndstream - offsetStream - strlen("\r\n") == lengthXRefObject); // hard-coded in /Length entry in XRef stream above |
| 2216 |
|
| 2217 |
// trailer |
| 2218 |
oss << "trailer << /Root 1 0 R /Size 3 >>\r\n"; |
| 2219 |
oss << "startxref " << offsetXRefObject << "\r\n"; |
| 2220 |
oss << "%EOF"; |
| 2221 |
|
| 2222 |
buffer = oss.str(); |
| 2223 |
|
| 2224 |
PdfIndirectObjectList objects; |
| 2225 |
PdfParserTest parser(objects, buffer); |
| 2226 |
parser.ReadXRefStreamContents(offsetXRefObject, false); |
| 2227 |
FAIL("Should throw exception"); |
| 2228 |
} |
| 2229 |
catch (PdfError& error) |
| 2230 |
{ |
| 2231 |
// this must match the error value thrown by PdfRecursionGuard |
| 2232 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2233 |
} |
| 2234 |
} |
| 2235 |
|
| 2236 |
void PdfParserTest::TestInvalidXRefEntries() |
| 2237 |
{ |
| 2238 |
auto currentLogSeverity = PdfCommon::GetMaxLoggingSeverity(); |
| 2239 |
try |
| 2240 |
{ |
| 2241 |
// Test invalid entries |
| 2242 |
PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 2243 |
string strInput = |
| 2244 |
"0000000000 65535 n\r\n" |
| 2245 |
"0000000001 65536 n\r\n" |
| 2246 |
"0000000003 00000 f\r\n" |
| 2247 |
"0000000000 65536 f\r\n"; |
| 2248 |
PdfIndirectObjectList objects; |
| 2249 |
PdfParserTest parser(objects, strInput); |
| 2250 |
parser.ReadXRefSubsection(0, 4); |
| 2251 |
parser.ReadObjectsInternal(); |
| 2252 |
PdfCommon::SetMaxLoggingSeverity(currentLogSeverity); |
| 2253 |
REQUIRE(objects.GetSize() == 0); |
| 2254 |
REQUIRE(objects.GetFreeObjects().size() == 0); |
| 2255 |
} |
| 2256 |
catch (...) |
| 2257 |
{ |
| 2258 |
PdfCommon::SetMaxLoggingSeverity(currentLogSeverity); |
| 2259 |
FAIL("should not throw"); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
// CVE-2021-30471 |
| 2264 |
TEST_CASE("TestNestedNameTree") |
| 2265 |
{ |
| 2266 |
// test for valid but deeply nested name tree |
| 2267 |
// maxDepth must be less than GetMaxObjectCount otherwise PdfParser::ResizeOffsets |
| 2268 |
// throws an error when reading the xref offsets table, and no outlines are read |
| 2269 |
ostringstream oss; |
| 2270 |
const size_t maxDepth = getStackOverflowDepth() - 6 - 1; |
| 2271 |
const size_t numObjects = maxDepth + 6; |
| 2272 |
vector<size_t> offsets(numObjects); |
| 2273 |
size_t xrefOffset = 0; |
| 2274 |
|
| 2275 |
offsets[0] = 0; |
| 2276 |
oss << "%PDF-1.0\r\n"; |
| 2277 |
|
| 2278 |
offsets[1] = (size_t)oss.tellp(); |
| 2279 |
oss << "1 0 obj<</Type/Catalog /Pages 2 0 R /Names 4 0 R>>endobj "; |
| 2280 |
|
| 2281 |
offsets[2] = (size_t)oss.tellp(); |
| 2282 |
oss << "2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj "; |
| 2283 |
|
| 2284 |
offsets[3] = (size_t)oss.tellp(); |
| 2285 |
oss << "3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj "; |
| 2286 |
|
| 2287 |
// the name dictionary |
| 2288 |
offsets[4] = (size_t)oss.tellp(); |
| 2289 |
oss << "4 0 obj<</Dests 5 0 R>>endobj "; |
| 2290 |
|
| 2291 |
// root of /Dests name tree |
| 2292 |
offsets[5] = (size_t)oss.tellp(); |
| 2293 |
oss << "5 0 obj<</Kids [6 0 R]>>endobj "; |
| 2294 |
|
| 2295 |
// create name tree nested to maxDepth where each intermediate node has one child |
| 2296 |
// except single leaf node at maxDepth |
| 2297 |
for (size_t objNo = 6; objNo < numObjects; objNo++) |
| 2298 |
{ |
| 2299 |
offsets[objNo] = (size_t)oss.tellp(); |
| 2300 |
|
| 2301 |
if (objNo < numObjects - 1) |
| 2302 |
oss << objNo << " 0 obj<</Kids [" << objNo + 1 << " 0 R] /Limits [(A) (Z)]>>endobj "; |
| 2303 |
else |
| 2304 |
oss << objNo << " 0 obj<</Limits [(A) (Z)] /Names [ (A) (Avalue) (Z) (Zvalue) ] >>endobj "; |
| 2305 |
} |
| 2306 |
|
| 2307 |
// output xref table |
| 2308 |
oss << "\r\n"; |
| 2309 |
xrefOffset = (size_t)oss.tellp(); |
| 2310 |
oss << "xref\r\n"; |
| 2311 |
oss << "0 " << numObjects << "\r\n"; |
| 2312 |
|
| 2313 |
oss << "0000000000 65535 f\r\n"; |
| 2314 |
|
| 2315 |
for (size_t objNo = 1; objNo < offsets.size(); objNo++) |
| 2316 |
{ |
| 2317 |
// write xref entries like |
| 2318 |
// "0000000010 00000 n\r\n" |
| 2319 |
char refEntry[21]; |
| 2320 |
snprintf(refEntry, 21, "%010zu 00000 n\r\n", offsets[objNo]); |
| 2321 |
|
| 2322 |
oss << refEntry; |
| 2323 |
} |
| 2324 |
|
| 2325 |
oss << "trailer<</Size " << numObjects << "/Root 1 0 R>>\r\n"; |
| 2326 |
oss << "startxref\r\n"; |
| 2327 |
oss << xrefOffset << "\r\n"; |
| 2328 |
oss << "%%EOF"; |
| 2329 |
|
| 2330 |
auto buffer = oss.str(); |
| 2331 |
|
| 2332 |
try |
| 2333 |
{ |
| 2334 |
PdfMemDocument doc; |
| 2335 |
doc.LoadFromBuffer(buffer, PdfLoadOptions::StrictParsing); |
| 2336 |
|
| 2337 |
auto names = doc.GetNames(); |
| 2338 |
if (names != nullptr) |
| 2339 |
{ |
| 2340 |
PdfStringMap<PdfObject> dict; |
| 2341 |
names->ToDictionary(PdfKnownNameTree::Dests, dict); |
| 2342 |
} |
| 2343 |
|
| 2344 |
FAIL("Should throw exception"); |
| 2345 |
} |
| 2346 |
catch (PdfError& error) |
| 2347 |
{ |
| 2348 |
// this must match the error value thrown by PdfRecursionGuard |
| 2349 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2350 |
} |
| 2351 |
} |
| 2352 |
|
| 2353 |
// CVE-2021-30471 |
| 2354 |
TEST_CASE("TestLoopingNameTree") |
| 2355 |
{ |
| 2356 |
string strNoLoop = |
| 2357 |
"%PDF-1.0\r\n" |
| 2358 |
"1 0 obj<</Type/Catalog/Pages 2 0 R /Names 4 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj 4 0 obj<</Dests 2 0 R>>endobj\r\n" |
| 2359 |
"xref\r\n" |
| 2360 |
"0 5\r\n" |
| 2361 |
"0000000000 65535 f\r\n" |
| 2362 |
"0000000010 00000 n\r\n" |
| 2363 |
"0000000066 00000 n\r\n" |
| 2364 |
"0000000115 00000 n\r\n" |
| 2365 |
"0000000161 00000 n\r\n" |
| 2366 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2367 |
"startxref\r\n" |
| 2368 |
"192\r\n" |
| 2369 |
"%%EOF"; |
| 2370 |
|
| 2371 |
{ |
| 2372 |
PdfMemDocument doc; |
| 2373 |
doc.LoadFromBuffer(strNoLoop); |
| 2374 |
|
| 2375 |
auto names = doc.GetNames(); |
| 2376 |
if (names != nullptr) |
| 2377 |
{ |
| 2378 |
PdfStringMap<PdfObject> dict; |
| 2379 |
names->ToDictionary(PdfKnownNameTree::Dests, dict); |
| 2380 |
} |
| 2381 |
} |
| 2382 |
|
| 2383 |
// CVE-2021-30471 /Dests points at pages tree root which has a /Kids entry loooping back to pages tree root |
| 2384 |
string strSelfLoop = |
| 2385 |
"%PDF-1.0\r\n" |
| 2386 |
"1 0 obj<</Type/Catalog/Pages 2 0 R /Names 4 0 R>>endobj 2 0 obj<</Type/Pages/Kids[2 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj 4 0 obj<</Dests 2 0 R>>endobj\r\n" |
| 2387 |
"xref\r\n" |
| 2388 |
"0 5\r\n" |
| 2389 |
"0000000000 65535 f\r\n" |
| 2390 |
"0000000010 00000 n\r\n" |
| 2391 |
"0000000066 00000 n\r\n" |
| 2392 |
"0000000115 00000 n\r\n" |
| 2393 |
"0000000161 00000 n\r\n" |
| 2394 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2395 |
"startxref\r\n" |
| 2396 |
"192\r\n" |
| 2397 |
"%%EOF"; |
| 2398 |
|
| 2399 |
try |
| 2400 |
{ |
| 2401 |
PdfMemDocument doc; |
| 2402 |
doc.LoadFromBuffer(strSelfLoop); |
| 2403 |
|
| 2404 |
auto names = doc.GetNames(); |
| 2405 |
if (names != nullptr) |
| 2406 |
{ |
| 2407 |
PdfStringMap<PdfObject> dict; |
| 2408 |
names->ToDictionary(PdfKnownNameTree::Dests, dict); |
| 2409 |
} |
| 2410 |
|
| 2411 |
FAIL("Should throw exception"); |
| 2412 |
} |
| 2413 |
catch (PdfError& error) |
| 2414 |
{ |
| 2415 |
// this must match the error value thrown by PdfRecursionGuard |
| 2416 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2417 |
} |
| 2418 |
|
| 2419 |
// CVE-2021-30471 /Dests points at pages tree which has a /Kids entry loooping back to ancestor (document root) |
| 2420 |
string strAncestorLoop = |
| 2421 |
"%PDF-1.0\r\n" |
| 2422 |
"1 0 obj<</Type/Catalog/Pages 2 0 R /Names 4 0 R>>endobj 2 0 obj<</Type/Pages/Kids[1 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj 4 0 obj<</Dests 2 0 R>>endobj\r\n" |
| 2423 |
"xref\r\n" |
| 2424 |
"0 5\r\n" |
| 2425 |
"0000000000 65535 f\r\n" |
| 2426 |
"0000000010 00000 n\r\n" |
| 2427 |
"0000000066 00000 n\r\n" |
| 2428 |
"0000000115 00000 n\r\n" |
| 2429 |
"0000000161 00000 n\r\n" |
| 2430 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2431 |
"startxref\r\n" |
| 2432 |
"192\r\n" |
| 2433 |
"%%EOF"; |
| 2434 |
|
| 2435 |
try |
| 2436 |
{ |
| 2437 |
PdfMemDocument doc; |
| 2438 |
doc.LoadFromBuffer(strAncestorLoop); |
| 2439 |
|
| 2440 |
auto names = doc.GetNames(); |
| 2441 |
if (names != nullptr) |
| 2442 |
{ |
| 2443 |
PdfStringMap<PdfObject> dict; |
| 2444 |
names->ToDictionary(PdfKnownNameTree::Dests, dict); |
| 2445 |
} |
| 2446 |
|
| 2447 |
FAIL("Should throw exception"); |
| 2448 |
} |
| 2449 |
catch (PdfError& error) |
| 2450 |
{ |
| 2451 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidDataType); |
| 2452 |
} |
| 2453 |
} |
| 2454 |
|
| 2455 |
// CVE-2021-30471 |
| 2456 |
TEST_CASE("TestNestedPageTree") |
| 2457 |
{ |
| 2458 |
// test for valid but deeply nested page tree |
| 2459 |
// maxDepth must be less than GetMaxObjectCount otherwise PdfParser::ResizeOffsets |
| 2460 |
// throws an error when reading the xref offsets table, and no outlines are read |
| 2461 |
ostringstream oss; |
| 2462 |
const size_t maxDepth = getStackOverflowDepth() - 4 - 1; |
| 2463 |
const size_t numObjects = maxDepth + 4; |
| 2464 |
vector<size_t> offsets(numObjects); |
| 2465 |
size_t xrefOffset = 0; |
| 2466 |
|
| 2467 |
offsets[0] = 0; |
| 2468 |
oss << "%PDF-1.0\r\n"; |
| 2469 |
|
| 2470 |
offsets[1] = (size_t)oss.tellp(); |
| 2471 |
oss << "1 0 obj<</Type/Catalog /AcroForm 2 0 R /Pages 3 0 R>>endobj "; |
| 2472 |
|
| 2473 |
offsets[2] = (size_t)oss.tellp(); |
| 2474 |
oss << "2 0 obj<</Type/AcroForm >>endobj "; |
| 2475 |
|
| 2476 |
offsets[3] = (size_t)oss.tellp(); |
| 2477 |
oss << "3 0 obj<</Type/Pages /Kids [4 0 R] /Count 1 >>endobj "; |
| 2478 |
|
| 2479 |
// create pages tree nested to maxDepth where each node has one child |
| 2480 |
// except single leaf node at maxDepth |
| 2481 |
for (size_t objNo = 4; objNo < numObjects; objNo++) |
| 2482 |
{ |
| 2483 |
offsets[objNo] = (size_t)oss.tellp(); |
| 2484 |
|
| 2485 |
if (objNo < numObjects - 1) |
| 2486 |
oss << objNo << " 0 obj<</Type/Pages /Kids [" << objNo + 1 << " 0 R] /Parent " << objNo - 1 << " 0 R /Count 1 >>endobj "; |
| 2487 |
else |
| 2488 |
oss << objNo << " 0 obj<</Type/Page /Parent " << objNo - 1 << " 0 R >>endobj "; |
| 2489 |
} |
| 2490 |
|
| 2491 |
// output xref table |
| 2492 |
oss << "\r\n"; |
| 2493 |
xrefOffset = (size_t)oss.tellp(); |
| 2494 |
oss << "xref\r\n"; |
| 2495 |
oss << "0 " << numObjects << "\r\n"; |
| 2496 |
|
| 2497 |
oss << "0000000000 65535 f\r\n"; |
| 2498 |
|
| 2499 |
for (size_t objNo = 1; objNo < offsets.size(); objNo++) |
| 2500 |
{ |
| 2501 |
// write xref entries like |
| 2502 |
// "0000000010 00000 n\r\n" |
| 2503 |
char refEntry[21]; |
| 2504 |
snprintf(refEntry, 21, "%010zu 00000 n\r\n", offsets[objNo]); |
| 2505 |
|
| 2506 |
oss << refEntry; |
| 2507 |
} |
| 2508 |
|
| 2509 |
oss << "trailer<</Size " << numObjects << "/Root 1 0 R>>\r\n"; |
| 2510 |
oss << "startxref\r\n"; |
| 2511 |
oss << xrefOffset << "\r\n"; |
| 2512 |
oss << "%%EOF"; |
| 2513 |
|
| 2514 |
auto buffer = oss.str(); |
| 2515 |
try |
| 2516 |
{ |
| 2517 |
PdfMemDocument doc; |
| 2518 |
doc.LoadFromBuffer(buffer, PdfLoadOptions::StrictParsing); |
| 2519 |
|
| 2520 |
auto& pages = doc.GetPages(); |
| 2521 |
for (unsigned pageNo = 0; pageNo < pages.GetCount(); pageNo++) |
| 2522 |
(void)pages.GetPageAt(pageNo); |
| 2523 |
|
| 2524 |
FAIL("Should throw exception"); |
| 2525 |
} |
| 2526 |
catch (PdfError& error) |
| 2527 |
{ |
| 2528 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2529 |
} |
| 2530 |
} |
| 2531 |
|
| 2532 |
// CVE-2021-30471 |
| 2533 |
TEST_CASE("TestLoopingPageTree") |
| 2534 |
{ |
| 2535 |
// test PDF without nested kids |
| 2536 |
string strNoLoop = |
| 2537 |
"%PDF-1.0\r\n" |
| 2538 |
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\r\n" |
| 2539 |
"xref\r\n" |
| 2540 |
"0 4\r\n" |
| 2541 |
"0000000000 65535 f\r\n" |
| 2542 |
"0000000010 00000 n\r\n" |
| 2543 |
"0000000053 00000 n\r\n" |
| 2544 |
"0000000102 00000 n\r\n" |
| 2545 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2546 |
"startxref\r\n" |
| 2547 |
"149\r\n" |
| 2548 |
"%%EOF"; |
| 2549 |
|
| 2550 |
{ |
| 2551 |
PdfMemDocument doc; |
| 2552 |
doc.LoadFromBuffer(strNoLoop); |
| 2553 |
auto& pages = doc.GetPages(); |
| 2554 |
for (unsigned pageNo = 0; pageNo < doc.GetPages().GetCount(); pageNo++) |
| 2555 |
(void)pages.GetPageAt(pageNo); |
| 2556 |
REQUIRE(true); |
| 2557 |
} |
| 2558 |
|
| 2559 |
// CVE-2021-30471 test for pages tree /Kids array that refer back to pages tree root |
| 2560 |
string strSelfLoop = |
| 2561 |
"%PDF-1.0\r\n" |
| 2562 |
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[2 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\r\n" |
| 2563 |
"xref\r\n" |
| 2564 |
"0 4\r\n" |
| 2565 |
"0000000000 65535 f\r\n" |
| 2566 |
"0000000010 00000 n\r\n" |
| 2567 |
"0000000053 00000 n\r\n" |
| 2568 |
"0000000102 00000 n\r\n" |
| 2569 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2570 |
"startxref\r\n" |
| 2571 |
"149\r\n" |
| 2572 |
"%%EOF"; |
| 2573 |
|
| 2574 |
try |
| 2575 |
{ |
| 2576 |
PdfMemDocument doc; |
| 2577 |
doc.LoadFromBuffer(strSelfLoop); |
| 2578 |
auto& pages = doc.GetPages(); |
| 2579 |
for (unsigned pageNo = 0; pageNo < pages.GetCount(); pageNo++) |
| 2580 |
(void)pages.GetPageAt(pageNo); |
| 2581 |
|
| 2582 |
FAIL("Should throw exception"); |
| 2583 |
} |
| 2584 |
catch (PdfError& error) |
| 2585 |
{ |
| 2586 |
REQUIRE(error.GetCode() == PdfErrorCode::BrokenFile); |
| 2587 |
} |
| 2588 |
|
| 2589 |
// CVE-2021-30471 test for pages tree /Kids array that refer back to an ancestor (document root object) |
| 2590 |
string strAncestorLoop = |
| 2591 |
"%PDF-1.0\r\n" |
| 2592 |
"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[1 0 R]/Count 1>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\r\n" |
| 2593 |
"xref\r\n" |
| 2594 |
"0 4\r\n" |
| 2595 |
"0000000000 65535 f\r\n" |
| 2596 |
"0000000010 00000 n\r\n" |
| 2597 |
"0000000053 00000 n\r\n" |
| 2598 |
"0000000102 00000 n\r\n" |
| 2599 |
"trailer<</Size 4/Root 1 0 R>>\r\n" |
| 2600 |
"startxref\r\n" |
| 2601 |
"149\r\n" |
| 2602 |
"%%EOF"; |
| 2603 |
|
| 2604 |
try |
| 2605 |
{ |
| 2606 |
PdfMemDocument doc; |
| 2607 |
doc.LoadFromBuffer(strAncestorLoop); |
| 2608 |
auto& pages = doc.GetPages(); |
| 2609 |
pages.GetPageAt(0); |
| 2610 |
FAIL("Should throw exception"); |
| 2611 |
} |
| 2612 |
catch (PdfError& error) |
| 2613 |
{ |
| 2614 |
REQUIRE(error.GetCode() == PdfErrorCode::BrokenFile); |
| 2615 |
} |
| 2616 |
} |
| 2617 |
|
| 2618 |
// CVE-2020-18971 |
| 2619 |
TEST_CASE("TestNestedOutlines") |
| 2620 |
{ |
| 2621 |
// test for valid but deeply nested outlines |
| 2622 |
// maxDepth must be less than GetMaxObjectCount otherwise PdfParser::ResizeOffsets |
| 2623 |
// throws an error when reading the xref offsets table, and no outlines are read |
| 2624 |
auto buffer = generateNestedOutlinesPdf(true); |
| 2625 |
try |
| 2626 |
{ |
| 2627 |
PdfMemDocument doc; |
| 2628 |
doc.LoadFromBuffer(buffer, PdfLoadOptions::StrictParsing); |
| 2629 |
|
| 2630 |
// load should succeed, then GetOutlines goes recursive due to /Outlines deep nesting |
| 2631 |
(void)doc.GetOutlines(); |
| 2632 |
FAIL("Should throw exception"); |
| 2633 |
} |
| 2634 |
catch (PdfError& error) |
| 2635 |
{ |
| 2636 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2637 |
} |
| 2638 |
} |
| 2639 |
|
| 2640 |
// Same fixture as TestNestedOutlines, but the catalog is missing /Pages. The |
| 2641 |
// chain itself is not a cycle, it's a plain acyclic linked list sized to sit |
| 2642 |
// just below the real stack limit. Failing the /Pages check in |
| 2643 |
// PdfParser::resolveCatalog() forces PdfParser::tryRebuildCrossReference(), |
| 2644 |
// which walks the whole object graph via PdfIndirectObjectList::CollectGarbage(). |
| 2645 |
TEST_CASE("TestNestedOutlinesRebuild") |
| 2646 |
{ |
| 2647 |
auto buffer = generateNestedOutlinesPdf(false); |
| 2648 |
try |
| 2649 |
{ |
| 2650 |
PdfMemDocument doc; |
| 2651 |
doc.LoadFromBuffer(buffer, PdfLoadOptions::StrictParsing); |
| 2652 |
FAIL("Should throw exception"); |
| 2653 |
} |
| 2654 |
catch (PdfError& error) |
| 2655 |
{ |
| 2656 |
// The rebuild succeeds, but the recovered catalog still has no /Pages, |
| 2657 |
// so resolveEntryPoint() rejects it again on the second attempt |
| 2658 |
REQUIRE(error.GetCode() == PdfErrorCode::InvalidTrailer); |
| 2659 |
} |
| 2660 |
} |
| 2661 |
|
| 2662 |
// CVE-2020-18971 |
| 2663 |
TEST_CASE("TestLoopingOutlines") |
| 2664 |
{ |
| 2665 |
// CVE-2020-18971 - PdfOutlineItem /Next refers a preceding sibling |
| 2666 |
// The catalog needs a /Pages key to pass PdfParser catalog validation |
| 2667 |
string_view strNextLoop = R"(%PDF-1.0 |
| 2668 |
1 0 obj<</Type/Catalog /AcroForm 2 0 R /Outlines 3 0 R /Pages 2 0 R>>endobj |
| 2669 |
2 0 obj<</Type/AcroForm >>endobj |
| 2670 |
3 0 obj<</Type/Outlines /First 4 0 R /Count 2 /Last 5 0 R >>endobj |
| 2671 |
4 0 obj<</Title (Outline Item 1) /Next 5 0 R>>endobj |
| 2672 |
5 0 obj<</Title (Outline Item 2) /Next 4 0 R>>endobj |
| 2673 |
xref |
| 2674 |
0 6 |
| 2675 |
0000000000 65535 f |
| 2676 |
0000000009 00000 n |
| 2677 |
0000000085 00000 n |
| 2678 |
0000000118 00000 n |
| 2679 |
0000000185 00000 n |
| 2680 |
0000000238 00000 n |
| 2681 |
trailer<</Size 6/Root 1 0 R>> |
| 2682 |
startxref |
| 2683 |
291 |
| 2684 |
%%EOF |
| 2685 |
)"sv; |
| 2686 |
|
| 2687 |
try |
| 2688 |
{ |
| 2689 |
PdfMemDocument doc; |
| 2690 |
doc.LoadFromBuffer(strNextLoop); |
| 2691 |
|
| 2692 |
// load should succeed, then GetOutlines goes recursive due to /Outlines loop |
| 2693 |
(void)doc.GetOutlines(); |
| 2694 |
FAIL("Should throw exception"); |
| 2695 |
} |
| 2696 |
catch (PdfError& error) |
| 2697 |
{ |
| 2698 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2699 |
} |
| 2700 |
|
| 2701 |
// https://sourceforge.net/p/podofo/tickets/25/ |
| 2702 |
// The catalog needs a /Pages key to pass PdfParser catalog validation |
| 2703 |
string_view strSelfLoop = R"(%PDF-1.0 |
| 2704 |
1 0 obj<</Type/Catalog/Outlines 2 0 R/Pages 2 0 R>>endobj |
| 2705 |
2 0 obj<</Type/Outlines /First 2 0 R /Last 2 0 R /Count 1>>endobj |
| 2706 |
xref |
| 2707 |
0 3 |
| 2708 |
0000000000 65535 f |
| 2709 |
0000000009 00000 n |
| 2710 |
0000000067 00000 n |
| 2711 |
trailer<</Size 3/Root 1 0 R>> |
| 2712 |
startxref |
| 2713 |
133 |
| 2714 |
%%EOF |
| 2715 |
)"sv; |
| 2716 |
|
| 2717 |
try |
| 2718 |
{ |
| 2719 |
PdfMemDocument doc; |
| 2720 |
doc.LoadFromBuffer(strNextLoop); |
| 2721 |
|
| 2722 |
// load should succeed, then GetOutlines goes recursive due to /Outlines loop |
| 2723 |
(void)doc.GetOutlines(); |
| 2724 |
FAIL("Should throw exception"); |
| 2725 |
} |
| 2726 |
catch (PdfError& error) |
| 2727 |
{ |
| 2728 |
REQUIRE(error.GetCode() == PdfErrorCode::MaxRecursionReached); |
| 2729 |
} |
| 2730 |
} |
| 2731 |
|
| 2732 |
TEST_CASE("TestReset") |
| 2733 |
{ |
| 2734 |
PdfMemDocument doc; |
| 2735 |
doc.Load(TestUtils::GetTestInputFilePath("Hierarchies1.pdf")); |
| 2736 |
|
| 2737 |
{ |
| 2738 |
REQUIRE(doc.GetMetadata().GetCreator().value().GetString() == "Adobe Acrobat 18.0"); |
| 2739 |
auto& page = doc.GetPages().GetPageAt(0); |
| 2740 |
auto& widget = dynamic_cast<PdfAnnotationWidget&>(page.GetAnnotations().GetAnnotAt(19)); |
| 2741 |
auto& textbox = dynamic_cast<PdfTextBox&>(widget.GetField()); |
| 2742 |
REQUIRE(textbox.GetName().value().GetString() == "barcodePagina1"); |
| 2743 |
} |
| 2744 |
|
| 2745 |
doc.Reset(); |
| 2746 |
|
| 2747 |
REQUIRE(doc.GetPages().GetCount() == 0); |
| 2748 |
REQUIRE(doc.GetMetadata().GetCreator() == nullptr); |
| 2749 |
} |
| 2750 |
|
| 2751 |
TEST_CASE("TestManyTrailer") |
| 2752 |
{ |
| 2753 |
// A document with a long chain of incremental updates must be parsed |
| 2754 |
// iteratively without overflowing the stack |
| 2755 |
PdfMemDocument doc; |
| 2756 |
doc.Load(TestUtils::GetTestInputFilePath("Empty160trailer.pdf")); |
| 2757 |
REQUIRE(doc.GetPages().GetCount() == 1); |
| 2758 |
} |
| 2759 |
|
| 2760 |
TEST_CASE("TestManyTrailerXRefStream") |
| 2761 |
{ |
| 2762 |
// A document with a long chain of XRef streams must be parsed |
| 2763 |
// iteratively without overflowing the stack |
| 2764 |
PdfMemDocument doc; |
| 2765 |
doc.Load(TestUtils::GetTestInputFilePath("EmptyXRefStream225Trailer.pdf")); |
| 2766 |
REQUIRE(doc.GetPages().GetCount() == 1); |
| 2767 |
} |
| 2768 |
|
| 2769 |
TEST_CASE("TestXRefPDF10") |
| 2770 |
{ |
| 2771 |
// Test ability to read an XRef stream with an older version PDF |
| 2772 |
PdfMemDocument doc; |
| 2773 |
doc.Load(TestUtils::GetTestInputFilePath("TestXRefCheckboxUnicode.pdf")); |
| 2774 |
|
| 2775 |
auto& page = doc.GetPages().GetPageAt(0); |
| 2776 |
vector<PdfCheckBox*> checkboxes; |
| 2777 |
for (auto field : page.GetFieldsIterator()) |
| 2778 |
checkboxes.push_back(dynamic_cast<PdfCheckBox*>(field)); |
| 2779 |
|
| 2780 |
REQUIRE(checkboxes[0]->IsChecked()); |
| 2781 |
REQUIRE(!checkboxes[1]->IsChecked()); |
| 2782 |
} |
| 2783 |
|
| 2784 |
TEST_CASE("TestReclaimObjectMemory") |
| 2785 |
{ |
| 2786 |
PdfObject obj; |
| 2787 |
REQUIRE(!obj.TryUnload()); |
| 2788 |
|
| 2789 |
PdfMemDocument doc; |
| 2790 |
doc.Load(TestUtils::GetTestInputFilePath("TestImage2.pdf")); |
| 2791 |
|
| 2792 |
auto& page = doc.GetPages().GetPageAt(0); |
| 2793 |
auto& resources = page.GetResources(); |
| 2794 |
auto imageObj = resources.GetResource(PdfResourceType::XObject, "X0"); |
| 2795 |
REQUIRE(!imageObj->IsDelayedLoadStreamDone()); |
| 2796 |
|
| 2797 |
unique_ptr<PdfImage> image; |
| 2798 |
REQUIRE(PdfXObject::TryCreateFromObject<PdfImage>(*imageObj, image)); |
| 2799 |
|
| 2800 |
charbuff buffer; |
| 2801 |
image->DecodeTo(buffer, PdfPixelFormat::BGRA); |
| 2802 |
|
| 2803 |
REQUIRE(imageObj->IsDelayedLoadStreamDone()); |
| 2804 |
REQUIRE(imageObj->TryUnload()); |
| 2805 |
REQUIRE(!imageObj->IsDelayedLoadDone()); |
| 2806 |
REQUIRE(!imageObj->IsDelayedLoadStreamDone()); |
| 2807 |
|
| 2808 |
imageObj->MustGetStream().Clear(); |
| 2809 |
REQUIRE(!imageObj->TryUnload()); |
| 2810 |
} |
| 2811 |
|
| 2812 |
// This tests saving the update on a document with |
| 2813 |
// compressed object stream with an indirect length |
| 2814 |
// still produces a readable file |
| 2815 |
TEST_CASE("TestCompressedObjectStreamIndirectLength") |
| 2816 |
{ |
| 2817 |
string outpath = TestUtils::GetTestOutputFilePath("TestCompressedObjectStreamIndirectLength.pdf"); |
| 2818 |
|
| 2819 |
PdfMemDocument doc; |
| 2820 |
{ |
| 2821 |
FileStreamDevice device(TestUtils::GetTestInputFilePath("PDFUA-Reference", "PDFUA-Ref-2-02_Invoice.pdf")); |
| 2822 |
FileStreamDevice out(outpath, FileMode::Create); |
| 2823 |
device.CopyTo(out); |
| 2824 |
} |
| 2825 |
|
| 2826 |
doc.Load(outpath); |
| 2827 |
doc.SaveUpdate(outpath); |
| 2828 |
doc.Load(outpath); |
| 2829 |
} |
| 2830 |
|
| 2831 |
TEST_CASE("TestEdgeCases") |
| 2832 |
{ |
| 2833 |
PdfMemDocument doc; |
| 2834 |
vector<string_view> test = { "512.pdf"sv, "513.pdf"sv, "514.pdf"sv, "big1.pdf"sv, "big2.pdf"sv, "false.pdf"sv }; |
| 2835 |
for (unsigned i = 0; i < test.size(); i++) |
| 2836 |
doc.Load(TestUtils::GetTestInputFilePath("ParserTests", test[i]), PdfLoadOptions::SkipXRefRecovery); |
| 2837 |
|
| 2838 |
// This just really requires recovery as it has the trailer preceding the xref sections |
| 2839 |
doc.Load(TestUtils::GetTestInputFilePath("ParserTests", "rev.pdf")); |
| 2840 |
} |
| 2841 |
|
| 2842 |
// Bug: PdfParser::TakeEntryPoint move-constructed the trailer PdfObject via |
| 2843 |
// the noexcept move constructor, which called DelayedLoadStream(). For a |
| 2844 |
// malformed XRef stream (e.g. /Length referencing a non-existent object) |
| 2845 |
// this threw PdfErrorCode::InvalidStream inside a noexcept context, |
| 2846 |
// triggering std::terminate. |
| 2847 |
TEST_CASE("TestXRefStreamMoveNoTerminate") |
| 2848 |
{ |
| 2849 |
string_view pdf = R"( |
| 2850 |
%PDF-1.5 |
| 2851 |
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj |
| 2852 |
2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj |
| 2853 |
3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 1 1]>>endobj |
| 2854 |
4 0 obj |
| 2855 |
<</Type/XRef/Size 5/W[1 2 1]/Root 1 0 R/Filter/ASCIIHexDecode/Length 99 0 R>> |
| 2856 |
stream |
| 2857 |
000000FF0100090001003400010065000100A000 |
| 2858 |
endstream |
| 2859 |
endobj |
| 2860 |
startxref |
| 2861 |
160 |
| 2862 |
%%EOF |
| 2863 |
)"; |
| 2864 |
|
| 2865 |
PdfMemDocument doc; |
| 2866 |
REQUIRE_NOTHROW(doc.LoadFromBuffer(pdf)); |
| 2867 |
} |
| 2868 |
|
| 2869 |
string generateXRefEntries(size_t count) |
| 2870 |
{ |
| 2871 |
string strXRefEntries; |
| 2872 |
|
| 2873 |
// generates a block of 20-byte xref entries |
| 2874 |
// 0000000000 65535 f\r\n |
| 2875 |
// 0000000120 00000 n\r\n |
| 2876 |
// 0000000120 00000 n\r\n |
| 2877 |
// 0000000120 00000 n\r\n |
| 2878 |
try |
| 2879 |
{ |
| 2880 |
strXRefEntries.reserve(count * 20); |
| 2881 |
for (size_t i = 0; i < count; i++) |
| 2882 |
{ |
| 2883 |
if (i == 0) |
| 2884 |
strXRefEntries.append("0000000000 65535 f\r\n"); |
| 2885 |
else |
| 2886 |
strXRefEntries.append("0000000120 00000 n\r\n"); |
| 2887 |
} |
| 2888 |
} |
| 2889 |
catch (exception&) |
| 2890 |
{ |
| 2891 |
// if this fails it's a bug in the unit tests and not PoDoFo |
| 2892 |
FAIL("generateXRefEntries memory allocation failure"); |
| 2893 |
} |
| 2894 |
|
| 2895 |
return strXRefEntries; |
| 2896 |
} |
| 2897 |
|
| 2898 |
bool canOutOfMemoryKillUnitTests() |
| 2899 |
{ |
| 2900 |
// test if out of memory conditions will kill the unit test process |
| 2901 |
// which prevents tests completing |
| 2902 |
|
| 2903 |
#if defined(_WIN32) |
| 2904 |
// on Windows 32/64 allocations close to size of VM address space always fail gracefully |
| 2905 |
bool canTerminateProcess = false; |
| 2906 |
#elif defined( __APPLE__ ) |
| 2907 |
// on macOS/iOS allocations close to size of VM address space fail gracefully |
| 2908 |
// unless Address Sanitizer (ASAN) is enabled |
| 2909 |
#if __has_feature(address_sanitizer) |
| 2910 |
// ASAN terminates the process if alloc fails - and using allocator_may_return_null=1 |
| 2911 |
// to continue after an allocation doesn't work in C++ because new returns null which is |
| 2912 |
// forbidden by the C++ spec and terminates process when 'this' is dereferenced in constructor |
| 2913 |
// see https://github.com/google/sanitizers/issues/295 |
| 2914 |
bool canTerminateProcess = true; |
| 2915 |
#else |
| 2916 |
// if alloc fails following message is logged |
| 2917 |
// *** mach_vm_map failed (error code=3) |
| 2918 |
// *** error: can't allocate region |
| 2919 |
// *** set a breakpoint in malloc_error_break to debug |
| 2920 |
bool canTerminateProcess = false; |
| 2921 |
#endif |
| 2922 |
#elif defined( __linux__ ) |
| 2923 |
// TODO do big allocs succeed then trigger OOM-killer fiasco?? |
| 2924 |
bool canTerminateProcess = false; |
| 2925 |
#else |
| 2926 |
// other systems - assume big allocs faily gracefully and throw bad_alloc |
| 2927 |
bool canTerminateProcess = false; |
| 2928 |
#endif |
| 2929 |
return canTerminateProcess; |
| 2930 |
} |
| 2931 |
|
| 2932 |
size_t getStackOverflowDepth() |
| 2933 |
{ |
| 2934 |
// calculate stack overflow depth - need to do this because a value that consistently overflows a 64-bit stack |
| 2935 |
// doesn't work on 32-bit systems because they run out of heap in ReadObjects before they get a chance to overflow stack |
| 2936 |
// this is because sizeof(PdfParserObject) = 472 bytes (and there's one of these for every object read) |
| 2937 |
constexpr size_t parserObjectSize = sizeof(PdfParserObject); |
| 2938 |
|
| 2939 |
#if defined(_WIN64) |
| 2940 |
// 1 MB default stack size, 64-bit address space, Windows x64 ABI |
| 2941 |
// each stack frame has at least 4 64-bit stack params, 4 64-bit register params, plus 64-bit return address |
| 2942 |
// stack frame size increases if function contains local variables or more than 4 parameters |
| 2943 |
// see https://docs.microsoft.com/en-us/cpp/build/stack-usage?view=msvc-170 |
| 2944 |
constexpr size_t stackSize = 1 * 1024 * 1024; |
| 2945 |
constexpr size_t frameSize = sizeof(void*) * (4 + 4 + 1); // 4 stack params + 4 register params + return address |
| 2946 |
constexpr size_t maxFrames = stackSize / frameSize; // overflows at 14,563 recursive calls (or sooner if functions contain local variables) |
| 2947 |
#elif defined(_WIN32) |
| 2948 |
// 1 MB default stack size, 32-bit address space (can't allocate more than 2GB), Windows x86 thiscall calling convention |
| 2949 |
// each stack frame has at least 32-bit EBP and return address |
| 2950 |
// stack frame size increases if function contains local variables or any parameters |
| 2951 |
constexpr size_t stackSize = 1 * 1024 * 1024; |
| 2952 |
constexpr size_t frameSize = sizeof(void*) * (1 + 1); // EBP and return address |
| 2953 |
constexpr size_t maxFrames = stackSize / frameSize; // overflows at 131,072 recursive calls (or sooner if functions contain local variables or has parameters) |
| 2954 |
#else |
| 2955 |
// assume 8MB macOS / Linux default stack size, 64-bit address space, System V AMD64 ABI |
| 2956 |
// each stack frame has at least 64-bit EBP and return address |
| 2957 |
// stack frame size increases if function contains local variables or any parameters |
| 2958 |
constexpr size_t stackSize = 8 * 1024 * 1024; |
| 2959 |
constexpr size_t frameSize = sizeof(void*) * (1 + 1); // EBP and return address |
| 2960 |
constexpr size_t maxFrames = stackSize / frameSize; // overflows at 524,288 recursive calls (or sooner if functions contain local variables or has parameters) |
| 2961 |
#endif |
| 2962 |
|
| 2963 |
// add a few frames to sure we go beyond end of stack |
| 2964 |
constexpr size_t overflowDepth = maxFrames + 1000; |
| 2965 |
|
| 2966 |
// overflowDepth must be less than GetMaxObjectCount otherwise PdfParser::ResizeOffsets |
| 2967 |
// throws an error when reading the xref offsets table, and no recursive calls are made |
| 2968 |
// must also be allocate less than half of address space to prevent out-of-memory exceptions |
| 2969 |
REQUIRE(overflowDepth < PdfCommon::GetMaxObjectCount()); |
| 2970 |
REQUIRE(overflowDepth * parserObjectSize < numeric_limits<size_t>::max() / 2); |
| 2971 |
|
| 2972 |
return overflowDepth; |
| 2973 |
} |
| 2974 |
|
| 2975 |
// Generates a PDF with outlines nested to just below the real stack overflow |
| 2976 |
// depth, as a linked list where each node has one child except the leaf node |
| 2977 |
string generateNestedOutlinesPdf(bool includePages) |
| 2978 |
{ |
| 2979 |
ostringstream oss; |
| 2980 |
const size_t maxDepth = getStackOverflowDepth() - 4 - 1; |
| 2981 |
const size_t numObjects = maxDepth + 4; |
| 2982 |
vector<size_t> offsets(numObjects); |
| 2983 |
size_t xrefOffset = 0; |
| 2984 |
|
| 2985 |
offsets[0] = 0; |
| 2986 |
oss << "%PDF-1.0\r\n"; |
| 2987 |
|
| 2988 |
offsets[1] = (size_t)oss.tellp(); |
| 2989 |
oss << "1 0 obj<</Type/Catalog /AcroForm 2 0 R /Outlines 3 0 R"; |
| 2990 |
if (includePages) |
| 2991 |
oss << " /Pages 2 0 R"; |
| 2992 |
oss << ">>endobj "; |
| 2993 |
|
| 2994 |
offsets[2] = (size_t)oss.tellp(); |
| 2995 |
oss << "2 0 obj<</Type/AcroForm >>endobj "; |
| 2996 |
|
| 2997 |
offsets[3] = (size_t)oss.tellp(); |
| 2998 |
oss << "3 0 obj<</Type/Outlines /First 4 0 R /Count " << maxDepth << " /Last 5 0 R >>endobj "; |
| 2999 |
|
| 3000 |
// create outlines tree nested to maxDepth where each node has one child |
| 3001 |
// except single leaf node at maxDepth |
| 3002 |
for (size_t objNo = 4; objNo < numObjects; objNo++) |
| 3003 |
{ |
| 3004 |
offsets[objNo] = (size_t)oss.tellp(); |
| 3005 |
|
| 3006 |
if (objNo < numObjects - 1) |
| 3007 |
oss << objNo << " 0 obj<</Title (Outline Item) /First " << objNo + 1 << " 0 R /Last " << objNo + 1 << " 0 R>>endobj "; |
| 3008 |
else |
| 3009 |
oss << objNo << " 0 obj<</Title (Outline Item)>>endobj "; |
| 3010 |
} |
| 3011 |
|
| 3012 |
// output xref table |
| 3013 |
oss << "\r\n"; |
| 3014 |
xrefOffset = (size_t)oss.tellp(); |
| 3015 |
oss << "xref\r\n"; |
| 3016 |
oss << "0 " << numObjects << "\r\n"; |
| 3017 |
|
| 3018 |
oss << "0000000000 65535 f\r\n"; |
| 3019 |
|
| 3020 |
for (size_t objNo = 1; objNo < offsets.size(); objNo++) |
| 3021 |
{ |
| 3022 |
// write xref entries like |
| 3023 |
// "0000000010 00000 n\r\n" |
| 3024 |
char szXrefEntry[21]; |
| 3025 |
snprintf(szXrefEntry, 21, "%010zu 00000 n\r\n", offsets[objNo]); |
| 3026 |
|
| 3027 |
oss << szXrefEntry; |
| 3028 |
} |
| 3029 |
|
| 3030 |
oss << "trailer<</Size " << numObjects << "/Root 1 0 R>>\r\n"; |
| 3031 |
oss << "startxref\r\n"; |
| 3032 |
oss << xrefOffset << "\r\n"; |
| 3033 |
oss << "%%EOF"; |
| 3034 |
|
| 3035 |
return oss.str(); |
| 3036 |
} |