Maruan Sahyoun created PDFBOX-6362:
--------------------------------------

             Summary: Limit stack size, executed instructions and nesting depth 
of Type 4 (PostScript calculator) functions
                 Key: PDFBOX-6362
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6362
             Project: PDFBox
          Issue Type: Bug
          Components: PDModel
            Reporter: Maruan Sahyoun
            Assignee: Maruan Sahyoun


Type 4 (PostScript calculator) functions are evaluated without any limits. The 
`copy` operator duplicates the top n entries of the operand stack, so a chain 
of `copy` operators doubles the stack with each step: a function of less than 
300 bytes builds a stack of 2^30 entries and causes an `OutOfMemoryError`. In 
addition, procedures that remain on the operand stack are executed by 
`InstructionSequence.execute` also inside nested procedures, which isn't 
PostScript behaviour. A procedure that duplicates itself, such as `{

{dup}

dup }`, recurses until a `StackOverflowError`, and procedures fetched from the 
stack can call each other in a tree with exponential running time. As functions 
are evaluated per pixel of a shading or per color value, a single malformed 
function can exhaust the memory or block rendering.

Found by a Claude security scan.

Proposed change
 - Limit the operand stack to 1000 entries (the PDF specification allows 100) 
and check it after each executed instruction.
 - Limit the instructions executed per evaluation to 10 times the number of 
instructions in the function, so the cost of an evaluation stays proportional 
to the size of the function.
 - Limit the nesting depth of executed procedures to 100.
 - Report an exceeded limit as an `IOException` from `PDFunctionType4.eval`, 
like the existing handling of an empty stack or a wrong operand type.
 - Add regression tests for a `copy` chain, a self-duplicating procedure and a 
procedure tree.

Claude on behalf of Maruan Sahyoun (msahyoun)



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to