From 407cf916563f59ae678ffbb30a18f3bb3260e688 Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Thu, 9 Apr 2026 18:48:39 +0200 Subject: [PATCH] crypto: guard against size_t overflow on experimental 32-bit arch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://hackerone.com/reports/3655230 Signed-off-by: Filip Skokan PR-URL: https://github.com/nodejs/node/pull/62626 Backport-PR-URL: https://github.com/nodejs/node/pull/63563 Refs: https://hackerone.com/reports/3655230 Reviewed-By: Juan José Arboleda Reviewed-By: James M Snell Reviewed-By: Yagiz Nizipli --- src/crypto/crypto_turboshake.cc | 12 +++++- ...webcrypto-kangarootwelve-32bit-overflow.js | 41 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/pummel/test-webcrypto-kangarootwelve-32bit-overflow.js diff --git a/src/crypto/crypto_turboshake.cc b/src/crypto/crypto_turboshake.cc index 60ef7a8dae5..d01adf09487 100644 --- a/src/crypto/crypto_turboshake.cc +++ b/src/crypto/crypto_turboshake.cc @@ -587,7 +587,6 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env, ByteSource* out, CryptoJobMode mode) { CHECK_GT(params.output_length, 0); - char* buf = MallocOpenSSL(params.output_length); const uint8_t* input = reinterpret_cast(params.data.data()); size_t input_len = params.data.size(); @@ -596,6 +595,17 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env, reinterpret_cast(params.customization.data()); size_t custom_len = params.customization.size(); + // Guard against size_t overflow in KangarooTwelve's s_len computation: + // s_len = msg_len + custom_len + LengthEncode(custom_len).size() + // LengthEncode produces at most sizeof(size_t) + 1 bytes. + static constexpr size_t kMaxLengthEncodeSize = sizeof(size_t) + 1; + if (input_len > SIZE_MAX - custom_len || + input_len + custom_len > SIZE_MAX - kMaxLengthEncodeSize) { + return false; + } + + char* buf = MallocOpenSSL(params.output_length); + switch (params.variant) { case KangarooTwelveVariant::KT128: KT128(input, diff --git a/test/pummel/test-webcrypto-kangarootwelve-32bit-overflow.js b/test/pummel/test-webcrypto-kangarootwelve-32bit-overflow.js new file mode 100644 index 00000000000..cb399ec18b8 --- /dev/null +++ b/test/pummel/test-webcrypto-kangarootwelve-32bit-overflow.js @@ -0,0 +1,41 @@ +'use strict'; +const common = require('../common'); + +if (!common.hasCrypto) + common.skip('missing crypto'); + +// KangarooTwelve: data + customization size_t overflow on 32-bit platforms. +// On 32-bit, msg_len + custom_len + LengthEncode(custom_len).size() can wrap +// size_t, causing an undersized allocation and heap buffer overflow. +// This test verifies the guard rejects such inputs. +// When kMaxLength < 2^32 two max-sized buffers can overflow a 32-bit size_t. + +const { kMaxLength } = require('buffer'); + +if (kMaxLength >= 2 ** 32) + common.skip('only relevant when kMaxLength < 2^32'); + +const assert = require('assert'); +const { subtle } = globalThis.crypto; + +let data, customization; +try { + data = new Uint8Array(kMaxLength); + customization = new Uint8Array(kMaxLength); +} catch { + common.skip('insufficient memory to allocate test buffers'); +} + +(async () => { + await assert.rejects( + subtle.digest( + { name: 'KT128', outputLength: 256, customization }, + data), + { name: 'OperationError' }); + + await assert.rejects( + subtle.digest( + { name: 'KT256', outputLength: 512, customization }, + data), + { name: 'OperationError' }); +})().then(common.mustCall());