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

Invalid base64 generation on ARM: extra padding in the middle of the stream! #69

Open
danergo opened this issue Apr 3, 2020 · 3 comments
Labels

Comments

@danergo
Copy link

danergo commented Apr 3, 2020

I'm using this library on an ARM embedded system.

I have a static function which is called from multiple threads in the same time. I'm using boost's mutex to make the important parts atomic:

char Base64Buffer[5500];

// buffer_size is max 4096
int writeOutput(uint8_t *buffer, int buffer_size) {
  mutex.lock();
  base64_encode((const char*)buffer, buffer_size, Base64Buffer, &Base64Length, 0);
  ...
  mutex.unlock();
  return 0;
}

It is working, however sometimes when writeOutput is called from different threads at the same time, base64_encode creates an invalid stream.

Here is a sample invalid base64 (unfortunately I don't have the raw unencoded data):
idqI4bA2ArMAfIkw0a5wmcSAxEpoMJTwOX/aR1BVTTZVRO+Hx4NT4OiOtvrhS8+W2LvbDO0IVh220MGMlZOG2x6MVJeIjeB4fkkNwnwiJ8tOwacCeJhHRCBZFKXvhU33fUw1lWH2dxw2sr9oihvNH3ot3PWrrl/KTC7wCatBcIHtMT6/cuH7dC5EgdwSYdKW4ibqMcjH9hGwT9nPZ92JUbIjmBrzmJI8aoMi8KEpEU61sbuugfxFFwfEtmVO7S5hwYjkormiBzTaDOSTpPi+O5TuTwKUXDb0bebTBVvL8JSicHsX7GVHPQdnAJgWSZ9uT1YU/MsrJYs/srGbvMw30sqGpbJ4/sNAcoVkpb8TFXJcRJZAl2jejm5qbY1TYzP6nz8y7LMMUnUdOwEKIdYu8Kub/Gb6jkxMQULsxc8UjNpS4EkDwYW6/Sm+LE6FIXwSrcIHwM93fBitXDJrbVEN0RYhGl1EB53QXlrUwabERn91tTq3J5i/90AF/KcJwpHCq7Lyrp8JeXuYnON4A84ygEO0p94JNgu1h/CO4iULOuw8GBCpZxFmbHkSIW7ihfPFRfHEV6+8BEoxhj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wqN9S3X4B/a8Z6jCjCgX98REihYMVaemApD7KV6eHT7OIVCBQaZzk4DEjRcy1g/nVJiSHYlbLESnIZyojW8q7ZnCH5stnsaYp1L1V4R1FYJJPqfGEgu+GQq9AWP9NQyDUlesbriOl/kXII5kaeRYj7mzo6MkIdh9m/eBjL0yCUodo/C6Sk9XsTN97Mjkhme+nQutEH00plVLXecBRP28257ybo+sjvZXtl+kG6Ew2/hskGhjpfWC0xg9YBXQE1u2Dr2uswm0uBBfOjSEimmrtIHTjWKmDxCBEQIJo+5i07MqAAABKtB4YD/KYke+rrUl0r9qozDpbbRNTI5mnydC/Nr3aj9y9QPNqLrU5B4GrXmA52IQVQQ0jC2B4dMcmKDZ5qlvDMQCg1WCC3cjX0u8tgqLdB0sKknj8Muo+SIPGBXKm8h7NBq6PHTk/Ir/sANPZxfyx83OaB+IZCsXnGOwg7iO7CeuOQ7T0h3CtBROJDq+J2jkqlqrS19R5SnUEJr6sP6oWf2xo3AldD2fyHwBTQQ6/bZyw==

Could you help me with hunting this?

How can multithread affect base64 generation if it's in an atomic mutex block?

@danergo
Copy link
Author

danergo commented Apr 3, 2020

The problem is that base64_encode put padding in the middle of the stream ('==').

@danergo danergo changed the title Invalid base64 generation on ARM Invalid base64 generation on ARM: extra padding in the middle of the stream! Apr 3, 2020
@aklomp
Copy link
Owner

aklomp commented Apr 10, 2020

Hey, thanks for the report. I'm a bit baffled by this, because like you say, if the library is run inside a mutex, it should be threadsafe. In fact, it should be fairly threadsafe to start with, because the only shared mutable datastructure it uses is the codec selection structure, which is written to once on the first call. All the other functions are reentrant and do not share common data. So that can't really explain the bug.

The padding is written by base64_stream_encode_final, just before base64_encode returns. So I don't see a way in which it can insert those characters into the middle of a stream. What kind of IO is writeOutput doing? It could be that the IO is not properly flushed between threads, causing the output to be interleaved.

@danergo
Copy link
Author

danergo commented Apr 11, 2020

Before unlocking the mutex, writeOutput adds header and trailer to the base64 data, and sends out through a socket. Now if there were incorrect flushing between threads, it shall be "all-or-nothing" fashion because of the mutex.

I'm not seeing this:
HEADER#(BASE64_1)#TRAILER HEADER#(BASE64_1)(BASE64_2)#TRAILER

But I'm seeing this:
HEADER#(BASE64_1)#TRAILER HEADER#(BASE64_2)(BASE64_3)#TRAILER

This can be easily worked around by splitting the stream at the decoder (or if there is no padding in the middle no need to split at all).

My only concern here is buffer size: I've optimized Base64Buffer to fit 4096 bytes.
Obviously it wont be able handle more data and could cause a segfault.

@aklomp aklomp added the bug label Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants