Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic on 256 bits RC4 keys #230

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

Orycterope
Copy link
Contributor

Found this bug while fuzzing the crate.

The spec says that in the ecryption dictonary, /Length is the length of the key, and should be between 40 to 128 bits. However, if we specify 256 the parser still accepts it but panics in Decoder::decrypt() while performing the b) step.

The b) step is implemented by truncating the key length to a maximum of 128 bits (16 bytes) before concatenating the object number and generation of the string being decrypted.

However, while computing the size of n, Decoder::decode() does not truncate the key size to 16, and this leads to an out of bound panic.

Note that I can't find the reason for this trucation to 16 bytes in the b) step. The spec does require truncating the key to 16 bytes, but that's in the d) step which is where we truncate the resulting hash.

Maybe a better fix would be to reject a /Length of 256 bits in the parser instead.

Found this bug while fuzzing the crate.

The spec says that in the ecryption dictonary, /Length is the length of the
key, and should be between 40 to 128 bits. However, if we specify 256 the
parser still accepts it but panics in Decoder::decrypt() while performing
the b) step.

The b) step is implemented by truncating the key length to a maximum of 128
bits (16 bytes) before concatenating the object number and generation of the
string being decrypted.

However, while computing the size of n, Decoder::decode() does not truncate the
key size to 16, and this leads to an out of bound panic.

Note that I can't find the reason for this trucation to 16 bytes in the b) step.
The spec does require truncating the key to 16 bytes, but that's in the d)
step which is where we truncate the resulting hash.

Maybe a better fix would be to reject a /Length of 256 bits in the parser instead.
@Orycterope
Copy link
Contributor Author

Highly relates to #231

@s3bk
Copy link
Contributor

s3bk commented Aug 2, 2024

I think not limiting the length and then doing the d) step correct might be the better solution?

@s3bk
Copy link
Contributor

s3bk commented Aug 2, 2024

Are there any valid pdfs with 256bit keys?

@s3bk
Copy link
Contributor

s3bk commented Aug 2, 2024

This fixes a panic. If you have a better solution that can be merged later.

@s3bk s3bk merged commit a40dbef into pdf-rs:master Aug 2, 2024
@Orycterope
Copy link
Contributor Author

Are there any valid pdfs with 256bit keys?

No idea 🤷‍♀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants