Skip to content

Conversation

multisme
Copy link
Contributor

@multisme multisme commented Jul 31, 2025

  • Public API changes documented in changelogs (optional)

Signed-off-by: multi [email protected]

Fixes #3697

@multisme multisme requested a review from a team as a code owner July 31, 2025 17:27
@multisme multisme requested review from stefanceriu and removed request for a team July 31, 2025 17:27
@poljar poljar requested review from poljar and removed request for stefanceriu August 1, 2025 07:52
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.

Not quite there yet I'm afraid.

Copy link

codspeed-hq bot commented Aug 2, 2025

CodSpeed Performance Report

Merging #5472 will not alter performance

Comparing multisme:refactor/improve_performance_sqlite_db (eca9e89) with main (b6433de)

Summary

✅ 37 untouched benchmarks

Copy link

codecov bot commented Aug 2, 2025

Codecov Report

❌ Patch coverage is 64.47368% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.61%. Comparing base (b6433de) to head (eca9e89).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk-sqlite/src/crypto_store.rs 38.46% 6 Missing and 2 partials ⚠️
crates/matrix-sdk-sqlite/src/state_store.rs 38.46% 6 Missing and 2 partials ⚠️
crates/matrix-sdk-sqlite/src/event_cache_store.rs 22.22% 6 Missing and 1 partial ⚠️
crates/matrix-sdk-sqlite/src/utils.rs 55.55% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5472      +/-   ##
==========================================
- Coverage   88.63%   88.61%   -0.02%     
==========================================
  Files         341      341              
  Lines       95138    95191      +53     
  Branches    95138    95191      +53     
==========================================
+ Hits        84322    84354      +32     
- Misses       6628     6650      +22     
+ Partials     4188     4187       -1     

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

@multisme
Copy link
Contributor Author

multisme commented Aug 2, 2025

Hey I'm unsure about the get_or_create_store_cipher could you point me in the right direction?

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.

Hey I'm unsure about the get_or_create_store_cipher could you point me in the right direction?

What about get_or_create_store_cipher() looks reasonable except that the secrets are copied and not zeroized.

You need to either not copy them or you need to zeroize them using the zeroize crate.

@multisme multisme force-pushed the refactor/improve_performance_sqlite_db branch from 065ec6e to 9718b79 Compare August 7, 2025 20:33
@multisme
Copy link
Contributor Author

multisme commented Aug 8, 2025

@poljar Hey I think I managed the implementation, could you provide me with feedback please?

@multisme multisme requested a review from poljar August 21, 2025 15:22
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.

Sorry for the late reply, I was unavailable the past two weeks. This seems almost good to go.

Though we have a tricky problem with hidden copies of the secret key material.

@@ -6,6 +6,14 @@ All notable changes to this project will be documented in this file.

## [Unreleased] - ReleaseDate

### Features
- Implement a new constructrot that allow to open SqliteCryptoStore with a Key
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Implement a new constructrot that allow to open SqliteCryptoStore with a Key
- Implement a new constructor that allows to open the SqliteCryptoStore with a cryptographic key.

Comment on lines +49 to +52
#[zeroize]
Key([u8; 32]),
#[zeroize]
PassPhrase(String),
Copy link
Contributor

Choose a reason for hiding this comment

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

The enum variants need to be documented.

@@ -43,13 +44,21 @@ pub use self::state_store::{SqliteStateStore, DATABASE_NAME as STATE_STORE_DATAB
#[cfg(test)]
matrix_sdk_test_utils::init_tracing_for_tests!();

#[derive(Clone, Debug, Eq, PartialEq, Zeroize)]
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 should derive ZeroizeOnDrop here so we don't need the manual zeroize() calls.

#[derive(Clone, Debug, Eq, PartialEq, Zeroize)]
pub enum Secret {
#[zeroize]
Key([u8; 32]),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be inside a Box or inside a Zeroizing struct so we avoid unintended copies due to moves being a memcpy call in Rust land. More info here: https://benma.github.io/2020/10/16/rust-zeroize-move.html.

In short, every time you pass the Secret to a function a memcpy call will copy the contents of the array.

If a Box is used, the pointer is instead copied.

#[zeroize]
Key([u8; 32]),
#[zeroize]
PassPhrase(String),
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, this will produce unintended copies, Zeroizing<String> would make sense.

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.

Improve performance of encrypted sqlite DB
2 participants