Skip to content

Commit 407cf91

Browse files
panvasxa
authored andcommitted
crypto: guard against size_t overflow on experimental 32-bit arch
Refs: https://hackerone.com/reports/3655230 Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #62626 Backport-PR-URL: #63563 Refs: https://hackerone.com/reports/3655230 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent bb2857b commit 407cf91

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/crypto/crypto_turboshake.cc‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,6 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env,
587587
ByteSource* out,
588588
CryptoJobMode mode) {
589589
CHECK_GT(params.output_length, 0);
590-
char* buf = MallocOpenSSL<char>(params.output_length);
591590

592591
constuint8_t* input = reinterpret_cast<constuint8_t*>(params.data.data());
593592
size_t input_len = params.data.size();
@@ -596,6 +595,17 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env,
596595
reinterpret_cast<constuint8_t*>(params.customization.data());
597596
size_t custom_len = params.customization.size();
598597

598+
// Guard against size_t overflow in KangarooTwelve's s_len computation:
599+
// s_len = msg_len + custom_len + LengthEncode(custom_len).size()
600+
// LengthEncode produces at most sizeof(size_t) + 1 bytes.
601+
staticconstexprsize_tkMaxLengthEncodeSize = sizeof(size_t) + 1;
602+
if (input_len > SIZE_MAX - custom_len ||
603+
input_len + custom_len > SIZE_MAX - kMaxLengthEncodeSize) {
604+
returnfalse;
605+
}
606+
607+
char* buf = MallocOpenSSL<char>(params.output_length);
608+
599609
switch (params.variant) {
600610
case KangarooTwelveVariant::KT128:
601611
KT128(input,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
4+
if(!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// KangarooTwelve: data + customization size_t overflow on 32-bit platforms.
8+
// On 32-bit, msg_len + custom_len + LengthEncode(custom_len).size() can wrap
9+
// size_t, causing an undersized allocation and heap buffer overflow.
10+
// This test verifies the guard rejects such inputs.
11+
// When kMaxLength < 2^32 two max-sized buffers can overflow a 32-bit size_t.
12+
13+
const{ kMaxLength }=require('buffer');
14+
15+
if(kMaxLength>=2**32)
16+
common.skip('only relevant when kMaxLength < 2^32');
17+
18+
constassert=require('assert');
19+
const{ subtle }=globalThis.crypto;
20+
21+
letdata,customization;
22+
try{
23+
data=newUint8Array(kMaxLength);
24+
customization=newUint8Array(kMaxLength);
25+
}catch{
26+
common.skip('insufficient memory to allocate test buffers');
27+
}
28+
29+
(async()=>{
30+
awaitassert.rejects(
31+
subtle.digest(
32+
{name: 'KT128',outputLength: 256, customization },
33+
data),
34+
{name: 'OperationError'});
35+
36+
awaitassert.rejects(
37+
subtle.digest(
38+
{name: 'KT256',outputLength: 512, customization },
39+
data),
40+
{name: 'OperationError'});
41+
})().then(common.mustCall());

0 commit comments

Comments
 (0)