PDF with xref table corrupted : memory leak
Context : a pdf with corrupted xref table (and with password protection change) do an infinite loop. file : RawData/RawDataParser.php method : getRawObject() When an object is corrupted there are cases where the $offset don't change, These are the case in loops "do { } while loop. " in case conditions '[' or ']' and '<' or '>' .
case '[': // \x5B LEFT SQUARE BRACKET
case ']': // \x5D RIGHT SQUARE BRACKET
// array object
$objtype = $char;
++$offset;
if ('[' == $char) {
// get array content
$objval = [];
do {
// get element
$element = $this->getRawObject($pdfData, $offset);
$offset = $element[2];
$objval[] = $element;
} while (']' != $element[0]);
// remove closing delimiter
array_pop($objval);
}
break;
case '<': // \x3C LESS-THAN SIGN
case '>': // \x3E GREATER-THAN SIGN
if (isset($pdfData[($offset + 1)]) and ($pdfData[($offset + 1)] == $char)) {
// dictionary object
$objtype = $char.$char;
$offset += 2;
if ('<' == $char) {
// get array content
$objval = [];
do {
// get element
$element = $this->getRawObject($pdfData, $offset);
$offset = $element[2];
$objval[] = $element;
} while ('>>' != $element[0]);
// remove closing delimiter
array_pop($objval);
this is similar of the loop "do{} while" in method getIndirectObject() where the $offset case that don't change is treated with memorising the last offset and comparing the old with the new. If they are same, we exit of the loop.
protected function getIndirectObject(...) {
....
do {
$oldOffset = $offset;
// get element
$element = $this->getRawObject($pdfData, $offset);
$offset = $element[2];
// decode stream using stream's dictionary information
if ($decoding and ('stream' == $element[0]) and (isset($objdata[($i - 1)][0])) and ('<<' == $objdata[($i - 1)][0])) {
$element[3] = $this->decodeStream($pdfData, $xref, $objdata[($i - 1)][1], $element[1]);
}
$objdata[$i] = $element;
++$i;
} while (('endobj' != $element[0]) and ($offset != $oldOffset));
....
I tried the same fix in the loops of getRawObject() method, and the corrupted file don't generate an infinite loop and nor memory leak. Both "case" in geRawObject() could become :
case '[': // \x5B LEFT SQUARE BRACKET
case ']': // \x5D RIGHT SQUARE BRACKET
// array object
$objtype = $char;
++$offset;
if ('[' == $char) {
// get array content
$objval = [];
do {
$oldOffset = $offset;
// get element
$element = $this->getRawObject($pdfData, $offset);
$offset = $element[2];
$objval[] = $element;
} while ((']' != $element[0]) and ($offset != $oldOffset));
// remove closing delimiter
array_pop($objval);
}
break;
case '<': // \x3C LESS-THAN SIGN
case '>': // \x3E GREATER-THAN SIGN
if (isset($pdfData[($offset + 1)]) and ($pdfData[($offset + 1)] == $char)) {
// dictionary object
$objtype = $char.$char;
$offset += 2;
if ('<' == $char) {
// get array content
$objval = [];
do {
$oldOffset = $offset;
// get element
$element = $this->getRawObject($pdfData, $offset);
$offset = $element[2];
$objval[] = $element;
} while (('>>' != $element[0]) and ($offset != $oldOffset));
// remove closing delimiter
array_pop($objval);
}
PdfParser version 0.16.1 I can't supply the PDF file because of confidential information on it. Below result of qpdf command :
>qpdf --check CAS_AGP202011201430242763.PDF
checking CAS_AGP202011201430242763.PDF
PDF Version: 1.4
R = 3
P = -2359356
User password =
extract for accessibility: allowed
extract for any purpose: not allowed
print low resolution: allowed
print high resolution: allowed
modify document assembly: allowed
modify forms: allowed
modify annotations: not allowed
modify other: not allowed
modify anything: not allowed
File is not linearized
WARNING: CAS_AGP202011201430242763.PDF (object 5 0, offset 7441): expected endstream
WARNING: CAS_AGP202011201430242763.PDF (object 5 0, offset 421): attempting to recover stream length
WARNING: CAS_AGP202011201430242763.PDF (object 5 0, offset 421): recovered stream length: 7021
WARNING: CAS_AGP202011201430242763.PDF (object 7 0, offset 12499): expected endstream
WARNING: CAS_AGP202011201430242763.PDF (object 7 0, offset 7658): attempting to recover stream length
WARNING: CAS_AGP202011201430242763.PDF (object 7 0, offset 7658): recovered stream length: 4842
WARNING: CAS_AGP202011201430242763.PDF (object 12 0, offset 554322): unknown token while reading object; treating as string
WARNING: CAS_AGP202011201430242763.PDF (object 12 0, offset 554338): unexpected )
WARNING: CAS_AGP202011201430242763.PDF (object 12 0, offset 554303): expected dictionary key but found non-name object; inserting key /QPDFFake1
WARNING: CAS_AGP202011201430242763.PDF (object 12 0, offset 554303): expected dictionary key but found non-name object; inserting key /QPDFFake2
PHP Errors :
( ! ) Fatal error: Allowed memory size of 4294967296 bytes exhausted (tried to allocate 536870920 bytes) in /web/vendor/smalot/pdfparser/src/Smalot/PdfParser/RawData/RawDataParser.php on line 648
...
11 | 3.4278 | 12581432 | Smalot\PdfParser\RawData\RawDataParser->parseData( ??? ) | .../Parser.php:95
12 | 3.4336 | 14341328 | Smalot\PdfParser\RawData\RawDataParser->getIndirectObject( ???, ???, ???, ???, ??? ) | .../RawDataParser.php:829
13 | 3.4336 | 14341864 | Smalot\PdfParser\RawData\RawDataParser->getRawObject( ???, ??? ) | .../RawDataParser.php:487
Thank you for your detailed report.
Just to be sure, can you try with the latest version please? 0.17.1
I have the same issue with 0.17.1 version.
10 | 2.6052 | 3238056 | Smalot\PdfParser\Parser->parseContent( ??? ) | ...
11 | 2.6052 | 3238056 | Smalot\PdfParser\RawData\RawDataParser->parseData( ??? ) | .../Parser.php:95
12 | 2.6121 | 4995904 | Smalot\PdfParser\RawData\RawDataParser->getIndirectObject( ???, ???, ???, ???, ??? ) | .../RawDataParser.php:849
13 | 2.6121 | 4996384 | Smalot\PdfParser\RawData\RawDataParser->getRawObject( ???, ??? ) | .../RawDataParser.php:500
14 | 276.8714 | 4285791648 | Smalot\PdfParser\RawData\RawDataParser->getRawObject( ???, ??? ) | .../RawDataParser.php:662
Same problem here @k00ni Can you check it to find a fix please?
@alkaan and @lucas-gp: Can you try out the fix from @partulaj (#377) and get back to me?
I am trying to build a simple test using a string, which cause an infinite loop.
@alkaan, you said:
These are the case in loops "do { } while loop. " in case conditions '[' or ']' and '<' or '>' .
Can you or maybe @partulaj give me an example string, which causes this problem?
What I tried so far is:
class RawDataParserHelper extends RawDataParser
{
/**
* Expose protected function "getRawObject".
*/
public function exposeGetRawObject($pdfData, $offset = 0) {
return $this->getRawObject($pdfData, $offset);
}
}
class RawDataParserTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->fixture = new RawDataParserHelper();
}
/*
* Tests for getRawObject
*/
public function testGetRawObject()
{
// First steps using the following value.
// What must a minimal example look like to trigger the infinite loop?
$data = '[[ < >]]';
// calling "getRawObject" via "exposeGetRawObject" has to result in an infinite loop
$result = $this->fixture->exposeGetRawObject($data);
}
}
Hi,
I manage to get the string that cause the problem, you should be able to reproduce using $data = '<</Producer (eDkºãa˜þõ‚LÅòÕPïÙ)©)>>'
Closed by #377. Please check and post if it is not fixed yet.
Thank you all.