Skip to content

Conversation

real-or-random
Copy link
Contributor

When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by #11558.

This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

Commit fbf327b ("Minimal code
changes to allow msvc compilation.") was indeed minimal in terms
of lines touched. But as a result of that minimalism it changed the
logic in memory_cleanse() to first call std::memset() and then
additionally the MSVC-specific SecureZeroMemory() function, and it
also moved a comment to the wrong location.

This commit removes the superfluous call to std::memset() on MSVC
and ensures that the comment is in the right position again.
@laanwj
Copy link
Member

laanwj commented Jun 6, 2019

This is one of the things that has been 'fixed' so many times, can we please be sure it's okay this time?
(change looks OK to me, though I don't think clearing twice is that bad, it can't hurt at least)

@real-or-random
Copy link
Contributor Author

Yeah, so there is no 100% optimal solution for clearing memory, but please note that the goal of this PR is not to start this discussion about what the right method is. This merely fixes a weirdness on in the logic introduced by #11558.

@practicalswift
Copy link
Contributor

utACK 751aff1 (read code - looks correct)

@maflcko
Copy link
Member

maflcko commented Jun 6, 2019

Fine on changing the docs, but is it really necessary to remove the redundant std::memset(ptr, 0, len).

@real-or-random
Copy link
Contributor Author

real-or-random commented Jun 6, 2019

Well, as you say, the call is redundant, so it's okay not to remove it but I'd argue it's desirable to remove it

  • for readability: It just looks wrong to do the same operation twice. I think people would complain about code that does x = 0; x = 0;.
  • for efficiency: While MSVC inlines the call to SecureZeroMemory, it is not clever enough to optimize the call to memset out, see https://godbolt.org/z/Y2TIWC.

@practicalswift
Copy link
Contributor

ACK 4e1941e (read code - looks correct)

Copy link
Contributor

@jonasnick jonasnick left a comment

Choose a reason for hiding this comment

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

ACK read the code and the comments. Comments are way better than before. The cited paper ("Dead Store Elimination (Still) Considered Harmful") is an excellent resource.

@practicalswift
Copy link
Contributor

@real-or-random Would you mind fixing the typo? After that I think this should be ready for merge :-)

So far, the documentation of memory_cleanse() is a verbatim copy of
the commit message in BoringSSL, where this code was originally
written. However, our code evolved since then, and the commit message
is not particularly helpful in the code but is rather of historical
interested in BoringSSL only.

This commit improves improves the comments around memory_cleanse()
and gives a better rationale for the method that we use. This commit
touches only comments.
@real-or-random
Copy link
Contributor Author

@practicalswift @jonasnick
I fixed the typo, can you reACK?

@practicalswift
Copy link
Contributor

utACK f53a70c :-)

@laanwj
Copy link
Member

laanwj commented Jul 3, 2019

code review ACK f53a70c

@laanwj laanwj merged commit f53a70c into bitcoin:master Jul 3, 2019
laanwj added a commit that referenced this pull request Jul 3, 2019
f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by #11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
@laanwj
Copy link
Member

laanwj commented Jul 3, 2019

Seeing the merge message this PR title is badly worded: yes, you can consider clearing the memory twice a logic error, but most people would consider it a logic error if it would result in not clearing the memory.

So to be 100% clear for anyone getting here: this is not a security fix.

zkbot added a commit to zcash/zcash that referenced this pull request Apr 30, 2020
Memory cleanse backports

Cherry-picked from the following upstream PRs:
- bitcoin/bitcoin#10308
- bitcoin/bitcoin#11196
- bitcoin/bitcoin#11558
  - Only the changes that did not conflict.
- bitcoin/bitcoin#16158

Part of #145.
zkbot added a commit to zcash/zcash that referenced this pull request Apr 30, 2020
memory_cleanse backports

Cherry-picked from the following upstream PRs:
- bitcoin/bitcoin#10308
- bitcoin/bitcoin#11196
- bitcoin/bitcoin#11558
  - Only the changes that did not conflict.
- bitcoin/bitcoin#16158

Part of #145.
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Nov 13, 2020
Summary:

bitcoin/bitcoin@cac30a4
> Commit fbf327b ("Minimal code
> changes to allow msvc compilation.") was indeed minimal in terms
> of lines touched. But as a result of that minimalism it changed the
> logic in memory_cleanse() to first call std::memset() and then
> additionally the MSVC-specific SecureZeroMemory() function, and it
> also moved a comment to the wrong location.
> This commit removes the superfluous call to std::memset() on MSVC
> and ensures that the comment is in the right position again.

bitcoin/bitcoin@f53a70c
> So far, the documentation of memory_cleanse() is a verbatim copy of
> the commit message in BoringSSL, where this code was originally
> written. However, our code evolved since then, and the commit message
> is not particularly helpful in the code but is rather of historical
> interested in BoringSSL only.
>
> This commit improves improves the comments around memory_cleanse()
> and gives a better rationale for the method that we use. This commit
> touches only comments.

This is a backport of Core [[bitcoin/bitcoin#16158 | PR16158]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Subscribers: majcosta, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D8375
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 27, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 28, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 29, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 12, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 13, 2021
…up docs

f53a70c Improve documentation of memory_cleanse() (Tim Ruffing)
cac30a4 Clean up logic in memory_cleanse() for MSVC (Tim Ruffing)

Pull request description:

  When working on bitcoin-core/secp256k1#185, I noticed that the logic in memory_cleanse(), which is supposed to clear memory securely, is weird on MSVC. While it's correct, it's at least a code smell because the code clears the memory twice on MSVC. This weirdness was introduced by bitcoin#11558.

  This PR fixes the logic on MSVC and also improves the docs around this function. Best reviewed in individual commits, see the commit messages for more rationale. The second commit touches only comments.

ACKs for top commit:
  practicalswift:
    utACK f53a70c :-)
  laanwj:
    code review ACK f53a70c

Tree-SHA512: 1c2fd98ae62b34b3e6e59d1178b293af969a9e06cbb7df02a699ce8802f145a336f72edb178c520e3ecec81f7e8083828f90a5ba6367d966a2c7d7c0dd6c0475
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants