-
Notifications
You must be signed in to change notification settings - Fork 144
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 Secure Session serialization format #658
Conversation
secure_session_save() saves short serialized Secure Session state. That is, it writes "is_client" as 4-byte value while its size in SESSION_CTX_SERIZALIZED_SIZE is computed as sizeof(bool) which is 1 byte on most platforms. This causes 3 least significant bytes of "in_seq" being missing from the serialized data. Correct size of Secure Session state is 60 bytes, we report only 57. This issue is not detected by unit tests of JavaThemis -- the only high-level wrapper supporting this interface -- which works only because the allocated memory for the output array is slightly bigger than requested 57 bytes, extra 3 bytes written past-the-end remain there in RAM and the unit test generally works, if the data stays where it is. However, sometimes garbage collection occurs at the right moment and the issue manifests itself as a failing Secure Session test. Now the output size is reported correctly and all Secure Session data is written within the allocated bounds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome comments
While we're here and touching this code anyway, let's correct the name of the constant too.
(sizeof((ctx)->session_id) + sizeof((ctx)->is_client) + sizeof((ctx)->session_master_key) \ | ||
+ sizeof((ctx)->out_seq) + sizeof((ctx)->in_seq)) | ||
/* | ||
* Data layout of serialized Secure Session state looks like this: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woa, that looks amazing!
*/ | ||
|
||
#define SESSION_CTX_SERIALIZED_SIZE \ | ||
(2 * sizeof(uint32_t) + SESSION_MASTER_KEY_LENGTH + 2 * sizeof(uint32_t)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bloody magic
secure_session_save() saves short serialized Secure Session state.
That is, it writes
is_client
as 4-byte value while its size in SESSION_CTX_SERIZALIZED_SIZE is computed assizeof(bool)
which is 1 byte on most platforms. This causes 3 least significant bytes ofin_seq
being missing from the serialized data.Correct size of Secure Session state is 60 bytes, we report only 57.
This issue is not detected by unit tests of JavaThemis—the only high-level wrapper supporting this interface—which works only because the allocated memory for the output array is slightly bigger than requested 57 bytes, extra 3 bytes written past-the-end remain there in RAM and the unit test generally works, if the data stays where it is. However, sometimes garbage collection occurs at the right moment and the issue manifests itself as a failing Secure Session test.
Now the output size is reported correctly and all Secure Session data is written within the allocated bounds.
Checklist