mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
InitializeOncePerProcessInternal() calls CSPRNG(nullptr, 0) to confirm OpenSSL's random source is seeded and installs a V8 entropy source that goes through CSPRNG() as well. The first RAND_status() of the process therefore runs before V8 starts, instantiates the DRBG, and with it constructs the default provider's algorithm and name tables (ossl_method_construct, ossl_namemap_stored): 3.7% of the samples of `node -e 0` on Linux x64, all of it before v8Start. V8 uses the entropy for hash seeds, address space layout randomization and Math.random(), none of which are cryptographic, so read the OS CSPRNG directly through uv_random(). AIX is the exception: uv_random() reads the blocking /dev/random there, so it stays on OpenSSL's DRBG, which seeds from /dev/urandom. Keep activating the default provider at startup, which the eager check did as a side effect and --openssl-legacy-provider depends on. Its explicit OSSL_PROVIDER_load() disables OpenSSL's provider fallback, so without a prior activation the default provider never loads. Run the seeding check itself only when that provider is unavailable or FIPS is in effect, the cases where an OpenSSL configuration from any source can leave the process without a DRBG and an early abort beats a hang at the first crypto call. Every crypto consumer stays on OpenSSL, and a system without a usable CSPRNG still aborts at startup, now from uv_random() failing. Two other behaviors change. A configuration whose [random] section names a DRBG that cannot be fetched used to abort at startup; it now starts and the first crypto call fails on the fetch. With --secure-heap the process DRBGs are instantiated after the secure heap exists, so they are allocated from it, and a Worker's isolate setup no longer aborts the process from the entropy callback when the heap cannot hold another per-thread DRBG. Tests cover both, and the default provider staying active under --openssl-legacy-provider. Measured on Linux x64 against an unpatched build of the same tree, both binaries interleaved, min of 300 runs: `node -e 0` 29.18 -> 27.82 ms, nodeStart to v8Start 2.91 -> 2.11 ms. RAND_status and the provider's table construction leave the startup profile (2.8% of samples before); the provider activation that remains is 0.05%. The first crypto.randomBytes() instantiates the DRBG in 0.19 ms. The `parallel`, `sequential`, `message`, `es-module` and `addons` suites show no failure the unpatched build does not have. Refs: https://github.com/nodejs/node/commit/5cc36c39d2 Refs: https://github.com/nodejs/node/pull/44493 Refs: https://github.com/nodejs/node/pull/46237 Signed-off-by: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/65796 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
131 lines
3.4 KiB
JavaScript
131 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto) {
|
|
common.skip('missing crypto');
|
|
}
|
|
|
|
if (common.isWindows) {
|
|
common.skip('Not supported on Windows');
|
|
}
|
|
|
|
if (common.isASan) {
|
|
common.skip('ASan does not play well with secure heap allocations');
|
|
}
|
|
|
|
if (common.hasV8Sandbox) {
|
|
common.skip('--secure-heap is not available with the V8 sandbox');
|
|
}
|
|
|
|
const {
|
|
isBoringSSL,
|
|
hasOpenSSL,
|
|
hasFIPS,
|
|
} = require('../common/crypto');
|
|
|
|
if (isBoringSSL) {
|
|
common.skip('BoringSSL does not support secure heap');
|
|
}
|
|
|
|
const assert = require('assert');
|
|
const { fork } = require('child_process');
|
|
const fixtures = require('../common/fixtures');
|
|
const {
|
|
secureHeapUsed,
|
|
createDiffieHellman,
|
|
getFips,
|
|
} = require('crypto');
|
|
|
|
if (process.argv[2] === 'child') {
|
|
|
|
const a = secureHeapUsed();
|
|
|
|
assert(a);
|
|
assert.strictEqual(typeof a, 'object');
|
|
assert.strictEqual(a.total, 65536);
|
|
assert.strictEqual(a.min, 4);
|
|
assert.strictEqual(a.used, 0);
|
|
|
|
{
|
|
const size = hasFIPS(3) ?
|
|
2048 : (getFips() === 1 || hasOpenSSL(3) ? 1024 : 256);
|
|
const dh1 = createDiffieHellman(size);
|
|
const p1 = dh1.getPrime('buffer');
|
|
const dh2 = createDiffieHellman(p1, 'buffer');
|
|
const key1 = dh1.generateKeys();
|
|
const key2 = dh2.generateKeys('hex');
|
|
dh1.computeSecret(key2, 'hex', 'base64');
|
|
dh2.computeSecret(key1, 'latin1', 'buffer');
|
|
|
|
const b = secureHeapUsed();
|
|
assert(b);
|
|
assert.strictEqual(typeof b, 'object');
|
|
assert.strictEqual(b.total, 65536);
|
|
assert.strictEqual(b.min, 4);
|
|
// The amount used can vary on a number of factors
|
|
assert(b.used > 0);
|
|
assert(b.utilization > 0.0);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (process.argv[2] === 'workers') {
|
|
// Eight Workers held alive at once. A 1 KiB secure heap has room for a
|
|
// few DRBGs only, so an isolate setup that drew its entropy through
|
|
// OpenSSL would fail for the later Workers and abort the process.
|
|
const { Worker } = require('worker_threads');
|
|
const i32 = new Int32Array(new SharedArrayBuffer(4));
|
|
let online = 0;
|
|
for (let i = 0; i < 8; i++) {
|
|
const worker = new Worker(
|
|
'const { workerData } = require("worker_threads");' +
|
|
'Atomics.wait(workerData.i32, 0, 0);',
|
|
{ eval: true, workerData: { i32 } });
|
|
worker.on('online', () => {
|
|
if (++online === 8) {
|
|
Atomics.store(i32, 0, 1);
|
|
Atomics.notify(i32, 0);
|
|
}
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
const child = fork(
|
|
process.argv[1],
|
|
['child'],
|
|
{ execArgv: ['--secure-heap=65536', '--secure-heap-min=4'] });
|
|
|
|
child.on('exit', common.mustCall((code) => {
|
|
assert.strictEqual(code, 0);
|
|
}));
|
|
|
|
// AIX keeps OpenSSL as V8's entropy source, so a Worker's isolate setup
|
|
// still draws on the secure heap there.
|
|
if (!common.isAIX) {
|
|
const child = fork(
|
|
process.argv[1],
|
|
['workers'],
|
|
{ execArgv: ['--secure-heap=1024', '--secure-heap-min=4'] });
|
|
child.on('exit', common.mustCall((code, signal) => {
|
|
assert.strictEqual(signal, null);
|
|
assert.strictEqual(code, 0);
|
|
}));
|
|
}
|
|
|
|
{
|
|
const child = fork(fixtures.path('a.js'), {
|
|
execArgv: ['--secure-heap=3', '--secure-heap-min=3'],
|
|
stdio: 'pipe'
|
|
});
|
|
let res = '';
|
|
child.on('exit', common.mustCall((code) => {
|
|
assert.notStrictEqual(code, 0);
|
|
assert.match(res, /--secure-heap must be a power of 2/);
|
|
assert.match(res, /--secure-heap-min must be a power of 2/);
|
|
}));
|
|
child.stderr.setEncoding('utf8');
|
|
child.stderr.on('data', (chunk) => res += chunk);
|
|
}
|