Skip to main content
Menu
Advisories

Unchecked recovery IDs trigger traps that leave backend stack state damaged

GHSA-RM9W-7FV8-3RHXPublished Updated GitHub

Description

The WebAssembly build of tiny-secp256k1 does not validate the recovery ID at runtime before calling its native public-key recovery function. An out-of-range ID can reach a native argument check and trigger a WASM trap.

The generated @bitcoin-js/tiny-secp256k1-asmjs backend has the same missing validation and translated trap path.

The trap bypasses shadow-stack cleanup. Repeated calls with invalid IDs can exhaust the stack of the shared module instance and cause later valid signing or verification calls to fail.

Details

recover(hash, signature, recoveryId) uses a TypeScript type for recovery IDs, but types do not enforce values at runtime. A JavaScript caller can pass 4 even though valid IDs are integers from 0 through 3.

The wrapper checks the hash and signature but does not reject that ID. Given otherwise suitable inputs, it reaches libsecp256k1's argument check. The check traps inside WASM rather than returning normally. The interrupted frame does not restore the shadow-stack pointer.

The WASM loader shares one instance. Catching the exception outside the call does not restore its stack pointer, so the damage accumulates across calls.

Static inspection of the integrity-checked asm.js packages shows that recovery decrements its translated mutable stack pointer before the invalid-ID argument check. The failure callback calls wasm2js_trap() before the restoration code can run. WASM exhaustion thresholds and downstream effects have not been measured for asm.js.

Fractional IDs and non-finite values also need explicit rejection: numeric coercion at the WASM boundary is not a substitute for validating an integer in the allowed range.

Example

With tiny-secp256k1 2.2.4 in an isolated process:

import * as ecc from "tiny-secp256k1";

const hash = new Uint8Array(32).fill(7);
const privateKey = new Uint8Array(32);
privateKey[31] = 1;
const { signature, recoveryId } = ecc.signRecoverable(hash, privateKey);
ecc.recover(hash, signature, recoveryId, true); // Valid control succeeds.

try {
  ecc.recover(hash, signature, 4, true);
} catch (error) {
  console.log(error.name, error.message); // RuntimeError: unreachable
}

This single invalid call and successful valid control were confirmed against npm 2.2.4 on Node 20.19.4. They establish the trap, not stack exhaustion. The reporter separately observed exhaustion after repeated invalid-ID calls on the tested WASM binary. The threshold depends on the binary and stack layout. The example uses a public test key and belongs in an isolated process.

Impact

An application that forwards attacker-controlled recovery IDs can lose the use of its shared cryptographic module. Later valid operations can fail until the instance is replaced. A network attacker requires an application path that exposes this parameter; the library does not receive network requests by itself.

This report establishes a stateful denial-of-service path. It does not establish private-key disclosure or a valid recovered key for an invalid recovery ID.

Affected versions

  • tiny-secp256k1: 2.2.0 through 2.2.4.
  • @bitcoin-js/tiny-secp256k1-asmjs: 2.2.2 through 2.2.4, based on the generated code's invalid-ID trap and skipped-restoration path.

The recover API first appears in tiny-secp256k1 2.2.0, so earlier releases are not included in this issue.

Remediation and workarounds

Upgrade tiny-secp256k1 and @bitcoin-js/tiny-secp256k1-asmjs, as applicable, to 2.2.5 or a later patched release. The JavaScript wrapper now requires Number.isInteger(recoveryId) and a value between 0 and 3 before calling the backend. The native wrapper also rejects out-of-range IDs by returning an error code before calling libsecp256k1. JavaScript handles that code after the backend call returns normally.

Applications can apply the runtime integer and range check before calling recover. Restart a worker or replace the module instance if it has already processed invalid IDs through this path.

References

Affected versions

References

GitHub · bitcoinjs/tiny-secp256k1