Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 407cf91

Browse filesBrowse 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: nodejs#62626 Backport-PR-URL: nodejs#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
Copy full SHA for 407cf91

2 files changed

+52-1Lines changed: 52 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/crypto/crypto_turboshake.cc‎

Copy file name to clipboardExpand all lines: src/crypto/crypto_turboshake.cc
+11-1Lines 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
const uint8_t* input = reinterpret_cast<const uint8_t*>(params.data.data());
593592
size_t input_len = params.data.size();
@@ -596,6 +595,17 @@ bool KangarooTwelveTraits::DeriveBits(Environment* env,
596595
reinterpret_cast<const uint8_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+
static constexpr size_t kMaxLengthEncodeSize = sizeof(size_t) + 1;
602+
if (input_len > SIZE_MAX - custom_len ||
603+
input_len + custom_len > SIZE_MAX - kMaxLengthEncodeSize) {
604+
return false;
605+
}
606+
607+
char* buf = MallocOpenSSL<char>(params.output_length);
608+
599609
switch (params.variant) {
600610
case KangarooTwelveVariant::KT128:
601611
KT128(input,
Collapse file
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
const common = 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+
const assert = require('assert');
19+
const { subtle } = globalThis.crypto;
20+
21+
let data, customization;
22+
try {
23+
data = new Uint8Array(kMaxLength);
24+
customization = new Uint8Array(kMaxLength);
25+
} catch {
26+
common.skip('insufficient memory to allocate test buffers');
27+
}
28+
29+
(async () => {
30+
await assert.rejects(
31+
subtle.digest(
32+
{ name: 'KT128', outputLength: 256, customization },
33+
data),
34+
{ name: 'OperationError' });
35+
36+
await assert.rejects(
37+
subtle.digest(
38+
{ name: 'KT256', outputLength: 512, customization },
39+
data),
40+
{ name: 'OperationError' });
41+
})().then(common.mustCall());

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.