GeezerVison is showing github.com — crypto: use internal random buffer bounds · nodejs/node@d5367e0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/random.js +50-11Lines changed: 50 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ const { | 10 | 10 | BigInt, | 11 | 11 | BigIntPrototypeToString, | 12 | 12 | DataView, | | 13 | + DataViewPrototypeGetByteLength, | 13 | 14 | DataViewPrototypeGetUint8, | 14 | 15 | DateNow, | 15 | 16 | FunctionPrototypeBind, | @@ -22,6 +23,10 @@ const { | 22 | 23 | StringPrototypePadStart, | 23 | 24 | TypedArrayPrototypeFill, | 24 | 25 | TypedArrayPrototypeGetBuffer, | | 26 | + TypedArrayPrototypeGetByteLength, | | 27 | + TypedArrayPrototypeGetLength, | | 28 | + TypedArrayPrototypeGetSymbolToStringTag, | | 29 | + Uint8Array, | 25 | 30 | } = primordials; | 26 | 31 |
| 27 | 32 | const { | @@ -59,6 +64,8 @@ const { | 59 | 64 | const { | 60 | 65 | isArrayBufferView, | 61 | 66 | isAnyArrayBuffer, | | 67 | + isDataView, | | 68 | + isSharedArrayBuffer, | 62 | 69 | isTypedArray, | 63 | 70 | isFloat16Array, | 64 | 71 | isFloat32Array, | @@ -78,6 +85,35 @@ const { | 78 | 85 | const kMaxInt32 = 2 ** 31 - 1; | 79 | 86 | const kMaxPossibleLength = MathMin(kMaxLength, kMaxInt32); | 80 | 87 |
| | 88 | +function getByteLength(buf) { | | 89 | + if (isArrayBufferView(buf)) { | | 90 | + return isDataView(buf) ? | | 91 | + DataViewPrototypeGetByteLength(buf) : TypedArrayPrototypeGetByteLength(buf); | | 92 | + } | | 93 | + if (isSharedArrayBuffer(buf)) | | 94 | + return TypedArrayPrototypeGetByteLength(new Uint8Array(buf)); | | 95 | + return ArrayBufferPrototypeGetByteLength(buf); | | 96 | +} | | 97 | + | | 98 | +function getElementSize(buf) { | | 99 | + switch (TypedArrayPrototypeGetSymbolToStringTag(buf)) { | | 100 | + case 'Int16Array': | | 101 | + case 'Uint16Array': | | 102 | + case 'Float16Array': | | 103 | + return 2; | | 104 | + case 'Int32Array': | | 105 | + case 'Uint32Array': | | 106 | + case 'Float32Array': | | 107 | + return 4; | | 108 | + case 'Float64Array': | | 109 | + case 'BigInt64Array': | | 110 | + case 'BigUint64Array': | | 111 | + return 8; | | 112 | + default: | | 113 | + return 1; | | 114 | + } | | 115 | +} | | 116 | + | 81 | 117 | function assertOffset(offset, elementSize, length) { | 82 | 118 | validateNumber(offset, 'offset'); | 83 | 119 | offset *= elementSize; | @@ -134,14 +170,15 @@ function randomFillSync(buf, offset = 0, size) { | 134 | 170 | buf); | 135 | 171 | } | 136 | 172 |
| 137 | | - const elementSize = buf.BYTES_PER_ELEMENT || 1; | | 173 | + const elementSize = getElementSize(buf); | | 174 | + const byteLength = getByteLength(buf); | 138 | 175 |
| 139 | | - offset = assertOffset(offset, elementSize, buf.byteLength); | | 176 | + offset = assertOffset(offset, elementSize, byteLength); | 140 | 177 |
| 141 | 178 | if (size === undefined) { | 142 | | - size = buf.byteLength - offset; | | 179 | + size = byteLength - offset; | 143 | 180 | } else { | 144 | | - size = assertSize(size, elementSize, offset, buf.byteLength); | | 181 | + size = assertSize(size, elementSize, offset, byteLength); | 145 | 182 | } | 146 | 183 |
| 147 | 184 | if (size === 0) | @@ -168,26 +205,27 @@ function randomFill(buf, offset, size, callback) { | 168 | 205 | buf); | 169 | 206 | } | 170 | 207 |
| 171 | | - const elementSize = buf.BYTES_PER_ELEMENT || 1; | | 208 | + const elementSize = getElementSize(buf); | 172 | 209 |
| 173 | 210 | if (typeof offset === 'function') { | 174 | 211 | callback = offset; | 175 | 212 | offset = 0; | 176 | 213 | // Size is a length here, assertSize() call turns it into a number of bytes | 177 | | - size = buf.length; | | 214 | + size = isTypedArray(buf) ? TypedArrayPrototypeGetLength(buf) : undefined; | 178 | 215 | } else if (typeof size === 'function') { | 179 | 216 | callback = size; | 180 | | - size = (buf.length ?? buf.byteLength) - offset; | | 217 | + size = (isTypedArray(buf) ? TypedArrayPrototypeGetLength(buf) : getByteLength(buf)) - offset; | 181 | 218 | } else { | 182 | 219 | validateFunction(callback, 'callback'); | 183 | 220 | } | 184 | 221 |
| 185 | | - offset = assertOffset(offset, elementSize, buf.byteLength); | | 222 | + const byteLength = getByteLength(buf); | | 223 | + offset = assertOffset(offset, elementSize, byteLength); | 186 | 224 |
| 187 | 225 | if (size === undefined) { | 188 | | - size = buf.byteLength - offset; | | 226 | + size = byteLength - offset; | 189 | 227 | } else { | 190 | | - size = assertSize(size, elementSize, offset, buf.byteLength); | | 228 | + size = assertSize(size, elementSize, offset, byteLength); | 191 | 229 | } | 192 | 230 |
| 193 | 231 | if (size === 0) { | @@ -337,7 +375,8 @@ function getRandomValues(data) { | 337 | 375 | 'The data argument must be an integer-type TypedArray', | 338 | 376 | 'TypeMismatchError'); | 339 | 377 | } | 340 | | - if (data.byteLength > 65536) { | | 378 | + const byteLength = TypedArrayPrototypeGetByteLength(data); | | 379 | + if (byteLength > 65536) { | 341 | 380 | throw lazyDOMException( | 342 | 381 | 'The requested length exceeds 65,536 bytes', | 343 | 382 | 'QuotaExceededError'); | |
Collapse file Copy file name to clipboard +120Lines changed: 120 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const { randomFill, randomFillSync } = require('crypto'); | | 9 | + | | 10 | +const typedArrays = [ | | 11 | + Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, | | 12 | + Int32Array, Uint32Array, Float32Array, Float64Array, | | 13 | + BigInt64Array, BigUint64Array, | | 14 | +]; | | 15 | +if (globalThis.Float16Array !== undefined) | | 16 | + typedArrays.push(globalThis.Float16Array); | | 17 | + | | 18 | +const factories = []; | | 19 | +for (const Backing of [ArrayBuffer, SharedArrayBuffer]) { | | 20 | + for (const Type of [...typedArrays, Buffer, DataView]) { | | 21 | + const elementSize = Type.BYTES_PER_ELEMENT || 1; | | 22 | + factories.push({ | | 23 | + name: `${Type.name} on ${Backing.name}`, | | 24 | + elementSize, | | 25 | + make(length = 64) { | | 26 | + const backing = new Backing(length + 64); | | 27 | + const input = Type === Buffer ? Buffer.from(backing, 32, length) : | | 28 | + new Type(backing, 32, length / elementSize); | | 29 | + return { input, backing, bytes: new Uint8Array(backing), start: 32, length }; | | 30 | + }, | | 31 | + }); | | 32 | + } | | 33 | + factories.push({ | | 34 | + name: Backing.name, | | 35 | + elementSize: 1, | | 36 | + make(length = 64) { | | 37 | + const input = new Backing(length); | | 38 | + return { input, backing: input, bytes: new Uint8Array(input), start: 0, length }; | | 39 | + }, | | 40 | + }); | | 41 | +} | | 42 | + | | 43 | +function shadowMetadata(target, mode) { | | 44 | + const values = { | | 45 | + byteLength: mode === 'zero' ? 0 : 4096, | | 46 | + length: mode === 'zero' ? 0 : 4096, | | 47 | + BYTES_PER_ELEMENT: mode === 'zero' ? 0 : 4096, | | 48 | + buffer: new ArrayBuffer(0), | | 49 | + byteOffset: mode === 'zero' ? 0 : 4096, | | 50 | + [Symbol.toStringTag]: 'NotAnArrayBufferView', | | 51 | + }; | | 52 | + for (const key of Reflect.ownKeys(values)) { | | 53 | + Object.defineProperty(target, key, mode === 'getters' ? | | 54 | + { get: common.mustNotCall(`Unexpected ${String(key)} getter`) } : | | 55 | + { value: values[key] }); | | 56 | + } | | 57 | +} | | 58 | + | | 59 | +function checkFilled({ input, bytes, start }, result, offset, size, label) { | | 60 | + assert.strictEqual(result, input, label); | | 61 | + assert(bytes.subarray(0, start + offset).every((byte) => byte === 0), label); | | 62 | + assert(bytes.subarray(start + offset + size).every((byte) => byte === 0), label); | | 63 | + // Every nonempty test range contains at least 32 random bytes. Detect a | | 64 | + // no-op without assuming that any particular generated byte is nonzero. | | 65 | + if (size !== 0) | | 66 | + assert(bytes.subarray(start + offset, start + offset + size).some((byte) => byte !== 0), label); | | 67 | +} | | 68 | + | | 69 | +for (const factory of factories) { | | 70 | + const { elementSize } = factory; | | 71 | + const ranges = [ | | 72 | + { args: [], offset: 0, size: 64 }, | | 73 | + { args: [16 / elementSize], offset: 16, size: 48 }, | | 74 | + { args: [16 / elementSize, 32 / elementSize], offset: 16, size: 32 }, | | 75 | + { args: [16 / elementSize, undefined], offset: 16, size: 48 }, | | 76 | + { args: [64 / elementSize], offset: 64, size: 0 }, | | 77 | + ]; | | 78 | + for (const mode of ['getters', 'zero', 'inflated']) { | | 79 | + for (const { args, offset, size } of ranges) { | | 80 | + for (const async of [false, true]) { | | 81 | + const value = factory.make(); | | 82 | + shadowMetadata(value.input, mode); | | 83 | + if (value.backing !== value.input) | | 84 | + shadowMetadata(value.backing, mode); | | 85 | + const label = `${factory.name}, ${mode}, ${async ? 'async' : 'sync'}, ${args}`; | | 86 | + if (async) { | | 87 | + assert.strictEqual(randomFill(value.input, ...args, common.mustSucceed((result) => { | | 88 | + checkFilled(value, result, offset, size, label); | | 89 | + })), undefined); | | 90 | + } else { | | 91 | + checkFilled(value, randomFillSync(value.input, ...args), offset, size, label); | | 92 | + } | | 93 | + } | | 94 | + } | | 95 | + } | | 96 | + | | 97 | + // Inflated public bounds must not let an invalid range reach the native job. | | 98 | + // Keep the real element width here so each range exceeds the actual buffer. | | 99 | + const units = 64 / elementSize; | | 100 | + for (const args of [[units + 1], [0, units + 1], [units, 1]]) { | | 101 | + const { input } = factory.make(); | | 102 | + Object.defineProperties(input, { | | 103 | + byteLength: { value: 4096 }, | | 104 | + length: { value: 4096 }, | | 105 | + }); | | 106 | + assert.throws(() => randomFillSync(input, ...args), { code: 'ERR_OUT_OF_RANGE' }); | | 107 | + assert.throws(() => randomFill(input, ...args, common.mustNotCall()), { | | 108 | + code: 'ERR_OUT_OF_RANGE', | | 109 | + }); | | 110 | + } | | 111 | + | | 112 | + for (const args of [[], [0], [0, 0], [0, undefined]]) { | | 113 | + const value = factory.make(0); | | 114 | + shadowMetadata(value.input, 'inflated'); | | 115 | + checkFilled(value, randomFillSync(value.input, ...args), 0, 0, factory.name); | | 116 | + assert.strictEqual(randomFill(value.input, ...args, common.mustSucceed((result) => { | | 117 | + checkFilled(value, result, 0, 0, factory.name); | | 118 | + })), undefined); | | 119 | + } | | 120 | +} |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const webcrypto = globalThis.crypto; | | 9 | + | | 10 | +for (const Type of [Uint8Array, Uint16Array, Uint32Array, BigUint64Array]) { | | 11 | + const storage = new Uint8Array(128); | | 12 | + const view = new Type(storage.buffer, 32, 32 / Type.BYTES_PER_ELEMENT); | | 13 | + for (const name of ['buffer', 'byteOffset', 'byteLength', 'BYTES_PER_ELEMENT']) { | | 14 | + Object.defineProperty(view, name, { get: common.mustNotCall() }); | | 15 | + Object.defineProperty(storage.buffer, name, { get: common.mustNotCall() }); | | 16 | + } | | 17 | + assert.strictEqual(webcrypto.getRandomValues(view), view); | | 18 | + assert(storage.subarray(32, 64).some((byte) => byte !== 0)); | | 19 | + assert(storage.subarray(0, 32).every((byte) => byte === 0)); | | 20 | + assert(storage.subarray(64).every((byte) => byte === 0)); | | 21 | +} | | 22 | + | | 23 | +const oversized = new Uint8Array(65537); | | 24 | +Object.defineProperty(oversized, 'byteLength', { value: 0 }); | | 25 | +assert.throws(() => webcrypto.getRandomValues(oversized), { | | 26 | + name: 'QuotaExceededError', | | 27 | +}); | | 28 | + | | 29 | +const empty = new Uint8Array(0); | | 30 | +Object.defineProperty(empty, 'byteLength', { value: 65537 }); | | 31 | +assert.strictEqual(webcrypto.getRandomValues(empty), empty); |
|