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

[PM-5693] CryptoService using memfd_secret on Linux #7

Open
wants to merge 57 commits into
base: main
Choose a base branch
from

Conversation

dani-garcia
Copy link
Member

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-5693

📔 Objective

Migrated from bitwarden/sdk-sm#979

I've been looking into using memfd_secret to protect keys in memory on Linux.

The memfd_secret API is similar to malloc in that we get some memory range allocated for us, but this memory is only visible to the process that has access to the file descriptor, which should protect us from any memory dumping from anything other than a kernel driver.

https://man.archlinux.org/man/memfd_secret.2.en

Using this requires changing how we store keys, so this is the perfect moment to go through removing the raw keys from the API surface.

Changes

  • Added a KeyRef trait and SymmetricKeyRef/AsymmetricKeyRef subtraits to represent the key types. Also a small macro to quickly implement them. KeyRef implementations are user defined types that implements Eq+Hash+Copy, and don't contain any key material
  • KeyEncryptable/KeyDecryptable are now Decryptable/Encryptable, and instead of taking the key as parameter, they take a CryptoServiceContext and a KeyRef. This way keys are never exposed to the clients.
  • KeyLocator trait is replaced by UsesKey. This trait only returns the KeyRef of the key required to decrypt it, instead of fetching the key itself.
  • Created a KeyStore trait, with some simple CRUD methods. We have two implementations, one based on memfd_secret for Linux, and another one based on a Rust boxed slice, that also applies mlock if possible.
  • Because both mlock and memfd_secret apply their protection over a specified memory area, we need a Rust data structure that is laid out linearly and also doesn't reallocate by itself. Also because memfd_secret allocates the memory for us, we can't use a std type like Vec (reason is the Vec must be allocated by the Rust allocator [ref]). This basically only leaves us with a Rust slice, on top of which we'd need to implement insert/get/delete. To allow for reuse and easier testing I've created SliceKeyStore, which implements the CRUD methods from the KeyStore trait on top of a plain slice. Then the actual mlock and memfd_secret implementations just need to implement a trait casting their data to a slice. The data is stored as a slice of Option<(KeyRef, KeyMaterial)>, and the keys are unique and sorted in the slice for easier lookups.
  • Created CryptoService, which contains the KeyStores and some encrypt/decrypt utility functions. From a CryptoService you can also initialize a CryptoServiceContext
  • A CryptoServiceContext contains a read only view of the keys inside the CryptoService, plus a read-write ephemeral key store, for use by Decryptable/Encryptable implementations when they need to temporarily store some keys (Cipher keys, attachment keys, send keys...).

Migrated the codebase to use these changes in a separate PR: bitwarden/sdk-sm#1117

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation
    team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed
    issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Copy link
Contributor

github-actions bot commented Oct 22, 2024

Logo
Checkmarx One – Scan Summary & Detailsc50a09d3-37c7-4d91-9a9c-1923beada9ae

No New Or Fixed Issues Found

Copy link

codecov bot commented Oct 22, 2024

Codecov Report

Attention: Patch coverage is 78.51662% with 252 lines in your changes missing coverage. Please review.

Project coverage is 65.55%. Comparing base (a89b588) to head (275574f).

Files with missing lines Patch % Lines
crates/bitwarden-crypto/src/store/context.rs 48.43% 148 Missing ⚠️
crates/bitwarden-crypto/src/keys/encryptable.rs 27.88% 75 Missing ⚠️
crates/bitwarden-crypto/src/store/mod.rs 85.23% 22 Missing ⚠️
...itwarden-crypto/src/store/backend/slice_backend.rs 98.67% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main       #7      +/-   ##
==========================================
+ Coverage   64.43%   65.55%   +1.12%     
==========================================
  Files         188      195       +7     
  Lines       13832    15005    +1173     
==========================================
+ Hits         8912     9836     +924     
- Misses       4920     5169     +249     

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

Copy link
Member

@Hinton Hinton left a comment

Choose a reason for hiding this comment

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

I've taken an initial look at this. I think the general concept is solid, there are some potential areas for improvements.

However the biggest pieces would be:

  1. Documentation, we need to ensure we write solid documentation on the difference areas and ensure we are clear about how it's intended to be used.
  2. Improve test coverage, since we're dealing with cryptography we want to be really sure nothing breaks in the future.

