]> git.kaiwu.me - njs.git/commitdiff
WebCrypto: avoid freeing HMAC verification stack storage
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 16 Nov 2026 23:37:34 +0000 (15:37 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Tue, 17 Nov 2026 22:09:33 +0000 (14:09 -0800)
Previously, HMAC verification assigned its stack digest buffer to
dst.  If HMAC() failed or returned an unexpected digest length, the
common error handler passed that stack pointer to js_free().

Keep dst reserved for allocated output and use the stack buffer
directly for verification.  The issue was introduced in 75ca26f8
(0.8.10).

external/qjs_webcrypto_module.c

index 399eac1fd8118b827104f6b831e67ba12d81f069..fdfd25b1cc3465dfe12d1664be6e7b515b986e2a 100644 (file)
@@ -4771,15 +4771,12 @@ qjs_webcrypto_sign(JSContext *cx, JSValueConst this_val, int argc,
                 JS_ThrowOutOfMemory(cx);
                 return JS_EXCEPTION;
             }
-
-        } else {
-            dst = (u_char *) &m[0];
         }
 
         outlen = m_len;
 
         p = HMAC(md, key->u.s.raw.start, key->u.s.raw.length, data.start,
-                 data.length, dst, &m_len);
+                 data.length, verify ? m : dst, &m_len);
 
         if (p == NULL || m_len != outlen) {
             qjs_webcrypto_error(cx, "HMAC() failed");
@@ -4788,7 +4785,7 @@ qjs_webcrypto_sign(JSContext *cx, JSValueConst this_val, int argc,
 
         if (verify) {
             rc = (sig.length == outlen
-                  && CRYPTO_memcmp(sig.start, dst, outlen) == 0);
+                  && CRYPTO_memcmp(sig.start, m, outlen) == 0);
         }
 
         break;