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

Fill BlindingFactor with zeros on Drop #2847

Merged
merged 8 commits into from
May 30, 2019

Conversation

eupn
Copy link
Contributor

@eupn eupn commented May 25, 2019

This is a first (and probably naïve) step towards security of sensitive data such as BlindingFactor.

In response to #2218. Cleaning of secp's SecretKey and wallet's mnemonic should be done in corresponding modules.

This PR:

  • Adds zeroize crate as a dependency for keychain
  • Adds Zeroize derive for BlindingFactor in a way that underlying byte array is filled with zeroes
  • Removes Copy derive from BlindingFactor since it is impossible to implement both Drop and Copy at the same time. All implicit copying were replaced with more explicit .clone()
  • Adds unit test for BlindingFactor zeroing

Some references:
dalek-cryptography/curve25519-dalek#11
rust-lang/rfcs#2533
https://www.youtube.com/watch?v=cQ9wTyYCdNU

Feedback is highly appreciated!

@hashmap
Copy link
Contributor

hashmap commented May 26, 2019

We use zeroize crate in some parts of grin, would you consider using it?

keychain/src/types.rs Outdated Show resolved Hide resolved
keychain/src/types.rs Outdated Show resolved Hide resolved
@hashmap hashmap merged commit 56b62a3 into mimblewimble:master May 30, 2019
@eupn eupn deleted the add-clear-on-drop branch May 30, 2019 21:44
@antiochp antiochp added this to the 1.1.0 milestone Jun 5, 2019
@antiochp antiochp added the release notes To be included in release notes (of relevant milestone). label Jun 5, 2019
@elichai
Copy link
Contributor

elichai commented Jun 5, 2019

FYI,
Every time you pass this struct by value it's get copied in memory without calling the Drop trait.

The better way to implement it is using the Pin struct or just putting it in a smart pointer(i.e Box) so that only the pointer will be copied and not the data itself

@hashmap
Copy link
Contributor

hashmap commented Jun 5, 2019

@elichai could you elaborate on why drop is not called when a cloned value gets out of its scope?

@elichai
Copy link
Contributor

elichai commented Jun 5, 2019

@hashmap because semantically when you pass something by value it's not dropped.
In reality the compiler will prefer to copy the data when passing by value (especially if it's on the stack)

Example https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=edc392a036c0d880224f0a94d6336f69

@garyyu
Copy link
Contributor

garyyu commented Jun 5, 2019

@elichai 👍
and that's exact what I'm worrying, as comments in #2851 (comment). thanks for a nice playground example to show that.

The better way to implement it is using the Pin struct or just putting it in a smart pointer(i.e Box) so that only the pointer will be copied and not the data itself

Could you please also demo this in your example?
And another method to solve this is always use the explicit b = a.clone() instead of b = a.

@elichai
Copy link
Contributor

elichai commented Jun 6, 2019

@garyyu With Box: https://play.rust-lang.org/?gist=26f5b9609348f4b6e287bf808ab0161a

The problem is that even though it works it's not guaranteed, Wrapping the Box in Pin guarantees it (e.g. https://play.rust-lang.org/?gist=3a3fabc5c44b3017b64dee1d22169d45 )

@cadmuspeverell
Copy link
Contributor

Maybe we could also use mlock to prevent paging to disk?

@elichai
Copy link
Contributor

elichai commented Jun 6, 2019

I think that paging is a whole different conversation that deserves its own issue

@garyyu
Copy link
Contributor

garyyu commented Jun 6, 2019

@elichai

The problem is that even though it works it's not guaranteed, Wrapping the Box in Pin guarantees it

👍 Thanks, now I understand. Would you mind to submit PRs to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release notes To be included in release notes (of relevant milestone).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants