The AES-CTR counter length from algorithm.length was read as a signed
integer and only rejected when it was zero or above 128, so a negative
value passed the check. It was then handed to njs_bn_counter128(),
whose bits parameter is njs_uint_t (uintptr_t on 64-bit builds), so a
negative length turned into a very large unsigned count. The derived
byte count then drove a memcpy() into a 16-byte stack buffer with an
attacker-influenced size, overflowing it and reading past the counter
block. The value reaches here directly from crypto.subtle.encrypt()
and decrypt() calls on both engines.
Reject any length below 1 as well, matching the 1 to 128 range the
error message already states and the WebCrypto specification requires.
- if (njs_slow_path(length == 0 || length > 128)) {
+ if (njs_slow_path(length < 1 || length > 128)) {
njs_vm_type_error(vm, "AES-CTR algorithm.length must be between "
"1 and 128");
return NJS_ERROR;
njs_vm_type_error(vm, "AES-CTR algorithm.length must be between "
"1 and 128");
return NJS_ERROR;
- if (length == 0 || length > 128) {
+ if (length < 1 || length > 128) {
JS_ThrowTypeError(cx, "AES-CTR algorithm.length must be between "
"1 and 128");
return JS_EXCEPTION;
JS_ThrowTypeError(cx, "AES-CTR algorithm.length must be between "
"1 and 128");
return JS_EXCEPTION;
{ name: "AES-CTR", data: "aabbcc", key: "001122330011223300112233001122330011223300112233" },
{ name: "AES-CTR", data: "aabbccdd", length: 129,
exception: "TypeError: AES-CTR algorithm.length must be between 1 and 128" },
{ name: "AES-CTR", data: "aabbcc", key: "001122330011223300112233001122330011223300112233" },
{ name: "AES-CTR", data: "aabbccdd", length: 129,
exception: "TypeError: AES-CTR algorithm.length must be between 1 and 128" },
+ { name: "AES-CTR", data: "aabbccdd", length: -1,
+ exception: "TypeError: AES-CTR algorithm.length must be between 1 and 128" },
+ { name: "AES-CTR", data: "aabbccdd", length: -4294967291,
+ exception: "TypeError: AES-CTR algorithm.length must be between 1 and 128" },
{ name: "AES-CTR", data: "", length: 128 },
{ name: "AES-CTR", data: "", counter: "ffffffffffffffffffffffffffffffff", length: 128 },
{ name: "AES-CTR", data: "", length: 128 },
{ name: "AES-CTR", data: "", counter: "ffffffffffffffffffffffffffffffff", length: 128 },