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-5728] Enable Zeroize support for keys and EncStrings #515

Merged
merged 12 commits into from
Jan 25, 2024

Conversation

dani-garcia
Copy link
Member

Type of change

- [ ] Bug fix
- [x] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

  • Implemented Zeroize support for the encryption keys and the encStrings.
  • Boxed the internals of the encryption keys to avoid Rust from moving them around on the stack.
  • Changed some functions that passed around owned copies of the GenericArrays to pass references instead. GenericArray implements Copy, so passing them around by value only means more stack copies if the compiler can't optimize them out.
  • Changed the from impls for SymmCryptoKey to take mut slices so we can zeroize them after load.

@dani-garcia dani-garcia requested a review from Hinton January 19, 2024 13:25
Copy link

codecov bot commented Jan 19, 2024

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (af880e6) 51.51% compared to head (9fbb514) 51.69%.

Files Patch % Lines
...bitwarden-crypto/src/keys/asymmetric_crypto_key.rs 50.00% 4 Missing ⚠️
crates/bitwarden/src/client/encryption_settings.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #515      +/-   ##
==========================================
+ Coverage   51.51%   51.69%   +0.17%     
==========================================
  Files         163      163              
  Lines        8168     8202      +34     
==========================================
+ Hits         4208     4240      +32     
- Misses       3960     3962       +2     

☔ 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 suspect we need to spend some more time thinking about vectors potentially copying on resize, this might also apply to strings?

Generally good improvements, I do wonder how we'll test this efficiently though, would be nice if we could produce memory dumps of tests somehow and assert.

crates/bitwarden-crypto/src/aes.rs Show resolved Hide resolved
crates/bitwarden-crypto/src/keys/symmetric_crypto_key.rs Outdated Show resolved Hide resolved
crates/bitwarden-crypto/src/util.rs Outdated Show resolved Hide resolved
# Conflicts:
#	crates/bitwarden-crypto/src/enc_string/asymmetric.rs
@bitwarden-bot
Copy link

bitwarden-bot commented Jan 22, 2024

Logo
Checkmarx One – Scan Summary & Detailse0ddd242-9062-41c4-96c9-2b1c1c3a6117

Fixed Issues

Severity Issue Source File / Package
HIGH Command_Injection /languages/python/example.py: 66
HIGH Command_Injection /languages/python/example.py: 35
HIGH Missing User Instruction /Dockerfile: 23
HIGH Reflected_XSS /languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt: 379
MEDIUM Apt Get Install Pin Version Not Defined /Dockerfile: 9
MEDIUM Privacy_Violation /languages/java/Example.java: 12
MEDIUM Privacy_Violation /languages/java/src/main/java/com/bitwarden/sdk/SecretsClient.java: 40
MEDIUM Privacy_Violation /languages/java/Example.java: 13
MEDIUM Unchecked_Input_for_Loop_Condition /languages/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt: 379
MEDIUM Unpinned Actions Full Length Commit SHA /build-cli-docker.yml: 151
MEDIUM Unpinned Actions Full Length Commit SHA /build-cli-docker.yml: 68
MEDIUM Unpinned Actions Full Length Commit SHA /publish-rust-crates.yml: 134
MEDIUM Unpinned Actions Full Length Commit SHA /release-napi.yml: 50
MEDIUM Unpinned Actions Full Length Commit SHA /release-cli.yml: 137
MEDIUM Unpinned Actions Full Length Commit SHA /release-cli.yml: 78
MEDIUM Unpinned Actions Full Length Commit SHA /release-cli.yml: 61
MEDIUM Unpinned Actions Full Length Commit SHA /version-bump.yml: 52
MEDIUM Unpinned Actions Full Length Commit SHA /build-cli-docker.yml: 75
MEDIUM Unpinned Actions Full Length Commit SHA /release-napi.yml: 135
MEDIUM Unpinned Actions Full Length Commit SHA /release-napi.yml: 104
MEDIUM Using Platform Flag with FROM Command /Dockerfile: 4
LOW Healthcheck Instruction Missing /Dockerfile: 23
LOW Missing_CSP_Header /about.hbs: 48

@dani-garcia
Copy link
Member Author

dani-garcia commented Jan 22, 2024

I've added some tests to SymmetricCryptoKey checking if the key got zeroized correctly with the help of some unsafe. It's not perfect as some of the memory gets reused, but it shows that the zeroize works at least.

Welp scratch that, those tests worked for me locally but they segfault on CI

Hinton
Hinton previously approved these changes Jan 23, 2024
@Hinton Hinton changed the title Enable Zeroize support for keys and EncStrings [PM-5728] Enable Zeroize support for keys and EncStrings Jan 23, 2024
Comment on lines +66 to +67
let mut dec: Vec<u8> = protected_user_key.decrypt_with_key(&device_private_key)?;
let user_key: SymmetricCryptoKey = dec.as_mut_slice().try_into()?;
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe this consumes the key. Do we need to zero dec?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

That feels potentially confusing? Do you expect a TryFrom of a slice to manipulate the input? I do see the benefit of ensuring it gets consumed though.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could also just pass an owned Vec<> and consume it in the function, but that will limit the use of the API for someone trying to pass a GenericArray or a [u8; 64] for example.

I think the fact that the function receives a &mut [u8] should make it clear enough that something is being modified, but we could add a comment mentioning explicitly that it's being zeroed.

Copy link
Member

Choose a reason for hiding this comment

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

Let's add a comment to make it explicit. We can revaluate if this turns out to be confusing, potentially not using try from and instead having an explicit function could make it less confusing.

Hinton
Hinton previously approved these changes Jan 25, 2024
# Conflicts:
#	crates/bitwarden-crypto/src/keys/asymmetric_crypto_key.rs
@dani-garcia dani-garcia merged commit bc995a8 into main Jan 25, 2024
55 of 56 checks passed
@dani-garcia dani-garcia deleted the ps/PM-5728-zeroize branch January 25, 2024 15:02
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.

3 participants