michaelrsweet / pdfio

PDFio is a simple C library for reading and writing PDF files.
https://www.msweet.org/pdfio
Apache License 2.0
198 stars 44 forks source link

Integer Overflow / Wraparound in _pdfioValueRead #52

Closed k00l-beanz closed 1 year ago

k00l-beanz commented 1 year ago

Describe the bug Hallo :wave:. I have found an integer wraparound bug in pdfio-value.c; _pdfioValueRead; line 388. If v->value.binary.datalen < ivlen, the arithmetic operation will cause the len parameter passed to _pdfioCryptoAESDecrypt to overflow/underflow as size_t is typically and unsigned integer https://en.wikipedia.org/wiki/C_data_types#stddef.h.

There are a few ways to prevent this but the simplest way would be to check if v->value.binary.datalen >= ivlen (granted, I've never been a software engineer so take this with a grain of salt).

To Reproduce The following pdf triggers the bug - bad.pdf

$ sha256sum bad.pdf
983fcdb12d77599bc0632906d5b3303e9ccacb055099c5d58fe2e4da8fe0643d  bad.pdf
$ ./pdfiototext ./bad.pdf 
Segmentation fault (core dumped)

Expected behavior pdfiototext should be able to gracefully exit if this occurs.

System Information:

Additional context Cheers mate :wine_glass:

k00l-beanz commented 1 year ago

Follow up: In pdfio-aes.c; when _pdfioCryptoAESDecrypt is called by _pdfioValueRead, if inbuffer != outbuffer, then a memcpy will execute leading to a heap overflow.

~ k00l_beanz