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

buffer overrun in base64Encode for small buffers #9

Open
alex-zadara opened this issue Jan 22, 2015 · 0 comments
Open

buffer overrun in base64Encode for small buffers #9

alex-zadara opened this issue Jan 22, 2015 · 0 comments

Comments

@alex-zadara
Copy link

base64Encode expects an output buffer whose size is ((4 * (inLen + 1)) / 3) bytes, as per comment. But for the input buffer of size 16, it overruns it. For the input buffer of size 16, the output buffer should be: ((4 * (16 + 1)) / 3) = 22 bytes. But in this case, base64Encode returns output length of 24 bytes, overrunning the input buffer. The following C code demonstrates it:

#define SRC_LEN 16
#define B64_LEN(n) (((n) + 1) * 4) / 3

int main(void)
{
    unsigned char in_buff[SRC_LEN] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    const unsigned int b64len = B64_LEN(SRC_LEN);
    char b64[B64_LEN(SRC_LEN) + 16] = {'\0'};
    unsigned int outLen = 0;

    printf("b64len = %u\n", b64len);
    printf("Before encoding b64[%u]=0x%X\n", b64len, b64[b64len]);
    outLen = base64Encode(in_buff, 16, b64);
    printf("After encoding outLen=%u, b64[%u]=0x%X\n", outLen, b64len, b64[b64len]);
    return 0;  
}

The output is:

b64len = 22
Before encoding b64[22]=0x0
After encoding outLen=24, b64[22]=0x3D

base64Encode should only touch bytes from b64[0] to b64[21] (because the required length is supposed to be 22). But it clearly touches b64[22] as well, thus overrunning the output buffer (should its length was as per the comment).

Note that for larger input sizes (like 20), this problem does not happen:

b64len = 28
Before encoding b64[28]=0x0
After encoding outLen=24, b64[28]=0x0

Here base64Encode uses only 24 bytes out of 28 bytes.

benmcclelland added a commit to benmcclelland/libs3 that referenced this issue Mar 1, 2017
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

1 participant