[ 
https://issues.apache.org/jira/browse/PDFBOX-6356?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Maruan Sahyoun updated PDFBOX-6356:
-----------------------------------
    Description: 
Found by a Claude security scan.

The Type 1 and Type 2 charstring parsers expand subroutine calls (`callsubr`, 
and `callgsubr` for Type 2) recursively while parsing a glyph: 
`Type1CharStringParser.processCallSubr()` and 
`Type2CharStringParser.processSubr()`. Since PDFBOX-6308 the nesting depth is 
limited (`MAX_SUBR_NESTING`: 106 for Type 1, 50 for Type 2), which stops 
endless recursion.

The total number of calls is not limited. If each subroutine calls the next one 
twice, a chain of 64 subroutines needs 2^64 calls while staying below the 
nesting limit, so parsing the glyph never finishes. This affects rendering, and 
text extraction when the PDF doesn't provide glyph widths.

Proposed change
 - Count the subroutine bytes parsed for each glyph, and throw an `IOException` 
when they exceed a budget (for example 1 MB). Every call costs at least two 
bytes in the calling code, so this also limits the number of calls and the size 
of the resulting sequence.
 - Track the budget per call to `parse()`, like the nesting level, not in 
fields: one parser instance is shared by all glyphs of a font and may be used 
by several threads.
 - Keep the existing nesting limits; lowering them would bring back the 
regressions fixed in PDFBOX-6308.
 - Tests for both parsers: a subroutine chain with exponential calls must fail 
within a timeout, and a chain nested deeper than the specification's 10 levels 
must still parse.

Claude on behalf of Maruan Sahyoun (msahyoun)

  was:
The Type 1 and Type 2 charstring parsers expand subroutine calls (`callsubr`, 
and `callgsubr` for Type 2) recursively while parsing a glyph: 
`Type1CharStringParser.processCallSubr()` and 
`Type2CharStringParser.processSubr()`. Since PDFBOX-6308 the nesting depth is 
limited (`MAX_SUBR_NESTING`: 106 for Type 1, 50 for Type 2), which stops 
endless recursion.

The total number of calls is not limited. If each subroutine calls the next one 
twice, a chain of 64 subroutines needs 2^64 calls while staying below the 
nesting limit, so parsing the glyph never finishes. This affects rendering, and 
text extraction when the PDF doesn't provide glyph widths.

Proposed change
 - Count the subroutine bytes parsed for each glyph, and throw an `IOException` 
when they exceed a budget (for example 1 MB). Every call costs at least two 
bytes in the calling code, so this also limits the number of calls and the size 
of the resulting sequence.
 - Track the budget per call to `parse()`, like the nesting level, not in 
fields: one parser instance is shared by all glyphs of a font and may be used 
by several threads.
 - Keep the existing nesting limits; lowering them would bring back the 
regressions fixed in PDFBOX-6308.
 - Tests for both parsers: a subroutine chain with exponential calls must fail 
within a timeout, and a chain nested deeper than the specification's 10 levels 
must still parse.

Claude on behalf of Maruan Sahyoun (msahyoun)


> Bound the total subroutine work per glyph in Type1CharStringParser and 
> Type2CharStringParser
> --------------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-6356
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-6356
>             Project: PDFBox
>          Issue Type: Bug
>          Components: FontBox
>    Affects Versions: 2.0.37, 3.0.8 PDFBox, 4.0.0
>            Reporter: Maruan Sahyoun
>            Assignee: Maruan Sahyoun
>            Priority: Major
>             Fix For: 2.0.38, 3.0.9 PDFBox, 4.0.0
>
>
> Found by a Claude security scan.
> The Type 1 and Type 2 charstring parsers expand subroutine calls (`callsubr`, 
> and `callgsubr` for Type 2) recursively while parsing a glyph: 
> `Type1CharStringParser.processCallSubr()` and 
> `Type2CharStringParser.processSubr()`. Since PDFBOX-6308 the nesting depth is 
> limited (`MAX_SUBR_NESTING`: 106 for Type 1, 50 for Type 2), which stops 
> endless recursion.
> The total number of calls is not limited. If each subroutine calls the next 
> one twice, a chain of 64 subroutines needs 2^64 calls while staying below the 
> nesting limit, so parsing the glyph never finishes. This affects rendering, 
> and text extraction when the PDF doesn't provide glyph widths.
> Proposed change
>  - Count the subroutine bytes parsed for each glyph, and throw an 
> `IOException` when they exceed a budget (for example 1 MB). Every call costs 
> at least two bytes in the calling code, so this also limits the number of 
> calls and the size of the resulting sequence.
>  - Track the budget per call to `parse()`, like the nesting level, not in 
> fields: one parser instance is shared by all glyphs of a font and may be used 
> by several threads.
>  - Keep the existing nesting limits; lowering them would bring back the 
> regressions fixed in PDFBOX-6308.
>  - Tests for both parsers: a subroutine chain with exponential calls must 
> fail within a timeout, and a chain nested deeper than the specification's 10 
> levels must still parse.
> 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