]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: reject out-of-range AES-CTR counter length master
authorbasavaraj-sm05 <basavaraj@digiscrypt.com>
Fri, 20 Nov 2026 16:57:11 +0000 (22:27 +0530)
committerDmitry Volyntsev <xeioexception@gmail.com>
Sat, 21 Nov 2026 23:49:08 +0000 (15:49 -0800)
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.

external/njs_webcrypto_module.c
external/qjs_webcrypto_module.c
test/webcrypto/aes.t.mjs

index f6932832c29972d84baaf63c673ce4d944a8d3b9..04e441fe73c61381f5db649e55c94c234eb0e799 100644 (file)
@@ -1302,7 +1302,7 @@ njs_cipher_aes_ctr(njs_vm_t *vm, njs_str_t *data, njs_webcrypto_key_t *key,
         return NJS_ERROR;
     }
 
         return NJS_ERROR;
     }
 
-    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;
index 23de3d71d3cfd98516c5dc2300ebbf50aa52c4d9..712a18ac7d5ae86bb97a99f3d9c679021c4cf577 100644 (file)
@@ -1183,7 +1183,7 @@ qjs_cipher_aes_ctr(JSContext *cx, njs_str_t *data, qjs_webcrypto_key_t *key,
         return JS_EXCEPTION;
     }
 
         return JS_EXCEPTION;
     }
 
-    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;
index 88ebe402dbe5d6948951c2e10a21cf72e3a26eff..6828e929131dfd8631c0d5c6e5dbea8825af47d2 100644 (file)
@@ -87,6 +87,10 @@ let aes_tsuite = {
         { 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 },