crates/bitwarden-crypto/Cargo.toml Outdated Show resolved Hide resolved
crates/bitwarden-crypto/src/service/context.rs Outdated Show resolved Hide resolved
crates/bitwarden-crypto/src/error.rs Show resolved Hide resolved
crates/bitwarden-crypto/src/service/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Do we have any documentation for why we need to implement our own slice vs using built in?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added some doc comments here:

/// The reason why we're not using a Rust collection like `Vec` or `HashMap` is that those types
/// expect their memory to be allocated by Rust, and they will deallocate/reallocate it as needed.
/// That will not work for our usecases where we want to have control over allocations/deallocations
/// and where some of our strategies rely on using system-allocated secure memory for the storage,
/// like the Linux-only `memfd_secret` API.

crates/bitwarden-crypto/src/service/context.rs Outdated Show resolved Hide resolved
crates/bitwarden-crypto/src/service/mod.rs Outdated Show resolved Hide resolved
crates/bitwarden-crypto/src/service/mod.rs Outdated Show resolved Hide resolved
use key_store::KeyStore;

#[derive(Clone)]
pub struct CryptoService<SymmKeyRef: SymmetricKeyRef, AsymmKeyRef: AsymmetricKeyRef> {
Copy link
Member

Choose a reason for hiding this comment

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

question: We define a Keys struct which essentially acts as the KeyStore. Would it be a cleaner interface to define a KeyStore trait which both CryptoService and Keys implements, and CryptoService simply forwards calls to the inner store.

Copy link
Member Author

Choose a reason for hiding this comment

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

Initially I think I've tried to do something like that, but it was hard to get a good API when using a trait here and then also trying to be generic over key type / Encryptable implementation.

@dani-garcia
Copy link
Member Author

dani-garcia commented Dec 12, 2024

I've made a few changes while documenting this a bit, plus some of the review suggestions are included here as well:

