Skip to content

Commit 615c8aa

Browse files
marco-ippolitoErickWendel
authored andcommitted
crypto: fix CipherBase Update int32 overflow
PR-URL: nodejs#45769 Fixes: nodejs#45757 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 0c0b0da commit 615c8aa

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/crypto/crypto_cipher.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,11 @@ CipherBase::UpdateResult CipherBase::Update(
803803
if (kind_ == kDecipher && IsAuthenticatedMode())
804804
CHECK(MaybePassAuthTagToOpenSSL());
805805

806-
int buf_len = len + EVP_CIPHER_CTX_block_size(ctx_.get());
806+
const int block_size = EVP_CIPHER_CTX_block_size(ctx_.get());
807+
CHECK_GT(block_size, 0);
808+
if (len + block_size > INT_MAX) return kErrorState;
809+
int buf_len = len + block_size;
810+
807811
// For key wrapping algorithms, get output size by calling
808812
// EVP_CipherUpdate() with null output.
809813
if (kind_ == kCipher && mode == EVP_CIPH_WRAP_MODE &&

test/parallel/test-crypto-cipheriv-decipheriv.js

+8
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,11 @@ for (let n = minIvLength; n < maxIvLength; n += 1) {
215215
() => crypto.createCipheriv('aes-128-ecb', Buffer.alloc(17), null),
216216
/Invalid key length/);
217217
}
218+
219+
{
220+
// https://github.com/nodejs/node/issues/45757
221+
// eslint-disable-next-line no-restricted-syntax
222+
assert.throws(() =>
223+
crypto.createCipheriv('aes-128-gcm', Buffer.alloc(16), Buffer.alloc(12))
224+
.update(Buffer.allocUnsafeSlow(2 ** 31 - 1)));
225+
}

0 commit comments

Comments
 (0)