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

crypto: Rotate fallback keys in a time-based manner #3151

Merged
merged 4 commits into from
Mar 19, 2024

Conversation

poljar
Copy link
Contributor

@poljar poljar commented Feb 21, 2024

Fallback keys until now have been rotated on the basis that the homeserver tells us that a fallback key has been used.

Now this leads to various problems if the server tells us too often that it has been used, i.e. if we receive the same sync response too often. It leaves us also open to the homeserver being dishonest and never telling us that the fallback key has been used.

Let's avoid all these problems by just rotating the fallback key in a time-based manner.

This fixes #3127.

  • Public API changes documented in changelogs (optional)

@poljar poljar requested a review from a team as a code owner February 21, 2024 15:27
@poljar poljar requested review from andybalaam and removed request for a team February 21, 2024 15:27
Fallback keys until now have been rotated on the basis that the
homeserver tells us that a fallback key has been used.

Now this leads to various problems if the server tells us too often that
it has been used, i.e. if we receive the same sync response too often.
It leaves us also open to the homeserver being dishonest and never
telling us that the fallback key has been used.

Let's avoid all these problems by just rotating the fallback key in a
time-based manner.
@poljar poljar force-pushed the poljar/fallback-key-time-based-rotation branch from d51f5b9 to aeec0c9 Compare February 21, 2024 15:28
@poljar poljar requested a review from dkasak February 21, 2024 15:29
Copy link

codecov bot commented Feb 21, 2024

Codecov Report

Attention: Patch coverage is 87.50000% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 83.85%. Comparing base (cced512) to head (f2200b2).
Report is 120 commits behind head on main.

Files Patch % Lines
crates/matrix-sdk-crypto/src/olm/account.rs 86.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3151      +/-   ##
==========================================
- Coverage   83.85%   83.85%   -0.01%     
==========================================
  Files         231      231              
  Lines       23865    23877      +12     
==========================================
+ Hits        20011    20021      +10     
- Misses       3854     3856       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

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

Just a few comments

crates/matrix-sdk-crypto/src/olm/account.rs Show resolved Hide resolved
crates/matrix-sdk-crypto/CHANGELOG.md Show resolved Hide resolved
crates/matrix-sdk-crypto/src/olm/account.rs Outdated Show resolved Hide resolved
crates/matrix-sdk-crypto/src/olm/account.rs Outdated Show resolved Hide resolved
@andybalaam
Copy link
Member

Looks good to me, from a code point of view i.e. I think it does what the PR description says it does. Not reviewing whether it's the right thing to do, and hoping @dkasak will do that :-)

Also made some optional overly-picking punctuation suggestions.

Co-authored-by: Andy Balaam <andy.balaam@matrix.org>
Signed-off-by: Damir Jelić <poljar@termina.org.uk>
///
/// [1]: https://signal.org/docs/specifications/x3dh/#publishing-keys
fn fallback_key_expired(&self) -> bool {
const FALLBACK_KEY_MAX_AGE: Duration = Duration::from_secs(3600 * 24 * 7);
Copy link
Member

Choose a reason for hiding this comment

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

Please can this be configurable by the caller, as for complement-crypto tests I'd want to set this to be a few seconds to ensure keys are being cycled, which means it needs to be visible to the FFI bindings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is something that really shouldn't be configurable, can't you fake time in complement?

Alternatively we can add a hidden compile-time feature flag, that should work as well since you compile the bindings yourself, right?

Copy link
Member

Choose a reason for hiding this comment

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

I might be able to use something like https://github.com/wolfcw/libfaketime but it'll be very brittle and likely to break randomly.

A compile-time feature flag works for me, as that can then be used for WASM bindings as well.

Copy link
Member

Choose a reason for hiding this comment

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

There are other places where this would be nice e.g we cannot test rotation_period_ms because rust SDK has a lower bound of 1 hour.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for allowing to provide config either at runtime or compile time - the integration tests we are writing use real homeservers in separate processes, so faking out the time at this level is difficult.

It's also arguable, at least in the case of rotation_period_ms that different use-cases might want different limits on these values, so config would be helpful for non-test situations. (E.g. a password manager might have different security requirements from a chat app).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can set the rotation_period_ms with the m.room.encryption event, it's just that we clamp the values so you don't get stuck in a loop where by the time you share the room key it already expired.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we are setting the rotation_period_ms with the m.room.encryption event but running into the clamp because we want our test to run in <1hr

Copy link
Member

@dkasak dkasak left a comment

Choose a reason for hiding this comment

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

Looks good to me too. One spot could use a comment for clarity perhaps.

crates/matrix-sdk-crypto/src/olm/account.rs Show resolved Hide resolved
@poljar poljar merged commit c59465c into main Mar 19, 2024
35 checks passed
@poljar poljar deleted the poljar/fallback-key-time-based-rotation branch March 19, 2024 12:16
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

Successfully merging this pull request may close these issues.

Fallback keys are cycled too quickly
5 participants