  • memsec is disabled on windows for now, and we're not using my fork
  • There's been a few renames:
    • CryptoService -> KeyStore. to move away from the service name
    • Keys -> KeyStoreInner. This only exists to be wrapped with the RwLock, and everything in KeyStore is pretty much delegated to it, so it seems like a better name.
    • CryptoServiceContext -> KeyStoreContext. To match the service rename
    • KeyStore -> StoreBackend. To signify there are different backends per platform
    • LinuxMemfdSecretKeyStore -> LinuxMemfdSecretBackend and RustStore -> RustBackend, to match the trait above.
  • Changed the generic params of the KeyStore to be a single KeyRefs, which makes the generic types everywhere much less verbose (Instead of having to do <SymmetricKeyRef, AsymmetricKeyRef> everywhere you can do <KeyRefs>.
  • Removed the generic type over KeyStoreContext global keys, and moved it to an enum.

The next steps for the crypto refactor after this are:

  • Move all the Encryptable implementations to the new trait: [PM-5693] Migrate SDK to CryptoService #8
  • Migrate existing APIs that deal with SymmetricCryptoKey/AsymmetricCryptoKey directly to deal with references instead (init_crypto functions, MasterKey, UserKey, PinKey, Key fingerprint etc).
  • Remove uses of KeyStoreContext::get_key|set_key from the non crypto crates, which shouldn't have any uses left after the previous step (maybe?).
  • Once no other uses of SymmetricCryptoKey/AsymmetricCryptoKey are left, make them private to bitwarden-crypto
  • Remove the now unnecessary boxing around SymmetricCryptoKey/AsymmetricCryptoKey

Some future API improvements:

  • Improve the design around local keys, instead of accepting any user type, create an opaque token and return it in the functions, so the only way for a user to get a valid local key ref is to obtain it from a function.
  • Currently the context implementation can be read-only or read-write over the global keys, depending on how it's initialized, and it's behavior changes slightly in both cases. I think it might be a better design to instead do something like SQL transactions. Contexts are always read-only, but they have a commit function that consumes them and applies the changes to the global KeyStore

@Hinton Hinton self-requested a review January 13, 2025 11:28
Copy link
Member

@Hinton Hinton left a comment

Choose a reason for hiding this comment

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

Still need to review the rust backend.

@@ -50,6 +50,9 @@ wasm-bindgen = { workspace = true, optional = true }
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }
zeroizing-alloc = ">=0.1.0, <0.2"

[target.'cfg(all(not(target_arch = "wasm32"), not(windows)))'.dependencies]
memsec = { version = "0.7.0", features = ["alloc_ext"] }
Copy link
Member

Choose a reason for hiding this comment

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

Did we explore the alternative crate (memfd) which has more downloads?

@@ -50,6 +50,9 @@ wasm-bindgen = { workspace = true, optional = true }
zeroize = { version = ">=1.7.0, <2.0", features = ["derive", "aarch64"] }
zeroizing-alloc = ">=0.1.0, <0.2"

[target.'cfg(all(not(target_arch = "wasm32"), not(windows)))'.dependencies]
Copy link
Member

Choose a reason for hiding this comment

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

Do we use memsec on mac?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the mlock/munlock APIs work on any unix system, including Mac and BSDs. memfd_secret is Linux only

@@ -23,6 +23,10 @@ pub enum CryptoError {
MissingKey(Uuid),
#[error("The item was missing a required field: {0}")]
MissingField(&'static str),
#[error("Missing Key for Ref. {0}")]
MissingKey2(String),
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this to MissingKeyRef? Put it under MissingKey and add a comment to rename this once we refactor the usage.

@@ -80,6 +80,8 @@ mod util;
pub use util::{generate_random_alphanumeric, generate_random_bytes, pbkdf2};
mod wordlist;
pub use wordlist::EFF_LONG_WORD_LIST;
pub mod store;
Copy link
Member

Choose a reason for hiding this comment

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

question: Do we want to use pub mod here?

@@ -1,5 +1,9 @@
mod key_encryptable;
pub use key_encryptable::{CryptoKey, KeyContainer, KeyDecryptable, KeyEncryptable, LocateKey};
mod encryptable;
pub use encryptable::{Decryptable, Encryptable, UsesKey};
pub mod key_ref;
Copy link
Member

Choose a reason for hiding this comment

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

question: keys isn't public so key_ref should probably not be public either?

Suggested change
pub mod key_ref;
pub key_ref;


// For Send+Sync to be safe, we need to ensure that the memory is only accessed mutably from one
// thread. To do this, we have to make sure that any funcion in `MemfdSecretImplKeyData` that
// accesses the pointer mutably is defined as &mut self, and that the pointer is never copied or
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// accesses the pointer mutably is defined as &mut self, and that the pointer is never copied or
// accesses the pointer mutably is defined as `&mut self`, and that the pointer is never copied or

Comment on lines +51 to +52
let ptr: NonNull<[u8]> = memsec::memfd_secret_sized(capacity * entry_size)
.expect("memfd_secret_sized failed");
Copy link
Member

Choose a reason for hiding this comment

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

When would memfd fail? If we run out of memory?

Copy link
Member Author

@dani-garcia dani-garcia Jan 13, 2025

Choose a reason for hiding this comment

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

According to the docs (https://man7.org/linux/man-pages/man2/memfd_secret.2.html):

       EINVAL flags included unknown bits.

       EMFILE The per-process limit on the number of open file
              descriptors has been reached.

       EMFILE The system-wide limit on the total number of open files
              has been reached.

       ENOMEM There was insufficient memory to create a new anonymous
              file.

       ENOSYS memfd_secret() is not implemented on this architecture, or
              has not been enabled on the kernel command-line with
              secretmem_enable=1.

So when the system is out of memory or the process has reached the file descriptor limit, or if the feature is disabled and not supported (but we check for that the first time)

Comment on lines +54 to +61
// Initialize the array with Nones using MaybeUninit
let uninit_slice: &mut [MaybeUninit<_>] = std::slice::from_raw_parts_mut(
ptr.as_ptr() as *mut MaybeUninit<Option<(Key, Key::KeyValue)>>,
capacity,
);
for elem in uninit_slice {
elem.write(None);
}
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +69 to +70
// SAFETY: The pointer is valid and points to a valid slice of the correct size.
// This function is &self so it only takes a immutable *const pointer.
Copy link
Member

Choose a reason for hiding this comment

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

Can you elaborate on how we know it's valid?

return Box::new(key_store);
}

Box::new(rust::RustBackend::new().expect("RustKeyStore should always be available"))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Box::new(rust::RustBackend::new().expect("RustKeyStore should always be available"))
Box::new(rust::RustBackend::new().expect("RustKeyStore to always be available"))

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.

2 participants