From 53cf996270a999ce506610435a46c48518b7f66e Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 4 Jan 2021 09:06:26 -0800 Subject: [PATCH] crypto: implement basic secure heap support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two new command line arguments: * `--secure-heap=n`, which causes node.js to initialize an openssl secure heap of `n` bytes on openssl initialization. * `--secure-heap-min=n`, which specifies the minimum allocation from the secure heap. * A new method `crypto.secureHeapUsed()` that returns details about the total and used secure heap allocation. The secure heap is an openssl feature that allows certain kinds of potentially sensitive information (such as private key BigNums) to be allocated from a dedicated memory area that is protected against pointer over- and underruns. The secure heap is a fixed size, so it's important that users pick a large enough size to cover the crypto operations they intend to utilize. The secure heap is disabled by default. Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/36779 Refs: https://github.com/nodejs/node/pull/36729 Reviewed-By: Tobias Nießen --- doc/api/cli.md | 37 ++++++++++ doc/api/crypto.md | 15 ++++ doc/node.1 | 7 ++ lib/crypto.js | 2 + lib/internal/crypto/util.js | 16 +++++ src/crypto/crypto_util.cc | 28 ++++++++ src/node_options.cc | 24 +++++++ src/node_options.h | 2 + test/parallel/test-crypto-secure-heap.js | 72 +++++++++++++++++++ ...rocess-env-allowed-flags-are-documented.js | 10 ++- 10 files changed, 211 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-crypto-secure-heap.js diff --git a/doc/api/cli.md b/doc/api/cli.md index c2c2efbcc0eef8..af1d6a7ad09751 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -848,6 +848,40 @@ Enables report to be generated on uncaught exceptions. Useful when inspecting the JavaScript stack in conjunction with native stack and other runtime environment data. +### `--secure-heap=n` + + +Initializes an OpenSSL secure heap of `n` bytes. When initialized, the +secure heap is used for selected types of allocations within OpenSSL +during key generation and other operations. This is useful, for instance, +to prevent sensitive information from leaking due to pointer overruns +or underruns. + +The secure heap is a fixed size and cannot be resized at runtime so, +if used, it is important to select a large enough heap to cover all +application uses. + +The heap size given must be a power of two. Any value less than 2 +will disable the secure heap. + +The secure heap is disabled by default. + +The secure heap is not available on Windows. + +See [`CRYPTO_secure_malloc_init`][] for more details. + +### `--secure-heap-min=n` + + +When using `--secure-heap`, the `--secure-heap-min` flag specifies the +minimum allocation from the secure heap. The minimum value is `2`. +The maximum value is the lesser of `--secure-heap` or `2147483647`. +The value given must be a power of two. + ### `--throw-deprecation` + +* Returns: {Object} + * `total` {number} The total allocated secure heap size as specified + using the `--secure-heap=n` command-line flag. + * `min` {number} The minimum allocation from the secure heap as + specified using the `--secure-heap-min` command-line flag. + * `used` {number} The total number of bytes currently allocated from + the secure heap. + * `utilization` {number} The calculated ratio of `used` to `total` + allocated bytes. + ### `crypto.setEngine(engine[, flags])`