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

base64_encode_expected_len() returns value that is not large enough #4773

Closed
jackjansen opened this issue May 28, 2018 · 3 comments
Closed
Milestone

Comments

@jackjansen
Copy link

base64_encode_expected_len() (from libb64/cencode.h) returns a number that is too small if it is used to allocate a buffer to subsequently pass to base64_encode_chars().

The reason is that the simple base64_encode_chars() includes a newline every 72 characters or so, and these newlines are not catered for in base64_encode_expected_len().

The result of this is that if you try to encode a binary blob larger than about 60 characters you will write past the end of your malloced space.

@d-a-v
Copy link
Collaborator

d-a-v commented May 29, 2018

Can you verify if this change in cores/esp8266/libb64/cencode.h fixes the buffer overflow ?

#define base64_encode_expected_len_nonewlines(n) ((((4 * n) / 3) + 3) & ~3)
#define base64_encode_expected_len(n) (base64_encode_expected_len_nonewlines(n) + ((n / 18) + 1))

@jackjansen
Copy link
Author

The returned number is now too high. For example, in my test case code the real encoded length will be 823 bytes, but base64_encode_expected_len() returns 846.

I've changed the number 18 to 54 (being CHARS_PER_LINE divided by 4, the multiplied by 3) and then 824 is returned, which is what is expected.

@devyte devyte modified the milestones: 2.4.2, 2.5.0 Jun 6, 2018
@devyte
Copy link
Collaborator

devyte commented Jun 8, 2018

Relevant PR is merged, closing.

@devyte devyte closed this as completed Jun 8, 2018
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

No branches or pull requests

3 participants