Skip to content

Conversation

@maaku
Copy link
Contributor

@maaku maaku commented Aug 30, 2017

The implementation we currently use from OpenSSL prevents the compiler from optimizing away clensing operations on blocks of memory that are about to be released, but this protection is not extended to link-time optimization. This commit copies the solution cooked up by Google compiler engineers which uses inline assembly directives to instruct the compiler not to optimize out the call under any circumstances. As the code is in-lined, this has the added advantage of removing one more OpenSSL dependency.

Regarding license compatibility, Google's contributions to BoringSSL library, including this code, is made available under the ISC license, which is MIT compatible.

BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f

Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

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

Big concept ACK; there's no need to use OpenSSL for something as simple as this

Copy link
Member

Choose a reason for hiding this comment

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

This comment doesn't seem to apply in the current form of the code.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure OpenSSL's headers expose such a macro, and even if they do, this risks breaking things if OpenSSL gets removed entirely at some point. I believe you just want _MSC_VER here to detect MSVC (as the else branch should work fine in MinGW).

@maaku maaku force-pushed the fix-memory-clense branch from 084674d to 9c4f8b5 Compare August 30, 2017 07:27
@maaku
Copy link
Contributor Author

maaku commented Aug 30, 2017

Nits addressed (don't push code late at night!)

@practicalswift
Copy link
Contributor

Concept ACK!

Too bad there is no bulletproof way to detect memset_s(…) :-(

@kallewoof
Copy link
Contributor

utACK 9c4f8b5188a53f39c0849f860c8227fbb5a77bc6

@Leviathn
Copy link

Concept ACK - minimizing OpenSSL exposure is good.

ISC seems less strict than even MIT, appears compatible.

@jtimon
Copy link
Contributor

jtimon commented Aug 31, 2017

Concept ACK

@theuni
Copy link
Member

theuni commented Aug 31, 2017

concept ACK. and utACK 9c4f8b5188a53f39c0849f860c8227fbb5a77bc6. It'll be great to be rid of the openssl dependency at such a low level.

The logic seems sane. It'd be nice if we could add a test, but I suppose we can't test reliably without affecting the outcome.

@gmaxwell
Copy link
Contributor

gmaxwell commented Sep 1, 2017

@cfields you can review the ASM at least.

@jonasschnelli
Copy link
Contributor

utACK 9c4f8b5188a53f39c0849f860c8227fbb5a77bc6

Copy link
Member

Choose a reason for hiding this comment

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

What does a bare __asm do?

Copy link
Member

Choose a reason for hiding this comment

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

Presumbly the same as the GCC extended asm block above, and hopefully prevents the code from being optimized out.

However, I'm not sure to what extent we want to support MSVC, especially as __asm is only supported on x86, not x86_64.

Copy link
Member

Choose a reason for hiding this comment

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

However, I'm not sure to what extent we want to support MSVC, especially as __asm is only supported on x86, not x86_64.

We don't support MSVC (I don't know of any dev who cares to maintain support for it) however It shouldn't be unnecessarily difficult to build with it, so if possible we should try to be compatible with it.

@laanwj
Copy link
Member

laanwj commented Sep 5, 2017

Concept ACK, though I don't understand why this works (certainly in the MSCV case), I would prefer a more explanatory comment.

Copy link
Contributor

@kallewoof kallewoof left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

μNit: Maybe <cstring> instead, since this is C++.

@practicalswift
Copy link
Contributor

practicalswift commented Sep 6, 2017

Since we - in contrast to BoringSSL - have the luxury of always compiling with assertions enabled, what about also adding an assertion assert(len == 0 || static_cast<uint8_t*>(ptr)[0] == 0); as a postcondition? Better safe than sorry :-)

@kallewoof
Copy link
Contributor

kallewoof commented Sep 6, 2017

@practicalswift: I think that affects the resulting code. It basically skips cleans for untouched memory. By touching memory it will forego that optimization.

…clearing even with link-time optimization.

The implementation we currently use from OpenSSL prevents the compiler from optimizing away clensing operations on blocks of memory that are about to be released, but this protection is not extended to link-time optimization. This commit copies the solution cooked up by Google compiler engineers which uses inline assembly directives to instruct the compiler not to optimize out the call under any circumstances. As the code is in-lined, this has the added advantage of removing one more OpenSSL dependency.

Regarding license compatibility, Google's contributions to BoringSSL library, including this code, is made available under the ISC license, which is MIT compatible.

BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f
@maaku maaku force-pushed the fix-memory-clense branch from 16770aa to 1444c2e Compare September 6, 2017 20:46
@TheBlueMatt
Copy link
Contributor

In the original boringssl review at https://boringssl-review.googlesource.com/c/boringssl/+/1339 it was noted that on WIndows its likely better to just use SecureZeroMemory...maybe do that in place of the string __asm for MSVC?

@laanwj
Copy link
Member

laanwj commented Sep 7, 2017

I vaguely remember there was something wrong/insecure about SecureZeroMemory but I can't remember what and a quick search doesn't turn up anything. I think it was something with being a Win32 API function (syscall) so it makes it easier for malware to hook the function and scoop up all the sensitive data. Anyhow, I don't know if it matters.

@maaku
Copy link
Contributor Author

maaku commented Sep 15, 2017

Should I switch to SecureZeroMemory on windows? I don't know enough about windows to evaluate that risk. Or is this ready to merge as-is?

@sipa
Copy link
Member

sipa commented Sep 15, 2017

I think this is fine as-is.

utACK 1444c2e

@laanwj
Copy link
Member

laanwj commented Sep 16, 2017

Should I switch to SecureZeroMemory on windows?

I also think it's fine as it is. Note that @TheBlueMatt doesn't say to use that call generally on Windows, but specifically on MSVC. Someone that maintains MSVC support should do the evaluation of the subtleties for what function/method to use there. Just using BoringSSL's code is "best effort" to support it as anything.
utACK 1444c2e

(also, if they make another decision re: MSVC we can always take it over)

@laanwj laanwj merged commit 1444c2e into bitcoin:master Sep 16, 2017
laanwj added a commit that referenced this pull request Sep 16, 2017
…ensure memory clearing even with -lto

1444c2e Switch memory_cleanse implementation to BoringSSL's to ensure memory clearing even with link-time optimization. (Adam Langley)

Pull request description:

  The implementation we currently use from OpenSSL prevents the compiler from optimizing away clensing operations on blocks of memory that are about to be released, but this protection is not extended to link-time optimization. This commit copies the solution cooked up by Google compiler engineers which uses inline assembly directives to instruct the compiler not to optimize out the call under any circumstances. As the code is in-lined, this has the added advantage of removing one more OpenSSL dependency.

  Regarding license compatibility, Google's contributions to BoringSSL library, including this code, is made available under the ISC license, which is MIT compatible.

  BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f

Tree-SHA512: 8134998663c1501e3ce48fbbd6ab41de981f0855e3f4d25d2e86ff8056c917d82c751c88e9c39660319ebfbc8283dce594c3e4fc7f87080a212a2cdba57ea511
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 23, 2019
…L's to ensure memory clearing even with -lto

1444c2e Switch memory_cleanse implementation to BoringSSL's to ensure memory clearing even with link-time optimization. (Adam Langley)

Pull request description:

  The implementation we currently use from OpenSSL prevents the compiler from optimizing away clensing operations on blocks of memory that are about to be released, but this protection is not extended to link-time optimization. This commit copies the solution cooked up by Google compiler engineers which uses inline assembly directives to instruct the compiler not to optimize out the call under any circumstances. As the code is in-lined, this has the added advantage of removing one more OpenSSL dependency.

  Regarding license compatibility, Google's contributions to BoringSSL library, including this code, is made available under the ISC license, which is MIT compatible.

  BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f

Tree-SHA512: 8134998663c1501e3ce48fbbd6ab41de981f0855e3f4d25d2e86ff8056c917d82c751c88e9c39660319ebfbc8283dce594c3e4fc7f87080a212a2cdba57ea511
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.
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 19, 2020
Summary:
...to ensure memory clearing even with link-time optimization.

```
The implementation we currently use from OpenSSL prevents the compiler
from optimizing away clensing operations on blocks of memory that are
about to be released, but this protection is not extended to link-time
optimization. This commit copies the solution cooked up by Google
compiler engineers which uses inline assembly directives to instruct the
compiler not to optimize out the call under any circumstances. As the
code is in-lined, this has the added advantage of removing one more
OpenSSL dependency.

Regarding license compatibility, Google's contributions to BoringSSL
library, including this code, is made available under the ISC license,
which is MIT compatible.

BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f
```

Backport of core [[bitcoin/bitcoin#11196 | PR11196]].

Test Plan:
  ninja all check

Run the windows Gitian build.

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6135
ftrader pushed a commit to bitcoin-cash-node/bitcoin-cash-node that referenced this pull request Aug 17, 2020
Summary:
...to ensure memory clearing even with link-time optimization.

```
The implementation we currently use from OpenSSL prevents the compiler
from optimizing away clensing operations on blocks of memory that are
about to be released, but this protection is not extended to link-time
optimization. This commit copies the solution cooked up by Google
compiler engineers which uses inline assembly directives to instruct the
compiler not to optimize out the call under any circumstances. As the
code is in-lined, this has the added advantage of removing one more
OpenSSL dependency.

Regarding license compatibility, Google's contributions to BoringSSL
library, including this code, is made available under the ISC license,
which is MIT compatible.

BoringSSL git commit: ad1907fe73334d6c696c8539646c21b11178f20f
```

Backport of core [[bitcoin/bitcoin#11196 | PR11196]].

Test Plan:
  ninja all check

Run the windows Gitian build.

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D6135
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 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.