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 Secure Session serialization format #658

Merged
merged 2 commits into from
Jun 16, 2020

Conversation

ilammy
Copy link
Collaborator

@ilammy ilammy commented Jun 16, 2020

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.

Checklist

  • Change is covered by automated tests
  • The coding guidelines are followed
  • Public API has proper documentation
  • Changelog is updated (in case of notable or breaking changes)

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.
@ilammy ilammy added bug core Themis Core written in C, its packages W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API labels Jun 16, 2020
Copy link
Collaborator

@Lagovas Lagovas left a 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:
Copy link
Contributor

@vixentael vixentael Jun 16, 2020

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bloody magic

@ilammy ilammy merged commit e83ec23 into cossacklabs:master Jun 16, 2020
@ilammy ilammy deleted the session-load branch June 16, 2020 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug core Themis Core written in C, its packages W-JavaThemis ☕ Wrapper: Java, Java and Kotlin API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants