The vulnerability lies in the parsing of XMP metadata in the pypdf library, where a crafted PDF with a malicious XMP stream can cause excessive memory usage. The root cause is in the pypdf.xmp.XmpInformation.__init__ function, which, before the patch, would read and parse the entire XMP data stream without any limits on its size or the number of XML elements.
The fix, as seen in commit 4f29b462664820df861cfc5e029ef72364d523d9, mitigates this by introducing two key limits:
- A check on the input stream's length (
XMP_MAX_INPUT_LENGTH) is added in XmpInformation.__init__ before parsing.
- A limit on the number of XML elements (
XMP_MAX_ELEMENT_COUNT) is enforced by adding a custom start_element_handler to the _XmpBuilder class, which is used for XML parsing.
During exploitation, a runtime profiler would show the XmpInformation.__init__ function being called, which would then lead to uncontrolled memory allocation as it attempts to parse the malicious XMP data. Therefore, this function is the primary indicator of the vulnerability being triggered.