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: fix backed-up keys being re-backed-up #3448

Merged
merged 8 commits into from
May 23, 2024
Merged

Conversation

richvdh
Copy link
Member

@richvdh richvdh commented May 23, 2024

This PR aims to fix #3447.

The first thing to do is to make sure that a backup version is stored for any keys that are imported from backup. As a quick fix, which doesn't break backwards compatibility, we therefore update BackupMachine::import_backed_up_room_keys to pass through the current backup version to the store.

However, that is very much a bodge: we have no guarantee that is the correct version. A better fix is to have the application pass through the version that it got the keys from. We therefore deprecate the inherently-broken BackupMachine::import_backed_up_room_keys, and chase the changes through to the SDK and FFI layers.

Should be reviewable commit-by-commit. [I'd also be open to splitting this into two PRs: one for a quick fix, and one for the deprecation stuff. LMK what you think]

@richvdh richvdh requested a review from a team as a code owner May 23, 2024 10:49
@richvdh richvdh requested review from bnjbvr and removed request for a team May 23, 2024 10:49
@poljar
Copy link
Contributor

poljar commented May 23, 2024

Should be reviewable commit-by-commit. [I'd also be open to splitting this into two PRs: one for a quick fix, and one for the deprecation stuff. LMK what you think]

No need, I think it's manageable.

@poljar poljar requested review from poljar and removed request for bnjbvr May 23, 2024 10:56
Copy link

codecov bot commented May 23, 2024

Codecov Report

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

Project coverage is 83.29%. Comparing base (7fb57ea) to head (0777aa6).

Files Patch % Lines
crates/matrix-sdk-crypto/src/store/memorystore.rs 92.30% 1 Missing ⚠️
crates/matrix-sdk-sqlite/src/crypto_store.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3448      +/-   ##
==========================================
+ Coverage   83.27%   83.29%   +0.02%     
==========================================
  Files         247      247              
  Lines       25091    25111      +20     
==========================================
+ Hits        20895    20917      +22     
+ Misses       4196     4194       -2     

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

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

Makes sense, I disagree a bit on the deprecations but don't care enough to argue.

I left some small nits, feel free to merge without further review.

@@ -619,7 +619,15 @@ impl BackupMachine {
}
}

self.store.import_room_keys(decrypted_room_keys, true, progress_listener).await
// This method is a bit flawed: we have no real idea which backup version these
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you annotate this with a TODO: or FIXME: , so we can more easily find this fact.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can, but this is the main reason I deprecated the method. The real fix is to remove it.

@@ -1823,7 +1823,11 @@ impl OlmMachine {
from_backup: bool,
progress_listener: impl Fn(usize, usize),
) -> StoreResult<RoomKeyImportResult> {
self.store().import_room_keys(exported_keys, from_backup, progress_listener).await
let backup_version =
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can just remove this method. We had a 0.7.x release.

Copy link
Member Author

Choose a reason for hiding this comment

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

Even better! Will remove it.

When we add a batch of inbound group sessions to the store, if they came from a
backup, we need to record which backup version they came from.
`CryptoStore::save_changes` doesn't give us an easy way to set the backup
version (we could add it to the `Changes` struct, but ugh).

So, we add a new method `save_inbound_group_sessions` which takes the backup
version as a separate param. This works fine, because whenever we import
sessions there are no other changes to store.

The new method isn't used outside of tests yet -- that comes in a follow-up
commit.
When we are importing a batch of room keys, use the newly-added
`CryptoStore::save_inbound_group_sessions` method instead of
`CryptoStore::save_changes`.

To do this, we need to pass the backup version into `Store::import_room_keys`
instead of just `from_backup` flag.
This whole method is a bit broken. It assumes that, when we did an import from
backup, the backup that they came from is the same as the "current" version.

We *could* add another argument, but to be honest I find the whole method a bit
pointless. It's not particularly tied to the `BackupMachine` abstraction. Let's
instead expose `Store::import_room_keys` and
`ExportedRooomKey::from_backed_up_room_key` publicly, and tell callers to use
that directly.
... and use its replacement, `Store::import_room_keys`
... and expose a new method `import_room_keys_from_backup` which does the right
thing.
@richvdh richvdh changed the title Fix backed-up keys being re-backed-up Crypto: fix backed-up keys being re-backed-up May 23, 2024
@richvdh richvdh merged commit dcc32da into main May 23, 2024
38 checks passed
@richvdh richvdh deleted the rav/fix_backup_import branch May 23, 2024 15:20
richvdh added a commit that referenced this pull request May 23, 2024
… stream

#3448 added a new method
`save_inbound_group_sessions` to `CrytoStore`, but forgot to wire it up to the
`room_keys_received` stream.
richvdh added a commit to matrix-org/matrix-rust-sdk-crypto-wasm that referenced this pull request May 24, 2024
Bump to a version of the rust SDK that includes matrix-org/matrix-rust-sdk#3448 and matrix-org/matrix-rust-sdk#3456, to fix matrix-org/matrix-rust-sdk#3447.

Also, pass the backup version into import_backed_up_room_keys to avoid using the deprecated method on BackupMachine.
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.

When using the in-memory crypto store, keys restored from backup are re-uploaded to backup
2 participants