matrix-org / matrix-org/matrix-rust-sdk

Key import race causing a worse key to be saved in place of a better version.

Open
#4,227 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-Encryption
Dominant language
Rust
Stars
2.3k
Forks
500
Avg merge
1d 16h
Merged PRs (30d)
106

Description

As seen in this rageshake https://rageshakes.element.io/api/listing/2024-11-06/130841-YOSPUGGB/console.2024-11-06-12.log.gz

There is slightly late room key, so there is an UTD and the sdk tries to query the key from key storage.
And just after the room key arrives down the sync.

We then have two concurent actions trying to import the key

The race is that they both try to see if there is an existing key in order to see if the new one is better or worse

https://github.com/matrix-org/matrix-rust-sdk/blob/8d07f36247e92c5cfae4c8d0b60f6ea56743bcdf/crates/matrix-sdk-crypto/src/store/mod.rs#L1748-L1756

and

https://github.com/matrix-org/matrix-rust-sdk/blob/8d07f36247e92c5cfae4c8d0b60f6ea56743bcdf/crates/matrix-sdk-crypto/src/machine/mod.rs#L891-L906

But they both think they are the first and that there is no matching key in store.

Therefore the first one to commit to the store wins. By-pasing the better/worse check.
The late key is better than the key from key storage, because it is safe (no gray shield)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with import_room_keys in crates/matrix-sdk-crypto/src/store/mod.rs and handle_key in crates/matrix-sdk-crypto/src/machine/mod.rs, focusing on their existing-key checks and store commits. Reproduce or trace concurrent imports from key storage and sync, then verify that the better late room key is retained rather than whichever import commits first.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cryptography, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.